Scott Baker | bf1610d | 2014-06-18 18:22:03 -0700 | [diff] [blame] | 1 | var opencloud_data = []; |
| 2 | var opencloud_data_received = false; |
| 3 | |
| 4 | function updateOpenCloud(onLoaded) { |
| 5 | $.ajax({url: "/admin/shelldata", |
| 6 | dataType: "json", |
| 7 | type: "GET", |
| 8 | success: function(data) { |
| 9 | opencloud_data = data; |
| 10 | if (!opencloud_data_received) { |
| 11 | opencloud_data_received = true; |
| 12 | if (onLoaded!=null) { |
| 13 | onLoaded(); |
| 14 | } |
| 15 | } |
| 16 | // do this again in 30 seconds |
| 17 | setTimeout(function() {updateOpenCloud(onLoaded)}, 10000); |
| 18 | }, |
| 19 | error: function() { |
| 20 | console.log("something went wrong. trying again"); |
| 21 | // do this again in 30 seconds |
| 22 | setTimeout(function() {updateOpenCloud(onLoaded)}, 10000); |
| 23 | } |
| 24 | }); |
| 25 | } |
| 26 | |
Scott Baker | 2c3d5a8 | 2014-06-19 16:51:16 -0700 | [diff] [blame] | 27 | function OpenCloudModel(name) { |
| 28 | this.all = function() { return opencloud_data[name]; }; |
| 29 | |
| 30 | this.match = function(filterDict,obj) { |
| 31 | for (var k in filterDict) { |
| 32 | if (obj[k] == filterDict[k]) { |
| 33 | return true; |
| 34 | } |
| 35 | } |
| 36 | return false; |
| 37 | }; |
| 38 | |
| 39 | this.filter = function(filterDict) { |
| 40 | result = [] |
| 41 | all_objs = this.all() |
| 42 | for (var k in all_objs) { |
| 43 | obj = all_objs[k]; |
| 44 | if (this.match(filterDict, obj)) { |
| 45 | result.push(obj); |
| 46 | } |
| 47 | } |
| 48 | return result; |
| 49 | }; |
| 50 | |
| 51 | this.get = function(filterDict) { |
| 52 | return this.filter(filterDict)[0]; |
| 53 | }; |
| 54 | |
| 55 | this.__str__ = function() { return '["all", "filter", "get"]' }; |
Scott Baker | bf1610d | 2014-06-18 18:22:03 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | function OpenCloud() { |
Scott Baker | 2c3d5a8 | 2014-06-19 16:51:16 -0700 | [diff] [blame] | 59 | this.slices = new OpenCloudModel("slices"); |
| 60 | this.slivers = new OpenCloudModel("slivers"); |
| 61 | this.nodes = new OpenCloudModel("nodes"); |
| 62 | this.sites = new OpenCloudModel("sites"); |
| 63 | this.__str__ = function() { return '["slices", "slivers", "nodes", "sites"]'; } |
Scott Baker | bf1610d | 2014-06-18 18:22:03 -0700 | [diff] [blame] | 64 | }; |