Created
February 3, 2012 03:23
-
-
Save bushxnyc/1727428 to your computer and use it in GitHub Desktop.
Query Lockup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sc_require('data'); | |
SC.mixin(V6Grapher, { | |
// Retrieve graph data from source | |
getGraphingData: function() { | |
return V6Grapher.Data.split("\n"); | |
}, | |
// Process graphing data | |
processData: function(graphData) { | |
// prepare each line for processing | |
graphData = this._clean(graphData); | |
// Split the lines into data chunks | |
this._process(graphData); | |
}, | |
_clean: function(graphData){ | |
var line; | |
for (var i=0; i<graphData.length; i++){ | |
// Chop off ".png" | |
line = graphData[i]; | |
var pngIndex = line.indexOf(".png"); | |
graphData[i] = line.substring(0, pngIndex); | |
} | |
return graphData; | |
}, | |
_process: function(pre_processed){ | |
this.devices = V6Grapher.store.find(V6Grapher.Device); | |
this.sites = V6Grapher.store.find(V6Grapher.Site); | |
this.time_ranges = V6Grapher.store.find(V6Grapher.TimeRange); | |
var line; | |
for (var i=0; i<pre_processed.length; i++){ | |
// Split up into data chunks | |
line = pre_processed[i]; | |
var processed = line.split('-'); | |
if (processed.length > 3){ | |
var time_name = processed[0] + " " + processed[1]; | |
var site_name = processed[2]; | |
var device_name = processed[3]; | |
} else { | |
var site_name = processed[2]; | |
var time_name = processed[0] + " " + processed[1]; | |
var device_name = "All - " + site_name ; | |
} | |
var site = this._get_site(site_name); | |
var time_range = this._get_time_range(time_name); | |
var device = this._get_device(device_name); | |
device.set('site', site); | |
device.get('time_ranges').pushObject(time_range); | |
} | |
}, | |
_get_site: function(name){ | |
var query = SC.Query.local(V6Grapher.Site, { | |
conditions: 'name = {name}', | |
parameters: { name: name } | |
}); | |
var site; | |
site = this.sites.find(query); | |
site = site.firstObject(); | |
if (site) { | |
return site; | |
} else { | |
return V6Grapher.store.createRecord(V6Grapher.Site, {name: name}); | |
} | |
}, | |
_get_device: function(name){ | |
var query = SC.Query.local(V6Grapher.Device, { | |
conditions: 'name = {name}', | |
parameters: { name: name } | |
}); | |
var device; | |
device = this.devices.find(query); | |
device = device.firstObject(); | |
if (device) { | |
return device; | |
} else { | |
return V6Grapher.store.createRecord(V6Grapher.Device, {name: name}); | |
} | |
}, | |
_get_time_range: function(name){ | |
var query = SC.Query.local(V6Grapher.TimeRange, { | |
conditions: 'name = {name}', | |
parameters: { name: name } | |
}); | |
var time_range; | |
time_range = this.time_ranges.find(query); | |
time_range = time_range.firstObject(); | |
if (time_range) { | |
return time_range; | |
} else { | |
return V6Grapher.store.createRecord(V6Grapher.TimeRange, {name: name}); | |
} | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment