From 97c4477c16c0cb3ce20567bd6efb7fe47aa8c363 Mon Sep 17 00:00:00 2001 From: chrismclarke Date: Thu, 10 Oct 2024 22:19:14 -0700 Subject: [PATCH] chore: code tidying --- .../modules/climate/climate-api.mapping.ts | 81 +++++----- .../rainfall-summary/rainfall-summary.ts | 9 +- .../station-details.component.html | 3 +- .../src/app/modules/climate/types/api.d.ts | 144 ++++++++---------- libs/models/src/climate.models.ts | 1 + 5 files changed, 113 insertions(+), 125 deletions(-) diff --git a/apps/picsa-apps/dashboard/src/app/modules/climate/climate-api.mapping.ts b/apps/picsa-apps/dashboard/src/app/modules/climate/climate-api.mapping.ts index ccde4b2de..db88bb339 100644 --- a/apps/picsa-apps/dashboard/src/app/modules/climate/climate-api.mapping.ts +++ b/apps/picsa-apps/dashboard/src/app/modules/climate/climate-api.mapping.ts @@ -94,48 +94,49 @@ export const ApiMapping = ( }, // forecasts: async (country_code: IAPICountryCode) => { - const { data, error } = await api - .getObservableClient(`forecasts/${country_code}`) - .GET(`/v1/forecasts/{country_code}`, { params: { path: { country_code } } }); - if (error) throw error; - const forecasts = data.map((d): IForecastInsert => { - const { date, filename, format, type } = d; - // TODO - handle format - return { date_modified: date, filename, country_code, type, id: filename.split('/').pop() as string }; - }); - const { error: dbError, data: dbData } = await db - .table('climate_forecasts') - .upsert(forecasts) - .select<'*', IForecastRow>('*'); - if (dbError) throw dbError; - return dbData || []; + // const { data, error } = await api + // .getObservableClient(`forecasts/${country_code}`) + // .GET(`/v1/forecasts/{country_code}`, { params: { path: { country_code } } }); + // if (error) throw error; + // const forecasts = data.map((d): IForecastInsert => { + // const { date, filename, format, type } = d; + // // TODO - handle format + // return { date_modified: date, filename, country_code, type, id: filename.split('/').pop() as string }; + // }); + // const { error: dbError, data: dbData } = await db + // .table('climate_forecasts') + // .upsert(forecasts) + // .select<'*', IForecastRow>('*'); + // if (dbError) throw dbError; + // return dbData || []; + return []; }, forecast_file: async (row: IForecastRow) => { - const { country_code, filename } = row; - const { data, error } = await api.getObservableClient(`forecasts/${filename}`).GET(`/v1/forecasts/{file_name}`, { - params: { path: { file_name: filename } }, - parseAs: 'blob', - }); - if (error) throw error; - // setup metadata - const fileBlob = data as Blob; - const bucketId = country_code as string; - const folderPath = 'climate/forecasts'; - // upload to storage - await storage.putFile({ bucketId, fileBlob, filename, folderPath }); - // TODO - handle error if filename already exists - const storageEntry = await storage.getFileAlt({ bucketId, filename, folderPath }); - if (storageEntry) { - const { error: dbError } = await db - .table('climate_forecasts') - .upsert({ ...row, storage_file: storageEntry.id }) - .select('*'); - if (dbError) { - throw dbError; - } - return; - } - throw new Error('Storage file not found'); + // const { country_code, filename } = row; + // const { data, error } = await api.getObservableClient(`forecasts/${filename}`).GET(`/v1/forecasts/{file_name}`, { + // params: { path: { file_name: filename } }, + // parseAs: 'blob', + // }); + // if (error) throw error; + // // setup metadata + // const fileBlob = data as Blob; + // const bucketId = country_code as string; + // const folderPath = 'climate/forecasts'; + // // upload to storage + // await storage.putFile({ bucketId, fileBlob, filename, folderPath }); + // // TODO - handle error if filename already exists + // const storageEntry = await storage.getFileAlt({ bucketId, filename, folderPath }); + // if (storageEntry) { + // const { error: dbError } = await db + // .table('climate_forecasts') + // .upsert({ ...row, storage_file: storageEntry.id }) + // .select('*'); + // if (dbError) { + // throw dbError; + // } + // return; + // } + // throw new Error('Storage file not found'); }, }; }; diff --git a/apps/picsa-apps/dashboard/src/app/modules/climate/pages/station-details/components/rainfall-summary/rainfall-summary.ts b/apps/picsa-apps/dashboard/src/app/modules/climate/pages/station-details/components/rainfall-summary/rainfall-summary.ts index ad5792e49..e79881efd 100644 --- a/apps/picsa-apps/dashboard/src/app/modules/climate/pages/station-details/components/rainfall-summary/rainfall-summary.ts +++ b/apps/picsa-apps/dashboard/src/app/modules/climate/pages/station-details/components/rainfall-summary/rainfall-summary.ts @@ -108,9 +108,10 @@ export class RainfallSummaryComponent { private loadData(summary: IRainfallSummary) { console.log('load data', summary); - this.tableOptions.exportFilename = `${this.activeStation.station_name}_rainfallSummary.csv`; + this.tableOptions.exportFilename = `${this.activeStation.id}.csv`; const { data, metadata } = summary; this.summaryData = this.convertAPIDataToLegacyFormat(data); + // this.summaryData = data; this.summaryMetadata = metadata; const { country_code } = this.activeStation; const definitions = CLIMATE_CHART_DEFINTIONS[country_code] || CLIMATE_CHART_DEFINTIONS.default; @@ -124,10 +125,12 @@ export class RainfallSummaryComponent { // HACK - use either end_rains or end_season depending on which has data populated // TODO - push for single value to be populated at api level End: el.end_rains_doy || el.end_season_doy, - Extreme_events: null as any, + // HACK - extreme events not currently supported + // Extreme_events: null as any, Length: el.season_length, // HACK - replace 0mm with null value - Rainfall: el.annual_rain || undefined, + // HACK - newer api returning seasonal_rain instead of annual_rain + Rainfall: el.seasonal_rain || undefined, Start: el.start_rains_doy, })); return data; diff --git a/apps/picsa-apps/dashboard/src/app/modules/climate/pages/station-details/station-details.component.html b/apps/picsa-apps/dashboard/src/app/modules/climate/pages/station-details/station-details.component.html index e65f6c9f4..89e5044a2 100644 --- a/apps/picsa-apps/dashboard/src/app/modules/climate/pages/station-details/station-details.component.html +++ b/apps/picsa-apps/dashboard/src/app/modules/climate/pages/station-details/station-details.component.html @@ -1,8 +1,7 @@
@if(station(); as station){

{{ station.station_name }}

- - +
@for(key of stationSummary().keys; track $index){ diff --git a/apps/picsa-apps/dashboard/src/app/modules/climate/types/api.d.ts b/apps/picsa-apps/dashboard/src/app/modules/climate/types/api.d.ts index b5e85f27f..2954d66e6 100644 --- a/apps/picsa-apps/dashboard/src/app/modules/climate/types/api.d.ts +++ b/apps/picsa-apps/dashboard/src/app/modules/climate/types/api.d.ts @@ -44,19 +44,9 @@ export interface paths { /** Read Stations */ get: operations["read_stations_v1_station__country___station_id__get"]; }; - "/v1/forecasts/{country_code}": { - /** - * List Forecasts - * @description Get latest forecasts for country - */ - get: operations["list_forecasts_v1_forecasts__country_code__get"]; - }; - "/v1/forecasts/{file_name}": { - /** - * Get Forecast - * @description Get forecast file - */ - get: operations["get_forecast_v1_forecasts__file_name__get"]; + "/v1/documents/{country}": { + /** Get Documents */ + get: operations["get_documents_v1_documents__country__get"]; }; } @@ -98,13 +88,13 @@ export interface components { AnnualRainfallSummariesParameters: { /** * Country - * @default zm_workshops + * @default mw_test * @enum {string} */ country?: "zm" | "mw" | "zm_test" | "mw_test" | "zm_workshops" | "mw_workshops"; /** * Station Id - * @default zambia_eastern + * @default Kasungu */ station_id?: string; /** @@ -143,20 +133,28 @@ export interface components { n_rain?: number; /** Start Rains Doy */ start_rains_doy?: number; - /** End Season Doy */ - end_season_doy?: number; - /** Season Length */ - season_length?: number; /** Start Rains Date */ start_rains_date?: unknown; + /** Start Rains Status */ + start_rains_status?: boolean; /** End Rains Doy */ end_rains_doy?: number; /** End Rains Date */ end_rains_date?: unknown; - /** End Season Date */ - end_season_date?: unknown; + /** End Rains Status */ + end_rains_status?: boolean; + /** Seasonal Rain */ + seasonal_rain?: number; /** N Seasonal Rain */ n_seasonal_rain?: number; + /** Season Length */ + season_length?: number; + /** End Season Doy */ + end_season_doy?: number; + /** End Season Date */ + end_season_date?: unknown; + /** End Season Status */ + end_season_status?: boolean; }; /** AnnualTempartureSummariesdata */ AnnualTempartureSummariesdata: { @@ -181,13 +179,13 @@ export interface components { AnnualTemperatureSummariesParameters: { /** * Country - * @default zm_workshops + * @default mw_test * @enum {string} */ country?: "zm" | "mw" | "zm_test" | "mw_test" | "zm_workshops" | "mw_workshops"; /** * Station Id - * @default zambia_eastern + * @default Kasungu */ station_id?: string; /** @@ -214,17 +212,23 @@ export interface components { /** Data */ data: components["schemas"]["AnnualTempartureSummariesdata"][]; }; + /** CropSuccessProbabilitiesMetadata */ + CropSuccessProbabilitiesMetadata: { + start_rains?: components["schemas"]["StartRains"]; + end_rains?: components["schemas"]["EndRains"]; + crops_success?: components["schemas"]["CropsSuccess"]; + }; /** CropSuccessProbabilitiesParameters */ CropSuccessProbabilitiesParameters: { /** * Country - * @default zm_workshops + * @default mw_test * @enum {string} */ country?: "zm" | "mw" | "zm_test" | "mw_test" | "zm_workshops" | "mw_workshops"; /** * Station Id - * @default zambia_eastern + * @default Kasungu */ station_id?: string; /** Water Requirements */ @@ -241,6 +245,25 @@ export interface components { */ override?: boolean; }; + /** CropSuccessProbabilitiesResponce */ + CropSuccessProbabilitiesResponce: { + metadata: components["schemas"]["CropSuccessProbabilitiesMetadata"]; + /** Data */ + data: components["schemas"]["CropSuccessProbabilitiesdata"][]; + }; + /** CropSuccessProbabilitiesdata */ + CropSuccessProbabilitiesdata: { + /** Station */ + station: string; + /** Rain Total */ + rain_total: number; + /** Plant Day */ + plant_day: number; + /** Plant Length */ + plant_length: number; + /** Prop Success */ + prop_success: number; + }; /** CropsSuccess */ CropsSuccess: { /** Water Requirements */ @@ -294,13 +317,13 @@ export interface components { ExtremesSummariesParameters: { /** * Country - * @default zm + * @default mw_test * @enum {string} */ country?: "zm" | "mw" | "zm_test" | "mw_test" | "zm_workshops" | "mw_workshops"; /** * Station Id - * @default test_1 + * @default Kasungu */ station_id?: string; /** @@ -318,23 +341,6 @@ export interface components { */ override?: boolean; }; - /** Forecast */ - Forecast: { - /** - * Date - * Format: date-time - */ - date: string; - /** Filename */ - filename: string; - /** Type */ - type: string; - /** - * Format - * @enum {string} - */ - format: "html" | "pdf"; - }; /** HTTPValidationError */ HTTPValidationError: { /** Detail */ @@ -365,13 +371,13 @@ export interface components { MonthlyTemperatureSummariesParameters: { /** * Country - * @default zm_workshops + * @default mw_test * @enum {string} */ country?: "zm" | "mw" | "zm_test" | "mw_test" | "zm_workshops" | "mw_workshops"; /** * Station Id - * @default zambia_eastern + * @default Kasungu */ station_id?: string; /** @@ -414,13 +420,13 @@ export interface components { SeasonStartProbabilitiesParameters: { /** * Country - * @default zm_workshops + * @default mw_test * @enum {string} */ country?: "zm" | "mw" | "zm_test" | "mw_test" | "zm_workshops" | "mw_workshops"; /** * Station Id - * @default zambia_eastern + * @default Kasungu */ station_id?: string; /** @@ -546,7 +552,7 @@ export interface components { */ country_code: "zm" | "mw" | "zm_test" | "mw_test" | "zm_workshops" | "mw_workshops"; /** Definitions Id */ - definitions_id: string[]; + definitions_id: unknown[]; /** Climsoft List */ climsoft_list?: string[]; data: components["schemas"]["StationDefinitionDataResponce"]; @@ -712,7 +718,7 @@ export interface operations { /** @description Successful Response */ 200: { content: { - "application/json": Record; + "application/json": components["schemas"]["CropSuccessProbabilitiesResponce"]; }; }; /** @description Validation Error */ @@ -834,39 +840,17 @@ export interface operations { }; }; }; - /** - * List Forecasts - * @description Get latest forecasts for country - */ - list_forecasts_v1_forecasts__country_code__get: { + /** Get Documents */ + get_documents_v1_documents__country__get: { parameters: { - path: { - country_code: "zm" | "mw" | "zm_test" | "mw_test" | "zm_workshops" | "mw_workshops"; - }; - }; - responses: { - /** @description Successful Response */ - 200: { - content: { - "application/json": components["schemas"]["Forecast"][]; - }; - }; - /** @description Validation Error */ - 422: { - content: { - "application/json": components["schemas"]["HTTPValidationError"]; - }; + query?: { + prefix?: unknown; + delimiter?: string; + max_results?: number; + match_glob?: string; }; - }; - }; - /** - * Get Forecast - * @description Get forecast file - */ - get_forecast_v1_forecasts__file_name__get: { - parameters: { path: { - file_name: string; + country: "zm" | "mw" | "zm_test" | "mw_test" | "zm_workshops" | "mw_workshops"; }; }; responses: { diff --git a/libs/models/src/climate.models.ts b/libs/models/src/climate.models.ts index 74d6cbcb7..34b81d884 100644 --- a/libs/models/src/climate.models.ts +++ b/libs/models/src/climate.models.ts @@ -17,6 +17,7 @@ export interface IStationMeta { latitude: number; longitude: number; countryCode: string; + district?: string; /** Data summaries for charts */ data?: IStationData[]; /** Definitions for charts */
{{ key }}