Skip to content

Commit 47c4f0d

Browse files
committed
date checking and avoiding duplicate data
1 parent fffd61b commit 47c4f0d

File tree

1 file changed

+13
-18
lines changed

1 file changed

+13
-18
lines changed

bin/serviceinfo.js

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,13 @@ db.init(function(err) {
121121
},
122122

123123
async next=>{
124-
125124
/* Get all service info */
126125
db.Serviceinfo.find({}).exec((err,services)=>{
127126
services.forEach(async(service)=>{
128127
let countData;
129128
const currentDate = new Date();
130129
if(!service.monthlyCounts || !service.monthlyCounts.length) {
131130
service.monthlyCounts = [];
132-
console.debug("No monthly count", service.service);
133131
//if no monthly count then add data from 2017
134132
// add if statement if current month and current year then stop
135133
for(let year = 2017; year <= currentDate.getFullYear(); year++){
@@ -154,18 +152,15 @@ db.init(function(err) {
154152
else service.monthlyCounts.push(countData[0].count);
155153
}
156154
}
157-
console.error(service.service,service.monthlyCounts.length,countData);
155+
console.log(service.service,service.monthlyCounts.length);
158156
service.save();
159157
} else {
160-
/* i have to implement a check
161-
if the month is already marked then updating it instead of pushing element
162-
*/
158+
//if the month is already marked then updating it instead of pushing element
163159
console.log("exists monthly Count",service.service);
164-
const start = new Date();
160+
const start = currentDate;
165161
const end = new Date(start);
166162
start.setMonth(end.getMonth()-1);
167-
const month_difference = (end.getFullYear() - new Date('2017-01-01').getFullYear())*12 + (end.getMonth() - new Date('2017-01-01').getMonth())
168-
console.log(month_difference);
163+
const month_difference = (end.getFullYear() - new Date('2017-01-01').getFullYear())*12 + (end.getMonth() - new Date('2017-01-01').getMonth()) - 1;
169164
countData = await db.Task.aggregate([
170165
{$match: {
171166
create_date: {$gte: start, $lt: end},
@@ -178,15 +173,15 @@ db.init(function(err) {
178173
}
179174
}
180175
]);
181-
/* improve this conditional
182-
first if index exists then update
183-
else push
184-
if coundata null then push undefined
185-
*/
186-
if(!countData.length) service.monthlyCounts.push(undefined);
187-
/* it can be undefined */
188-
else if(service.monthlyCounts[month_difference-1] || service.monthlyCounts[month_difference-1] == undefined) service.monthlyCounts[month_difference-1] = countData[0].count;
189-
else service.monthlyCounts.push(countData[0].count);
176+
177+
/* if index exists then update else push values*/
178+
if(service.monthlyCounts[month_difference] == undefined || service.monthlyCounts[month_difference]) {
179+
if(!countData.length) service.monthlyCounts[month_difference] = undefined;
180+
else service.monthlyCounts[month_difference] = countData[0].count;
181+
} else {
182+
if(!countData.length) service.monthlyCounts.push(undefined);
183+
else service.monthlyCounts.push(countData[0].count);
184+
}
190185
service.save();
191186
}
192187
})

0 commit comments

Comments
 (0)