Created
September 29, 2016 12:11
-
-
Save DmitrySkripkin/c18cd80e8364b6ef1d44881debf3e5f7 to your computer and use it in GitHub Desktop.
httpBuffer.example.js
This file contains hidden or 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
export class HttpBuffer { | |
constructor ($injector) { | |
'ngInject'; | |
this.buffer = []; | |
this.$http = null; | |
this.$injector = $injector; | |
} | |
retryHttpRequest(config, deferred) { | |
function successCallback(response) { | |
if (deferred) { | |
deferred.resolve(response); | |
} | |
} | |
function errorCallback(response) { | |
deferred.reject(response); | |
} | |
this.$http = this.$http || this.$injector.get('$http'); | |
this.$http(config) | |
.then(successCallback, errorCallback); | |
} | |
append(config, deferred) { | |
this.buffer.push({ | |
config: config, | |
deferred: deferred | |
}); | |
} | |
rejectAll(reason) { | |
var i = 0; | |
if (reason) { | |
for (i; i < this.buffer.length; ++i) { | |
this.buffer[i].deferred.reject(reason); | |
} | |
} | |
this.buffer = []; | |
} | |
retryAll(updater) { | |
var i = 0; | |
for (i; i < buffer.length; ++i) { | |
this.retryHttpRequest(updater(this.buffer[i].config, this.buffer[i].deferred)); | |
} | |
this.buffer = []; | |
} | |
} |
This file contains hidden or 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
importScripts('lodash.js'); | |
var tasks = { | |
convertStore: function(data, options, name){ | |
var result = data.reduce(function(cumulativeObj, currObj) { | |
var tuples = cumulativeObj.tuples; | |
var currLevel = cumulativeObj.dimensionHierarchy.root; | |
while ('child' in currLevel) { | |
var dimKey = currObj[currLevel.dimension]; | |
if (!(dimKey in tuples)) { | |
if (currLevel.child.dimension) { | |
tuples[dimKey] = {}; | |
} else { | |
tuples[dimKey] = []; | |
} | |
} | |
currLevel = currLevel.child; | |
tuples = tuples[dimKey]; | |
} | |
// Now at the measures level | |
cumulativeObj.measuresOrder.forEach(function(measure, idx) { | |
tuples[idx] = currObj[measure]; | |
}); | |
return cumulativeObj; | |
}, | |
{ | |
dimensionHierarchy: { | |
root: { | |
dimension: 'usage_interval_start', | |
child: { | |
dimension: 'mcu_name', | |
child: { | |
measures: true | |
} | |
} | |
} | |
}, | |
measuresOrder: [ | |
'occupied_audio_port_hours', | |
'audio_utilization_percentage', | |
'peak_occupied_audio_ports', | |
'peak_audio_utilization_percentage', | |
'occupied_video_port_hours', | |
'video_utilization_percentage', | |
'peak_occupied_video_ports', | |
'peak_video_utilization_percentage' | |
], | |
tuples: {} | |
} | |
); | |
return {result: result, name: name}; | |
}, | |
transformDatavis: function(data, options, name) { | |
var storeData = data; | |
var tuples = storeData.tuples; | |
var measureIdx = storeData.measuresOrder.indexOf(options.valueBy); | |
if (!_.isUndefined(options.period)) { | |
switch (options.period) { | |
case 'year': | |
var sortedKeys = _.sortBy(Object.keys(tuples), function(o) {var date = new Date(o); return date.getMonth(); }); | |
break; | |
case 'week': | |
var sortedKeys = _.sortBy(Object.keys(tuples), function(o) {var date = new Date(o); return date.getDay(); }); | |
break; | |
case 'day': | |
var sortedKeys = _.sortBy(Object.keys(tuples), function(o) {var date = new Date(o); return date.getHours(); }); | |
break; | |
} | |
} | |
else { | |
var sortedKeys = Object.keys(tuples); | |
} | |
var transposedObj = sortedKeys.reduce(function(cumulativeObj, currKey) { | |
currObj = tuples[currKey]; | |
Object.keys(currObj).forEach(function(entry) { | |
if (!(entry in cumulativeObj)) { | |
cumulativeObj[entry] = { | |
name: entry, | |
x: [], | |
y: [] | |
}; | |
if (cumulativeObj[entry].name == 'null') { | |
cumulativeObj[entry].name = 'All' | |
} | |
} | |
points = cumulativeObj[entry]; | |
if (!_.isUndefined(options.period)) { | |
var date = new Date(currKey); | |
switch (options.period) { | |
case 'year': | |
date = date.getMonth(); | |
points.x.push(months[date]); | |
break; | |
case 'week': | |
date = date.getDay(); | |
points.x.push(weekDays[date]); | |
break; | |
case 'day': | |
date = date.getHours(); | |
points.x.push(hours[date]); | |
break; | |
} | |
} | |
else { | |
points.x.push(currKey.replace('T', ' ')); | |
} | |
points.y.push(currObj[entry][measureIdx]); | |
}); | |
return cumulativeObj; | |
}, {}); | |
var result = { | |
traces: [], | |
data: [] | |
}; | |
Object.keys(transposedObj).forEach(function(entry) { | |
result.data.push(transposedObj[entry]); | |
}); | |
result.data = _.sortBy(result.data, ['name']); | |
var i = 0; | |
_.forEach(result.data, function(item, key) { | |
if (options.chart === 'line') { | |
result.data[key].line = {shape: 'linear'}; | |
} | |
if (options.chart === 'bar') { | |
result.data[key].type = 'bar'; | |
} | |
if (options.chart === 'box') { | |
result.data[key].type = 'box'; | |
} | |
if (options.orientation) { | |
result.data[key].orientation = 'h'; | |
var temp = result.data[key].x; | |
result.data[key].x = result.data[key].y; | |
result.data[key].y = temp; | |
} | |
result.data[key].marker = {}; | |
result.data[key].marker.color = colors[i]; | |
i++; | |
}); | |
return {result: result, name: name}; | |
} | |
}; | |
onmessage = function(e) { | |
var workerResult = tasks[e.data.task](e.data.data, e.data.options, e.data.name); | |
postMessage(workerResult); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment