Skip to content

Commit

Permalink
Merge pull request #174 from Deltares/173-add-workflows
Browse files Browse the repository at this point in the history
Add workflows and moduleruntimes endpoint
  • Loading branch information
hvangeffen authored Jan 23, 2025
2 parents 2456703 + 4d02886 commit 3886d76
Show file tree
Hide file tree
Showing 12 changed files with 221 additions and 4 deletions.
6 changes: 5 additions & 1 deletion scripts/generateTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { exec } from "child_process";

const config = {
url: "https://fewsdocs.deltares.nl/webservices/rest-api/v1/schemas/pirest",
message: "/*tslint:disable*/",
message: "/* tslint:disable */",
};

const piCommands = [
Expand Down Expand Up @@ -62,6 +62,10 @@ const piCommands = [
url: `${config.url}/pi_rest_topology_thresholds.json`,
output: "src/response/topology/thresholdsNodeResponse.ts",
},
{
url: `${config.url}/pi_rest_workflows.json`,
output: "src/response/workflows/workflowsResponse.ts",
},
];

const archiveCommands = [
Expand Down
49 changes: 47 additions & 2 deletions src/piWebserviceProvider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {TimeSeriesResponse} from './response/timeseries'
import type {TaskRunsResponse} from './response/tasks'
import type {ModuleRuntimesResponse, TaskRunsResponse} from './response/tasks'
import type {LocationsResponse} from './response/locations'
import type {ImportStatusResponse} from './response/importStatus'
import type {VersionResponse} from './response/version'
Expand All @@ -17,7 +17,9 @@ import type {
RunTaskFilter,
timeSeriesGridActionsFilter,
TimeSeriesTopologyActionsFilter,
filterActionsFilter
filterActionsFilter,
WorkflowsFilter,
ModuleRuntimesFilter
} from "./requestParameters";
import type {TopologyNodeResponse} from "./response/topology";
import type {TopologyActionFilter} from "./requestParameters/topologyActionFilter";
Expand All @@ -42,6 +44,7 @@ import DataRequestResult from "@deltares/fews-web-oc-utils/lib/types/restservice
import { TimeSeriesGridMaxValuesFilter } from './requestParameters/timeSeriesGridMaxValuesFilter'
import type { TopologyThresholdNodeResponse } from './response/topology/thresholdsNodeResponse'
import type { ReportsResponse } from './response/reports/reportsResponse'
import type { WorkflowResponse } from './response/workflows/workflowsResponse'
import {LogsDisplaysResponse} from "@/response";

export class PiWebserviceProvider {
Expand Down Expand Up @@ -406,6 +409,33 @@ export class PiWebserviceProvider {
return res.data;
}


/**
* Get the workflows
*
* @param filter search options
* @returns Workflows API response
* @throws 'Fetch Error' if fetch result is not ok
*/
async getWorkflows(filter: WorkflowsFilter): Promise<WorkflowResponse> {
const url = this.workflowsUrl(filter)
const res = await this.webservice.getData<WorkflowResponse>(url.toString());
return res.data;
}

/**
* Get the module run times
*
* @param filter search options
* @returns Module run times API response
* @throws 'Fetch Error' if fetch result is not ok
*/
async getModuleRunTimes(filter: ModuleRuntimesFilter): Promise<ModuleRuntimesResponse> {
const url = this.moduleRunTimesUrl(filter)
const res = await this.webservice.getData<ModuleRuntimesResponse>(url.toString());
return res.data;
}

/**
* Post time series edits.
*
Expand Down Expand Up @@ -790,5 +820,20 @@ export class PiWebserviceProvider {
)
}

workflowsUrl(filter: WorkflowsFilter): URL {
const queryParameters = filterToParams(filter)
return new URL(
`${this._baseUrl.pathname}${this.API_ENDPOINT}/workflows${queryParameters}`,
this._baseUrl
)
}

moduleRunTimesUrl(filter: ModuleRuntimesFilter): URL {
const queryParameters = filterToParams(filter)
return new URL(
`${this._baseUrl.pathname}${this.API_ENDPOINT}/moduleruntimes${queryParameters}`,
this._baseUrl
)
}

}
3 changes: 2 additions & 1 deletion src/requestParameters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ export * from './processDataFilter.js'
export * from './timeSeriesGridActionsFilter.js'
export * from './filterActionsFilter.js'
export * from './timeSeriesTopologyActionsFilter.js'
export * from './runTaskFilter.js'
export * from './runTaskFilter.js'
export * from './workflowsFilter.js'
3 changes: 3 additions & 0 deletions src/requestParameters/moduleRunTimesFilter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import type { BaseFilter } from "./baseFilter";

export interface ModuleRuntimesFilter extends BaseFilter {
workflowId?: string;

// TODO: Is this being used? Seems to be an invalid parameter.
// Unique sequence number that can be used to order asynchronous api requests.
draw?: number;
}
3 changes: 3 additions & 0 deletions src/requestParameters/workflowsFilter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import type { BaseFilter } from "./baseFilter";

export interface WorkflowsFilter extends BaseFilter {}
1 change: 1 addition & 0 deletions src/response/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ export * from './timeseries/index.js'
export * from './timeseriesparameters/index.js'
export * from './topology/index.js'
export * from './version/index.js'
export * from './workflows/index.js'
1 change: 1 addition & 0 deletions src/response/workflows/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './workflowsResponse.js'
37 changes: 37 additions & 0 deletions src/response/workflows/workflowsResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* tslint:disable */

/**
* WorkflowResponse PI_JSON
*/
export interface WorkflowResponse {
/**
* Workflows
*/
workflows: Workflow[];
}
export interface Workflow {
/**
* the id of the workflow
*/
id: string;
/**
* The name of the workflow
*/
name: string;
/**
* The description of the workflow
*/
description: string;
/**
* The whatif template id
*/
whatIfTemplateId?: string;
/**
* the minimum forecast length of the workflow
*/
minimumForecastLength?: string;
/**
* the maximum forecast length of the workflow
*/
maximumForecastLength?: string;
}
47 changes: 47 additions & 0 deletions test/unit/fews/moduleRunTimes.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { DocumentFormat, PiWebserviceProvider, ModuleRuntimesFilter } from "../../../src";

import expectedResponse from "../mock/moduleRunTimes.json";
import "cross-fetch/polyfill";
import fetchMock from "fetch-mock";

describe("moduleRunTimes", function () {
afterAll(() => {
fetchMock.restore();
});

it("gets called when done", async () => {
fetchMock.get(
"https://mock.dev/fewswebservices/rest/fewspiservice/v1/moduleruntimes?documentFormat=PI_JSON",
{
status: 200,
body: JSON.stringify(expectedResponse),
}
);

const provider = new PiWebserviceProvider(
"https://mock.dev/fewswebservices"
);

const filter: ModuleRuntimesFilter = {
documentFormat: DocumentFormat.PI_JSON,
}

const results = await provider.getModuleRunTimes(filter);
expect(results).toStrictEqual(expectedResponse);
expect(results.moduleRunTimes.length).toBe(2);
expect(results.moduleRunTimes[0].workflowId).toBe("testWorkflowId1");
expect(results.moduleRunTimes[0].moduleInstanceId).toBe("testModuleInstanceId1");
expect(results.moduleRunTimes[0].mcId).toBe("testId1");
expect(results.moduleRunTimes[0].expectedStartTime).toBe(1737640822000);
expect(results.moduleRunTimes[0].expectedCompletionTime).toBe(1737640822000);
expect(results.moduleRunTimes[0].expectedPendingDuration).toBe(21568);
expect(results.moduleRunTimes[0].expectedRunningDuration).toBe(41);
expect(results.moduleRunTimes[1].workflowId).toBe("testWorkflowId2");
expect(results.moduleRunTimes[1].moduleInstanceId).toBe("testModuleInstanceId2");
expect(results.moduleRunTimes[1].mcId).toBe("testId2");
expect(results.moduleRunTimes[1].expectedStartTime).toBe(1737640822000);
expect(results.moduleRunTimes[1].expectedCompletionTime).toBe(1737641175000);
expect(results.moduleRunTimes[1].expectedPendingDuration).toBe(21610);
expect(results.moduleRunTimes[1].expectedRunningDuration).toBe(353755);
});
});
39 changes: 39 additions & 0 deletions test/unit/fews/workflows.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { DocumentFormat, PiWebserviceProvider, WorkflowsFilter } from "../../../src";

import expectedResponse from "../mock/workflows.json";
import "cross-fetch/polyfill";
import fetchMock from "fetch-mock";

describe("workflows", function () {
afterAll(() => {
fetchMock.restore();
});

it("gets called when done", async () => {
fetchMock.get(
"https://mock.dev/fewswebservices/rest/fewspiservice/v1/workflows?documentFormat=PI_JSON",
{
status: 200,
body: JSON.stringify(expectedResponse),
}
);

const provider = new PiWebserviceProvider(
"https://mock.dev/fewswebservices"
);

const workflowsFilter: WorkflowsFilter = {
documentFormat: DocumentFormat.PI_JSON,
}

const results = await provider.getWorkflows(workflowsFilter);
expect(results).toStrictEqual(expectedResponse);
expect(results.workflows.length).toBe(2);
expect(results.workflows[0].id).toBe("ImportHistorical");
expect(results.workflows[0].name).toBe("Import Historical Data");
expect(results.workflows[0].description).toBe("");
expect(results.workflows[1].id).toBe("ImportTEST");
expect(results.workflows[1].name).toBe("Import TEST");
expect(results.workflows[1].description).toBe("Import TEST");
});
});
22 changes: 22 additions & 0 deletions test/unit/mock/moduleRunTimes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"moduleRunTimes": [
{
"workflowId": "testWorkflowId1",
"moduleInstanceId": "testModuleInstanceId1",
"mcId": "testId1",
"expectedStartTime": 1737640822000,
"expectedCompletionTime": 1737640822000,
"expectedPendingDuration": 21568,
"expectedRunningDuration": 41
},
{
"workflowId": "testWorkflowId2",
"moduleInstanceId": "testModuleInstanceId2",
"mcId": "testId2",
"expectedStartTime": 1737640822000,
"expectedCompletionTime": 1737641175000,
"expectedPendingDuration": 21610,
"expectedRunningDuration": 353755
}
]
}
14 changes: 14 additions & 0 deletions test/unit/mock/workflows.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"workflows": [
{
"id": "ImportHistorical",
"name": "Import Historical Data",
"description": ""
},
{
"id": "ImportTEST",
"name": "Import TEST",
"description": "Import TEST"
}
]
}

0 comments on commit 3886d76

Please sign in to comment.