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: I879356fd3d3f197efa51d4c16b5e27a1cccc153e
---
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: I879356fd3d3f197efa51d4c16b5e27a1cccc153e
Gerrit-PatchSet: 1
Gerrit-Project: analytics/wikistats2
Gerrit-Branch: master
Gerrit-Owner: Nuria <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits