DBhelper.js
9.1 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
// 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 checkUser(){
let user = realm.objects('ID');
console.log(user.length);
if(user.length>0)
return true;
return false;
}
export function get_id(){
let user = realm.objects('ID');
return user[0].id;
}
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(var 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[recentWeight.length-1].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(){
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).sum('diff');
let wastes2 = realm.objects('Waste').filtered('date >= $0 && date <$1', daybefore5, daybefore4).sum('diff');
let wastes3 = realm.objects('Waste').filtered('date >= $0 && date <$1', daybefore4, daybefore3).sum('diff');
let wastes4 = realm.objects('Waste').filtered('date >= $0 && date <$1', daybefore3, daybefore2).sum('diff');
let wastes5 = realm.objects('Waste').filtered('date >= $0 && date <$1', daybefore2, daybefore1).sum('diff');
let wastes6 = realm.objects('Waste').filtered('date >= $0 && date <$1', daybefore1, today).sum('diff');
let wastes7 = realm.objects('Waste').filtered('date >= $0', today).sum('diff');
var week = ['SUN','MON','TUE', 'WED', 'THU', 'FRI', 'SAT'];
var data1 = [
{ label: week[day[0]], value: wastes1 },
{ label: week[day[1]], value: wastes2 },
{ label: week[day[2]], value: wastes3 },
{ label: week[day[3]], value: wastes4 },
{ label: week[day[4]], value: wastes5 },
{ label: week[day[5]], value: wastes6 },
{ label: week[day[6]], value: wastes7 }
]
console.log(data1)
return data1;
}
function getM_D(time){
console.log("time",time)
var day = new Date(time);
console.log(day.getDate())
return day.getMonth()+1+ "/" +day.getDate();
}
export function get_month_data(){
var day7 = (24*60*60*1000) * 7;
var today = new Date();
today.setHours(0,0,0,0);
var day = today.getDay();
var diff = today.getDate() - day + (day == 0 ? -6:1);
var sep5 = new Date(today.setDate(diff));
var sep5 = sep5.getTime();
var sep4 = sep5 - day7;
var sep3 = sep4 - day7;
var sep2 = sep3 - day7;
var sep1 = sep2 - day7;
console.log("first day of week: ", sep5, sep4, sep3, sep2, sep1);
let wastes1 = realm.objects('Waste').filtered('date >= $0 && date <$1', sep1, sep2).sum('diff');
let wastes2 = realm.objects('Waste').filtered('date >= $0 && date <$1', sep2, sep3).sum('diff');
let wastes3 = realm.objects('Waste').filtered('date >= $0 && date <$1', sep3, sep4).sum('diff');
let wastes4 = realm.objects('Waste').filtered('date >= $0 && date <$1', sep4, sep5).sum('diff');
let wastes5 = realm.objects('Waste').filtered('date >= $0', sep5).sum('diff');
var data2 = [
{ label: getM_D(sep1), value: wastes1 },
{ label: getM_D(sep2), value: wastes2 },
{ label: getM_D(sep3), value: wastes3 },
{ label: getM_D(sep4), value: wastes4 },
{ label: getM_D(sep5), value: wastes5 }
]
console.log(data2)
return data2;
}
export function get_year_data(){
var today = new Date();
var mon1 = new Date(today.getFullYear(), 0, 1);
var mon2 = new Date(today.getFullYear(), 1, 1);
var mon3 = new Date(today.getFullYear(), 2, 1);
var mon4 = new Date(today.getFullYear(), 3, 1);
var mon5 = new Date(today.getFullYear(), 4, 1);
var mon6 = new Date(today.getFullYear(), 5, 1);
var mon7 = new Date(today.getFullYear(), 6, 1);
var mon8 = new Date(today.getFullYear(), 7, 1);
var mon9 = new Date(today.getFullYear(), 8, 1);
var mon10 = new Date(today.getFullYear(), 9, 1);
var mon11 = new Date(today.getFullYear(), 10, 1);
var mon12 = new Date(today.getFullYear(), 11, 1);
var nYear = new Date(today.getFullYear()+1, 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).sum('diff');
let wastes2 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon2, mon3).sum('diff');
let wastes3 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon3, mon4).sum('diff');
let wastes4 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon4, mon5).sum('diff');
let wastes5 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon5, mon6).sum('diff');
let wastes6 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon6, mon7).sum('diff');
let wastes7 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon7, mon8).sum('diff');
let wastes8 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon8, mon9).sum('diff');
let wastes9 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon9, mon10).sum('diff');
let wastes10 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon10, mon11).sum('diff');
let wastes11 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon11, mon12).sum('diff');
let wastes12 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon12, nYear).sum('diff');
var data3 = [
{ label: 'Jan', value: wastes1 },
{ label: 'Feb', value: wastes2 },
{ label: 'Mar', value: wastes3 },
{ label: 'Apr', value: wastes4 },
{ label: 'May', value: wastes5 },
{ label: 'Jun', value: wastes6 },
{ label: 'Jul', value: wastes7 },
{ label: 'Aug', value: wastes8 },
{ label: 'Sep', value: wastes9 },
{ label: 'Oct', value: wastes10 },
{ label: 'Nov', value: wastes11 },
{ label: 'Dec', value: wastes12 }
]
console.log(data3)
return data3;
}
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(yyyy,mm,dd).getTime();
console.log(togettime)
return togettime;
}