-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathreport_utility.js
208 lines (189 loc) · 5.79 KB
/
report_utility.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
205
206
207
208
//Following function is from: http://stackoverflow.com/questions/6491463/accessing-nested-javascript-objects-with-string-key
//Usage: Object.byString(someObj, 'part3[0].name');
Object.byString = function(o, s) {
s = s.replace(/\[(\w+)\]/g, '.$1'); // convert indexes to properties
s = s.replace(/^\./, ''); // strip a leading dot
var a = s.split('.');
while (a.length) {
var n = a.shift();
if (n in o) {
o = o[n];
} else {
return;
}
}
return o;
}
// Online disk storage data structures and the presentation data structures (Data Table rows)
// are different. This function converts one into another.
// This function essentially flattens associative arrays, nested structure into a flat row.
function create_datatable_consumable_records(report, vo, keys, arr_function) {
var records = [];
for (var k in report[vo]) {
if (report[vo].hasOwnProperty(k)) {
var rec_object = report[vo][k];
var rec_entry = [];
rec_entry.push(k);
if (keys.length > 0) {
for (var i = 0; i < keys.length; i++) {
var value = Object.byString(rec_object, keys[i]);
//var value = rec_object[keys[i]];
if (value instanceof Array) {
if (arr_function == undefined) {
rec_entry.push(value.join(", "));
}
else {
all_vals = value.map(arr_function);
rec_entry.push(all_vals.join(", "));
}
}
else {
if (value == "donno") {
rec_entry.push("not sure");
}
else {
rec_entry.push(value);
}
}
}
records.push(rec_entry);
}
else {
var value = rec_object;
if (value instanceof Array) {
//Exception condition
if (vo == "common_fields") {
rec_entry.push(value.length);
}
rec_entry.push(value.join(", "));
}
else {
rec_entry.push(value);
}
records.push(rec_entry);
}
}
}
return records;
}
function get_report_duration(report) {
var start_date = new Date(report.initialize_time);
var end_date = null;
if (report.user_approved != false) {
end_date = new Date(report.user_approved);
}
else {
end_date = new Date();
}
var diff = end_date - start_date;
return get_duration(diff, "no");
}
function get_duration(total_time_diff_ms, tag_it) {
var total_seconds = Math.floor(total_time_diff_ms / 1000);
var days = Math.floor(total_seconds / 86400);
var hours = Math.floor((total_seconds % 86400)/ 3600);
var minutes = Math.floor(((total_seconds % 86400) % 3600) / 60);
var ret_val = "";
if (days == 0 && hours == 0) {
ret_val = minutes + " min";
}
else if (days == 0) {
ret_val = hours + " hours, " + minutes + " min";
}
else {
ret_val = days + " days, " + hours + " hr";
}
if (tag_it != undefined) {
return ret_val;
}
ret_val = "<span class='report-time-1'>" + ret_val + "</span>";
return ret_val;
}
function format_display_time(milliseconds, tag_it) {
var total_seconds = Math.floor(milliseconds / 1000);
var hours = Math.floor(total_seconds / 3600);
var minutes = Math.floor((total_seconds % 3600) / 60);
var seconds = Math.floor((total_seconds % 3600) % 60);
var ret_val = "";
if (milliseconds == 0) {
ret_val = "0 sec";
}
if (hours == 0 && minutes == 0) {
ret_val = seconds + " sec";
}
else if (hours == 0) {
ret_val = minutes + " min, " + seconds + " sec";
}
else {
ret_val = hours + " hr, " + minutes + " min";
}
if (tag_it != undefined) {
return ret_val;
}
ret_val = "<span class='report-time-1'>" + ret_val + "</span>";
return ret_val;
}
function pad(d) {
return (d < 10) ? '0' + d.toString() : d.toString();
}
function format_display_date(epoch_time, add_additional_info) {
var date_info = "";
var today = new Date();
var date = new Date(epoch_time);
var date_plus_one = new Date(epoch_time + 24 * 60 * 60 * 1000);
var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDate();
var today_year = today.getFullYear();
var today_month = (today.getMonth() + 1);
var today_day = today.getDate();
var hour = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
if (epoch_time == 0) {
return "None";
}
if (year == today_year && month == today_month && day == today_day) {
date_info = "Today<br/>" +
"<span class='report-time'>" + pad(hour) + "h:" + pad(minutes) + "m</span>";
}
else if (date_plus_one.getFullYear() == today_year &&
(date_plus_one.getMonth() + 1) == today_month &&
date_plus_one.getDate() == today_day) {
date_info = "Yesterday<br/>"
+ "<span class='report-time'>" + pad(hour) + "h:" + pad(minutes) + "m</span>";
}
else {
if (today > date) {
date_diff = Math.ceil((today - date)/(24 * 60 * 60 * 1000));
date_info = year + "-" + pad(month) + "-" + pad(day);
date_info += "<br/>";
if (date_diff < 100 && add_additional_info) {
date_info += ("<span class='report-time-add-info'>(" + date_diff + " days ago) </span>");
}
date_info += "<span class='report-time'>" + pad(hour) + "h:" +
pad(minutes) + "m</span>";
}
else {
date_diff = Math.ceil((date - today)/(24 * 60 * 60 * 1000));
date_info = year + "-" + pad(month) + "-" + pad(day);
date_info += "<br/>";
if (date_diff < 100 && add_additional_info) {
date_info += ("<span class='report-time-add-info'>(" + date_diff + " days after) </span>");
}
date_info += "<span class='report-time'>" + pad(hour) + "h:" +
pad(minutes) + "m</span>";
}
}
return date_info ;
}
//Why the heck JS does not have reduce?
function cumulative_value(obj, property_name) {
var cumu_val = 0;
for (var k in obj) {
if (obj.hasOwnProperty(k)) {
cumu_val += obj[k][property_name];
}
}
return cumu_val;
}