Skip to content

Instantly share code, notes, and snippets.

@tdharris
Created March 10, 2016 21:46
Show Gist options
  • Save tdharris/35843fb1f08621238174 to your computer and use it in GitHub Desktop.
Save tdharris/35843fb1f08621238174 to your computer and use it in GitHub Desktop.
How to solve calculating totalForAllResults.. some reference issue here..
if(err) return done(err);
app.logger.google(info, 'Finished processing days from date range:', results.length);
app.logger.google(info, 'Beginning to calculate totalsForAllResults');
/*---------- Get totalsForAllResults ----------*/
var totalsForAllResults = {};
// loop through each day
results.forEach(function(day){
// loop through the array of data
day.data.forEach(function(item) {
// loop through the object keys in data
Object.keys(item).forEach(function(key) {
var value = item[key];
// keep running sum of any metrics
app.logger.warn(value, typeof value); // for troubleshooting
if(typeof value == 'number') {
if(!totalsForAllResults[key]) totalsForAllResults[key] = 0;
totalsForAllResults[key] += value; // add day value to running sum
}
/**
* TODO: problems...
* TopTids handling
* - keep running sum of metrics per tid (there is likely a better scalable approach)
*/
if(key == 'pageTitle') {
// make a container for top tids
var hashOfPageTitle = hash(item.pageTitle);
if(!totalsForAllResults.tids) totalsForAllResults.tids = {};
if(!totalsForAllResults.tids[hashOfPageTitle]) {
totalsForAllResults.tids[hashOfPageTitle] = {
pagePath: item.pagePath,
pageTitle: item.pageTitle,
pageviews: 0
};
} else {
// sum of metrics for topTids
totalsForAllResults.tids[hashOfPageTitle].pageviews += item.pageviews;
}
}
});
if(day.data.length <= 1) day.data = day.data[0]; // simplify encapsulation of data if only one
});
});
done(null, {
totalsForAllResults: totalsForAllResults,
days: results
});
// TOP-TID Counts should be:
// mdadm 24 | 24
// zesuser 23 25 21 | 69
// ^M 27 34 | 61
@tdharris
Copy link
Author

solved by removing the else surrounding l41.. woops

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment