Skip to content
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

feature: Add metadataTypes endpoint - readonly; available for everyone #1428

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/datasets/datasets.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,29 @@
return this.datasetsService.metadataKeys(parsedFilters);
}

// GET /datasets/metadataTypes
// @UseGuards(PoliciesGuard)
// @CheckPolicies((ability: AppAbility) =>
// ability.can(Action.DatasetRead, DatasetClass),
// )
@Get("/metadataTypes")
@ApiOperation({
summary: "Retrieve all available scientific metadata field types.",
description:
"This endpoint returns the field names and their corresponding data types from the `scientificMetadata` fields of all datasets in the system. It aggregates all the unique field names and their associated types from the dataset collection.",
})
@ApiResponse({
status: 200,
type: Array,
description:
"Returns an array of objects where each object contains a metadata field name and its associated type.",
})
async metadataTypes(): Promise<any[]> {

Check failure on line 908 in src/datasets/datasets.controller.ts

View workflow job for this annotation

GitHub Actions / eslint

Unexpected any. Specify a different type
const result = this.datasetsService.getScientificMetadataTypes();

return result;
}

// GET /datasets/findOne
@UseGuards(PoliciesGuard)
@CheckPolicies("datasets", (ability: AppAbility) =>
Expand Down Expand Up @@ -1021,7 +1044,7 @@
description: "Return dataset with pid specified",
})
async findById(
@Req() request: Request,

Check failure on line 1047 in src/datasets/datasets.controller.ts

View workflow job for this annotation

GitHub Actions / eslint

Unexpected any. Specify a different type
@Param("pid") id: string,
): Promise<DatasetClass | null> {
const dataset = await this.checkPermissionsForDataset(request, id);
Expand Down
37 changes: 37 additions & 0 deletions src/datasets/datasets.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,43 @@ export class DatasetsService {
}
}

async getScientificMetadataTypes() {
// TODO performance research required, say 1K records, 100K and 1M
return this.datasetModel.aggregate([
{
// Step 1: Convert the scientificMetadata object into an array of key-value pairs
$project: {
scientificMetadataArray: { $objectToArray: "$scientificMetadata" },
},
},
{
// Step 2: Unwind the array to treat each field individually
$unwind: "$scientificMetadataArray",
},
{
// Step 3: Group by the field name and accumulate types
$group: {
_id: "$scientificMetadataArray.k", // Group by field name (key)
types: { $addToSet: { $type: "$scientificMetadataArray.v.value" } }, // Add the data type of the value
},
},
{
// Step 4: Convert the array of types into a string for each field name
$project: {
_id: 0, // Don't include the default _id
metadataKey: "$_id",
metadataType: {
$cond: {
if: { $eq: [{ $size: "$types" }, 1] }, // If only one type is present
then: { $arrayElemAt: ["$types", 0] }, // Use the type
else: "mixed", // If there are multiple types, mark it as 'mixed'
},
},
},
},
]);
}

async isElasticSearchDBEmpty() {
if (!this.ESClient) return;
const count = await this.ESClient.getCount();
Expand Down
Loading