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