-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdataManager.js
More file actions
83 lines (74 loc) · 2.21 KB
/
dataManager.js
File metadata and controls
83 lines (74 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
class DataModelsManager {
constructor() {
this.data = null;
this.models = null;
}
parseDataCSV(csvText) {
const parsedData = d3.csvParse(csvText, function (d) {
return {
time: +d.time,
flux: +d.flux,
flux_err: +d.flux_err,
fwhm: +d.fwhm,
pixel_shift: +d.pixel_shift
};
});
return parsedData;
}
parseModelsCSV(csvText) {
const lines = csvText.split('\n');
const columns = lines[0].trim().split(',');
const parsedData = d3.csvParse(csvText, function (d) {
const rowData = {};
columns.forEach((column, index) => {
rowData[column] = +d[index];
});
return rowData;
});
return parsedData;
}
fetchFile(token, fileType, parseFunction) {
const url = token ? `/get_${fileType}/${token}` : `/get_${fileType}/`;
console.log(`Fetching ${fileType} - URL:`, url);
return fetch(url)
.then(response => {
console.log(`Response for ${fileType}:`, response);
if (!response.ok) {
throw new Error(`Network response was not ok, status: ${response.status}`);
}
return response.arrayBuffer();
})
.then(arrayBuffer => {
const decompressedData = pako.inflate(arrayBuffer, { to: 'string' });
return decompressedData;
})
.then(response_data => parseFunction(response_data))
.catch(error => {
console.error(`Error during fetch ${fileType}:`, error);
throw error;
});
}
fetchData(token = null) {
return Promise.all([
this.fetchFile(token, 'data', this.parseDataCSV),
this.fetchFile(token, 'models', this.parseModelsCSV)
]).then(([newData, newModels]) => ({ newData, newModels }));
}
getNewData(token = null) {
return new Promise((resolve, reject) => {
this.fetchData(token)
.then(({ newData, newModels }) => {
this.data = newData;
this.models = newModels;
console.log('Data:', this.data);
console.log('Models:', this.models);
resolve();
})
.catch(error => {
console.error('Error during getNewData:', error);
reject(error);
});
});
}
}
export default DataModelsManager;