Skip to content

Commit

Permalink
chore: code tidying
Browse files Browse the repository at this point in the history
  • Loading branch information
chrismclarke committed Oct 11, 2024
1 parent 422ff56 commit 97c4477
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 125 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<IForecastInsert>(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<IForecastInsert>(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<IForecastInsert>({ ...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<IForecastInsert>({ ...row, storage_file: storageEntry.id })
// .select('*');
// if (dbError) {
// throw dbError;
// }
// return;
// }
// throw new Error('Storage file not found');
},
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<div class="page-content">
@if(station(); as station){
<h2>{{ station.station_name }}</h2>
<!-- <picsa-data-table [options]="tableOptions" [data]="stationSummaries()"></picsa-data-table> -->
<table>
<table style="max-width: 600px">
<tr>
@for(key of stationSummary().keys; track $index){
<th>{{ key }}</th>
Expand Down
Loading

0 comments on commit 97c4477

Please sign in to comment.