-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaggregator.js
163 lines (151 loc) · 4.91 KB
/
aggregator.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
import Agenda from 'agenda'
import Log from "./models/Log.js"
import Hourly from "./models/Hourly.js"
import Daily from "./models/Daily.js"
import Health from './models/Health.js'
import Classifier from "./classifier.js"
import ValueTransformer from "./valueLanguageProcessor.js"
import dotenv from 'dotenv'
dotenv.config()
const agenda = new Agenda({
db: {
address: process.env.MONGODB
}
});
//Job creation and kickoff
const startScheduler = async function() {
await agenda.start();
await hourlyAggregationJob();
await dailyAggregationJob();
await AITrainingJob();
await agenda.every("00 * * * *", "hourly");
await agenda.every("00 00 * * *", "daily");
await agenda.every("00 00 * * 1", "modelTraining");
}
const AITrainingJob = async function() {
agenda.define("modelTraining", async(job) => {
await Classifier.classify()
})
}
//gotta rework the logic here
const hourlyAggregationJob = async function() {
agenda.define("hourly", async (job) => {
const query = await Log.aggregate(
[{
$group: {
_id: {
characteristic: "$characteristic",
year: {
$year: "$created_at"
},
month: {
$month: "$created_at"
},
day: {
$dayOfMonth: "$created_at"
},
hour: {
$hour: "$created_at"
},
},
count: {
$sum:1
},
avgQuantity: {
$avg: "$value"
}
}
}]
).exec();
query.forEach(log => {
hourlyAggregation(log._id.characteristic, new Date(log._id.year, log._id.month - 1, log._id.day, log._id.hour + 1, 0, 0, 0), log.avgQuantity, log.count)
});
await Log.collection.drop();
});
}
const dailyAggregationJob = async function() {
agenda.define("daily", async (job) => {
const query = await Hourly.aggregate(
[{
$group: {
_id: {
characteristic: "$characteristic",
year: {
$year: "$hour_timestamp"
},
month: {
$month: "$hour_timestamp"
},
day: {
$dayOfMonth: "$hour_timestamp"
}
},count: {
$sum:1
},
count: {
$sum:1
},
avgQuantity: {
$avg: "$hour_value"
}
}
}]
).sort({created_at:-1}).exec(); //18 00.00 - 18 00.00 - 19 0.00
const lastEntry = await Daily.findOne().sort({day_timestamp:-1}).exec(); // 19 00 00
const today = new Date() // 20 00 00
query.forEach(hour => {
var hourDate = new Date(hour._id.year, hour._id.month - 1, hour._id.day, 0, 0, 0, 0) //19 00 00
if (lastEntry.day_timestamp <= hourDate &&
hourDate < new Date(today.getFullYear(),today.getMonth(),today.getDate(),0,0,0,0)) {
dailyAggregation(hour._id.characteristic, today, hour.avgQuantity, hour.count)
}
});
});
}
const AIAggregationJob = async function(state) {
const lastWeek = await Daily.find().sort({day_timestamp:-1}).limit(10).exec();
var soilValue = 0;
var tempValue = 0;
lastWeek.forEach(day => {
if (day.characteristic == "SOIL") {
soilValue += day.day_value
}
if(day.characteristic == "TEMP") {
tempValue += day.day_value
}
});
var text = ValueTransformer.transform(soilValue/5,tempValue/5)
AIAggregation((text.tempValue),(text.soilValue), state)
}
async function hourlyAggregation(characteristic, date, avg, count) {
var entry = new Hourly({
characteristic: characteristic,
hour_value: avg,
hour_timestamp: date,
log_count: count
})
entry.save()
}
async function dailyAggregation(characteristic, date, avg, count) {
var entry = new Daily({
characteristic: characteristic,
day_value: avg,
day_timestamp: date,
hour_count: count
})
entry.save()
}
async function AIAggregation(tempValue, soilValue, state) {
var entry = new Health({
temp: tempValue,
soil: soilValue,
state: state
})
entry.save()
}
export default {
dailyAggregationJob,
hourlyAggregationJob,
AIAggregationJob,
startScheduler
}