Skip to content

Commit

Permalink
Move 'pending' flow category as shortcut
Browse files Browse the repository at this point in the history
  • Loading branch information
manelcecs committed Dec 18, 2023
1 parent be18378 commit 0828cde
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 58 deletions.
55 changes: 43 additions & 12 deletions src/domain-services/flows/flow-search-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { ReportDetailService } from '../report-details/report-detail-service';
import { type UsageYear } from '../usage-years/grpahql/types';
import { UsageYearService } from '../usage-years/usage-year-service';
import {
type FlowCategoryFilters,
type FlowCategory,
type FlowObjectFilters,
type SearchFlowsArgs,
type SearchFlowsArgsNonPaginated,
Expand Down Expand Up @@ -62,8 +62,14 @@ export class FlowSearchService {
models: Database,
filters: SearchFlowsArgs
): Promise<FlowSearchResult> {
const { limit, nextPageCursor, prevPageCursor, sortField, sortOrder } =
filters;
const {
limit,
nextPageCursor,
prevPageCursor,
sortField,
sortOrder,
pending: isPendingFlows,
} = filters;

const orderBy: FlowOrderBy = this.buildOrderBy(sortField, sortOrder);

Expand All @@ -79,7 +85,8 @@ export class FlowSearchService {
const { strategy, conditions } = this.determineStrategy(
flowFilters,
flowObjectFilters,
flowCategoryFilters
flowCategoryFilters,
isPendingFlows
);

// Fetch one more item to check for hasNextPage
Expand All @@ -91,7 +98,8 @@ export class FlowSearchService {
models,
orderBy,
limitComputed,
cursorCondition
cursorCondition,
isPendingFlows
);

// Remove the extra item used to check hasNextPage
Expand Down Expand Up @@ -432,29 +440,39 @@ export class FlowSearchService {
determineStrategy(
flowFilters: SearchFlowsFilters,
flowObjectFilters: FlowObjectFilters[],
flowCategoryFilters: FlowCategoryFilters
flowCategoryFilters: FlowCategory[],
isFilterByPendingFlows: boolean
): { strategy: FlowSearchStrategy; conditions: any } {
const isFlowFilterDefined = flowFilters !== undefined;
const isFlowObjectFilterDefined = flowObjectFilters !== undefined;
const isFlowObjectFiltersNotEmpty =
isFlowObjectFilterDefined && flowObjectFilters.length !== 0;

const isFlowCategoryFilterDefined = flowCategoryFilters !== undefined;
const isFlowCategoryFilterNotEmpty =
isFlowCategoryFilterDefined && flowCategoryFilters.length !== 0;

const isFilterByPendingFlowsDefined = isFilterByPendingFlows !== undefined;
if (
(!isFlowFilterDefined &&
(!isFlowObjectFilterDefined || !isFlowObjectFiltersNotEmpty) &&
!isFlowCategoryFilterDefined) ||
!isFlowCategoryFilterNotEmpty &&
!isFilterByPendingFlowsDefined) ||
(isFlowFilterDefined &&
(!isFlowObjectFilterDefined || !isFlowObjectFiltersNotEmpty) &&
!isFlowCategoryFilterDefined)
!isFlowCategoryFilterNotEmpty &&
!isFilterByPendingFlowsDefined)
) {
const flowConditions = this.prepareFlowConditions(flowFilters);
return {
strategy: this.onlyFlowFiltersStrategy,
conditions: flowConditions,
};
} else if (isFlowObjectFiltersNotEmpty || isFlowCategoryFilterDefined) {
} else if (
isFlowObjectFiltersNotEmpty ||
isFlowCategoryFilterNotEmpty ||
isFilterByPendingFlowsDefined
) {
const flowConditions = this.prepareFlowConditions(flowFilters);
const flowObjectConditions =
this.prepareFlowObjectConditions(flowObjectFilters);
Expand Down Expand Up @@ -632,15 +650,28 @@ export class FlowSearchService {
models: Database,
args: SearchFlowsArgsNonPaginated
): Promise<FlowSearchTotalAmountResult> {
const { flowFilters, flowObjectFilters, flowCategoryFilters } = args;
const {
flowFilters,
flowObjectFilters,
flowCategoryFilters,
pending: isPendingFlows,
} = args;

const { strategy, conditions } = this.determineStrategy(
flowFilters,
flowObjectFilters,
flowCategoryFilters
flowCategoryFilters,
isPendingFlows
);

const { flows, count } = await strategy.search(conditions, models);
const { flows, count } = await strategy.search(
conditions,
models,
undefined,
undefined,
undefined,
isPendingFlows
);

const flowsAmountUSD: Array<string | number> = flows.map(
(flow) => flow.amountUSD
Expand Down
13 changes: 8 additions & 5 deletions src/domain-services/flows/graphql/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ export class SearchFlowsFilters {

@InputType()
export class FlowCategoryFilters {
@Field({ nullable: true })
pending: boolean;

@Field(() => [FlowCategory], { nullable: true })
categoryFilters: FlowCategory[];
}
Expand Down Expand Up @@ -81,8 +78,11 @@ export class SearchFlowsArgs extends PaginationArgs<FlowSortField> {
@Field({ nullable: true })
includeChildrenOfParkedFlows: boolean;

@Field(() => [FlowCategory], { nullable: true })
flowCategoryFilters: FlowCategory[];

@Field({ nullable: true })
flowCategoryFilters: FlowCategoryFilters;
pending: boolean;
}

@ArgsType()
Expand All @@ -96,6 +96,9 @@ export class SearchFlowsArgsNonPaginated {
@Field({ nullable: true })
includeChildrenOfParkedFlows: boolean;

@Field(() => [FlowCategory], { nullable: true })
flowCategoryFilters: FlowCategory[];

@Field({ nullable: true })
flowCategoryFilters: FlowCategoryFilters;
pending: boolean;
}
3 changes: 2 additions & 1 deletion src/domain-services/flows/strategy/flow-search-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface FlowSearchStrategy {
models: Database,
orderBy?: any,
limit?: number,
cursorCondition?: any
cursorCondition?: any,
filterByPendingFlows?: boolean
): Promise<FlowSearchStrategyResponse>;
}
11 changes: 8 additions & 3 deletions src/domain-services/flows/strategy/flowID-search-strategy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type Database } from '@unocha/hpc-api-core/src/db';
import { type FlowId } from '@unocha/hpc-api-core/src/db/models/flow';
import { type FlowCategoryFilters } from '../graphql/args';
import { type FlowCategory } from '../graphql/args';

export interface FlowIdSearchStrategyResponse {
flowIDs: FlowId[];
Expand All @@ -10,8 +10,13 @@ export interface FlowIDSearchStrategy {
search(
models: Database,
flowObjectsConditions: Map<string, Map<string, number[]>>,
flowCategoryConditions: FlowCategoryFilters
flowCategoryConditions: FlowCategory[],
filterByPendingFlows?: boolean
): Promise<FlowIdSearchStrategyResponse>;

generateWhereClause(flowIds: FlowId[], conditions: any): any;
generateWhereClause(
flowIds: FlowId[],
conditions: any,
filterByPendingFlows?: boolean
): any;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { type Database } from '@unocha/hpc-api-core/src/db';
import { Cond } from '@unocha/hpc-api-core/src/db/util/conditions';
import { Service } from 'typedi';
import { FlowService } from '../../flow-service';
import { type FlowCategoryFilters } from '../../graphql/args';
import { type FlowCategory } from '../../graphql/args';
import {
type FlowSearchStrategy,
type FlowSearchStrategyResponse,
Expand All @@ -27,12 +27,13 @@ export class FlowObjectFiltersStrategy implements FlowSearchStrategy {
async search(
flowConditions: {
conditionsMap: Map<string, any>;
flowCategoryFilters: FlowCategoryFilters;
flowCategoryFilters: FlowCategory[];
},
models: Database,
orderBy?: any,
limit?: number,
cursorCondition?: any
cursorCondition?: any,
filterByPendingFlows?: boolean
): Promise<FlowSearchStrategyResponse> {
const flowConditionsMap = flowConditions.conditionsMap;
// Obtain flowObjects conditions
Expand All @@ -45,41 +46,40 @@ export class FlowObjectFiltersStrategy implements FlowSearchStrategy {
const flowEntityConditions = flowConditionsMap.get('flow') ?? new Map();

// Obtain flowCategory conditions
const flowCategoryConditions = flowConditions.flowCategoryFilters ?? {};
const flowCategoryConditions = flowConditions.flowCategoryFilters ?? [];

console.log('filterByPendingFlows', filterByPendingFlows);
const searchFlowIdsStrategy: FlowIDSearchStrategy = this.determineStrategy(
flowObjectsConditions,
flowCategoryConditions
flowCategoryConditions,
filterByPendingFlows
);

const { flowIDs: flowIdsToFilter }: FlowIdSearchStrategyResponse =
await searchFlowIdsStrategy.search(
models,
flowObjectsConditions,
flowCategoryConditions
flowCategoryConditions,
filterByPendingFlows
);

const whereClauseFromStrategy = searchFlowIdsStrategy.generateWhereClause(
flowIdsToFilter,
flowCategoryConditions,
filterByPendingFlows
);

// Combine conditions from flowObjects FlowIDs and flow conditions
const countConditions = {
[Cond.AND]: [
flowEntityConditions ?? {},

searchFlowIdsStrategy.generateWhereClause(
flowIdsToFilter,
flowCategoryConditions
),
],
[Cond.AND]: [flowEntityConditions ?? {}, whereClauseFromStrategy ?? {}],
};

// Combine cursor condition with flow conditions
const searchConditions = {
[Cond.AND]: [
flowEntityConditions ?? {},
cursorCondition ?? {},
searchFlowIdsStrategy.generateWhereClause(
flowIdsToFilter,
flowCategoryConditions
),
whereClauseFromStrategy ?? {},
],
};

Expand Down Expand Up @@ -111,24 +111,33 @@ export class FlowObjectFiltersStrategy implements FlowSearchStrategy {
// otherwise keep all flowIDs from the one that is not empty
determineStrategy(
flowObjectsConditions: Map<string, Map<string, number[]>>,
flowCategoryConditions: any
flowCategoryConditions: any,
filterByPendingFlows?: boolean
): any {
const isFlowObjectsConditionsIsDefined =
flowObjectsConditions !== undefined;
const isFlowCategoryConditionsIsDefined =
flowCategoryConditions !== undefined;
const isFilterByPendingFlowsIsDefined = filterByPendingFlows !== undefined;

console.log('filterByPendingFlows', filterByPendingFlows);
const flowObjectsConditionsIsNotEmpty =
isFlowObjectsConditionsIsDefined && flowObjectsConditions.size;
const flowCategoryConditionsIsNotEmpty =
isFlowCategoryConditionsIsDefined &&
Object.keys(flowCategoryConditions).length;

if (flowObjectsConditionsIsNotEmpty && flowCategoryConditionsIsNotEmpty) {
const isFlowCategoryConditionsIsNotEmpty =
isFlowCategoryConditionsIsDefined && flowCategoryConditions.length !== 0;

console.log('pending flows', isFilterByPendingFlowsIsDefined);
if (
flowObjectsConditionsIsNotEmpty &&
(isFlowCategoryConditionsIsNotEmpty || isFilterByPendingFlowsIsDefined)
) {
return this.getFlowIdsFromMixedConditions;
} else if (flowObjectsConditionsIsNotEmpty) {
return this.getFlowIdsFromObjectConditions;
} else if (flowCategoryConditionsIsNotEmpty) {
} else if (
isFlowCategoryConditionsIsNotEmpty ||
isFilterByPendingFlowsIsDefined
) {
return this.getFlowIdsFromCategoryConditions;
}
throw new Error(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Op } from '@unocha/hpc-api-core/src/db/util/conditions';
import { createBrandedValue } from '@unocha/hpc-api-core/src/util/types';
import { Service } from 'typedi';
import { CategoryService } from '../../../categories/category-service';
import { type FlowCategoryFilters } from '../../graphql/args';
import { type FlowCategory } from '../../graphql/args';
import {
type FlowIDSearchStrategy,
type FlowIdSearchStrategyResponse,
Expand All @@ -21,9 +21,11 @@ export class GetFlowIdsFromCategoryConditionsStrategyImpl
async search(
models: Database,
_flowObjectsConditions: Map<string, Map<string, number[]>>,
flowCategoryConditions: FlowCategoryFilters
flowCategoryConditions: FlowCategory[],
filterByPendingFlows: boolean
): Promise<FlowIdSearchStrategyResponse> {
const whereClause = mapFlowCategoryConditionsToWhereClause(
filterByPendingFlows,
flowCategoryConditions
);

Expand Down Expand Up @@ -55,9 +57,10 @@ export class GetFlowIdsFromCategoryConditionsStrategyImpl

generateWhereClause(
flowIds: FlowId[],
flowCategoryConditions: FlowCategoryFilters
_conditions: any,
filterByPendingFlows: boolean
) {
const operation = flowCategoryConditions.pending ? Op.IN : Op.NOT_IN;
const operation = filterByPendingFlows === true ? Op.IN : Op.NOT_IN;
return {
id: {
[operation]: flowIds,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { type Database } from '@unocha/hpc-api-core/src/db';
import { type FlowId } from '@unocha/hpc-api-core/src/db/models/flow';
import { Op } from '@unocha/hpc-api-core/src/db/util/conditions';
import { Service } from 'typedi';
import { type FlowCategoryFilters } from '../../graphql/args';
import { type FlowCategory } from '../../graphql/args';
import {
type FlowIDSearchStrategy,
type FlowIdSearchStrategyResponse,
Expand All @@ -23,7 +23,8 @@ export class GetFlowIdsFromMixedConditionsStrategyImpl
async search(
models: Database,
flowObjectsConditions: Map<string, Map<string, number[]>>,
flowCategoryConditions: FlowCategoryFilters
flowCategoryConditions: FlowCategory[],
filterByPendingFlows: boolean
): Promise<FlowIdSearchStrategyResponse> {
const { flowIDs: flowIdsFromFlowObjects }: FlowIdSearchStrategyResponse =
await this.getFlowIdsFromObjectConditionsStrategy.search(
Expand All @@ -35,7 +36,8 @@ export class GetFlowIdsFromMixedConditionsStrategyImpl
await this.getFlowIdsFromCategoryConditionsStrategy.search(
models,
flowObjectsConditions,
flowCategoryConditions
flowCategoryConditions,
filterByPendingFlows
);

const mergeFlowIDs: FlowId[] =
Expand Down
Loading

0 comments on commit 0828cde

Please sign in to comment.