download-csv-js
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
download-csv-js [2022/05/12 08:35] – [Button release script] emozolyak | download-csv-js [2023/10/15 18:07] (current) – [Script text] emozolyak | ||
---|---|---|---|
Line 23: | Line 23: | ||
<code javascript> | <code javascript> | ||
- | const DATA = { | + | const DATA = { |
- | | + | TIME: {START: |
- | | + | DOWNLOAD_BTN: |
- | | + | GRAPH_ID: 1, // placeholder for the graph id that |
- | | + | GRAPH_SELECTION_REG: |
}; | }; | ||
- | const MAX_SLICES = 2999; // max number of the slices in the graph data api call | + | const MAX_SLICES |
- | const TIMEOUT = 5e3; | + | TIMEOUT |
- | const START_GRAPH_ID = 1; // start number of the range for graphs ids | + | START_GRAPH_ID = 1, // start number of the range for graphs ids |
- | const END_GRAPH_ID = 2; // end number | + | END_GRAPH_ID |
+ | |||
let _i; | let _i; | ||
- | let preventMultiply = false; | + | var preventMultiply = false, |
- | let dataToSend = {}; | + | dataToSend = {}, |
- | let promises = []; | + | promises = []; |
+ | |||
App.on(' | App.on(' | ||
+ | |||
function triggerUponRegChange(newValuesArray) { | function triggerUponRegChange(newValuesArray) { | ||
- | let ifButtonPressed = newValuesArray.filter(readVal => | + | let ifButtonPressed = (newValuesArray.length > 0) |
- | | + | && |
- | && parseInt(readVal.value) > 0).length > 0; | + | |
+ | | ||
if (ifButtonPressed && !preventMultiply) { | if (ifButtonPressed && !preventMultiply) { | ||
+ | preventMultiply = true; | ||
+ | console.log(' | ||
let selectedGraph = new Register(DATA.GRAPH_SELECTION_REG).getValue(); | let selectedGraph = new Register(DATA.GRAPH_SELECTION_REG).getValue(); | ||
selectedGraph = parseInt(selectedGraph.replace(/ | selectedGraph = parseInt(selectedGraph.replace(/ | ||
DATA.GRAPH_ID = (selectedGraph >= START_GRAPH_ID && selectedGraph <= END_GRAPH_ID) ? selectedGraph : 1; | DATA.GRAPH_ID = (selectedGraph >= START_GRAPH_ID && selectedGraph <= END_GRAPH_ID) ? selectedGraph : 1; | ||
- | | + | |
- | Promise.all(makeApiCall()).then(exportDataAsCSVToBrowser).catch(e => console.log(e)); | + | Promise.all(makeApiCall()) |
+ | | ||
+ | | ||
+ | .finally(() => {preventMultiply = false ; console.log(' | ||
} | } | ||
} | } | ||
- | + | ||
- | function | + | function |
- | let paramsSet = []; | + | |
- | calcSlices(parseInt(new Register(DATA.TIME.START).getValue()), | + | |
- | | + | |
| | ||
- | | + | let slicesParams |
- | if (s > e) { | + | |
- | console.log(' | + | |
- | } | + | |
- | if (e - s <= MAX_SLICES) | + | |
- | paramsSet.push({start: | + | |
- | else { | + | |
- | | + | |
- | paramsSet.push({start: s, end: newEnd, slices: MAX_SLICES}); | + | |
- | calcSlices(newEnd + 1, e); | + | |
- | } | + | |
- | } | + | |
- | return paramsSet; | + | |
- | } | + | |
- | + | ||
- | function makeApiCall() { | + | |
- | let slicesParams | + | |
dataToSend = {}; | dataToSend = {}; | ||
- | promises = []; | + | promises = []; |
- | | + | |
- | setTimeout(function(){clearTimeout(_i); | + | |
- | | + | |
slicesParams.forEach(function(paramsSet, | slicesParams.forEach(function(paramsSet, | ||
let promise = new Promise(function(resolve, | let promise = new Promise(function(resolve, | ||
Line 90: | Line 75: | ||
" | " | ||
" | " | ||
- | " | ||
}, | }, | ||
dataType: ' | dataType: ' | ||
Line 105: | Line 89: | ||
promises.push(promise); | promises.push(promise); | ||
}); | }); | ||
- | | + | |
return promises; | return promises; | ||
} | } | ||
+ | |||
function exportDataAsCSVToBrowser() { | function exportDataAsCSVToBrowser() { | ||
let response = []; | let response = []; | ||
Line 128: | Line 112: | ||
rep.push(row); | rep.push(row); | ||
}); | }); | ||
- | | + | |
if (window.navigator.msSaveBlob) { // IE | if (window.navigator.msSaveBlob) { // IE | ||
let blobObject = new Blob([rep.join(" | let blobObject = new Blob([rep.join(" | ||
Line 142: | Line 126: | ||
} | } | ||
} | } | ||
+ | } | ||
+ | |||
+ | function getSlicesFromTS() { | ||
+ | let paramsSet = []; | ||
+ | calcSlices(parseInt(new Register(DATA.TIME.START).getValue()), | ||
+ | | ||
+ | |||
+ | function calcSlices(s, | ||
+ | if (s > e) { | ||
+ | console.log(' | ||
+ | } | ||
+ | if (e - s <= MAX_SLICES) | ||
+ | paramsSet.push({start: | ||
+ | else { | ||
+ | let newEnd = s + MAX_SLICES - 1; | ||
+ | paramsSet.push({start: | ||
+ | calcSlices(newEnd + 1, e); | ||
+ | } | ||
+ | } | ||
+ | return paramsSet; | ||
} | } | ||
download-csv-js.txt · Last modified: 2023/10/15 18:07 by emozolyak