DBhelper.js
10.3 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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// Define your models and their properties
import Realm from 'realm'
class Waste {}
Waste.schema = {
name: 'Waste',
properties: {
weight: 'int',
diff: 'int',
date: 'float',
}
};
class ID {}
ID.schema={
name:'ID',
properties:{
id : 'string',
}
};
export function registerID(_id){
if(_id != ''){
realm.write(()=>{
let user = realm.create('ID',{id:_id});
console.log('success')
});
}
}
export function getId(){
let user = realm.objects('ID');
console.log(user.length);
if(user.length>0)
return true;
return false;
}
export function deleteId(){
let user = realm.objects('ID');
realm.write(()=>{
realm.delete(user);
console.log('success')
})
}
// Get the default Realm with support for our objects
let realm = new Realm({schema: [Waste, ID]});
export function add_data(responseJson){
for(i = 0; i < responseJson.length; i++){
realm.write(() => {
// responseJson[0].ras_id.S
let myWaste = realm.create('Waste', {
weight: parseFloat(responseJson[i].weight.N),
diff: parseFloat(responseJson[i].diff.N),
date: parseFloat(responseJson[i].save_date.S),
});
});
console.log(responseJson[i].save_date.S)
}
return responseJson.length;
}
export function get_now_weight(){
let wastes = realm.objects('Waste');
let recent = wastes.max('date');
var recentWeight = wastes.filtered('date = $0', recent);
console.log("now_weight",recentWeight)
if(recentWeight.length != 0)
return recentWeight[0].weight;
else return 0;
}
export function get_last_update(){
let recent = realm.objects('Waste').max('date');
let recentWeight = wastes.filtered('date = %0', recent);
console.log(recentWeight[0].weight)
return recentWeight[0].weight;
}
export function get_recent_date(){
return realm.objects('Waste').max("date");
}
// export function get_today_data(today){
// let wastes = realm.objects('Waste');
// var list = [];
// for(var i =0; i<wastes.length; i++){
// if(get_date(wastes[i].date) == get_date(today)){
// temp = [waste[i].date, waste[i].diff, waste[i].weight]
// list.append(temp);
// }
// }
// return list;
// }
export function get_today_data(){
var today_start = get_today_start();
console.log("today_start: ",today_start)
let wastes = realm.objects('Waste').filtered('date >= $0',today_start);
var list = [];
for(var i = 0; i < wastes.length; i++){
var save_time = new Date(wastes[i].date);
var temp = [save_time.getHours()+":"+save_time.getMinutes()+":"+save_time.getSeconds(), wastes[i].diff, wastes[i].weight+"g"]
list.push(temp);
}
console.log(list);
console.log("count:", wastes.length)
return list;
}
export function get_week_data(){
var today = new Date();
var daybefore1 = new Date();
daybefore1.setDate(today.getDate()-1);
var daybefore2 = new Date();
daybefore2.setDate(today.getDate()-2);
var daybefore3 = new Date();
daybefore3.setDate(today.getDate()-3);
var daybefore4 = new Date();
daybefore4.setDate(today.getDate()-4);
var daybefore5 = new Date();
daybefore5.setDate(today.getDate()-5);
var daybefore6 = new Date();
daybefore6.setDate(today.getDate()-6);
var day = [daybefore6.getDay(), daybefore5.getDay(),daybefore4.getDay(),
daybefore3.getDay(),daybefore2.getDay(),daybefore1.getDay(), today.getDay()]
today = today.getTime();
daybefore1 = daybefore1.getTime();
daybefore2 = daybefore2.getTime();
daybefore3 = daybefore3.getTime();
daybefore4 = daybefore4.getTime();
daybefore5 = daybefore5.getTime();
daybefore6 = daybefore6.getTime();
let wastes1 = realm.objects('Waste').filtered('date >= $0 && date <$1', daybefore6, daybefore5);
let wastes2 = realm.objects('Waste').filtered('date >= $0 && date <$1', daybefore5, daybefore4);
let wastes3 = realm.objects('Waste').filtered('date >= $0 && date <$1', daybefore4, daybefore3);
let wastes4 = realm.objects('Waste').filtered('date >= $0 && date <$1', daybefore3, daybefore2);
let wastes5 = realm.objects('Waste').filtered('date >= $0 && date <$1', daybefore2, daybefore1);
let wastes6 = realm.objects('Waste').filtered('date >= $0 && date <$1', daybefore1, today);
let wastes7 = realm.objects('Waste').filtered('date >= $0', today);
var list = [];
var total1 =0, total2=0, total3=0, total4=0, total5=0, total6=0, total7 = 0;
for(var i = 0; i< wastes1.length; i++){
total1 += wastes1[i].diff;
}
for(var i = 0; i< wastes2.length; i++){
total2 += wastes2[i].diff;
}
for(var i = 0; i< wastes3.length; i++){
total3 += wastes3[i].diff;
}
for(var i = 0; i< wastes4.length; i++){
total4 += wastes4[i].diff;
}
for(var i = 0; i< wastes5.length; i++){
total5 += wastes5[i].diff;
}
for(var i = 0; i< wastes6.length; i++){
total6 += wastes6[i].diff;
}
for(var i = 0; i< wastes7.length; i++){
total7 += wastes7[i].diff;
}
list = [[total1, total2, total3, total4, total5, total6, total7],day]
return list;
}
export function get_month_data(){
var today = new Date();
var sep1 = new Date(today.getFullYear(), today.getMonth()-1, 1);
var sep2 = new Date(today.getFullYear(), today.getMonth()-1, 7);
var sep3 = new Date(today.getFullYear(), today.getMonth()-1, 14);
var sep4 = new Date(today.getFullYear(), today.getMonth()-1, 21);
var sep5 = new Date(today.getFullYear(), today.getMonth()-1, 28);
var sep6 = new Date(today.getFullYear(), today.getMonth(), 1)
sep1 = sep1.getTime();
sep2 = sep2.getTime();
sep3 = sep3.getTime();
sep4 = sep4.getTime();
sep5 = sep5.getTime();
sep6 = sep6.getTime();
let wastes1 = realm.objects('Waste').filtered('date >= $0 && date <$1', sep1, sep2);
let wastes2 = realm.objects('Waste').filtered('date >= $0 && date <$1', sep2, sep3);
let wastes3 = realm.objects('Waste').filtered('date >= $0 && date <$1', sep3, sep4);
let wastes4 = realm.objects('Waste').filtered('date >= $0 && date <$1', sep4, sep5);
let wastes5 = realm.objects('Waste').filtered('date >= $0 && date <$1', sep5, sep6);
var list = [];
var total1 =0, total2=0, total3=0, total4=0, total5=0;
for(var i = 0; i< wastes1.length; i++){
total1 += wastes1[i].diff;
}
for(var i = 0; i< wastes2.length; i++){
total2 += wastes2[i].diff;
}
for(var i = 0; i< wastes3.length; i++){
total3 += wastes3[i].diff;
}
for(var i = 0; i< wastes4.length; i++){
total4 += wastes4[i].diff;
}
for(var i = 0; i< wastes5.length; i++){
total5 += wastes5[i].diff;
}
list = [total1, total2, total3, total4, total5]
return list;
}
export function get_year_data(){
var today = new Date();
var mon1 = new Date(today.getFullYear()-1, 0, 1);
var mon2 = new Date(today.getFullYear()-1, 1, 1);
var mon3 = new Date(today.getFullYear()-1, 2, 1);
var mon4 = new Date(today.getFullYear()-1, 3, 1);
var mon5 = new Date(today.getFullYear()-1, 4, 1);
var mon6 = new Date(today.getFullYear()-1, 5, 1);
var mon7 = new Date(today.getFullYear()-1, 6, 1);
var mon8 = new Date(today.getFullYear()-1, 7, 1);
var mon9 = new Date(today.getFullYear()-1, 8, 1);
var mon10 = new Date(today.getFullYear()-1, 9, 1);
var mon11 = new Date(today.getFullYear()-1, 10, 1);
var mon12 = new Date(today.getFullYear()-1, 11, 1);
var nYear = new Date(today.getFullYear(), 0, 1);
mon1 = mon1.getTime();
mon2 = mon2.getTime();
mon3 = mon3.getTime();
mon4 = mon4.getTime();
mon5 = mon5.getTime();
mon6 = mon6.getTime();
mon7 = mon7.getTime();
mon8 = mon8.getTime();
mon9 = mon9.getTime();
mon10 = mon10.getTime();
mon11 = mon11.getTime();
mon12 = mon12.getTime();
nYear = nYear.getTime();
let wastes1 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon1, mon2);
let wastes2 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon2, mon3);
let wastes3 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon3, mon4);
let wastes4 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon4, mon5);
let wastes5 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon5, mon6);
let wastes6 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon6, mon7);
let wastes7 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon7, mon8);
let wastes8 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon8, mon9);
let wastes9 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon9, mon10);
let wastes10 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon10, mon11);
let wastes11 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon11, mon12);
let wastes12 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon12, nYear);
var list = [];
var total1 =0, total2=0, total3=0, total4=0, total5=0,
total6 =0, total7=0, total8=0, total9=0, total10=0,
total11 =0, total12=0;
for(var i = 0; i< wastes1.length; i++){
total1 += wastes1[i].diff;
}
for(var i = 0; i< wastes2.length; i++){
total2 += wastes2[i].diff;
}
for(var i = 0; i< wastes3.length; i++){
total3 += wastes3[i].diff;
}
for(var i = 0; i< wastes4.length; i++){
total4 += wastes4[i].diff;
}
for(var i = 0; i< wastes5.length; i++){
total5 += wastes5[i].diff;
}
for(var i = 0; i< wastes6.length; i++){
total6 += wastes6[i].diff;
}
for(var i = 0; i< wastes7.length; i++){
total7 += wastes7[i].diff;
}
for(var i = 0; i< wastes8.length; i++){
total8 += wastes8[i].diff;
}
for(var i = 0; i< wastes9.length; i++){
total9 += wastes9[i].diff;
}
for(var i = 0; i< wastes10.length; i++){
total10 += wastes10[i].diff;
}
for(var i = 0; i< wastes11.length; i++){
total11 += wastes11[i].diff;
}
for(var i = 0; i< wastes12.length; i++){
total12 += wastes12[i].diff;
}
list = [total1, total2, total3, total4, total5,total6, total7, total8, total9, total10,total11, total12]
return list;
}
export function delete_all_data(today){
let all_wastes = realm.objects('Waste');
realm.write(() => {
realm.delete(all_wastes);
})
var wastes = realm.objects('Waste')
console.log("delete, count: ", wastes.length)
}
function get_today_start(){
var today = new Date();
var dd = today. getDate();
var mm = today.getMonth();
var yyyy = today. getFullYear();
var togettime = new Date(2019,11,13).getTime();
console.log(togettime)
return togettime;
}