-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.js
executable file
·137 lines (95 loc) · 3.1 KB
/
log.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
/*
AUTHOR: LAURA
PROJECT: ACCENTURE FABRIC THAT REMEMBERS
DESC: THIS CODE READS FROM AND WRITES DATA TO CHROME LOCAL STORAGE. ALSO MANAGES IT
SO ITS ALWAYS THE MOST RECENT DATA.
TO SEE THE DATA OPEN CHROME CODE INSPECTOR -> APPLICATION -> LOCAL STORAGE
SAVES VALUES FOR ALL SESSIONS (EVEN IF YOU CLOSE AND REOPEN)
*/
var LOGMAX = 650000;
//Update to Write to Local Storage.
function logData(data){
//get date in seconds
var timestamp = Math.floor(Date.now() / 1000);
timestamp = "aftr:"+timestamp+":"+data.region
let value = data.scale;
let min = timestamp;
let min_x = -1;
//pop the last value from the list and write a new one.
//this ensures that the last 650000 values will be recorded and displayed
if(localStorage.length >= LOGMAX){
console.log("LOCAL STORAGE FULL, OVERWRITING PREVIOUS DATA");
//remove the oldest entry // write the
for(var x in localStorage){
var ts = split(x, ":")
if(ts[0] < min){
min = ts[0];
min_x = x;
}
}
localStorage.removeItem(min_x);
}
localStorage.setItem(timestamp, value);
}
//prints the size of each entry and total use of the local store
//each entry is ~ 0.000008 MB
//store has max of 5MB
// we can safely store 625000 entries before we over run
function calculateLocalStoreUsage(){
var total = 0;
for(var x in localStorage) {
var amount = (localStorage[x].length * 2) / 1024 / 1024;
total += amount;
console.log( x + " = " + amount.toFixed(8) + " MB");
}
//console.log( "Total: " + total.toFixed(8) + " MB");
}
//careful! calling this from console will clear all data in local storage
function clearLocalStorage(){
var total = 0;
for(var x in localStorage) {
localStorage.removeItem(x);
}
console.log( "LOCAL STORAGE CLEARED");
console.log("local storage size now "+localStorage.length);
}
//call this from console when you want to write a file of the data
function downloadLocalStorage(){
let d_log = loadRawLog();
let oldest_stamp = d_log[0].timestamp;
let newest_stamp = d_log[0].timestamp
for(var d in d_log){
if(d_log[d].timestamp > newest_stamp) newest_stamp = d_log[d].timestamp;
if(d_log[d].timestamp < oldest_stamp) oldest_stamp = d_log[d].timestamp;
}
let writer = createWriter(oldest_stamp+"_"+newest_stamp+".csv");
writer.write(["timestamp", "region", "value"]);
writer.write('\n');
for(var d in d_log){
writer.write([d_log[d].timestamp, d_log[d].region, d_log[d].value]);
writer.write('\n');
}
writer.close();
}
//load raw log into memory so we can process it for the visualization
//this will be called once everytime we switch into vis mode, though log entries may be
//accumulated in the backgroudn that won't affect this
function loadRawLog(){
//clear the log so we can load it fresh
var d_log = [];
//console.log(localStorage.length);
for(var x in localStorage) {
if(typeof(localStorage[x]) == "string"){
time_region = x.split(":")
if(time_region[0].localeCompare("aftr") === 0){
value = localStorage[x];
d_log.push({
timestamp: time_region[1],
region: time_region[2],
value: value}
);
}
}
}
return d_log;
}