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