Remove old chart data processing code.

This commit is contained in:
Marina Samuel 2014-06-10 12:20:23 -04:00
Родитель 80b8289213
Коммит 91f7edb8d4
2 изменённых файлов: 0 добавлений и 349 удалений

Просмотреть файл

@ -1,229 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
const {storage} = require("sdk/simple-storage");
const {Cu} = require("chrome");
const {TypeNamespace} = require("TypeNamespace");
Cu.import("resource://gre/modules/Services.jsm");
function ChartDataProcessor() {
}
function TimelineDataProcessor() {
}
function WeightIntensityDataProcessor() {
}
function IntentInterestDataProcessor() {
}
DataProcessorHelper = {
initChartInStorage: function(dataNameString) {
if (!storage.chartData) {
storage.chartData = {};
}
if (!storage.chartData[dataNameString]) {
storage.chartData[dataNameString] = {};
}
},
iterateOverTypeNamespace: function(bucketData, storageData, dataProcessingFunction) {
for (let type in bucketData) {
for (let namespace in bucketData[type]) {
if (!storageData[type]) {
storageData[type] = {};
}
if (!storageData[type][namespace]) {
storageData[type][namespace] = {};
}
dataProcessingFunction(bucketData[type][namespace], storageData[type][namespace], type, namespace);
}
}
},
}
ChartDataProcessor.prototype = {
clear: function() {
if (storage.chartData) {
storage.chartData.genericChartData = {};
}
},
consume: function(bucketData) {
DataProcessorHelper.initChartInStorage("genericChartData");
let storageData = storage.chartData.genericChartData;
for (let day in bucketData) {
for (let type in bucketData[day]) {
for (let namespace in bucketData[day][type]) {
if (!storageData[type]) {
storageData[type] = {};
}
if (!storageData[type][namespace]) {
storageData[type][namespace] = new TypeNamespace(type, namespace);
}
if (!(storageData[type][namespace] instanceof TypeNamespace)) {
storageData[type][namespace] = TypeNamespace.deserialize(storageData[type][namespace]);
}
for (let category in bucketData[day][type][namespace]) {
let domainsToCountMap = bucketData[day][type][namespace][category];
storageData[type][namespace].addDayToCategory(category, day, domainsToCountMap);
}
}
}
}
for (let type in storageData) {
for (let namespace in storageData[type]) {
if (!storageData[type][namespace]) {
storageData[type][namespace] = new TypeNamespace(type, namespace);
}
if (!(storageData[type][namespace] instanceof TypeNamespace)) {
typeNamespace = storageData[type][namespace] = TypeNamespace.deserialize(typeNamespace);
}
storageData[type][namespace].sortCategories("dayCount", "x");
storageData[type][namespace].sortCategories("maxWeight", "y");
storageData[type][namespace].setXYMaxMin();
storageData[type][namespace].setIntentAndInterestDistForCategories();
}
}
return storage.chartData.genericChartData;
}
}
TimelineDataProcessor.prototype = {
clear: function() {
if (storage.chartData) {
storage.chartData.timelineData = {};
}
},
consume: function(bucketData) {
DataProcessorHelper.initChartInStorage("timelineData");
DataProcessorHelper.iterateOverTypeNamespace(bucketData, storage.chartData.timelineData, (bucketData, storedData) => {
let chartJSON = [];
let interestList = Object.keys(bucketData.categories);
for (let i = 0; i < interestList.length; i++) {
let dataPoints = bucketData.categories[interestList[i]].days;
chartJSON.push({
key: interestList[i],
values: Object.keys(dataPoints).map(key => {
dataPoints[key]["y"] = i;
return dataPoints[key];
})
});
}
storedData["interestList"] = interestList;
storedData["chartJSON"] = chartJSON;
});
Services.obs.notifyObservers(null, "chart-update",
JSON.stringify({"type": "timeline", "data": storage.chartData.timelineData}));
return bucketData;
},
}
WeightIntensityDataProcessor.prototype = {
clear: function() {
if (storage.chartData) {
storage.chartData.weightIntensityData = {};
}
},
consume: function(bucketData) {
DataProcessorHelper.initChartInStorage("weightIntensityData");
DataProcessorHelper.iterateOverTypeNamespace(bucketData, storage.chartData.weightIntensityData, (bucketData, storedData) => {
// pointToInterestsMap is used to make up for a bug in nvd3 where multiple points can't
// appear in the same location.
let pointToInterestsMap = {};
let values = [];
storedData["xMin"] = bucketData.xMin;
storedData["yMin"] = bucketData.yMin;
storedData["xMax"] = bucketData.xMax;
storedData["yMax"] = bucketData.yMax;
for (let categoryName in bucketData.categories) {
let x = bucketData.categories[categoryName].x;
let y = bucketData.categories[categoryName].y;
let hash = x.toString() + y.toString();
if (!pointToInterestsMap[hash]) {
pointToInterestsMap[hash] = [];
values.push({"x": x, "y": y});
}
pointToInterestsMap[hash].push(categoryName);
}
storedData["chartJSON"] = [{
key: "key",
values: values
}];
storedData["pointToInterestsMap"] = pointToInterestsMap;
});
Services.obs.notifyObservers(null, "chart-update",
JSON.stringify({"type": "weightIntensity", "data": storage.chartData.weightIntensityData}));
return bucketData;
},
}
IntentInterestDataProcessor.prototype = {
clear: function() {
if (storage.chartData) {
storage.chartData.intentInterestData = {};
}
},
_createChartData: function(domainList, storedData, dataType, title) {
let chartData = [];
for (let domain in domainList) {
let obj = {
"label": domain,
"value": domainList[domain]
};
chartData.push(obj);
}
storedData[dataType].push({
"chartJSON": chartData,
"title": title
});
},
consume: function(bucketData) {
DataProcessorHelper.initChartInStorage("intentInterestData");
DataProcessorHelper.iterateOverTypeNamespace(bucketData, storage.chartData.intentInterestData, (bucketData, storedData) => {
storedData["sortedInterests"] = [];
storedData["sortedIntents"] = [];
for (let intentData of bucketData.sortedIntents.splice(0, 10)) {
let maxWeightDate = intentData.maxWeightDate;
let domainList = intentData.days[maxWeightDate]["domainList"];
let maxIntentDate = (new Date(intentData.days[maxWeightDate]["x"])).toLocaleDateString();
let title = intentData.name + " (" + maxIntentDate + ")";
let chartJSON = this._createChartData(domainList, storedData, "sortedIntents", title);
}
for (let interestData of bucketData["sortedInterests"].splice(0, 10)) {
let domainList = {};
for (let dateInfo in interestData.days) {
for (let domain in interestData.days[dateInfo]["domainList"]) {
if (!domainList[domain]) {
domainList[domain] = 0;
}
domainList[domain] += interestData.days[dateInfo]["domainList"][domain];
}
}
let title = interestData.name;
let chartJSON = this._createChartData(domainList, storedData, "sortedInterests", title);
}
});
Services.obs.notifyObservers(null, "chart-update",
JSON.stringify({"type": "intentInterest", "data": storage.chartData.intentInterestData}));
},
}
exports.ChartDataProcessor = ChartDataProcessor;
exports.TimelineDataProcessor = TimelineDataProcessor;
exports.WeightIntensityDataProcessor = WeightIntensityDataProcessor;
exports.IntentInterestDataProcessor = IntentInterestDataProcessor;

