blob: 5efe2391a4fed4b7990e3a616ecdd32446163889 [file] [log] [blame]
Scott Bakerbf1610d2014-06-18 18:22:03 -07001# /opt/planetstack/core/dashboard/views/helloworld.py
Scott Baker0b48b4b2014-06-19 15:24:58 -07002import datetime
Scott Bakerbf1610d2014-06-18 18:22:03 -07003import os
4import sys
Scott Baker0b48b4b2014-06-19 15:24:58 -07005import time
Scott Bakerbf1610d2014-06-18 18:22:03 -07006import json
7from django.http import HttpResponse, HttpResponseServerError, HttpResponseForbidden
8from django.views.generic import TemplateView, View
9from core.models import *
10from django.forms.models import model_to_dict
11
Scott Baker0b48b4b2014-06-19 15:24:58 -070012def 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 Bakerbf5f2542014-06-19 16:59:33 -070018 elif v.__class__.__name__ == "Geoposition":
19 pass
Scott Baker0b48b4b2014-06-19 15:24:58 -070020 else:
21 d2[k] = v
22 return d2
23
Scott Bakerbf5f2542014-06-19 16:59:33 -070024def 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
30def 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
35def node_to_dict(node):
36 d = model_to_dict(node)
37 d["slivers"] = []
38
39
40class 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 Bakerbf1610d2014-06-18 18:22:03 -070086class ShellDataView(View):
87 url = r'^shelldata/'
88
89 def get(self, request, **kwargs):
Scott Bakerbf5f2542014-06-19 16:59:33 -070090 result = OpenCloudData(request.user).get_opencloud_data()
Scott Bakerbf1610d2014-06-18 18:22:03 -070091
92 return HttpResponse(json.dumps(result), mimetype='application/json')