Skip to content

Commit 4c5feac

Browse files
committed
refactored code
1 parent bab82ca commit 4c5feac

File tree

1 file changed

+29
-52
lines changed

1 file changed

+29
-52
lines changed

bin/serviceinfo.js

Lines changed: 29 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -124,64 +124,41 @@ db.init(function(err) {
124124
db.Serviceinfo.find({}).exec((err,services)=>{
125125
if(err) return next(err);
126126
services.forEach(async(service)=>{
127-
let countData;
128127
const currentDate = new Date();
129-
if(!service.monthlyCounts || !service.monthlyCounts.length) {
130-
service.monthlyCounts = [];
131-
//if no monthly count then add data from 2017
132-
for(let year = 2017; year <= currentDate.getFullYear(); year++){
133-
for(let month = 1; month <=12; month++){
134-
if(year == currentDate.getFullYear() && month > currentDate.getMonth()+1) break;
135-
const start = new Date(year+"-"+month+"-01");
136-
const end = new Date(start);
137-
end.setMonth(end.getMonth()+1);
138-
countData = await db.Task.aggregate([
139-
{$match: {
140-
create_date: {$gte: start, $lt: end},
141-
service: service.service
142-
}},{
143-
$group: {
144-
_id: {service: "$service"},
145-
count: {$sum: 1},
146-
walltime: {$sum: "$runtime"}
147-
}
128+
let startDate = new Date(currentDate);
129+
if(!service.monthlyCounts || !service.monthlyCounts.length) startDate = new Date('2017-02-01');
130+
//2017-02-01 is January 2017
131+
for(let year = startDate.getFullYear(); year <= currentDate.getFullYear(); year++){
132+
for(let month = startDate.getMonth()+1 ; month <=12; month++){
133+
if(year == currentDate.getFullYear() && month > currentDate.getMonth()+1) break;
134+
const start = new Date(year+"-"+month+"-01");
135+
const end = new Date(start);
136+
end.setMonth(end.getMonth()+1);
137+
// console.log(start,end,year,start.getMonth());
138+
countData = await db.Task.aggregate([
139+
{$match: {
140+
create_date: {$gte: start, $lt: end},
141+
service: service.service
142+
}},{
143+
$group: {
144+
_id: {service: "$service"},
145+
count: {$sum: 1},
146+
walltime: {$sum: "$runtime"}
148147
}
149-
]);
150-
if(!countData.length) service.monthlyCounts.push(undefined);
151-
else service.monthlyCounts.push(countData[0].count);
152-
}
153-
}
154-
// console.log(service.service,service.monthlyCounts.length);
155-
service.save();
156-
} else {
157-
//if the month is already marked then updating it instead of pushing element
158-
const start = currentDate;
159-
const end = new Date(start);
160-
start.setMonth(end.getMonth()-1);
161-
const month_difference = (end.getFullYear() - new Date('2017-01-01').getFullYear())*12 + (end.getMonth() - new Date('2017-01-01').getMonth()) - 1;
162-
countData = await db.Task.aggregate([
163-
{$match: {
164-
create_date: {$gte: start, $lt: end},
165-
service: service.service
166-
}},{
167-
$group: {
168-
_id: {service: "$service"},
169-
count: {$sum: 1},
170-
walltime: {$sum: "$runtime"}
171148
}
149+
]);
150+
const month_difference = (end.getFullYear() - new Date('2017-01-01').getFullYear())*12 + (end.getMonth() - new Date('2017-01-01').getMonth()) - 1;
151+
if(service.monthlyCounts[month_difference] == undefined || service.monthlyCounts[month_difference]) {
152+
if(!countData.length) service.monthlyCounts[month_difference] = undefined;
153+
else service.monthlyCounts[month_difference] = countData[0].count;
154+
} else {
155+
if(!countData.length) service.monthlyCounts.push(undefined);
156+
else service.monthlyCounts.push(countData[0].count);
172157
}
173-
]);
174-
175-
/* if index exists then update else push values*/
176-
if(service.monthlyCounts[month_difference] == undefined || service.monthlyCounts[month_difference]) {
177-
if(!countData.length) service.monthlyCounts[month_difference] = undefined;
178-
else service.monthlyCounts[month_difference] = countData[0].count;
179-
} else {
180-
if(!countData.length) service.monthlyCounts.push(undefined);
181-
else service.monthlyCounts.push(countData[0].count);
158+
// console.log(service.service,service.monthlyCounts,service.monthlyCounts.length);
182159
}
183-
service.save();
184160
}
161+
service.save();
185162
});
186163
});
187164
},

0 commit comments

Comments
 (0)