Просмотреть файл

@ -1,120 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
const test = require("sdk/test");
const {testUtils} = require("./helpers");
const {storage} = require("sdk/simple-storage");
const sampleData = require("./sampleData");
const chartData = require("./chartData");
const {ChartDataProcessor} = require("ChartDataProcessor");
const {TimelineDataProcessor} = require("ChartDataProcessor");
const {WeightIntensityDataProcessor} = require("ChartDataProcessor");
const {IntentInterestDataProcessor} = require("ChartDataProcessor");
exports["test consume"] = function test_consume(assert) {
let chartDataProcessor = new ChartDataProcessor();
let timelineDataProcessor = new TimelineDataProcessor();
let weightIntensityDataProcessor = new WeightIntensityDataProcessor();
let intentInterestDataProcessor = new IntentInterestDataProcessor();
chartDataProcessor.clear();
timelineDataProcessor.clear();
weightIntensityDataProcessor.clear();
intentInterestDataProcessor.clear();
// Testing ChartDataProcessor's consume().
let chartDataProcessorResults = chartDataProcessor.consume(sampleData.dayAnnotatedThree);
testUtils.isIdentical(assert, JSON.stringify(storage.chartData.genericChartData),
JSON.stringify(chartData.dayAnnotatedThreeChartProcessorConsumeResults), "Unexpected chart data in storage");
// Test expected properties are there.
for (let type in chartDataProcessorResults) {
for (let namespace in chartDataProcessorResults[type]) {
assert.equal(Object.keys(chartDataProcessorResults[type][namespace]).length, 9);
assert.ok(chartDataProcessorResults[type][namespace].hasOwnProperty("_type"));
assert.ok(chartDataProcessorResults[type][namespace].hasOwnProperty("_namespace"));
assert.ok(chartDataProcessorResults[type][namespace].hasOwnProperty("categories"));
assert.ok(chartDataProcessorResults[type][namespace].hasOwnProperty("sortedInterests"));
assert.ok(chartDataProcessorResults[type][namespace].hasOwnProperty("sortedIntents"));
assert.ok(chartDataProcessorResults[type][namespace].hasOwnProperty("xMax"));
assert.ok(chartDataProcessorResults[type][namespace].hasOwnProperty("xMin"));
assert.ok(chartDataProcessorResults[type][namespace].hasOwnProperty("yMax"));
assert.ok(chartDataProcessorResults[type][namespace].hasOwnProperty("yMin"));
}
}
// Testing TimelineDataProcessor's consume()
timelineDataProcessor.consume(chartDataProcessorResults);
testUtils.isIdentical(assert, JSON.stringify(storage.chartData.timelineData),
JSON.stringify(chartData.dayAnnotatedThreeTimelineConsumeResults), "Unexpected timeline data in storage");
// Test expected properties are there
for (let type in chartDataProcessorResults) {
for (let namespace in chartDataProcessorResults[type]) {
let timelineData = storage.chartData.timelineData;
assert.equal(Object.keys(timelineData[type][namespace]).length, 2);
assert.ok(timelineData[type][namespace].hasOwnProperty("interestList"));
assert.ok(timelineData[type][namespace].hasOwnProperty("chartJSON"));
for (let point of timelineData[type][namespace]["chartJSON"]) {
assert.equal(Object.keys(point).length, 2);
assert.ok(point.hasOwnProperty("key"));
assert.ok(point.hasOwnProperty("values"));
for (let value of point["values"]) {
assert.equal(Object.keys(value).length, 4);
assert.ok(value.hasOwnProperty("x"));
assert.ok(value.hasOwnProperty("y"));
assert.ok(value.hasOwnProperty("size"));
assert.ok(value.hasOwnProperty("domainList"));
}
}
}
}
// Testing WeightIntensityDataProcessor's consume()
weightIntensityDataProcessor.consume(chartDataProcessorResults);
testUtils.isIdentical(assert, JSON.stringify(storage.chartData.weightIntensityData),
JSON.stringify(chartData.dayAnnotatedThreeWeightIntensityConsumeResults), "Unexpected weight intensity data in storage");
// Test expected properties are there
for (let type in chartDataProcessorResults) {
for (let namespace in chartDataProcessorResults[type]) {
let weightIntensityData = storage.chartData.weightIntensityData;
assert.equal(Object.keys(weightIntensityData[type][namespace]).length, 6);
assert.ok(weightIntensityData[type][namespace].hasOwnProperty("xMax"));
assert.ok(weightIntensityData[type][namespace].hasOwnProperty("xMin"));
assert.ok(weightIntensityData[type][namespace].hasOwnProperty("yMax"));
assert.ok(weightIntensityData[type][namespace].hasOwnProperty("yMin"));
assert.ok(weightIntensityData[type][namespace].hasOwnProperty("chartJSON"));
assert.ok(weightIntensityData[type][namespace].hasOwnProperty("pointToInterestsMap"));
for (let point of weightIntensityData[type][namespace]["chartJSON"]) {
assert.equal(Object.keys(point).length, 2);
assert.ok(point.hasOwnProperty("key"));
assert.ok(point.hasOwnProperty("values"));
for (let value of point["values"]) {
assert.equal(Object.keys(value).length, 2);
assert.ok(value.hasOwnProperty("x"));
assert.ok(value.hasOwnProperty("y"));
}
}
}
}
// Testing IntentInterestDataProcessor's consume()
intentInterestDataProcessor.consume(chartDataProcessorResults);
testUtils.isIdentical(assert, JSON.stringify(storage.chartData.intentInterestData),
JSON.stringify(chartData.dayAnnotatedThreeIntentInterestConsumeResults), "Unexpected intent interest data in storage");
//Test expected properties are there.
for (let type in chartDataProcessorResults) {
for (let namespace in chartDataProcessorResults[type]) {
let intentInterestData = storage.chartData.intentInterestData;
assert.equal(Object.keys(intentInterestData[type][namespace]).length, 2);
assert.ok(intentInterestData[type][namespace].hasOwnProperty("sortedIntents"));
assert.ok(intentInterestData[type][namespace].hasOwnProperty("sortedInterests"));
}
}
}
test.run(exports);