Scott Baker | c7325a4 | 2014-05-30 16:06:46 -0700 | [diff] [blame] | 1 | import os |
| 2 | import sys |
| 3 | from django.views.generic import TemplateView, View |
| 4 | import datetime |
| 5 | from pprint import pprint |
| 6 | import json |
jcnelson | 160012b | 2014-07-10 19:32:58 -0400 | [diff] [blame^] | 7 | from syndicate_storage.models import * |
Scott Baker | c7325a4 | 2014-05-30 16:06:46 -0700 | [diff] [blame] | 8 | from core.models import * |
| 9 | from hpc.models import ContentProvider |
| 10 | from operator import attrgetter |
| 11 | from django import template |
| 12 | from django.views.decorators.csrf import csrf_exempt |
| 13 | from django.http import HttpResponse, HttpResponseServerError, HttpResponseForbidden |
| 14 | from django.core import urlresolvers |
| 15 | from django.contrib.gis.geoip import GeoIP |
| 16 | from django.db.models import Q |
| 17 | from ipware.ip import get_ip |
| 18 | from operator import itemgetter, attrgetter |
| 19 | import traceback |
| 20 | import math |
| 21 | |
| 22 | if os.path.exists("/home/smbaker/projects/vicci/cdn/bigquery"): |
| 23 | sys.path.append("/home/smbaker/projects/vicci/cdn/bigquery") |
| 24 | else: |
| 25 | sys.path.append("/opt/planetstack/hpc_wizard") |
Scott Baker | c7325a4 | 2014-05-30 16:06:46 -0700 | [diff] [blame] | 26 | from planetstack_analytics import DoPlanetStackAnalytics, PlanetStackAnalytics, RED_LOAD, BLUE_LOAD |
| 27 | |
| 28 | def getDashboardContext(user, context={}, tableFormat = False): |
| 29 | context = {} |
| 30 | |
| 31 | userSliceData = getSliceInfo(user) |
| 32 | if (tableFormat): |
| 33 | context['userSliceInfo'] = userSliceTableFormatter(userSliceData) |
| 34 | else: |
| 35 | context['userSliceInfo'] = userSliceData |
| 36 | context['cdnData'] = getCDNOperatorData(wait=False) |
| 37 | context['cdnContentProviders'] = getCDNContentProviderData() |
| 38 | |
| 39 | (dashboards, unusedDashboards)= getDashboards(user) |
| 40 | unusedDashboards=[x for x in unusedDashboards if x!="Customize"] |
| 41 | context['dashboards'] = dashboards |
| 42 | context['unusedDashboards'] = unusedDashboards |
| 43 | |
| 44 | return context |
| 45 | |
| 46 | def getDashboards(user): |
| 47 | dashboards = user.get_dashboards() |
| 48 | |
| 49 | dashboard_names = [d.name for d in dashboards] |
| 50 | |
| 51 | unused_dashboard_names = [] |
| 52 | for dashboardView in DashboardView.objects.all(): |
| 53 | if not dashboardView.name in dashboard_names: |
| 54 | unused_dashboard_names.append(dashboardView.name) |
| 55 | |
| 56 | return (dashboard_names, unused_dashboard_names) |
| 57 | |
| 58 | def getSliceInfo(user): |
| 59 | sliceList = Slice.objects.all() |
| 60 | slicePrivs = SlicePrivilege.objects.filter(user=user) |
| 61 | userSliceInfo = [] |
| 62 | for entry in slicePrivs: |
| 63 | |
| 64 | slicename = Slice.objects.get(id=entry.slice.id).name |
| 65 | slice = Slice.objects.get(name=Slice.objects.get(id=entry.slice.id).name) |
| 66 | sliverList=Sliver.objects.all() |
| 67 | sites_used = {} |
| 68 | for sliver in slice.slivers.all(): |
| 69 | #sites_used['deploymentSites'] = sliver.node.deployment.name |
| 70 | # sites_used[sliver.image.name] = sliver.image.name |
| 71 | sites_used[sliver.node.site.name] = sliver.numberCores |
| 72 | sliceid = Slice.objects.get(id=entry.slice.id).id |
| 73 | try: |
| 74 | sliverList = Sliver.objects.filter(slice=entry.slice.id) |
| 75 | siteList = {} |
| 76 | for x in sliverList: |
| 77 | if x.node.site not in siteList: |
| 78 | siteList[x.node.site] = 1 |
| 79 | slivercount = len(sliverList) |
| 80 | sitecount = len(siteList) |
| 81 | except: |
| 82 | traceback.print_exc() |
| 83 | slivercount = 0 |
| 84 | sitecount = 0 |
| 85 | |
| 86 | userSliceInfo.append({'slicename': slicename, 'sliceid':sliceid, |
| 87 | 'sitesUsed':sites_used, |
| 88 | 'role': SliceRole.objects.get(id=entry.role.id).role, |
| 89 | 'slivercount': slivercount, |
| 90 | 'sitecount':sitecount}) |
| 91 | |
| 92 | return userSliceInfo |
| 93 | |
| 94 | def getCDNContentProviderData(): |
| 95 | cps = [] |
| 96 | for dm_cp in ContentProvider.objects.all(): |
| 97 | cp = {"name": dm_cp.name, |
| 98 | "account": dm_cp.account} |
| 99 | cps.append(cp) |
| 100 | |
| 101 | return cps |
| 102 | |
| 103 | def getCDNOperatorData(randomizeData = False, wait=True): |
| 104 | HPC_SLICE_NAME = "HyperCache" |
| 105 | |
| 106 | bq = PlanetStackAnalytics() |
| 107 | |
| 108 | rows = bq.get_cached_query_results(bq.compose_cached_query(), wait) |
| 109 | |
| 110 | # wait=False on the first time the Dashboard is opened. This means we might |
| 111 | # not have any rows yet. The dashboard code polls every 30 seconds, so it |
| 112 | # will eventually pick them up. |
| 113 | |
| 114 | if rows: |
| 115 | rows = bq.postprocess_results(rows, filter={"event": "hpc_heartbeat"}, maxi=["cpu"], count=["hostname"], computed=["bytes_sent/elapsed"], groupBy=["Time","site"], maxDeltaTime=80) |
| 116 | |
| 117 | # dictionaryize the statistics rows by site name |
| 118 | stats_rows = {} |
| 119 | for row in rows: |
| 120 | stats_rows[row["site"]] = row |
| 121 | else: |
| 122 | stats_rows = {} |
| 123 | |
| 124 | slice = Slice.objects.filter(name=HPC_SLICE_NAME) |
| 125 | if slice: |
| 126 | slice_slivers = list(slice[0].slivers.all()) |
| 127 | else: |
| 128 | slice_slivers = [] |
| 129 | |
| 130 | new_rows = {} |
| 131 | for site in Site.objects.all(): |
| 132 | # compute number of slivers allocated in the data model |
| 133 | allocated_slivers = 0 |
| 134 | for sliver in slice_slivers: |
| 135 | if sliver.node.site == site: |
| 136 | allocated_slivers = allocated_slivers + 1 |
| 137 | |
| 138 | stats_row = stats_rows.get(site.name,{}) |
| 139 | |
| 140 | max_cpu = stats_row.get("max_avg_cpu", stats_row.get("max_cpu",0)) |
| 141 | cpu=float(max_cpu)/100.0 |
| 142 | hotness = max(0.0, ((cpu*RED_LOAD) - BLUE_LOAD)/(RED_LOAD-BLUE_LOAD)) |
| 143 | |
Scott Baker | 7945490 | 2014-06-26 10:26:48 -0700 | [diff] [blame] | 144 | try: |
| 145 | lat=float(site.location.longitude) |
| 146 | long=float(site.location.longitude) |
| 147 | except: |
| 148 | lat=0 |
| 149 | long=0 |
| 150 | |
Scott Baker | c7325a4 | 2014-05-30 16:06:46 -0700 | [diff] [blame] | 151 | # format it to what that CDN Operations View is expecting |
Scott Baker | 7945490 | 2014-06-26 10:26:48 -0700 | [diff] [blame] | 152 | new_row = {"lat": lat, |
| 153 | "long": long, |
Scott Baker | c7325a4 | 2014-05-30 16:06:46 -0700 | [diff] [blame] | 154 | "health": 0, |
| 155 | "numNodes": int(site.nodes.count()), |
| 156 | "activeHPCSlivers": int(stats_row.get("count_hostname", 0)), # measured number of slivers, from bigquery statistics |
| 157 | "numHPCSlivers": allocated_slivers, # allocated number of slivers, from data model |
| 158 | "siteUrl": str(site.site_url), |
| 159 | "bandwidth": stats_row.get("sum_computed_bytes_sent_div_elapsed",0), |
| 160 | "load": max_cpu, |
| 161 | "hot": float(hotness)} |
| 162 | new_rows[str(site.name)] = new_row |
| 163 | |
| 164 | # get rid of sites with 0 slivers that overlap other sites with >0 slivers |
| 165 | for (k,v) in new_rows.items(): |
| 166 | bad=False |
| 167 | if v["numHPCSlivers"]==0: |
| 168 | for v2 in new_rows.values(): |
| 169 | if (v!=v2) and (v2["numHPCSlivers"]>=0): |
| 170 | d = haversine(v["lat"],v["long"],v2["lat"],v2["long"]) |
| 171 | if d<100: |
| 172 | bad=True |
| 173 | if bad: |
| 174 | del new_rows[k] |
| 175 | |
| 176 | return new_rows |
| 177 | |
| 178 | def slice_increase_slivers(user, user_ip, siteList, slice, count, noAct=False): |
| 179 | sitesChanged = {} |
| 180 | |
| 181 | # let's compute how many slivers are in use in each node of each site |
| 182 | for site in siteList: |
| 183 | site.nodeList = list(site.nodes.all()) |
| 184 | for node in site.nodeList: |
| 185 | node.sliverCount = 0 |
| 186 | for sliver in node.slivers.all(): |
| 187 | if sliver.slice.id == slice.id: |
| 188 | node.sliverCount = node.sliverCount + 1 |
| 189 | |
| 190 | # Allocate slivers to nodes |
| 191 | # for now, assume we want to allocate all slivers from the same site |
| 192 | nodes = siteList[0].nodeList |
| 193 | while (count>0): |
| 194 | # Sort the node list by number of slivers per node, then pick the |
| 195 | # node with the least number of slivers. |
| 196 | nodes = sorted(nodes, key=attrgetter("sliverCount")) |
| 197 | node = nodes[0] |
| 198 | |
| 199 | print "adding sliver at node", node.name, "of site", node.site.name |
| 200 | |
| 201 | if not noAct: |
| 202 | sliver = Sliver(name=node.name, |
| 203 | slice=slice, |
| 204 | node=node, |
| 205 | image = Image.objects.all()[0], |
| 206 | creator = User.objects.get(email=user), |
| 207 | deploymentNetwork=node.deployment, |
| 208 | numberCores =1 ) |
| 209 | sliver.save() |
| 210 | |
| 211 | node.sliverCount = node.sliverCount + 1 |
| 212 | |
| 213 | count = count - 1 |
| 214 | |
| 215 | sitesChanged[node.site.name] = sitesChanged.get(node.site.name,0) + 1 |
| 216 | |
| 217 | return sitesChanged |
| 218 | |
| 219 | def slice_decrease_slivers(user, siteList, slice, count, noAct=False): |
| 220 | sitesChanged = {} |
| 221 | sliverList ={} |
| 222 | if siteList: |
| 223 | siteNames = [site.name for site in siteList] |
| 224 | else: |
| 225 | siteNames = None |
| 226 | |
| 227 | for sliver in slice.slivers.all(): |
| 228 | if(not siteNames) or (sliver.node.site.name in siteNames):
|
| 229 | node = sliver.node
|
| 230 | sliverList[sliver.name]=node.name |
| 231 | |
| 232 | for key in sliverList: |
| 233 | if count>0: |
| 234 | sliver = Sliver.objects.filter(name=key)[0]
|
| 235 | sliver.delete()
|
| 236 | print "deleting sliver",sliverList[key],"at node",sliver.node.name
|
| 237 | count=count-1
|
| 238 | sitesChanged[sliver.node.site.name] = sitesChanged.get(sliver.node.site.name,0) - 1
|
| 239 |
|
| 240 | return sitesChanged |
| 241 | |
| 242 | def haversine(site_lat, site_lon, lat, lon): |
| 243 | d=0 |
| 244 | if lat and lon and site_lat and site_lon: |
| 245 | site_lat = float(site_lat) |
| 246 | site_lon = float(site_lon) |
| 247 | lat = float(lat) |
| 248 | lon = float(lon) |
| 249 | R = 6378.1 |
| 250 | a = math.sin( math.radians((lat - site_lat)/2.0) )**2 + math.cos( math.radians(lat) )*math.cos( math.radians(site_lat) )*(math.sin( math.radians((lon - site_lon)/2.0 ) )**2) |
| 251 | c = 2 * math.atan2( math.sqrt(a), math.sqrt(1 - a) ) |
| 252 | d = R * c |
| 253 | |
| 254 | return d |
| 255 | |
| 256 | def userSliceTableFormatter(data): |
| 257 | formattedData = { |
| 258 | 'rows' : data |
| 259 | } |
| 260 | return formattedData |