Skip to content

Commit

Permalink
feat(asset): compute "Now" in a way that can be saved (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
Catalin-web authored Oct 29, 2024
1 parent bcf60f0 commit 728d47f
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/core/query-builder.utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe('QueryBuilderUtils', () => {
});

it('should handle unsupported operations correctly', () => {
const query = 'Object1 > "value1" AND Object2 < "value2"';
const query = 'Object1 % "value1" AND Object2 % "value2"';
const result = transformComputedFieldsQuery(query, computedDataFields);
expect(result).toBe(query);
});
Expand Down
2 changes: 1 addition & 1 deletion src/core/query-builder.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type ExpressionTransformFunction = (value: string, operation: string, opt
/**
* Supported operations for computed fields
*/
export const computedFieldsupportedOperations = ['=', '!='];
export const computedFieldsupportedOperations = ['=', '!=', '>', '>=', '<', '<='];

/**
* The function will replace the computed fields with their transformation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,18 @@ describe('AssetQueryBuilder', () => {
expect(conditionsContainer.item(0)?.textContent).toContain(globalVariableOption.label);
});

it('should select user friendly value for calibration due date', () => {
const workspace = { id: '1', name: 'Selected workspace' } as Workspace;
const system = { id: '1', alias: 'Selected system' } as SystemMetadata;
[['${__from:date}', 'From'], ['${__to:date}', 'To' ], ['${__now:date}', 'Now' ]].forEach(([value, label]) =>
{
it( `should select user friendly value for calibration due date`, () =>
{
const workspace = { id: '1', name: 'Selected workspace' } as Workspace;
const system = { id: '1', alias: 'Selected system' } as SystemMetadata;

const { conditionsContainer } = renderElement([workspace], [system], 'ExternalCalibration.NextRecommendedDate > \"${__from:date}\"');
const { conditionsContainer } = renderElement([workspace], [system], `ExternalCalibration.NextRecommendedDate > \"${value}\"` );

expect(conditionsContainer?.length).toBe(1);
expect(conditionsContainer.item(0)?.textContent).toContain("From");
expect(conditionsContainer?.length).toBe(1);
expect(conditionsContainer.item(0)?.textContent).toContain(label);
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export const AssetQueryBuilder: React.FC<AssetCalibrationQueryBuilderProps> = ({
...(calibrationField.lookup?.dataSource || []),
{ label: 'From', value: '${__from:date}' },
{ label: 'To', value: '${__to:date}' },
{ label: 'Now', value: new Date().toISOString() },
{ label: 'Now', value: '${__now:date}' },
],
},
};
Expand Down
1 change: 1 addition & 0 deletions src/datasources/asset/constants/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export enum AllFieldNames {
VENDOR_NAME = 'VendorName',
BUS_TYPE = 'BusType',
ASSET_TYPE = 'AssetType',
CALIBRATION_DUE_DATE = 'ExternalCalibration.NextRecommendedDate',
}

export const QUERY_LIMIT = 1000;
14 changes: 13 additions & 1 deletion src/datasources/asset/data-sources/AssetDataSourceBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ export abstract class AssetDataSourceBase extends DataSourceBase<AssetQuery, Ass
}

public readonly queryTransformationOptions = new Map<string, Map<string, unknown>>([
[AllFieldNames.LOCATION, this.systemAliasCache]
[AllFieldNames.LOCATION, this.systemAliasCache],
[AllFieldNames.CALIBRATION_DUE_DATE, new Map<string, unknown>()]
]);

public readonly assetComputedDataFields = new Map<string, ExpressionTransformFunction>([
Expand All @@ -116,6 +117,17 @@ export abstract class AssetDataSourceBase extends DataSourceBase<AssetQuery, Ass
}

return `Locations.Any(l => l.MinionId ${operation} "${value}" ${this.getLocicalOperator(operation)} l.PhysicalLocation ${operation} "${value}")`;
}],
[
AllFieldNames.CALIBRATION_DUE_DATE,
(value: string, operation: string, options?: Map<string, unknown>) =>
{
if (value === '${__now:date}' )
{
return `${AllFieldNames.CALIBRATION_DUE_DATE} ${operation} "${new Date().toISOString()}"`;
}

return `${AllFieldNames.CALIBRATION_DUE_DATE} ${operation} "${value}"`;
}]]);

protected multipleValuesQuery(field: string): ExpressionTransformFunction {
Expand Down

0 comments on commit 728d47f

Please sign in to comment.