-
Notifications
You must be signed in to change notification settings - Fork 0
/
histogramManger.js
205 lines (182 loc) · 8.17 KB
/
histogramManger.js
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
class HistogramManager {
types = ["learn", "gene", "individual", "social"];
constructor(histograms, type = "learn") {
this.histogramSelectorElement =
document.getElementById("histogramType");
this.histogramSelectorCallback = event => this.histogramType = event.target.value;
this.histogramSelectorElement
?.addEventListener("change", this.histogramSelectorCallback);
this.histogramCollectionRateElement =
document.getElementById("histogramCollectionRate");
this.histogramCollectionCallback = event => this.collectionRate = event.target.value;
this.histogramCollectionRateElement
?.addEventListener("change", this.histogramCollectionCallback);
this.histogramDrawLastElement =
document.getElementById("histogramDrawLast");
this.histogramDrawLastCallback = event => this.drawLast = Number(event.target.value);
this.histogramDrawLastElement
?.addEventListener("change", this.histogramDrawLastCallback);
this.histogramDownloadCurrentElement = document.getElementById("histogramDownloadCurrent");
this.histogramDownloadCurrentCallback = _ => this.downloadCSVForType();
this.histogramDownloadCurrentElement
?.addEventListener("click", this.histogramDownloadCurrentCallback);
this.histogramDownloadAllElement = document.getElementById("histogramDownloadAll");
this.histogramDownloadAllCallback = _ => this.downloadCSVForAllTypes();
this.histogramDownloadAllElement
?.addEventListener("click", this.histogramDownloadAllCallback);
this.histogramUploadCurrentElement = document.getElementById("histogramUploadCurrent");
this.histogramUploadCurrentCallback = _ => this.uploadForType();
this.histogramUploadCurrentElement
?.addEventListener("click", this.histogramUploadCurrentCallback);
this.histogramUploadAllElement = document.getElementById("histogramUploadAll");
this.histogramUploadAllCallback = _ => this.uploadForAllTypes();
this.histogramUploadAllElement
?.addEventListener("click", this.histogramUploadAllCallback);
this.histogramDrawingElement = document.getElementById("histogramDrawing");
this.histogramDrawingElementCallback = _ => this.histogramType = this.histogramType;
this.histogramDrawingElement
?.addEventListener("click", this.histogramDrawingElementCallback);
this.histograms = histograms;
this.drawLast = histograms[0][0][type].drawLast;
this.collectionRate = histograms[0][0][type].unitTimePerUpdate;
this.histogramType = type;
}
drop() {
// I hate this. Big mistake :(
this.histogramSelectorElement
?.removeEventListener("change", this.histogramSelectorCallback);
this.histogramCollectionRateElement
?.removeEventListener("change", this.histogramCollectionCallback);
this.histogramDrawLastElement
?.removeEventListener("change", this.histogramDrawLastCallback);
this.histogramDownloadCurrentElement
?.removeEventListener("click", this.histogramDownloadCurrentCallback);
this.histogramDownloadAllElement
?.removeEventListener("click", this.histogramDownloadAllCallback);
this.histogramUploadCurrentElement
?.removeEventListener("click", this.histogramUploadCurrentCallback);
this.histogramUploadAllElement
?.removeEventListener("click", this.histogramUploadAllCallback);
this.histogramDrawingElement
?.removeEventListener("click", this.histogramDrawingElementCallback);
}
get collectionRate() { return this._collectionRate; }
set collectionRate(collectionRate) {
this._collectionRate = collectionRate;
for (const row of this.histograms) {
for (const histogramInfo of row) {
this.types.forEach(type =>
histogramInfo[type].unitTimePerUpdate = collectionRate);
}
}
this.histogramCollectionRateElement.value = this.collectionRate;
}
get drawLast() { return this._drawLast; }
set drawLast(drawLast) {
this._drawLast = drawLast;
for (const row of this.histograms) {
for (const histogramInfo of row) {
this.types.forEach(type =>
histogramInfo[type].drawLast = drawLast);
}
}
this.histogramDrawLastElement.value = this.drawLast;
}
get histogramType() { return this._histogramType; }
set histogramType(type) {
if (type) this._histogramType = type;
this.histograms.forEach(row =>
row.forEach(histogramInfo =>
Object.keys(histogramInfo).forEach(key =>
histogramInfo[key].isDrawing =
this.histogramDrawingElement.checked && (key === type))));
if (this.histogramSelectorElement)
this.histogramSelectorElement.value = this.histogramType;
}
toCSVTextArray(type) {
type = type ?? this.histogramType;
const csvTextArray = [];
for (const [i, row] of this.histograms.entries()) {
csvTextArray[i] = [];
for (const [j, histogramInfo] of row.entries()) {
csvTextArray[i][j] = histogramInfo[type].toCSVText();
}
}
return csvTextArray;
}
downloadZip(zip) {
console.log("Beginning to Zip");
let lastProgress = -1;
zip.generateAsync(
{
type: "blob",
compression: "DEFLATE",
compressionOptions: {
level: 9,
},
},
metadata => {
let newProgress = floor(metadata.percent.toFixed(0) / 5) * 5;
if (lastProgress === newProgress) return;
lastProgress = newProgress;
console.log(`Zipping Progress: ${metadata.percent.toFixed(0)}%`)
},
).then(content => {
const currentDate = new Date();
const dateString = `${currentDate.getFullYear()}-${currentDate.getMonth()}-${currentDate.getDate()}-${currentDate.getHours()}-${currentDate.getMinutes()}-${currentDate.getSeconds()}`;
const allCSVData = new Blob([content]);
const url = URL.createObjectURL(allCSVData);
const link = document.createElement("a");
link.href = url;
link.target = "_blank";
link.download = `${dateString}.zip`;
link.click();
});
}
downloadCSVForType(type) {
const zip = new JSZip();
for (const [i, row] of this.toCSVTextArray(type).entries()) {
for (const [j, csvData] of row.entries()) {
zip.file(`${i}.${j}.csv`, csvData);
}
}
this.downloadZip(zip);
}
downloadCSVForAllTypes() {
const zip = new JSZip();
for (const type of this.types) {
for (const [i, row] of this.toCSVTextArray(type).entries()) {
for (const [j, csvData] of row.entries()) {
zip.folder(type).file(`${i}.${j}.csv`, csvData);
}
}
}
this.downloadZip(zip);
}
uploadForType(type) {
type = type ?? this.histogramType;
let ticks = 0;
const data = this.histograms.reduce((acc, row, i) => {
acc.push([]);
row.forEach((histogramInfo, j) => {
acc[i][j] = histogramInfo[type].allCounts.slice();
ticks = ticks || acc[i][j].length;
});
return acc;
}, []);
const payload = {
db: "genetic-modeling",
collection: "histogramData",
data: {
data,
type,
ticks,
date: new Date(),
},
};
console.log("Sending: ", payload)
if (connection.isConnected) socket.emit("insert", payload);
else alert("Could not upload. Server is not connected.");
}
uploadForAllTypes() { this.types.forEach(type => this.uploadForType(type)); }
}