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

Implement aggregate operations on dates #9444

Merged
merged 5 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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 @@ -4,6 +4,7 @@ import { aggregateOperationComponentState } from '@/object-record/record-board/r
import { availableFieldIdsForAggregateOperationComponentState } from '@/object-record/record-board/record-board-column/states/availableFieldIdsForAggregateOperationComponentState';
import { getAggregateOperationLabel } from '@/object-record/record-board/record-board-column/utils/getAggregateOperationLabel';
import { recordIndexKanbanAggregateOperationState } from '@/object-record/record-index/states/recordIndexKanbanAggregateOperationState';
import { convertExtendedAggregateOperationToAggregateOperation } from '@/object-record/utils/convertExtendedAggregateOperationToAggregateOperation';
import { DropdownMenuHeader } from '@/ui/layout/dropdown/components/DropdownMenuHeader';
import { DropdownMenuItemsContainer } from '@/ui/layout/dropdown/components/DropdownMenuItemsContainer';
import { useRecoilComponentValueV2 } from '@/ui/utilities/state/component-state/hooks/useRecoilComponentValueV2';
Expand Down Expand Up @@ -47,6 +48,8 @@ export const RecordBoardColumnHeaderAggregateDropdownFieldsContent = () => {

if (!isDefined(aggregateOperation)) return <></>;

const convertedAggregateOperation =
convertExtendedAggregateOperationToAggregateOperation(aggregateOperation);
return (
<>
<DropdownMenuHeader
Expand All @@ -72,7 +75,7 @@ export const RecordBoardColumnHeaderAggregateDropdownFieldsContent = () => {
onClick={() => {
updateViewAggregate({
kanbanAggregateOperationFieldMetadataId: fieldId,
kanbanAggregateOperation: aggregateOperation,
kanbanAggregateOperation: convertedAggregateOperation,
});
closeDropdown();
}}
Expand All @@ -82,7 +85,7 @@ export const RecordBoardColumnHeaderAggregateDropdownFieldsContent = () => {
recordIndexKanbanAggregateOperation?.fieldMetadataId ===
fieldId &&
recordIndexKanbanAggregateOperation?.operation ===
aggregateOperation
convertedAggregateOperation
? IconCheck
: undefined
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { aggregateOperationComponentState } from '@/object-record/record-board/r
import { availableFieldIdsForAggregateOperationComponentState } from '@/object-record/record-board/record-board-column/states/availableFieldIdsForAggregateOperationComponentState';
import { getAggregateOperationLabel } from '@/object-record/record-board/record-board-column/utils/getAggregateOperationLabel';
import { AGGREGATE_OPERATIONS } from '@/object-record/record-table/constants/AggregateOperations';
import { ExtendedAggregateOperations } from '@/object-record/record-table/types/ExtendedAggregateOperations';
import { TableOptionsHotkeyScope } from '@/object-record/record-table/types/TableOptionsHotkeyScope';
import { AvailableFieldsForAggregateOperation } from '@/object-record/types/AvailableFieldsForAggregateOperation';
import { DropdownMenuHeader } from '@/ui/layout/dropdown/components/DropdownMenuHeader';
Expand Down Expand Up @@ -69,7 +70,7 @@ export const RecordBoardColumnHeaderAggregateDropdownOptionsContent = ({
availableAggregationOperation !== AGGREGATE_OPERATIONS.count
) {
setAggregateOperation(
availableAggregationOperation as AGGREGATE_OPERATIONS,
availableAggregationOperation as ExtendedAggregateOperations,
);

setAvailableFieldsForAggregateOperation(
Expand All @@ -87,7 +88,7 @@ export const RecordBoardColumnHeaderAggregateDropdownOptionsContent = ({
}
}}
text={getAggregateOperationLabel(
availableAggregationOperation as AGGREGATE_OPERATIONS,
availableAggregationOperation as ExtendedAggregateOperations,
)}
hasSubMenu={
availableAggregationOperation === AGGREGATE_OPERATIONS.count
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { recordIndexFiltersState } from '@/object-record/record-index/states/rec
import { recordIndexKanbanAggregateOperationState } from '@/object-record/record-index/states/recordIndexKanbanAggregateOperationState';
import { recordIndexKanbanFieldMetadataIdState } from '@/object-record/record-index/states/recordIndexKanbanFieldMetadataIdState';
import { recordIndexViewFilterGroupsState } from '@/object-record/record-index/states/recordIndexViewFilterGroupsState';
import { UserContext } from '@/users/contexts/UserContext';
import { useIsFeatureEnabled } from '@/workspace/hooks/useIsFeatureEnabled';
import { useContext } from 'react';
import { useRecoilValue } from 'recoil';
Expand Down Expand Up @@ -80,12 +81,17 @@ export const useAggregateRecordsForRecordBoardColumn = () => {
skip: !isAggregateQueryEnabled,
});

const { dateFormat, timeFormat, timeZone } = useContext(UserContext);

const { value, labelWithFieldName } = computeAggregateValueAndLabel({
data,
objectMetadataItem,
fieldMetadataId: recordIndexKanbanAggregateOperation?.fieldMetadataId,
aggregateOperation: recordIndexKanbanAggregateOperation?.operation,
fallbackFieldName: kanbanFieldName,
dateFormat,
timeFormat,
timeZone,
});

return {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { RecordBoardColumnHeaderAggregateDropdownComponentInstanceContext } from '@/object-record/record-board/contexts/RecordBoardColumnHeaderAggregateDropdownComponentInstanceContext';
import { AGGREGATE_OPERATIONS } from '@/object-record/record-table/constants/AggregateOperations';
import { ExtendedAggregateOperations } from '@/object-record/record-table/types/ExtendedAggregateOperations';
import { createComponentStateV2 } from '@/ui/utilities/state/component-state/utils/createComponentStateV2';

export const aggregateOperationComponentState =
createComponentStateV2<AGGREGATE_OPERATIONS | null>({
createComponentStateV2<ExtendedAggregateOperations | null>({
key: 'aggregateOperationComponentFamilyState',
defaultValue: null,
componentInstanceContext:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { DateFormat } from '@/localization/constants/DateFormat';
import { TimeFormat } from '@/localization/constants/TimeFormat';
import { FieldMetadataItem } from '@/object-metadata/types/FieldMetadataItem';
import { ObjectMetadataItem } from '@/object-metadata/types/ObjectMetadataItem';
import { AggregateRecordsData } from '@/object-record/hooks/useAggregateRecords';
import { computeAggregateValueAndLabel } from '@/object-record/record-board/record-board-column/utils/computeAggregateValueAndLabel';
import { AGGREGATE_OPERATIONS } from '@/object-record/record-table/constants/AggregateOperations';
import { FieldMetadataType } from '~/generated/graphql';
Expand All @@ -20,13 +23,20 @@ describe('computeAggregateValueAndLabel', () => {
],
} as ObjectMetadataItem;

const defaultParams = {
dateFormat: DateFormat.DAY_FIRST,
timeFormat: TimeFormat.HOUR_24,
timeZone: 'UTC',
};

it('should return empty object for empty data', () => {
const result = computeAggregateValueAndLabel({
data: {},
data: {} as AggregateRecordsData,
objectMetadataItem: mockObjectMetadata,
fieldMetadataId: MOCK_FIELD_ID,
aggregateOperation: AGGREGATE_OPERATIONS.sum,
fallbackFieldName: MOCK_KANBAN_FIELD_NAME,
...defaultParams,
});

expect(result).toEqual({});
Expand All @@ -37,14 +47,15 @@ describe('computeAggregateValueAndLabel', () => {
amount: {
[AGGREGATE_OPERATIONS.sum]: 2000000,
},
};
} as AggregateRecordsData;

const result = computeAggregateValueAndLabel({
data: mockData,
objectMetadataItem: mockObjectMetadata,
fieldMetadataId: MOCK_FIELD_ID,
aggregateOperation: AGGREGATE_OPERATIONS.sum,
fallbackFieldName: MOCK_KANBAN_FIELD_NAME,
...defaultParams,
});

expect(result).toEqual({
Expand Down Expand Up @@ -74,14 +85,15 @@ describe('computeAggregateValueAndLabel', () => {
percentage: {
[AGGREGATE_OPERATIONS.avg]: 0.3,
},
};
} as AggregateRecordsData;

const result = computeAggregateValueAndLabel({
data: mockData,
objectMetadataItem: mockObjectMetadataWithPercentageField,
fieldMetadataId: MOCK_FIELD_ID,
aggregateOperation: AGGREGATE_OPERATIONS.avg,
fallbackFieldName: MOCK_KANBAN_FIELD_NAME,
...defaultParams,
});

expect(result).toEqual({
Expand Down Expand Up @@ -111,14 +123,15 @@ describe('computeAggregateValueAndLabel', () => {
decimals: {
[AGGREGATE_OPERATIONS.sum]: 0.009,
},
};
} as AggregateRecordsData;

const result = computeAggregateValueAndLabel({
data: mockData,
objectMetadataItem: mockObjectMetadataWithDecimalsField,
fieldMetadataId: MOCK_FIELD_ID,
aggregateOperation: AGGREGATE_OPERATIONS.sum,
fallbackFieldName: MOCK_KANBAN_FIELD_NAME,
...defaultParams,
});

expect(result).toEqual({
Expand All @@ -128,17 +141,88 @@ describe('computeAggregateValueAndLabel', () => {
});
});

it('should handle datetime field with min operation', () => {
const mockObjectMetadataWithDatetimeField: ObjectMetadataItem = {
id: '123',
fields: [
{
id: MOCK_FIELD_ID,
name: 'createdAt',
label: 'Created At',
type: FieldMetadataType.DateTime,
} as FieldMetadataItem,
],
} as ObjectMetadataItem;

const mockData = {
createdAt: {
[AGGREGATE_OPERATIONS.min]: '2023-01-01T12:00:00Z',
},
} as AggregateRecordsData;

const result = computeAggregateValueAndLabel({
data: mockData,
objectMetadataItem: mockObjectMetadataWithDatetimeField,
fieldMetadataId: MOCK_FIELD_ID,
aggregateOperation: AGGREGATE_OPERATIONS.min,
fallbackFieldName: MOCK_KANBAN_FIELD_NAME,
...defaultParams,
});

expect(result).toEqual({
label: 'Earliest date',
labelWithFieldName: 'Earliest date of Created At',
value: '1 Jan, 2023 12:00',
});
});

it('should handle datetime field with max operation', () => {
const mockObjectMetadataWithDatetimeField: ObjectMetadataItem = {
id: '123',
fields: [
{
id: MOCK_FIELD_ID,
name: 'updatedAt',
label: 'Updated At',
type: FieldMetadataType.DateTime,
} as FieldMetadataItem,
],
} as ObjectMetadataItem;

const mockData = {
updatedAt: {
[AGGREGATE_OPERATIONS.max]: '2023-12-31T23:59:59Z',
},
} as AggregateRecordsData;

const result = computeAggregateValueAndLabel({
data: mockData,
objectMetadataItem: mockObjectMetadataWithDatetimeField,
fieldMetadataId: MOCK_FIELD_ID,
aggregateOperation: AGGREGATE_OPERATIONS.max,
fallbackFieldName: MOCK_KANBAN_FIELD_NAME,
...defaultParams,
});

expect(result).toEqual({
value: '31 Dec, 2023 23:59',
label: 'Latest date',
labelWithFieldName: 'Latest date of Updated At',
});
});

it('should default to count when field not found', () => {
const mockData = {
[MOCK_KANBAN_FIELD_NAME]: {
[AGGREGATE_OPERATIONS.count]: 42,
},
};
} as AggregateRecordsData;

const result = computeAggregateValueAndLabel({
data: mockData,
objectMetadataItem: mockObjectMetadata,
fallbackFieldName: MOCK_KANBAN_FIELD_NAME,
...defaultParams,
});

expect(result).toEqual({
Expand All @@ -153,13 +237,14 @@ describe('computeAggregateValueAndLabel', () => {
amount: {
[AGGREGATE_OPERATIONS.sum]: undefined,
},
};
} as AggregateRecordsData;

const result = computeAggregateValueAndLabel({
data: mockData,
objectMetadataItem: mockObjectMetadata,
fieldMetadataId: MOCK_FIELD_ID,
aggregateOperation: AGGREGATE_OPERATIONS.sum,
...defaultParams,
});

expect(result).toEqual({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getAggregateOperationLabel } from '@/object-record/record-board/record-board-column/utils/getAggregateOperationLabel';
import { AGGREGATE_OPERATIONS } from '@/object-record/record-table/constants/AggregateOperations';
import { expect } from '@storybook/test';

describe('getAggregateOperationLabel', () => {
it('should return correct labels for each operation', () => {
Expand All @@ -8,6 +9,8 @@ describe('getAggregateOperationLabel', () => {
expect(getAggregateOperationLabel(AGGREGATE_OPERATIONS.avg)).toBe(
'Average',
);
expect(getAggregateOperationLabel('EARLIEST')).toBe('Earliest date');
expect(getAggregateOperationLabel('LATEST')).toBe('Latest date');
Comment on lines +12 to +13
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Testing new date aggregate operations 'EARLIEST' and 'LATEST' which are not defined in AGGREGATE_OPERATIONS enum. Consider adding these to the enum for consistency.

expect(getAggregateOperationLabel(AGGREGATE_OPERATIONS.sum)).toBe('Sum');
expect(getAggregateOperationLabel(AGGREGATE_OPERATIONS.count)).toBe(
'Count all',
Expand Down
Loading
Loading