From 5949d140af6520cb338eb32b5e917c21bf1f1118 Mon Sep 17 00:00:00 2001 From: William Moore Date: Thu, 19 Jan 2023 21:05:20 -0600 Subject: [PATCH] Update to add purger --- src/index.ts | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) 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