Skip to content

Commit

Permalink
TEMP
Browse files Browse the repository at this point in the history
  • Loading branch information
manelcecs committed May 3, 2024
1 parent 504b7c4 commit f1d5756
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 29 deletions.
5 changes: 5 additions & 0 deletions src/domain-services/flow-object/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,8 @@ export const FlowObjectType = t.union([
]);

export type FlowObjectType = t.TypeOf<typeof FlowObjectType>;
export type FlowObjectDirection = 'source' | 'destination';
export type FlowObjectFilterGrouped = Map<
FlowObjectType,
Map<FlowObjectDirection, number[]>
>;
79 changes: 71 additions & 8 deletions src/domain-services/flows/flow-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ import { type InstanceOfModel } from '@unocha/hpc-api-core/src/db/util/types';
import { createBrandedValue } from '@unocha/hpc-api-core/src/util/types';
import type Knex from 'knex';
import { Service } from 'typedi';
import { FlowObjectType, type FlowObject } from '../flow-object/model';
import {
FlowObjectType,
type FlowObject,
type FlowObjectFilterGrouped,
} from '../flow-object/model';
import { type FlowParkedParentSource } from './graphql/types';
import {
type FlowEntity,
type FlowOrderBy,
type GetFlowsArgs,
type UniqueFlowEntity,
} from './model';
import {
applySearchFilters,
mapFlowOrderBy,
removeDuplicatesUniqueFlowEntities,
} from './strategy/impl/utils';
import { applySearchFilters, mapFlowOrderBy } from './strategy/impl/utils';

@Service()
export class FlowService {
Expand Down Expand Up @@ -151,7 +151,7 @@ export class FlowService {
if (entity === 'externalReference') {
const entityList = await dbConnection
.queryBuilder()
.select('flowID', 'versionID')
.distinct('flowID', 'versionID')
.from('externalReference')
.orderBy(mappedOrderBy.column, mappedOrderBy.order)
.orderBy('flowID', orderBy.order);
Expand Down Expand Up @@ -207,7 +207,7 @@ export class FlowService {
versionID: flowObject.versionID,
}));

return removeDuplicatesUniqueFlowEntities(mapFlowsToUniqueFlowEntities);
return mapFlowsToUniqueFlowEntities;
}

async getParketParents(
Expand Down Expand Up @@ -300,4 +300,67 @@ export class FlowService {

return mappedParkedParentOrganizations;
}

async getParkedParentFlowsByFlowObjectFilter(
models: Database,
databaseConnection: Knex,
flowObjectFilters: FlowObjectFilterGrouped
): Promise<UniqueFlowEntity[]> {
let query = databaseConnection
.queryBuilder()
.select('fChild.id', 'fChild.versionID')
.from('categoryRef as cr')
.where('cr.categoryID', 1252)
.andWhere('cr.objectType', 'flow')
// Flow link join + where
.innerJoin('flowLink as fl', function () {
this.on('fl.parentID', '=', 'cr.objectID');
})
.where('fl.depth', '>', 0)
// Flow parent join + where
.innerJoin('flow as fParent', function () {
this.on('cr.objectID', '=', 'fParent.id').andOn(
'cr.versionID',
'=',
'fParent.versionID'
);
})
.where('fParent.deletedAt', null)
.andWhere('fParent.activeStatus', true)
// Flow child join + where
.innerJoin('flow as fChild', function () {
this.on('fl.childID', '=', 'fChild.id');
})
.where('fChild.deletedAt', null)
.andWhere('fChild.activeStatus', true);
// Flow object join + where
// This alg iterates over the flowObjectFilters and creates a join for each flowObjectType
// and refDirection allowing to filter the flowObjects by the flowObjectType and refDirection
// inclusivelly for each
for (const [flowObjectType, group] of flowObjectFilters.entries()) {
for (const [direction, ids] of group.entries()) {
const groupingAlias = `${flowObjectType}_${direction}`;
query = query
.innerJoin(`flowObject as ${groupingAlias}`, function () {
this.on('fParent.id', '=', `${groupingAlias}.flowID`).andOn(
'fParent.versionID',
'=',
`${groupingAlias}.versionID`
);
})
.where(function () {
this.whereIn(`${groupingAlias}.objectID`, ids)
.andWhere(`${groupingAlias}.objectType`, flowObjectType)
.andWhere(`${groupingAlias}.refDirection`, direction);
});
}
}

const childsOfParkedParents = await query;
const mappedChildsOfParkedParents = childsOfParkedParents.map((child) => ({
id: child.id,
versionID: child.versionID,
}));
return mappedChildsOfParkedParents;
}
}
13 changes: 3 additions & 10 deletions src/domain-services/flows/graphql/args.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ArgsType, Field, InputType, Int } from 'type-graphql';
import { PaginationArgs } from '../../../utils/graphql/pagination';
import { FlowObjectType } from '../../flow-object/model';
import { type SystemID } from '../../report-details/graphql/types';
import { type FlowSortField, type FlowStatusFilter } from './types';

