Skip to content

Commit

Permalink
setup, cleanup field querying
Browse files Browse the repository at this point in the history
  • Loading branch information
Ciaran Schutte committed Dec 16, 2024
1 parent 4445d4b commit e354ba1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
1 change: 1 addition & 0 deletions modules/server/src/network/setup/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ObjectValues } from '@/utils/types';
/**
* Supported aggregations that can be used by network aggregation
*/
// todo SUPPORTED_AGGREGATONS TO AGGREGATION
export const SUPPORTED_AGGREGATIONS = {
Aggregations: 'Aggregations',
NumericAggregations: 'NumericAggregations',
Expand Down
43 changes: 27 additions & 16 deletions modules/server/src/network/setup/fields.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
import { GQLFieldType } from '../queries';
import {
NodeConfig,
SupportedAggregations,
SupportedNetworkFieldType,
UnsupportedAggregations,
} from '../types/types';
import { SUPPORTED_AGGREGATIONS_LIST } from './constants';
import { SupportedAggregation, SUPPORTED_AGGREGATIONS_LIST } from './constants';
import { NodeConfig } from './query';

export type NetworkFields = { name: string; fields: GQLFieldType[] };
type NetworkFieldType<T> = {
name: string;
type: T;
};
type SupportedNetworkFieldType = NetworkFieldType<SupportedAggregation>;
type SupportedAggregations = SupportedNetworkFieldType[];
type UnsupportedAggregations = NetworkFieldType<string>[];

const isSupportedType = (
fieldObject: NetworkFieldType<string>,
supportedList: string[],
): fieldObject is SupportedNetworkFieldType => {
return supportedList.includes(fieldObject.type);
};

/**
* Parse network fields into supported and unsupported
*
* @param fields
* @returns { supportedAggregations: [], unsupportedAggregations: [] }
* @returns An object containing supported types and unsupported types
*/
export const getFieldTypes = (fields: NodeConfig['aggregations']) => {
const fieldTypes = fields.reduce(
Expand All @@ -24,8 +31,7 @@ export const getFieldTypes = (fields: NodeConfig['aggregations']) => {
},
field,
) => {
const isAggregationTypeSupported = SUPPORTED_AGGREGATIONS_LIST.includes(field.type);
if (isAggregationTypeSupported) {
if (isSupportedType(field, SUPPORTED_AGGREGATIONS_LIST)) {
return {
...aggregations,
supportedAggregations: aggregations.supportedAggregations.concat(field),
Expand All @@ -47,18 +53,22 @@ export const getFieldTypes = (fields: NodeConfig['aggregations']) => {
};

/**
* Takes in all node aggregation field types and returns a single array.
* - dedupes
* - ensures types are supported
*
* @param nodeConfigs
* @param supportedTypes
* @returns unique fields
* @param nodeConfigs -
* @returns Unique and supported aggregation field types.
*/
export const getAllFieldTypes = (nodeConfigs: NodeConfig[]): SupportedNetworkFieldType[] => {
export const normalizeFieldTypes = (nodeConfigs: NodeConfig[]): SupportedNetworkFieldType[] => {
// split into supported and unsupported types
const nodeFieldTypes = nodeConfigs.map((config) => {
const { supportedAggregations, unsupportedAggregations } = getFieldTypes(config.aggregations);

return { supportedAggregations, unsupportedAggregations };
});

// flatten to single object
const allSupportedAggregations = nodeFieldTypes.flatMap(
(fieldType) => fieldType.supportedAggregations,
);
Expand All @@ -72,5 +82,6 @@ export const getAllFieldTypes = (nodeConfigs: NodeConfig[]): SupportedNetworkFie
const uniqueSupportedAggregations = Array.from(
new Set(allSupportedAggregations.map((nodeField) => JSON.stringify(nodeField))),
).map((nodeFieldString) => JSON.parse(nodeFieldString));

return uniqueSupportedAggregations;
};

0 comments on commit e354ba1

Please sign in to comment.