Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 11 additions & 113 deletions apps/triggers/src/sources-data/schedule-sources-data.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import {
DhmWaterLevelAdapter,
DhmRainfallAdapter,
DhmService,
RiverStationData,
RainfallStationData,
} from '@lib/dhm-adapter';
import { GlofasAdapter, GlofasServices } from '@lib/glofas-adapter';
import { GfhAdapter, GfhService } from '@lib/gfh-adapter';
Expand Down Expand Up @@ -104,56 +102,12 @@ export class ScheduleSourcesDataService

riverData.data.forEach(async (indicator) => {
const riverId = (indicator.location as any)?.basinId;
// Currently we do not have station data so we are using dummy data
const info: RiverStationData = {
id: 5554,
onm: '',
name: riverId,
tags: [],
basin: 'Mahakali',
images: [
{
id: 2625,
name: 'bdca0624990c25570064692a7b807baa',
size: 75478,
type: 0,
description: '',
},
{
id: 2669,
name: '8a0a35e1047c0877bef6f16699bbc61f',
size: 61510,
type: 0,
description: '',
},
],
status: 'BELOW WARNING LEVEL',
steady: 'STEADY',
district: 'Kanchanpur',
latitude: 28.8526,
elevation: 162,
longitude: 80.4344,
series_id: 29089,
description: 'Doda (Machheli) river at East West Highway',
danger_level: '3.8',
stationIndex: '281.5',
warning_level: '3.4',
waterLevel: {
value: indicator.value,
datetime: indicator.issuedAt,
},
history: indicator.info,
indicator: indicator.indicator,
units: indicator.units,
value: indicator.value,
};

const result = await this.dhmService.saveDataInDhm(
await this.dhmService.saveDataInDhm(
SourceType.WATER_LEVEL,
riverId,
info,
indicator.info,
);
console.log('result', result);
});
}

Expand All @@ -170,73 +124,17 @@ export class ScheduleSourcesDataService
} else {
this.logger.warn(rainfallData.details);
}
// return;
return;
}

console.log('rainfallData', rainfallData);
// Currently rainfall api is not working so we are using dummy data
const info: RainfallStationData = {
id: 111,
name: 'Doda river at East-West Highway',
basin: 'Koshi',
blink: false,
status: 'BELOW WARNING LEVEL',
history: [
{
max: 0,
min: 0,
value: 0,
datetime: '2025-10-14T05:00:00.000Z',
},
{
max: 0,
min: 0,
value: 0,
datetime: '2025-10-14T06:00:00.000Z',
},
{
max: 0,
min: 0,
value: 0,
datetime: '2025-10-14T07:00:00.000Z',
},
{
max: 0,
min: 0,
value: 0,
datetime: '2025-10-14T08:00:00.000Z',
},
{
max: 0,
min: 0,
value: 0,
datetime: '2025-10-14T09:00:00.000Z',
},
{
max: 0,
min: 0,
value: 0,
datetime: '2025-10-14T10:00:00.000Z',
},
],
district: 'Sunsari',
interval: null,
latitude: 26.855192,
longitude: 87.152283,
series_id: 1505,
description: 'Hydrological Station with RLS',
stationIndex: '695',
indicator: 'water_level_m',
units: 'mm',
value: 10.9,
};

const result = await this.dhmService.saveDataInDhm(
SourceType.RAINFALL,
info.name,
info,
);
console.log('result', result);
rainfallData.data.forEach(async (indicator) => {
const riverId = (indicator.location as any)?.basinId;
await this.dhmService.saveDataInDhm(
SourceType.RAINFALL,
riverId,
indicator.info,
);
});
}

// run every hour
Expand Down
71 changes: 67 additions & 4 deletions packages/dhm-adapter/src/adapters/dhm-rainfall.adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import {
DhmNormalizedItem,
DhmSourceDataTypeEnum,
DhmFetchResponse,
RainfallStationItem,
RiverStationItem,
DhmStationResponse,
} from "../types/dhm-observation.type";
import {
Indicator,
Expand All @@ -27,7 +30,6 @@ import {
RainfallWaterLevelConfig,
PrismaService,
} from "@lib/database";
import { AxiosError } from "axios";
import { EventEmitter2 } from "@nestjs/event-emitter";

@Injectable()
Expand Down Expand Up @@ -69,6 +71,61 @@ export class DhmRainfallAdapter extends ObservationAdapter<DhmFetchParams> {
});
}

private async getStationsDetailsBySeriesId(
seriesId: number
): Promise<RainfallStationItem> {
//TODO: DHM is changing the APIs so for not let it be constant
const baseUrl = `https://dhm.gov.np/home/getAPIData/3`;

const defaultStation: RainfallStationItem = {
name: "",
blink: false,
interval: 0,
id: 0,
stationIndex: "",
basin: "",
district: "",
latitude: 0,
longitude: 0,
series_id: 0,
status: "",
description: "",
indicator: "",
units: "",
value: 0,
};
try {
const response =
await this.httpService.axiosRef.get<DhmStationResponse>(baseUrl);
const riverWatch = response.data.rainfall_watch;

const station = riverWatch.find(
(station) => station.series_id === seriesId
);

if (!station) {
this.logger.warn(`Station not found for seriesId ${seriesId}`);
return defaultStation;
}

return station;
} catch (error: any) {
this.logger.error(
`Failed to get stations details for seriesId ${seriesId}`,
error
);
return defaultStation;
}
}

private getLatestObservationValue(
stationDetail: RiverStationItem | RainfallStationItem
): number {
if ("latest_observation" in stationDetail) {
return stationDetail.latest_observation?.value ?? 0;
}
return 0;
}
/**
* Fetch raw HTML/data from DHM website
*/
Expand Down Expand Up @@ -102,6 +159,7 @@ export class DhmRainfallAdapter extends ObservationAdapter<DhmFetchParams> {

return {
data: await this.httpService.axiosRef.post(baseUrl, form),
stationDetail: await this.getStationsDetailsBySeriesId(seriesId),
location: cfg.LOCATION,
seriesId,
};
Expand Down Expand Up @@ -169,7 +227,9 @@ export class DhmRainfallAdapter extends ObservationAdapter<DhmFetchParams> {

for (const {
data: { data: rawData },
stationDetail,
seriesId,
location,
} of rawDatas) {
const data = scrapeDataFromHtml(rawData.data.table);

Expand All @@ -184,7 +244,9 @@ export class DhmRainfallAdapter extends ObservationAdapter<DhmFetchParams> {

observations.push({
data: normalizedData,
stationDetail,
seriesId,
location,
});
}

Expand All @@ -210,12 +272,13 @@ export class DhmRainfallAdapter extends ObservationAdapter<DhmFetchParams> {
location: {
type: "STATION" as const,
seriesId: obs.seriesId,
basinId: obs.location!,
},
source: {
key: "DHM",
metadata: { originalUnit: "m" },
},
history: obs.data,
info: { ...obs.stationDetail, history: obs.data },
};

const results: Indicator[] = [];
Expand All @@ -224,8 +287,8 @@ export class DhmRainfallAdapter extends ObservationAdapter<DhmFetchParams> {
results.push({
...baseIndicator,
indicator: "rainfall_mm",
units: "m",
value: obs.data[0]?.value || 0,
units: "mm",
value: this.getLatestObservationValue(obs.stationDetail),
});

return results;
Expand Down
Loading