From 6806abc2ddd47ecbe5de02babbe56e5b03999139 Mon Sep 17 00:00:00 2001 From: William Horn Date: Fri, 7 Mar 2025 14:38:56 -0900 Subject: [PATCH] Add support for new hyp3 costs format --- .../processing-options.component.ts | 2 +- src/app/models/hyp3.model.ts | 20 +++++-- src/app/services/hyp3.service.ts | 58 ++++++++++++++++--- src/app/store/hyp3/hyp3.reducer.ts | 30 +--------- 4 files changed, 71 insertions(+), 39 deletions(-) diff --git a/src/app/components/header/processing-queue/processing-options/processing-options.component.ts b/src/app/components/header/processing-queue/processing-options/processing-options.component.ts index dfd8520c9..52580172c 100644 --- a/src/app/components/header/processing-queue/processing-options/processing-options.component.ts +++ b/src/app/components/header/processing-queue/processing-options/processing-options.component.ts @@ -19,7 +19,7 @@ export class ProcessingOptionsComponent implements OnInit { public JobTypesList = models.hyp3JobTypesList; public optionValues = {}; - public costs: models.Hyp3CostsByJobType; + public costs: models.Hyp3Costs; constructor( private store$: Store, diff --git a/src/app/models/hyp3.model.ts b/src/app/models/hyp3.model.ts index e14bb0001..80a2f970d 100644 --- a/src/app/models/hyp3.model.ts +++ b/src/app/models/hyp3.model.ts @@ -104,13 +104,25 @@ export interface Hyp3InSarGammaParameters { granules: string[]; } -export type Hyp3Costs = Hyp3JobCost[] -export type Hyp3CostsByJobType = { - [jobType: string]: Hyp3JobCost; +export type Hyp3Costs = { + [jobType: string]: Hyp3JobCost } +export type Hyp3JobCost = Hyp3JobCostFixed | Hyp3JobCostTable; +export type Hyp3JobCostFixed = { cost: number; } +export type Hyp3JobCostTable = { + cost_parameters: string[]; + cost_table: Hyp3CostTable +} + +export type Hyp3CostTable = { + [parameterValue: string]: number | Hyp3CostTable +} + +// TODO: Remove when hyp3 updates +export type Hyp3CostsOld = Hyp3JobCostOld[] -export interface Hyp3JobCost { +export interface Hyp3JobCostOld { job_type: string; cost_parameter?: number | string; cost_table?: any; diff --git a/src/app/services/hyp3.service.ts b/src/app/services/hyp3.service.ts index b7b659a20..8cfe98609 100644 --- a/src/app/services/hyp3.service.ts +++ b/src/app/services/hyp3.service.ts @@ -198,10 +198,41 @@ export class Hyp3Service { return this.http.patch(signupFormURL, body, { withCredentials: true }); } - public getCosts$() { + public getCosts$(): Observable { const costsUrl = `${this.apiUrl}/costs`; - return this.http.get(costsUrl); + return this.http.get(costsUrl).pipe( + map(costsResp => this.formatCosts(costsResp)) + ); + } + + public formatCosts(costsResp: models.Hyp3CostsOld | models.Hyp3Costs): models.Hyp3Costs { + if (Array.isArray(costsResp)) { + // TODO: This is for Hyp3CostsOld support. Remove this after hyp3 is updated. + const byType = costsResp.reduce((byJobType, jobCost) => { + + if (!jobCost.cost_table) { + byJobType[jobCost.job_type] = jobCost; + } else { + const byCostTableValue = jobCost.cost_table.reduce((byValue, costTableValue) => { + byValue[costTableValue.parameter_value] = costTableValue.cost; + + return byValue; + }, {}); + + byJobType[jobCost.job_type] = { + cost_parameters: [jobCost.cost_parameter], + cost_table: byCostTableValue, + }; + } + + return byJobType; + }, {}); + + return byType; + } else { + return costsResp; + } } public getHyp3ableProducts(products: models.CMRProduct[][]): {byJobType: models.Hyp3ableProductByJobType[]; total: number} { @@ -338,15 +369,28 @@ export class Hyp3Service { return output; } - public calculateCredits(options: models.Hyp3ProcessingOptions, cost: models.Hyp3JobCost) { - if (cost?.cost) { + public calculateCredits(options: models.Hyp3ProcessingOptions, cost: models.Hyp3JobCost): number { + if (!cost) { + return 1; + } + + if ('cost' in cost) { const fixedCost = cost; return fixedCost.cost; - } + } else if ('cost_table' in cost){ + const selectedCostValue = cost.cost_parameters.reduce( + (costLookup, parameterKey) => { + const lookupValue = options[parameterKey]; - const selectedCostValue = options[cost.cost_parameter]; + return costLookup[lookupValue]; + }, + cost.cost_table + ); - return cost.cost_table[selectedCostValue] || 1; + return selectedCostValue || 1; + } else { + return 1; + } } private expirationDays(expiration_time: moment.Moment): number { diff --git a/src/app/store/hyp3/hyp3.reducer.ts b/src/app/store/hyp3/hyp3.reducer.ts index c3f680094..d7ca2870a 100644 --- a/src/app/store/hyp3/hyp3.reducer.ts +++ b/src/app/store/hyp3/hyp3.reducer.ts @@ -3,7 +3,7 @@ import { createFeatureSelector, createSelector } from '@ngrx/store'; import { Hyp3ActionType, Hyp3Actions } from './hyp3.action'; import { Hyp3Job, Hyp3User, Hyp3ProcessingOptions, - hyp3DefaultJobOptions, Hyp3CostsByJobType, + hyp3DefaultJobOptions, Hyp3Costs, ApplicationStatus } from '@models'; @@ -18,7 +18,7 @@ export interface Hyp3State { processingOptions: Hyp3ProcessingOptions; projectName: string; userId: string; - costs: Hyp3CostsByJobType; + costs: Hyp3Costs; debug_status: ApplicationStatus | null; } @@ -35,19 +35,15 @@ const initState: Hyp3State = { costs: { "AUTORIFT": { "cost": 1, - "job_type": "AUTORIFT" }, "INSAR_GAMMA": { "cost": 1, - "job_type": "INSAR_GAMMA" }, "RTC_GAMMA": { "cost": 1, - "job_type": "RTC_GAMMA" }, "INSAR_ISCE_BURST": { "cost": 1, - "job_type": "INSAR_ISCE_BURST" } } }; @@ -112,29 +108,9 @@ export function hyp3Reducer(state = initState, action: Hyp3Actions): Hyp3State { } case Hyp3ActionType.SET_COSTS: { - const byType = action.payload.reduce((byJobType, job) => { - - if (!job.cost_table) { - byJobType[job.job_type] = job; - } else { - const byCostTableValue = job.cost_table.reduce((byValue, costTableValue) => { - byValue[costTableValue.parameter_value] = costTableValue.cost; - - return byValue; - }, {}); - - byJobType[job.job_type] = { - ...job, - cost_table: byCostTableValue, - }; - } - - return byJobType; - }, {}); - return { ...state, - costs: byType + costs: action.payload }; }