Skip to content

Commit

Permalink
Add new method of fetching scenarios using fetchScenariosPost which a…
Browse files Browse the repository at this point in the history
…llows filtering by any item on the scenario during the Post.

Add this methodology into the ScenariosOld component allowing users to input the QueryFilter as a prop which scenarios will then be fetched using.

This allows for more efficient fetching of data as only the scenarios matching the required filters are pulled across, reducing required filtering on the frontend and load on the api.
  • Loading branch information
JacobJsuh committed Jun 10, 2024
1 parent 67b00aa commit d3f24b2
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 14 deletions.
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 @@ -61,10 +63,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 @@ -98,18 +100,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 d3f24b2

Please sign in to comment.