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
Original file line number Diff line number Diff line change
Expand Up @@ -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<AppState>,
Expand Down
20 changes: 16 additions & 4 deletions src/app/models/hyp3.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
58 changes: 51 additions & 7 deletions src/app/services/hyp3.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,41 @@ export class Hyp3Service {
return this.http.patch(signupFormURL, body, { withCredentials: true });
}

public getCosts$() {
public getCosts$(): Observable<models.Hyp3Costs> {
const costsUrl = `${this.apiUrl}/costs`;

return this.http.get<models.Hyp3Costs>(costsUrl);
return this.http.get<models.Hyp3Costs | models.Hyp3CostsOld>(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 <models.Hyp3Costs>byType;
} else {
return costsResp;
}
}

public getHyp3ableProducts(products: models.CMRProduct[][]): {byJobType: models.Hyp3ableProductByJobType[]; total: number} {
Expand Down Expand Up @@ -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 = <number>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 {
Expand Down
30 changes: 3 additions & 27 deletions src/app/store/hyp3/hyp3.reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -18,7 +18,7 @@ export interface Hyp3State {
processingOptions: Hyp3ProcessingOptions;
projectName: string;
userId: string;
costs: Hyp3CostsByJobType;
costs: Hyp3Costs;
debug_status: ApplicationStatus | null;
}

Expand All @@ -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"
}
}
};
Expand Down Expand Up @@ -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
};
}

Expand Down
Loading