-
Notifications
You must be signed in to change notification settings - Fork 36
feat(OrigDatablock): add file count endpoints #2795
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -383,6 +383,48 @@ export class OrigDatablocksService { | |
| return { count }; | ||
| } | ||
|
|
||
| async countFiles( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (complexity): Consider simplifying the new countFiles aggregation pipeline by counting list sizes directly and cleaning up the fields projection handling. You can simplify 1. Remove You don’t need to explode async countFiles(
filter: FilterQuery<OrigDatablockDocument>,
): Promise<CountApiResponse> {
const whereFilter: FilterQuery<OrigDatablockDocument> = filter.where ?? {};
const fieldsProjection = filter.fields ?? [];
const pipeline: PipelineStage[] = [{ $match: whereFilter }];
this.addLookupFields(pipeline, filter.include);
if (!isEmpty(fieldsProjection)) {
const projection = parsePipelineProjection(fieldsProjection);
pipeline.push({ $project: projection });
}
// keep dataset constraint but simplify the stages
pipeline.push({
$lookup: {
from: "Dataset",
as: "dataset_temp",
let: { datasetId: "$datasetId" },
pipeline: [{ $match: { $expr: { $eq: ["$pid", "$$datasetId"] } } }],
},
});
// only keep origdatablocks for which a dataset exists
pipeline.push({
$match: {
$expr: { $gt: [{ $size: "$dataset_temp" }, 0] },
},
});
pipeline.push({ $unset: "dataset_temp" });
// count files by summing sizes of dataFileList
pipeline.push(
{
$project: {
fileCount: { $size: "$dataFileList" },
},
},
{
$group: {
_id: null,
count: { $sum: "$fileCount" },
},
},
);
const [result] = await this.origDatablockModel
.aggregate<{ count: number }>(pipeline)
.exec();
return { count: result?.count ?? 0 };
}This keeps the dataset-existence constraint but removes 2. Fix Right now type FieldsInput = string[] | undefined; // adjust to whatever your filter type is
const fieldsProjection: string[] =
Array.isArray(filter.fields) ? filter.fields : [];Or, if const fieldsProjection: string[] = filter.fields ?? [];This removes the implicit type mismatch and makes the projection logic easier to follow. |
||
| filter: FilterQuery<OrigDatablockDocument>, | ||
| ): Promise<CountApiResponse> { | ||
| const whereFilter: FilterQuery<OrigDatablockDocument> = filter.where ?? {}; | ||
| const fieldsProjection: string[] = filter.fields ?? []; | ||
|
|
||
| const pipeline: PipelineStage[] = [{ $match: whereFilter }]; | ||
| this.addLookupFields(pipeline, filter.include); | ||
|
|
||
| if (!isEmpty(fieldsProjection)) { | ||
| const projection = parsePipelineProjection(fieldsProjection); | ||
| pipeline.push({ $project: projection }); | ||
| } | ||
|
|
||
| pipeline.push({ | ||
| $lookup: { | ||
| from: "Dataset", | ||
| as: "dataset_temp", | ||
| let: { datasetId: "$datasetId" }, | ||
| pipeline: [{ $match: { $expr: { $eq: ["$pid", "$$datasetId"] } } }], | ||
| }, | ||
| }); | ||
|
|
||
| pipeline.push({ | ||
| $addFields: { | ||
| datasetExist: { $gt: [{ $size: "$dataset_temp" }, 0] }, | ||
| }, | ||
| }); | ||
|
|
||
| pipeline.push({ $unset: "dataset_temp" }); | ||
|
|
||
| pipeline.push({ $unwind: "$dataFileList" }); | ||
|
|
||
| pipeline.push({ $count: "count" }); | ||
|
|
||
| const [result] = await this.origDatablockModel | ||
| .aggregate<{ count: number }>(pipeline) | ||
| .exec(); | ||
|
|
||
| return { count: result?.count ?? 0 }; | ||
| } | ||
|
|
||
| async aggregateSizeAndFileCount( | ||
| datasetId: string, | ||
| ): Promise<{ size: number; numberOfFiles: number }> { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.