Expand Down Expand Up @@ -47,16 +48,8 @@ export class FlowObjectFilters {
@Field({ nullable: false })
direction: 'source' | 'destination';

@Field({ nullable: false })
objectType:
| 'location'
| 'organization'
| 'plan'
| 'usageYear'
| 'category'
| 'project'
| 'globalCluster'
| 'emergency';
@Field(() => String, { nullable: false })
objectType: FlowObjectType;

@Field({ nullable: true, defaultValue: false })
inclusive: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export class GetFlowIdsFromCategoryConditionsStrategyImpl
.queryBuilder()
.distinct('flow.id', 'flow.versionID')
.from('flow')
.where('flow.deletedAt', null)
.join('categoryRef', function () {
this.on('flow.id', '=', 'categoryRef.objectID').andOn(
'flow.versionID',
Expand All @@ -77,9 +78,10 @@ export class GetFlowIdsFromCategoryConditionsStrategyImpl

if (categoriesIds.length > 0) {
joinQuery = joinQuery.andWhere(function () {
this.where('categoryRef.categoryID', 'IN', categoriesIds)
.andWhere('categoryRef.objectType', 'flow')
.andWhere('flow.deletedAt', null);
this.where('categoryRef.categoryID', 'IN', categoriesIds).andWhere(
'categoryRef.objectType',
'flow'
);
});
}

Expand All @@ -89,9 +91,7 @@ export class GetFlowIdsFromCategoryConditionsStrategyImpl
'categoryRef.categoryID',
'IN',
categoriesIdsFromShortcutFilterIN
)
.andWhere('categoryRef.objectType', 'flow')
.andWhere('flow.deletedAt', null);
).andWhere('categoryRef.objectType', 'flow');
});
}

Expand All @@ -101,9 +101,7 @@ export class GetFlowIdsFromCategoryConditionsStrategyImpl
'categoryRef.categoryID',
'NOT IN',
categoriesIdsFromShortcutFilterNOTIN
)
.andWhere('categoryRef.objectType', 'flow')
.andWhere('flow.deletedAt', null);
).andWhere('categoryRef.objectType', 'flow');
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
buildSearchFlowsConditions,
defaultFlowOrderBy,
intersectUniqueFlowEntities,
mapFlowFiltersToFlowObjectFiltersGrouped,
mapFlowOrderBy,
mergeUniqueEntities,
prepareFlowConditions,
Expand Down Expand Up @@ -76,7 +77,7 @@ export class SearchFlowByFiltersStrategy implements FlowSearchStrategy {
const flowsToSort: UniqueFlowEntity[] =
await this.flowService.getFlowsAsUniqueFlowEntity({
databaseConnection,
conditions: flowFilters,
//conditions: flowFilters,
orderBy: orderByForFlow,
offset,
limit,
Expand Down Expand Up @@ -156,6 +157,12 @@ export class SearchFlowByFiltersStrategy implements FlowSearchStrategy {

const flowsFromObjectFilters: UniqueFlowEntity[] = [];
if (isFilterByFlowObjects) {
// Firts step is to map the filters to the FlowObjectFiltersGrouped
// To allow doing inclusive filtering between filters of the same type+direction
// But exclusive filtering between filters of different type+direction
const flowObjectFiltersGrouped =
mapFlowFiltersToFlowObjectFiltersGrouped(flowObjectFilters);

const { flows }: FlowIdSearchStrategyResponse =
await this.getFlowIdsFromObjectConditions.search({
databaseConnection,
Expand All @@ -174,6 +181,20 @@ export class SearchFlowByFiltersStrategy implements FlowSearchStrategy {
for (const flow of flows) {
flowsFromObjectFilters.push(flow);
}

// TODO: Here if 'shouldIncludeChildOfParkedParents' is true
// We need to obtain the flowIDs from the childs whose parent flows are parked
const childs =
await this.flowService.getParkedParentFlowsByFlowObjectFilter(
models,
databaseConnection,
flowObjectFiltersGrouped
);

// TODO: review if the deduplicate action does not affect the resultSet
for (const child of childs) {
flowsFromObjectFilters.push(child);
}
}

// Lastly, we need to check if we need to filter by flow
Expand Down
45 changes: 44 additions & 1 deletion src/domain-services/flows/strategy/impl/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@ import { type FlowId } from '@unocha/hpc-api-core/src/db/models/flow';
import { Cond, Op } from '@unocha/hpc-api-core/src/db/util/conditions';
import type Knex from 'knex';
import { type OrderBy } from '../../../../utils/database-types';
import { type FlowCategory, type SearchFlowsFilters } from '../../graphql/args';
import type {
FlowObjectDirection,
FlowObjectFilterGrouped,
FlowObjectType,
} from '../../../flow-object/model';
import type {
FlowCategory,
FlowObjectFilters,
SearchFlowsFilters,
} from '../../graphql/args';
import { type FlowStatusFilter } from '../../graphql/types';
import { type UniqueFlowEntity } from '../../model';

Expand Down Expand Up @@ -327,3 +336,37 @@ export function buildSearchFlowsConditions(
[Cond.OR]: whereClauses,
};
}

export function mapFlowFiltersToFlowObjectFiltersGrouped(
flowObjectFilters: FlowObjectFilters[]
): FlowObjectFilterGrouped {
const flowObjectFilterGrouped = new Map<
FlowObjectType,
Map<FlowObjectDirection, number[]>
>();

for (const flowObjectFilter of flowObjectFilters) {
const objectType = flowObjectFilter.objectType;
const flowDirection = flowObjectFilter.direction;
const objectId = flowObjectFilter.objectID;

// Get the map of flow object IDs for the given object type
// Or create a new map if it doesn't exist
const directionWithIDsMap =
flowObjectFilterGrouped.get(objectType) ??
new Map<FlowObjectDirection, number[]>();

// Get the list of flow object IDs for the given direction
// Or create a new list if it doesn't exist
const flowObjectIDs = directionWithIDsMap.get(flowDirection) ?? [];
flowObjectIDs.push(objectId);

// Update the map with the new list of flow object IDs for the given direction
directionWithIDsMap.set(flowDirection, flowObjectIDs);

// Update the map with the new map of direction+ids for the given object type
flowObjectFilterGrouped.set(objectType, directionWithIDsMap);
}

return flowObjectFilterGrouped;
}

0 comments on commit f1d5756

Please sign in to comment.