[MediaWiki-commits] [Gerrit] analytics/wikistats2[master]: Replacing JSON download with CSV download

2018-01-10 Thread Fdans (Code Review)
Fdans has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/401814 )

Change subject: Replacing JSON download with CSV download
..


Replacing JSON download with CSV download

While ticket asks to add a csv download option I think it makes sense
to have just one download option and have that be in csv format.

In order to convert seamlessly from json to csv the json data is flattened
before being passed to d3 to do the conversion.

Bug: T183192
Change-Id: I879356fd3d3f197efa51d4c16b5e27a1153e
---
M src/components/detail/GraphPanel.vue
M src/models/GraphModel.js
2 files changed, 40 insertions(+), 3 deletions(-)

Approvals:
  jenkins-bot: Verified
  Fdans: Looks good to me, approved



diff --git a/src/components/detail/GraphPanel.vue 
b/src/components/detail/GraphPanel.vue
index 520077e..8beff48 100644
--- a/src/components/detail/GraphPanel.vue
+++ b/src/components/detail/GraphPanel.vue
@@ -78,6 +78,7 @@
 import TableChart from './chart/TableChart';
 import EmptyChart from './chart/EmptyChart';
 import StatusOverlay from '../StatusOverlay';
+import *  as d3Formatter from 'd3-dsv';
 
 export default {
 name: 'graph-panel',
@@ -152,10 +153,10 @@
 this.$emit('toggleFullscreen');
 },
 download () {
-const data = this.graphModel.graphData;
+let csvData = 
d3Formatter.csvFormat(this.graphModel.downloadData());
 let a = window.document.createElement('a');
-a.href = window.URL.createObjectURL(new 
Blob([JSON.stringify(data)], {type: 'text/json'}));
-a.download = this.graphModel.config.name + '.json';
+a.href = window.URL.createObjectURL(new Blob([csvData], {type: 
'text/csv'}));
+a.download = this.graphModel.config.name + '.csv';
 document.body.appendChild(a);
 a.click();
 document.body.removeChild(a);
diff --git a/src/models/GraphModel.js b/src/models/GraphModel.js
index dd27eb6..08180a8 100644
--- a/src/models/GraphModel.js
+++ b/src/models/GraphModel.js
@@ -53,6 +53,19 @@
 const month = createDate(ts);
 return {month: month, total: row[yAxisValue]};
 });
+
+
+}
+
+/** Data for downloading as csv needs to be a flat key/value pair object 
**/
+downloadData(){
+let jsonData = JSON.parse(JSON.stringify(this.graphData));
+// data is an array of objects that might be deeply nested (with more 
than 1 level)
+let flatJSONData = []
+_.forEach(jsonData, function(item){
+flatJSONData.push(flatten(item));
+});
+return flatJSONData;
 }
 
 refreshData () {
@@ -140,4 +153,27 @@
 }
 }
 
+/**
+* Convert an nested object in a set of flat key value pairs
+* {some: { a:1, b:2 }} will be converted to {some.a :1, some.b:2}
+**/
+function flatten(obj) {
+let accumulator = {};
+
+function _flatten(obj, keyPrefix) {
+
+ _.forEach(obj, function(value, key){
+
+if (typeof(obj[key]) === 'object'){
+_flatten(obj[key], key);
+
+} else {
+!keyPrefix ? accumulator[key] = value : accumulator[keyPrefix 
+'.'+ key] = value;
+}
+})
+}
+_flatten(obj);
+return accumulator;
+}
+
 export default GraphModel;

-- 
To view, visit https://gerrit.wikimedia.org/r/401814
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I879356fd3d3f197efa51d4c16b5e27a1153e
Gerrit-PatchSet: 7
Gerrit-Project: analytics/wikistats2
Gerrit-Branch: master
Gerrit-Owner: Nuria 
Gerrit-Reviewer: Fdans 
Gerrit-Reviewer: Milimetric 
Gerrit-Reviewer: Nuria 
Gerrit-Reviewer: jenkins-bot <>

___
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits


[MediaWiki-commits] [Gerrit] analytics/wikistats2[master]: Replacing JSON download with CSV download

2018-01-03 Thread Nuria (Code Review)
Nuria has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/401814 )

Change subject: Replacing JSON download with CSV download
..

Replacing JSON download with CSV download

While ticket asks to add a csv download option I think it makes sense
to have just one download option and have that be in csv format.

In order to convert seamlessly from json to csv the json data is flattened
before being passed to d3 to do the `conversion

Bug: T183192
Change-Id: I879356fd3d3f197efa51d4c16b5e27a1153e
---
M src/components/detail/GraphPanel.vue
1 file changed, 37 insertions(+), 2 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/analytics/wikistats2 
refs/changes/14/401814/1

diff --git a/src/components/detail/GraphPanel.vue 
b/src/components/detail/GraphPanel.vue
index 520077e..fcfc3f9 100644
--- a/src/components/detail/GraphPanel.vue
+++ b/src/components/detail/GraphPanel.vue
@@ -78,6 +78,7 @@
 import TableChart from './chart/TableChart';
 import EmptyChart from './chart/EmptyChart';
 import StatusOverlay from '../StatusOverlay';
+import *  as d3Formatter from 'd3-dsv';
 
 export default {
 name: 'graph-panel',
@@ -152,10 +153,44 @@
 this.$emit('toggleFullscreen');
 },
 download () {
+/**
+* Convert an nested object in a set of flat key value pairs
+* {some: { a:1, b:2 }} will be converted to {some.a :1, some.b:2}
+**/
+
+function flatten(obj) {
+let accumulator = {};
+
+
+ function _flatten(obj, keyPrefix) {
+
+ _.forEach(obj, function(value, key){
+
+if (typeof(obj[key]) === 'object'){
+_flatten(obj[key], key);
+
+} else {
+!keyPrefix ? accumulator[key] = value : 
accumulator[keyPrefix +'.'+ key] = value;
+}
+
+})
+}
+_flatten(obj);
+return accumulator;
+}
+
 const data = this.graphModel.graphData;
+let jData = JSON.parse(JSON.stringify(data));
+
+// data is an array of objects that might be deeply nested (with 
more than 1 level)
+let flattenjData = []
+_.forEach(jData, function(item){
+flattenjData.push(flatten(item));
+})
+let cvsdata = d3Formatter.csvFormat(flattenjData);
 let a = window.document.createElement('a');
-a.href = window.URL.createObjectURL(new 
Blob([JSON.stringify(data)], {type: 'text/json'}));
-a.download = this.graphModel.config.name + '.json';
+a.href = window.URL.createObjectURL(new Blob([cvsdata], {type: 
'text/csv'}));
+a.download = this.graphModel.config.name + '.csv';
 document.body.appendChild(a);
 a.click();
 document.body.removeChild(a);

-- 
To view, visit https://gerrit.wikimedia.org/r/401814
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I879356fd3d3f197efa51d4c16b5e27a1153e
Gerrit-PatchSet: 1
Gerrit-Project: analytics/wikistats2
Gerrit-Branch: master
Gerrit-Owner: Nuria 

___
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits