Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(react-components): react components rule core improvements to fix several issues in the Rule Builder time series selection/searching/selecting/filtering #4805

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
dac34e7
update hooks to support execute parallel, chunks and argument name
danpriori Oct 9, 2024
b2e61ab
add hook to get the resource from relationship
danpriori Oct 10, 2024
036c44d
move get resource relationship and add timeseries data type on relati…
danpriori Oct 11, 2024
88bf908
chore: remove unused function
danpriori Oct 11, 2024
d331ba5
chore: lint fix
danpriori Oct 11, 2024
727c8df
lint
danpriori Oct 11, 2024
bbe2ff5
using a hook instead of exposing the get resource relationship
danpriori Oct 14, 2024
918e8d3
chore: remove dist
danpriori Oct 14, 2024
be20f80
Merge branch 'master' into danpriori/BND3D-4356-rule-builder-assets-w…
danpriori Oct 14, 2024
cdbaaa7
change to useQuery
danpriori Oct 14, 2024
c99aea9
Merge remote-tracking branch 'origin/danpriori/BND3D-4356-rule-builde…
danpriori Oct 14, 2024
86ae42c
fix: change the logic to get the time series first and search for the…
danpriori Oct 17, 2024
32c112f
add querykey - cr
danpriori Oct 18, 2024
43bd5f4
add utests for relationship and time series and separate the hook for…
danpriori Oct 21, 2024
3401988
Merge branch 'master' into danpriori/BND3D-4356-rule-builder-assets-w…
danpriori Oct 21, 2024
240fd11
lint
danpriori Oct 21, 2024
15a2b0d
Merge remote-tracking branch 'origin/danpriori/BND3D-4356-rule-builde…
danpriori Oct 21, 2024
7a84c26
Merge branch 'master' into danpriori/BND3D-4356-rule-builder-assets-w…
danpriori Oct 22, 2024
1c460b7
Merge branch 'master' into danpriori/BND3D-4356-rule-builder-assets-w…
danpriori Oct 22, 2024
02b90dd
Merge branch 'master' into danpriori/BND3D-4356-rule-builder-assets-w…
danpriori Oct 22, 2024
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 @@ -96,7 +96,7 @@ export function RuleBasedOutputsSelector({
const { isLoading: isLoadingAssetIdsAndTimeseriesData, data: assetIdsWithTimeseriesData } =
useAssetsAndTimeseriesLinkageDataQuery({
timeseriesExternalIds,
contextualizedAssetNodes
assetNodes: contextualizedAssetNodes
});

const flatAssetsMappingsListPerModel = useCreateAssetMappingsMapPerModel(models, assetMappings);
Expand Down
2 changes: 2 additions & 0 deletions react-components/src/components/RuleBasedOutputs/index.ts
danpriori marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export * from './hooks';
export { RuleBasedOutputsPanel } from './RuleBasedOutputsPanel';
export { getRuleTriggerTypes } from './utils';

export * from './utils/index';

export type {
RuleAndEnabled,
TriggerType,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*!
* Copyright 2024 Cognite AS
*/
import { type RelationshipResourceType, type CogniteClient } from '@cognite/sdk/dist/src';
danpriori marked this conversation as resolved.
Show resolved Hide resolved
import { type ExtendedRelationshipWithSourceAndTarget } from '../../../data-providers/types';
import { getRelationships } from '../../../hooks/network/getRelationships';

export const getResourceRelationship = async (
danpriori marked this conversation as resolved.
Show resolved Hide resolved
sdk: CogniteClient,
resourceIds: string[],
resourceTypes: RelationshipResourceType[]
): Promise<ExtendedRelationshipWithSourceAndTarget[]> => {
if (resourceIds.length === 0 || resourceIds.filter((id) => id.length > 0).length === 0) {
return [];
}
return await getRelationships(sdk, {
resourceExternalIds: resourceIds,
relationshipResourceTypes: resourceTypes
});
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/*!
* Copyright 2024 Cognite AS
*/
export { getResourceRelationship } from './getResourceRelationship';
8 changes: 7 additions & 1 deletion react-components/src/data-providers/index.ts
danpriori marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,11 @@
* Copyright 2024 Cognite AS
*/

export type { FdmInstanceWithView, InstanceReference, AssetInstanceReference } from './types';
export type {
FdmInstanceWithView,
InstanceReference,
AssetInstanceReference,
AssetIdsAndTimeseriesData,
ExtendedRelationship
} from './types';
export type { Source, DmsUniqueIdentifier, SimpleSource } from './FdmSDK';
11 changes: 11 additions & 0 deletions react-components/src/data-providers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,21 @@ export type ExtendedRelationship = {
relation: 'Source' | 'Target';
} & Relationship;

export type ExtendedRelationshipWithSourceAndTarget = {
relation: 'Source' | 'Target';
} & Relationship &
Partial<RelationshipSourceAndTargetTimeseries>;

export type RelationshipSourceAndTargetTimeseries = {
source: Timeseries;
target: Timeseries;
};

export type RelationshipSourceAndTarget = {
source: RelationshipSourceAndTargetData;
target: RelationshipSourceAndTargetData;
};

export type RelationshipSourceAndTargetData = {
externalId?: string;
id?: number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,25 @@
* Copyright 2024 Cognite AS
*/
import { type CogniteClient, type IdEither, type Datapoints } from '@cognite/sdk';
import { executeParallel } from '../../utilities/executeParallel';
import { isDefined } from '../../utilities/isDefined';
import { chunk } from 'lodash';

const FETCH_CHUNK = 100;

export const getTimeseriesLatestDatapoints = async (
sdk: CogniteClient,
timeseriesIds: IdEither[]
): Promise<Datapoints[]> => {
return await sdk.datapoints.retrieveLatest(timeseriesIds);
const timeseriesChunks = chunk(timeseriesIds, FETCH_CHUNK);

const timeseriesDatapoints = await executeParallel(
timeseriesChunks.map(
(timeseriesIds) => async () => await sdk.datapoints.retrieveLatest(timeseriesIds)
),
2
);

const cleanDatapoints = timeseriesDatapoints.filter(isDefined).flat();
return cleanDatapoints;
};
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,19 @@ import { getTimeseriesByIds } from '../hooks/network/getTimeseriesByIds';
import { isDefined } from '../utilities/isDefined';
import { getAssetsByIds } from '../hooks/network/getAssetsByIds';
import { getTimeseriesLatestDatapoints } from '../hooks/network/getTimeseriesLatestDatapoints';
import { chunk, uniqBy } from 'lodash';
import { executeParallel } from '../utilities/executeParallel';

const FETCH_RELATIONSHIP_CHUNK = 1000;

type Props = {
timeseriesExternalIds: CogniteExternalId[];
contextualizedAssetNodes: Asset[];
assetNodes: Asset[];
};

export function useAssetsAndTimeseriesLinkageDataQuery({
timeseriesExternalIds,
contextualizedAssetNodes
assetNodes
}: Props): UseQueryResult<AssetIdsAndTimeseriesData, unknown> {
const sdk = useSDK();

Expand Down Expand Up @@ -64,16 +68,18 @@ export function useAssetsAndTimeseriesLinkageDataQuery({
timeseries
?.map((timeseries) => {
return getAssetIdsFromTimeseries(
contextualizedAssetNodes,
assetNodes,
timeseries,
assetAndTimeseriesIdsFromRelationship
);
})
.flat()
.filter(isDefined) ?? [];

const uniqueAssetIds = uniqBy(assetIdsFound, 'externalId');

const assetFromTimeseries =
assetIdsFound.length > 0 ? await getAssetsByIds(sdk, assetIdsFound) : [];
assetIdsFound.length > 0 ? await getAssetsByIds(sdk, uniqueAssetIds) : [];

const assetIdsWithTimeseries =
timeseries
Expand Down Expand Up @@ -102,13 +108,23 @@ const getLinkFromRelationships = async (
timeseriesExternalIds: string[],
relationshipResourceTypes: RelationshipResourceType[]
): Promise<AssetAndTimeseriesIds[]> => {
const dataRelationship = await getRelationships(sdk, {
resourceExternalIds: timeseriesExternalIds,
relationshipResourceTypes
});
const timeseriesChunks = chunk(timeseriesExternalIds, FETCH_RELATIONSHIP_CHUNK);

const dataRelationship = await executeParallel(
timeseriesChunks.map(
(timeseriesIds) => async () =>
await getRelationships(sdk, {
resourceExternalIds: timeseriesIds,
relationshipResourceTypes
})
),
2
);

const cleanDataRelationship = dataRelationship.filter(isDefined).flat();

const assetAndTimeseriesIdsFromRelationship =
dataRelationship?.map((item) => {
cleanDataRelationship?.map((item) => {
const assetAndTimeseriesIds: AssetAndTimeseriesIds = {
assetIds: { externalId: '' },
timeseriesIds: { externalId: '' }
Expand Down
Loading