Skip to content

Commit

Permalink
Merge pull request #370 from DHI/feat/Scenarios-Old-Fetch-Scenarios-B…
Browse files Browse the repository at this point in the history
…y-Query-With-Filter

feat/scenarios old fetch scenarios by query with filter
  • Loading branch information
JacobJsuh authored Jun 12, 2024
2 parents 37aa1c2 + d77eebb commit 7be0206
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 15 deletions.
2 changes: 1 addition & 1 deletion packages/react-components/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.8.49",
"version": "0.8.50",
"license": "MIT",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand Down
62 changes: 52 additions & 10 deletions packages/react-components/src/Scenarios/ScenariosOLD/Scenarios.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import {
postScenario,
updateScenario,
} from '../../api';
import { JobParameters } from '../../api/types';
import { JobParameters, QueryFilter } from '../../api/types';
import GeneralDialog from '../../common/GeneralDialog/GeneralDialog';
import GeneralDialogProps from '../../common/GeneralDialog/types';
import { checkConditions, getObjectProperty, setObjectProperty } from '../../utils/Utils';
import { ScenarioListOLD } from '../ScenarioListOLD/ScenarioList';
import { MenuItem, QueryDates, ScenarioOLD } from '../types';
import ScenariosOLDProps from './types';
import useStyles from './useStyles';
import { fetchScenariosPost } from '../../api/Scenarios/ScenariosApi';

const ScenariosOLD = (props: ScenariosOLDProps) => {
const {
Expand All @@ -41,6 +42,7 @@ const ScenariosOLD = (props: ScenariosOLDProps) => {
showStatus,
status,
queryDates,
queryFilter,
frequency = 10,
onContextMenuClick,
onScenarioSelected,
Expand All @@ -62,10 +64,10 @@ const ScenariosOLD = (props: ScenariosOLDProps) => {
useEffect(() => {
let interval: ReturnType<typeof setTimeout>;

if (queryDates) {
fetchScenariosByDateList(queryDates);
if (queryDates || queryFilter) {
fetchScenariosByQuery(queryDates, queryFilter);

interval = setInterval(() => fetchScenariosByDateList(queryDates), frequency * 1000);
interval = setInterval(() => fetchScenariosByQuery(queryDates, queryFilter), frequency * 1000);
} else {
fetchScenariosList();

Expand Down Expand Up @@ -99,18 +101,58 @@ const ScenariosOLD = (props: ScenariosOLDProps) => {
}
}, [addScenario]);

const fetchScenariosByDateList = (queryDates: QueryDates) => {
const fetchScenariosByQuery = (queryDates: QueryDates, queryFilter?: QueryFilter[]) => {
const dataSelectors = [
nameField,
...descriptionFields!.map((descriptionField) => descriptionField.field),
...extraFields!.map((descriptionField) => descriptionField.field),
];
if (queryFilter) {
fetchScenariosByQueryFilter(queryFilter, dataSelectors);
} else {
fetchScenariosByDateList(queryDates, dataSelectors);
}
}

const fetchScenariosByQueryFilter = (queryFilter: QueryFilter[], dataSelectors: any[]) => {
fetchScenariosPost(
{
host,
connection: scenarioConnection,
dataSelectors,
queryFilter,
},
token,
).subscribe(
(res) => {
const rawScenarios = res.map((s: { data: string }) => {
s.data = s.data ? JSON.parse(s.data) : s.data;

return s;
});

const newScenarios = rawScenarios.filter((scenario) => checkConditions(scenario, dataFilterbyProperty));

setScenarios(newScenarios);

if (onScenariosReceived) {
onScenariosReceived(newScenarios);
}
},
(error) => {
console.log(error);
},
);
}

const fetchScenariosByDateList = (queryDates: QueryDates, dataSelectors: any[]) => {
fetchScenariosByDate(
{
host,
connection: scenarioConnection,
from: queryDates.from,
to: queryDates.to,
dataSelectors: [
nameField,
...descriptionFields!.map((descriptionField) => descriptionField.field),
...extraFields!.map((descriptionField) => descriptionField.field),
],
dataSelectors,
},
token,
).subscribe(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { JobParameters } from '../../api/types';
import { JobParameters, QueryFilter } from '../../api/types';
import { DescriptionField, MenuItem, QueryDates, Scenario, Status, StatusOverride } from '../types';

interface ScenariosOLDProps {
Expand Down Expand Up @@ -72,6 +72,10 @@ interface ScenariosOLDProps {
* Value range to fetch scenario by date
*/
queryDates?: QueryDates;
/**
* Query filters to use when fetching the scenarios data.
*/
queryFilter?: QueryFilter[];
/**
* Customising translation dialog
*/
Expand Down
27 changes: 25 additions & 2 deletions packages/react-components/src/api/Scenarios/ScenariosApi.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { tap } from 'rxjs/operators';
import { DataSource } from '../../api/types';
import { ScenarioItem } from '../../Scenarios/ScenarioItem/ScenarioItem';
import { fetchUrl } from '../helpers';

/**
Expand Down Expand Up @@ -40,6 +39,30 @@ const fetchScenarios = (dataSource: DataSource, token: string) => {
});
};

/**
* /api/scenarios/{connectionId}/query
* Gets all scenarios that match the given filtering criteria:
* @param dataSource
* @param filtering
* @param token
*/
const fetchScenariosPost = (dataSource: DataSource, token: string) => {
const dataSelectors =
dataSource.dataSelectors && dataSource.dataSelectors.length > 0
? `?dataSelectors=[${dataSource.dataSelectors
.map((dataSelector) => dataSelector.replace('data.', ''))
.join(',')}]`
: '';

return fetchUrl(`${dataSource.host}/api/scenarios/${dataSource.connection}/query${dataSelectors}`, {
method: 'Post',
additionalHeaders: {
Authorization: `Bearer ${token}`,
},
body: JSON.stringify(dataSource?.queryFilter),
});
};

/**
* /api/scenarios/{connectionId}
* Gets all scenarios within the given time span
Expand Down Expand Up @@ -126,4 +149,4 @@ const updateScenario = (dataSource: DataSource, token: string, scenario: any) =>
body: JSON.stringify(scenario),
});

export { fetchScenario, fetchScenarios, fetchScenariosByDate, deleteScenario, postScenario, updateScenario };
export { fetchScenario, fetchScenarios, fetchScenariosPost, fetchScenariosByDate, deleteScenario, postScenario, updateScenario };
14 changes: 13 additions & 1 deletion packages/react-components/src/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ interface DataSource {
* Format: new Date();
*/
to?: string;
/**
* Query filters to apply to the request.
*/
queryFilter?: QueryFilter[];
sheetName?: string;
tokenJobLog?: string;
hostJobLog?: string;
Expand Down Expand Up @@ -71,4 +75,12 @@ interface JobParameters {
[key: string]: string | string[];
}

export { Options, DataSource, Header, JobQuery, JobParameters, User };

interface QueryFilter {
item: string;
queryOperator: string;
value: string;
negation?: boolean;
}

export { Options, DataSource, Header, JobQuery, JobParameters, User, QueryFilter };

0 comments on commit 7be0206

Please sign in to comment.