diff --git a/src/index.ts b/src/index.ts index b4644be..657f75d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -15,7 +15,8 @@ const RAIN_GAUGE_ENDPOINT = process.env.RAIN_GAUGE_ENDPOINT; const SOIL_SENSOR_ENDPOINT = process.env.SOIL_SENSOR_ENDPOINT; const TEASENSE_ENDPOINT = process.env.TEASENSE_ENDPOINT; const PORT = process.env.PORT || 3000; -const RAIN_GAUGE_SLEEP = 100; +const DATA_COLLECT_SLEEP = 100; +const PURGE_SLEEP = 60000; mongoose.connect(process.env.DB_URL ?? ''); @@ -112,7 +113,17 @@ const collectData = async () => { console.log(e); } } - setTimeout(() => collectData(), RAIN_GAUGE_SLEEP); -} + setTimeout(() => collectData(), DATA_COLLECT_SLEEP); +}; -setTimeout(() => collectData(), RAIN_GAUGE_SLEEP); \ No newline at end of file +const purger = async () => { + const startTime = subHours(new Date(), 24); + await DataModel.deleteMany({ + timestamp: { + $lt: startTime, + }, + }); +}; + +setTimeout(() => collectData(), DATA_COLLECT_SLEEP); +setInterval(() => purger(), PURGE_SLEEP); \ No newline at end of file