Skip to content

Commit

Permalink
Add a created after filter to the rule listing endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
MytsV authored and maany committed Sep 29, 2024
1 parent b0c7a7f commit 7eb485c
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/lib/core/use-case/list-rules-usecase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ export default class ListRulesUseCase
}

async makeGatewayRequest(requestModel: AuthenticatedRequestModel<ListRulesRequest>): Promise<ListRulesDTO> {
const { rucioAuthToken, scope, account } = requestModel;
const dto: ListRulesDTO = await this.gateway.listRules(rucioAuthToken, { scope, account });
const { rucioAuthToken, scope, account, created_after } = requestModel;
const dto: ListRulesDTO = await this.gateway.listRules(rucioAuthToken, { scope, account, created_after });
return dto;
}

Expand Down
1 change: 1 addition & 0 deletions src/lib/core/usecase-models/list-rules-usecase-models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Rule } from '@/lib/core/entity/rucio';
export interface ListRulesRequest {
account?: string;
scope?: string;
created_after?: Date;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/lib/infrastructure/controller/list-rules-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import USECASE_FACTORY from '@/lib/infrastructure/ioc/ioc-symbols-usecase-factor
export type ListRulesControllerParameters = TAuthenticatedControllerParameters & {
scope?: string;
account?: string;
created_after?: string;
};

@injectable()
Expand All @@ -22,6 +23,7 @@ class ListRulesController extends BaseController<ListRulesControllerParameters,
rucioAuthToken: parameters.rucioAuthToken,
account: parameters.account,
scope: parameters.scope,
created_after: parameters.created_after ? new Date(parameters.created_after) : undefined,
};
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {RuleDTO} from '@/lib/core/dto/rule-dto';
import {BaseStreamableDTO} from '@/lib/sdk/dto';
import {BaseStreamableEndpoint} from '@/lib/sdk/gateway-endpoints';
import {HTTPRequest} from '@/lib/sdk/http';
import {Response} from 'node-fetch';
import {convertToRuleDTO, ListRulesFilter, TRucioRule} from '../rule-gateway-utils';
import { RuleDTO } from '@/lib/core/dto/rule-dto';
import { BaseStreamableDTO } from '@/lib/sdk/dto';
import { BaseStreamableEndpoint } from '@/lib/sdk/gateway-endpoints';
import { HTTPRequest } from '@/lib/sdk/http';
import { Response } from 'node-fetch';
import { convertToRuleDTO, formatFilterDate, ListRulesFilter, TRucioRule } from '../rule-gateway-utils';

const DEFAULT_PARAMETER = '*';

Expand All @@ -19,17 +19,23 @@ export default class ListRulesEndpoint extends BaseStreamableEndpoint<BaseStream
await super.initialize();
const rucioHost = await this.envConfigGateway.rucioHost();
const endpoint = `${rucioHost}/rules/`;

const params: any = {
account: this.filter?.account ?? DEFAULT_PARAMETER,
scope: this.filter?.scope ?? DEFAULT_PARAMETER,
};
if (this.filter?.created_after) {
params.created_after = formatFilterDate(this.filter.created_after);
}

const request: HTTPRequest = {
method: 'GET',
url: endpoint,
headers: {
'X-Rucio-Auth-Token': this.rucioAuthToken,
'Content-Type': 'application/x-json-stream',
},
params: {
account: this.filter?.account ?? DEFAULT_PARAMETER,
scope: this.filter?.scope ?? DEFAULT_PARAMETER,
}
params: params,
};
this.request = request;
this.initialized = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RuleDTO, RuleReplicaLockStateDTO } from '@/lib/core/dto/rule-dto';
import { LockState, RuleState } from '@/lib/core/entity/rucio';
import { DateISO, LockState, RuleState } from '@/lib/core/entity/rucio';

export type TRucioRule = {
error: null | string;
Expand Down Expand Up @@ -139,4 +139,21 @@ export function getEmptyRuleReplicaLockDTO(): RuleReplicaLockStateDTO {
export type ListRulesFilter = {
account?: string;
scope?: string;
created_after?: Date;
};

export const formatFilterDate = (date: Date) => {
const daysOfWeek = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];

const dayOfWeek = daysOfWeek[date.getUTCDay()];
const day = String(date.getUTCDate()).padStart(2, '0');
const month = months[date.getUTCMonth()];
const year = date.getUTCFullYear();

const hours = String(date.getUTCHours()).padStart(2, '0');
const minutes = String(date.getUTCMinutes()).padStart(2, '0');
const seconds = String(date.getUTCSeconds()).padStart(2, '0');

return `${dayOfWeek}, ${day} ${month} ${year} ${hours}:${minutes}:${seconds} UTC`;
};
4 changes: 3 additions & 1 deletion src/pages/api/feature/list-rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ async function listRules(req: NextApiRequest, res: NextApiResponse, rucioAuthTok

const account = sessionUser.rucioAccount;

const { scope } = req.query as { scope?: string };
// TODO: check if created_after is an actual date
const { scope, created_after } = req.query as { scope?: string; created_after?: string };

const controllerParameters: ListRulesControllerParameters = {
response: res,
rucioAuthToken: rucioAuthToken,
account: account,
scope: scope,
created_after: created_after,
};

const controller = appContainer.get<BaseController<ListRulesControllerParameters, ListRulesRequest>>(CONTROLLERS.LIST_RULES);
Expand Down

0 comments on commit 7eb485c

Please sign in to comment.