Scott Baker | bf1610d | 2014-06-18 18:22:03 -0700 | [diff] [blame] | 1 | # /opt/planetstack/core/dashboard/views/helloworld.py |
Scott Baker | 0b48b4b | 2014-06-19 15:24:58 -0700 | [diff] [blame] | 2 | import datetime |
Scott Baker | bf1610d | 2014-06-18 18:22:03 -0700 | [diff] [blame] | 3 | import os |
| 4 | import sys |
Scott Baker | 0b48b4b | 2014-06-19 15:24:58 -0700 | [diff] [blame] | 5 | import time |
Scott Baker | bf1610d | 2014-06-18 18:22:03 -0700 | [diff] [blame] | 6 | import json |
| 7 | from django.http import HttpResponse, HttpResponseServerError, HttpResponseForbidden |
| 8 | from django.views.generic import TemplateView, View |
| 9 | from core.models import * |
| 10 | from django.forms.models import model_to_dict |
| 11 | |
Scott Baker | 0b48b4b | 2014-06-19 15:24:58 -0700 | [diff] [blame] | 12 | def ensure_serializable(d): |
| 13 | d2={} |
| 14 | for (k,v) in d.items(): |
| 15 | # datetime is not json serializable |
| 16 | if isinstance(v, datetime.datetime): |
| 17 | d2[k] = time.mktime(v.timetuple()) |
Scott Baker | bf5f254 | 2014-06-19 16:59:33 -0700 | [diff] [blame^] | 18 | elif v.__class__.__name__ == "Geoposition": |
| 19 | pass |
Scott Baker | 0b48b4b | 2014-06-19 15:24:58 -0700 | [diff] [blame] | 20 | else: |
| 21 | d2[k] = v |
| 22 | return d2 |
| 23 | |
Scott Baker | bf5f254 | 2014-06-19 16:59:33 -0700 | [diff] [blame^] | 24 | def sliver_to_dict(sliver): |
| 25 | d = model_to_dict(sliver) |
| 26 | d["slice_id"] = sliver.slice.id |
| 27 | d["node_id"] = sliver.node.id |
| 28 | return d |
| 29 | |
| 30 | def slice_to_dict(slice): |
| 31 | d = model_to_dict(slice) |
| 32 | d["slivers"] = [sliver_to_dict(x) for x in slice.slivers] |
| 33 | return d |
| 34 | |
| 35 | def node_to_dict(node): |
| 36 | d = model_to_dict(node) |
| 37 | d["slivers"] = [] |
| 38 | |
| 39 | |
| 40 | class OpenCloudData: |
| 41 | def __init__(self, user): |
| 42 | self.loadAll() |
| 43 | |
| 44 | def loadAll(self): |
| 45 | self.allNodes = list(Node.objects.all()) |
| 46 | self.allSlices = list(Slice.objects.all()) |
| 47 | self.allSlivers = list(Sliver.objects.all()) |
| 48 | self.allSites = list(Site.objects.all()) |
| 49 | |
| 50 | self.site_id = {} |
| 51 | for site in self.allSites: |
| 52 | d = model_to_dict(site) |
| 53 | d["node_ids"] = [] |
| 54 | d["slice_ids"] = [] |
| 55 | self.site_id[site.id] = ensure_serializable(d) |
| 56 | |
| 57 | self.node_id = {} |
| 58 | for node in self.allNodes: |
| 59 | d = model_to_dict(node) |
| 60 | d["sliver_ids"] = [] |
| 61 | self.node_id[node.id] = ensure_serializable(d) |
| 62 | self.site_id[node.site_id]["node_ids"].append(node.id) |
| 63 | |
| 64 | self.slice_id = {} |
| 65 | for slice in self.allSlices: |
| 66 | d = model_to_dict(slice) |
| 67 | d["sliver_ids"] = [] |
| 68 | self.slice_id[slice.id] = ensure_serializable(d) |
| 69 | self.site_id[slice.site_id]["slice_ids"].append(site.id) |
| 70 | |
| 71 | print self.slice_id.keys() |
| 72 | |
| 73 | self.sliver_id = {} |
| 74 | for sliver in self.allSlivers: |
| 75 | self.sliver_id[sliver.id] = model_to_dict(sliver) |
| 76 | |
| 77 | self.slice_id[sliver.slice_id]["sliver_ids"].append(sliver.id) |
| 78 | self.node_id[sliver.node_id]["sliver_ids"].append(sliver.id) |
| 79 | |
| 80 | def get_opencloud_data(self): |
| 81 | return {"slices": self.slice_id.values(), |
| 82 | "slivers": self.sliver_id.values(), |
| 83 | "nodes": self.node_id.values(), |
| 84 | "sites": self.site_id.values()} |
| 85 | |
Scott Baker | bf1610d | 2014-06-18 18:22:03 -0700 | [diff] [blame] | 86 | class ShellDataView(View): |
| 87 | url = r'^shelldata/' |
| 88 | |
| 89 | def get(self, request, **kwargs): |
Scott Baker | bf5f254 | 2014-06-19 16:59:33 -0700 | [diff] [blame^] | 90 | result = OpenCloudData(request.user).get_opencloud_data() |
Scott Baker | bf1610d | 2014-06-18 18:22:03 -0700 | [diff] [blame] | 91 | |
| 92 | return HttpResponse(json.dumps(result), mimetype='application/json') |