Skip to content

Commit

Permalink
feat: Get Weather alert and save it on db (#314)
Browse files Browse the repository at this point in the history
  • Loading branch information
Charlesdoiron authored Dec 24, 2023
1 parent ec8c030 commit 46e5d33
Show file tree
Hide file tree
Showing 11 changed files with 505 additions and 37,678 deletions.
37,564 changes: 0 additions & 37,564 deletions api-node/data/v_commune_2023.csv

This file was deleted.

102 changes: 0 additions & 102 deletions api-node/data/v_departement_2023.csv

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
-- CreateEnum
CREATE TYPE "DataAvailabilityEnum" AS ENUM ('AVAILABLE', 'NOT_AVAILABLE');

-- CreateEnum
CREATE TYPE "CodeAlertEnums" AS ENUM ('GREEN', 'YELLOW', 'ORANGE', 'RED');

-- CreateEnum
CREATE TYPE "PhenomenonsEnum" AS ENUM ('VIOLENT_WIND', 'RAIN_FLOOD', 'STORM', 'FLOOD', 'SNOW_ICE', 'HEAT_WAVE', 'COLD_WAVE', 'AVALANCHE', 'WAVES_SUBMERSION');

-- CreateEnum
CREATE TYPE "PollutionStateEnum" AS ENUM ('ALERT', 'INFORMATION_AND_RECOMMANDATION', 'NO_ALERT', 'ALERT_PERSISTANCE');

-- CreateEnum
CREATE TYPE "PollutionCodeEnum" AS ENUM ('SULFUR_DIOXIDE', 'NITROGEN_DIOXIDE', 'FINES_PARTICLES', 'OZONE', 'PM10', 'PM25', 'BENZENE');

-- CreateTable
CREATE TABLE "User" (
"id" TEXT NOT NULL,
"matomo_id" TEXT NOT NULL,
"municipality_insee_code" TEXT,
"municipality_name" TEXT,
"municipality_zip_code" TEXT,
"push_notif_token" TEXT,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

CONSTRAINT "User_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "IndiceUv" (
"id" TEXT NOT NULL,
"validity_start" TIMESTAMP(3) NOT NULL,
"validity_end" TIMESTAMP(3) NOT NULL,
"diffusion_date" TIMESTAMP(3) NOT NULL,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"municipality_insee_code" TEXT NOT NULL,
"data_availability" "DataAvailabilityEnum" DEFAULT 'AVAILABLE',
"uv_j0" INTEGER NOT NULL,
"uv_j1" INTEGER NOT NULL,
"uv_j2" INTEGER NOT NULL,
"uv_j3" INTEGER NOT NULL,

CONSTRAINT "IndiceUv_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "PollenAllergyRisk" (
"id" TEXT NOT NULL,
"validity_start" TIMESTAMP(3) NOT NULL,
"validity_end" TIMESTAMP(3) NOT NULL,
"diffusion_date" TIMESTAMP(3) NOT NULL,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"municipality_insee_code" TEXT NOT NULL,
"data_availability" "DataAvailabilityEnum" DEFAULT 'AVAILABLE',
"cypres" INTEGER DEFAULT 0,
"noisetier" INTEGER DEFAULT 0,
"aulne" INTEGER DEFAULT 0,
"peuplier" INTEGER DEFAULT 0,
"saule" INTEGER DEFAULT 0,
"frene" INTEGER DEFAULT 0,
"charme" INTEGER DEFAULT 0,
"bouleau" INTEGER DEFAULT 0,
"platane" INTEGER DEFAULT 0,
"chene" INTEGER DEFAULT 0,
"olivier" INTEGER DEFAULT 0,
"tilleul" INTEGER DEFAULT 0,
"chataignier" INTEGER DEFAULT 0,
"rumex" INTEGER DEFAULT 0,
"graminees" INTEGER DEFAULT 0,
"plantain" INTEGER DEFAULT 0,
"urticacees" INTEGER DEFAULT 0,
"armoises" INTEGER DEFAULT 0,
"ambroisies" INTEGER DEFAULT 0,
"total" INTEGER DEFAULT 0,

CONSTRAINT "PollenAllergyRisk_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "Phenomenon" (
"id" TEXT NOT NULL,
"name" "PhenomenonsEnum",
"code_alert" "CodeAlertEnums",
"weatherAlertId" TEXT,

CONSTRAINT "Phenomenon_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "WeatherAlert" (
"id" TEXT NOT NULL,
"validity_start" TIMESTAMP(3) NOT NULL,
"validity_end" TIMESTAMP(3) NOT NULL,
"diffusion_date" TIMESTAMP(3) NOT NULL,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"municipality_insee_code" TEXT NOT NULL,
"data_availability" "DataAvailabilityEnum" DEFAULT 'AVAILABLE',

CONSTRAINT "WeatherAlert_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "IndiceAtmosperic" (
"id" TEXT NOT NULL,
"validity_start" TIMESTAMP(3) NOT NULL,
"validity_end" TIMESTAMP(3) NOT NULL,
"diffusion_date" TIMESTAMP(3) NOT NULL,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"municipality_insee_code" TEXT NOT NULL,
"data_availability" "DataAvailabilityEnum" DEFAULT 'AVAILABLE',
"no2" INTEGER NOT NULL,
"so2" INTEGER NOT NULL,
"o3" INTEGER NOT NULL,
"pm10" INTEGER NOT NULL,
"pm25" INTEGER NOT NULL,
"max_value" INTEGER NOT NULL,

CONSTRAINT "IndiceAtmosperic_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "AlertPollutionAtmospheric" (
"id" TEXT NOT NULL,
"validity_start" TIMESTAMP(3) NOT NULL,
"validity_end" TIMESTAMP(3) NOT NULL,
"diffusion_date" TIMESTAMP(3) NOT NULL,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"municipality_insee_code" TEXT NOT NULL,
"data_availability" "DataAvailabilityEnum" DEFAULT 'AVAILABLE',
"state" "PollutionStateEnum" NOT NULL,
"code" "PollutionCodeEnum" NOT NULL,
"comment_short" TEXT NOT NULL,
"comment_long" TEXT NOT NULL,

CONSTRAINT "AlertPollutionAtmospheric_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "User_matomo_id_key" ON "User"("matomo_id");

-- CreateIndex
CREATE INDEX "User_push_notif_token_idx" ON "User" USING HASH ("push_notif_token");

-- AddForeignKey
ALTER TABLE "Phenomenon" ADD CONSTRAINT "Phenomenon_weatherAlertId_fkey" FOREIGN KEY ("weatherAlertId") REFERENCES "WeatherAlert"("id") ON DELETE SET NULL ON UPDATE CASCADE;
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
Warnings:
- You are about to drop the `Phenomenon` table. If the table is not empty, all the data it contains will be lost.
- Added the required column `code_alert` to the `WeatherAlert` table without a default value. This is not possible if the table is not empty.
- Added the required column `phenomenon` to the `WeatherAlert` table without a default value. This is not possible if the table is not empty.
*/
-- DropForeignKey
ALTER TABLE "Phenomenon" DROP CONSTRAINT "Phenomenon_weatherAlertId_fkey";

-- AlterTable
ALTER TABLE "WeatherAlert" ADD COLUMN "code_alert" "CodeAlertEnums" NOT NULL,
ADD COLUMN "phenomenon" "PhenomenonsEnum" NOT NULL;

-- DropTable
DROP TABLE "Phenomenon";
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- AlterTable
ALTER TABLE "WeatherAlert" ALTER COLUMN "code_alert" DROP NOT NULL,
ALTER COLUMN "phenomenon" DROP NOT NULL;
3 changes: 3 additions & 0 deletions api-node/prisma/migrations/migration_lock.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (i.e. Git)
provider = "postgresql"
5 changes: 3 additions & 2 deletions api-node/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,9 @@ model WeatherAlert {
// a row will be created ONLY if there is an alert
// an alert is a combination of a code_alert and a phenomenon
// example: alert HEAT_WAVE code RED
phenomenon PhenomenonsEnum
code_alert CodeAlertEnums
code_alert CodeAlertEnums?
phenomenon PhenomenonsEnum?
}

model IndiceAtmosperic {
Expand Down
7 changes: 5 additions & 2 deletions api-node/src/aggregators/pollens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export default async function getPollensIndicator() {
// Step 2: Validate data
const date = Object.keys(data[0])[0];
logStep(`diffusionDate: ${date}`);

try {
z.array(
z.object({
Expand Down Expand Up @@ -82,6 +81,8 @@ export default async function getPollensIndicator() {
diffusion_date: diffusionDate,
},
});
// TODO: Maybe Add check if the data in the db is data_availability: NOT_AVAILABLE
// In some scenarios, the data is not available and can be available later, no ?.
if (existingPollens > 0) {
logStep(
`Pollens already fetched for diffusionDate ${date}: ${existingPollens} rows`,
Expand All @@ -93,6 +94,7 @@ export default async function getPollensIndicator() {
const pollensByDepartment: Record<DepartmentCode, PollenAllergyRisk> = {};
for (const row of data) {
const departmentCode = row[date];
// we need to format the department code to have a 2 digits string
let formattedDepCode =
departmentCode < 10 && departmentCode != '2A' && departmentCode != '2B'
? '0' + departmentCode
Expand All @@ -117,10 +119,11 @@ export default async function getPollensIndicator() {
);

// Step 6: loop on municipalities and create rows to insert
logStep('fetching muunicipalities DONE');
logStep('fetching municipalities DONE');
const pollensRows = [];
for (const municipality of municipalities) {
const pollenData = pollensByDepartment[municipality.DEP];
// if no data for this department, we say that data is not available.
if (!pollenData) {
pollensRows.push({
diffusion_date: diffusionDate,
Expand Down
Loading

0 comments on commit 46e5d33

Please sign in to comment.