-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
204fcc3
commit f21ef15
Showing
5 changed files
with
183 additions
and
44 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
api-node/prisma/migrations/20240311073515_delete_pollution_table/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/* | ||
Warnings: | ||
- You are about to drop the `AlertPollutionAtmospheric` table. If the table is not empty, all the data it contains will be lost. | ||
*/ | ||
-- DropForeignKey | ||
ALTER TABLE "AlertPollutionAtmospheric" DROP CONSTRAINT "AlertPollutionAtmospheric_municipality_insee_code_fkey"; | ||
|
||
-- DropTable | ||
DROP TABLE "AlertPollutionAtmospheric"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import { getAtmoIndicator } from '~/aggregators/indice_atmo.ts'; | ||
import { getPollensIndicator } from '~/aggregators/pollens.ts'; | ||
import { getIndiceUVIndicator } from '~/aggregators/indice_uv.ts'; | ||
import { setupCronJob } from './utils'; | ||
import { capture } from '~/third-parties/sentry'; | ||
import { getWeatherAlert } from '~/aggregators/weather_alert'; | ||
import { getBathingWaterIndicator } from '~/aggregators/bathing_water'; | ||
import { groupUsersByMunicipality } from '~/utils/municipalities'; | ||
import prisma from '~/prisma'; | ||
import dayjs from 'dayjs'; | ||
|
||
/* | ||
* | ||
* | ||
Initialization of the cron jobs | ||
We call them one after the other, | ||
in order to avoid to launch them all at the same time | ||
and have logs that are mixed and not readable. | ||
Test it: run `npm run dev-cronjobs` and check the logs | ||
*/ | ||
|
||
async function cleanIndicatorsData() { | ||
try { | ||
const municipalitiesByUser = await groupUsersByMunicipality(); | ||
const inseeCodes = municipalitiesByUser.map( | ||
(row) => row.municipality_insee_code, | ||
); | ||
await prisma.bathingWater | ||
.deleteMany({ | ||
where: { | ||
municipality_insee_code: { | ||
notIn: inseeCodes, | ||
}, | ||
validity_end: { | ||
lt: dayjs().add(-1, 'weeks').toDate(), | ||
}, | ||
}, | ||
}) | ||
.then(() => { | ||
console.log('Bathing water data cleaned up'); | ||
}) | ||
.catch(capture); | ||
await prisma.indiceAtmospheric | ||
.deleteMany({ | ||
where: { | ||
municipality_insee_code: { | ||
notIn: inseeCodes, | ||
}, | ||
validity_end: { | ||
lt: dayjs().add(-1, 'weeks').toDate(), | ||
}, | ||
}, | ||
}) | ||
.then(() => { | ||
console.log('Atmospheric data cleaned up'); | ||
}) | ||
.catch(capture); | ||
await prisma.indiceUv | ||
.deleteMany({ | ||
where: { | ||
municipality_insee_code: { | ||
notIn: inseeCodes, | ||
}, | ||
validity_end: { | ||
lt: dayjs().add(-1, 'weeks').toDate(), | ||
}, | ||
}, | ||
}) | ||
.then(() => { | ||
console.log('UV data cleaned up'); | ||
}) | ||
.catch(capture); | ||
await prisma.weatherAlert | ||
.deleteMany({ | ||
where: { | ||
municipality_insee_code: { | ||
notIn: inseeCodes, | ||
}, | ||
validity_end: { | ||
lt: dayjs().add(-1, 'weeks').toDate(), | ||
}, | ||
}, | ||
}) | ||
.then(() => { | ||
console.log('Weather alert data cleaned up'); | ||
}) | ||
.catch(capture); | ||
await prisma.pollenAllergyRisk | ||
.deleteMany({ | ||
where: { | ||
municipality_insee_code: { | ||
notIn: inseeCodes, | ||
}, | ||
validity_end: { | ||
lt: dayjs().add(-1, 'weeks').toDate(), | ||
}, | ||
}, | ||
}) | ||
.then(() => { | ||
console.log('Pollen data cleaned up'); | ||
}) | ||
.catch(capture); | ||
} catch (error: any) { | ||
capture(error, { level: 'error' }); | ||
} | ||
} | ||
|
||
export async function initIndicatorsCleaning() { | ||
await Promise.resolve() | ||
.then(() => { | ||
console.log('Inside cleaning cronjobs'); | ||
}) | ||
.then( | ||
async () => | ||
await setupCronJob({ | ||
name: 'Clean indicators data', | ||
cronTime: '3 3 * * *', // every day at 3:03am | ||
job: cleanIndicatorsData, | ||
runOnInit: false, | ||
}), | ||
) | ||
.then(() => { | ||
console.log('All indicators data is cleaned up'); | ||
}) | ||
.catch(capture); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters