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 all commits
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"@elastic/elasticsearch": "^8.15.0",
"@nestjs-modules/mailer": "^2.0.2",
"@nestjs/axios": "^3.0.0",
"@nestjs/cache-manager": "^2.3.0",
"@nestjs/common": "^10.3.8",
"@nestjs/config": "^3.0.0",
"@nestjs/core": "^10.3.8",
Expand Down
9 changes: 9 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { EventEmitterModule } from "@nestjs/event-emitter";
import { AdminModule } from "./admin/admin.module";
import { HealthModule } from "./health/health.module";
import { LoggerModule } from "./loggers/logger.module";
import { CacheModule } from "@nestjs/cache-manager";

@Module({
imports: [
Expand Down Expand Up @@ -94,6 +95,14 @@ import { LoggerModule } from "./loggers/logger.module";
UsersModule,
AdminModule,
HealthModule,
CacheModule.register({
ttl: 3600, // cache duration in seconds (1 hour)
max: 100, // maximum number of items in cache
// Uncomment and configure Redis as needed
// store: redisStore,
// host: 'localhost',
// port: 6379,
}),
],
controllers: [],
providers: [
Expand Down
28 changes: 0 additions & 28 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { flattenObject, parseBoolean } from "src/common/utils";
import { Issuer } from "openid-client";
import { ReturnedAuthLoginDto } from "./dto/returnedLogin.dto";
import { ReturnedUserDto } from "src/users/dto/returned-user.dto";
import { CreateUserSettingsDto } from "src/users/dto/create-user-settings.dto";

@Injectable()
export class AuthService {
Expand Down Expand Up @@ -44,7 +43,6 @@ export class AuthService {
async login(user: Omit<User, "password">): Promise<ReturnedAuthLoginDto> {
const expiresIn = this.configService.get<number>("jwt.expiresIn");
const accessToken = this.jwtService.sign(user, { expiresIn });
await this.postLoginTasks(user);
return {
access_token: accessToken,
id: accessToken,
Expand Down Expand Up @@ -124,30 +122,4 @@ export class AuthService {

return { logout: "successful" };
}
/**
* postLoginTasks: Executes additional tasks after user login.
*
* - Checks if the user has userSettings record.
* - If user has no userSetting, it creates default userSetting for the user.
* @param user - The logged-in user (without password).
*/
async postLoginTasks(user: Omit<User, "password">) {
if (!user) return;

const userId = user._id;

const userSettings = await this.usersService.findByIdUserSettings(userId);

if (!userSettings) {
Logger.log(
`Adding default settings to user ${user.username} with userId: ${user._id}`,
"postLoginTasks",
);
const createUserSettingsDto: CreateUserSettingsDto = {
userId,
externalSettings: {},
};
await this.usersService.createUserSettings(userId, createUserSettingsDto);
}
}
}
11 changes: 11 additions & 0 deletions src/config/default-filters.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[
{ "type": "LocationFilterComponent", "visible": true },
{ "type": "PidFilterComponent", "visible": true },
{ "type": "PidFilterContainsComponent", "visible": false },
{ "type": "PidFilterStartsWithComponent", "visible": false },
{ "type": "GroupFilterComponent", "visible": true },
{ "type": "TypeFilterComponent", "visible": true },
{ "type": "KeywordFilterComponent", "visible": true },
{ "type": "DateRangeFilterComponent", "visible": true },
{ "type": "TextFilterComponent", "visible": true }
]
187 changes: 82 additions & 105 deletions src/config/frontend.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,87 @@
"jsonMetadataEnabled": true,
"jupyterHubUrl": "",
"landingPage": "doi.ess.eu/detail/",
"lbBaseURL": "http://localhost:3000",
"lbBaseURL": "http://127.0.0.1:3000",
"localColumns": [
{
"name": "select",
"order": 0,
"type": "standard",
"enabled": true
},
{
"name": "pid",
"order": 1,
"type": "standard",
"enabled": true
},
{
"name": "datasetName",
"order": 2,
"type": "standard",
"enabled": true
},
{
"name": "runNumber",
"order": 3,
"type": "standard",
"enabled": true
},
{
"name": "sourceFolder",
"order": 4,
"type": "standard",
"enabled": true
},
{
"name": "size",
"order": 5,
"type": "standard",
"enabled": true
},
{
"name": "creationTime",
"order": 6,
"type": "standard",
"enabled": true
},
{
"name": "type",
"order": 7,
"type": "standard",
"enabled": true
},
{
"name": "image",
"order": 8,
"type": "standard",
"enabled": true
},
{
"name": "metadata",
"order": 9,
"type": "standard",
"enabled": false
},
{
"name": "proposalId",
"order": 10,
"type": "standard",
"enabled": true
},
{
"name": "ownerGroup",
"order": 11,
"type": "standard",
"enabled": false
},
{
"name": "dataStatus",
"order": 12,
"type": "standard",
"enabled": false
}
],
"logbookEnabled": true,
"loginFormEnabled": true,
"maxDirectDownloadSize": 5000000000,
Expand Down Expand Up @@ -100,108 +180,5 @@
"enabled": "#Selected",
"authorization": ["#datasetAccess", "#datasetPublic"]
}
],
"labelMaps": {
"filters": {
"LocationFilter": "Location",
"PidFilter": "Pid",
"GroupFilter": "Group",
"TypeFilter": "Type",
"KeywordFilter": "Keyword",
"DateRangeFilter": "Start Date - End Date",
"TextFilter": "Text"
}
},
"defaultDatasetsListSettings": {
"columns": [
{
"name": "select",
"order": 0,
"type": "standard",
"enabled": true
},
{
"name": "pid",
"order": 1,
"type": "standard",
"enabled": true
},
{
"name": "datasetName",
"order": 2,
"type": "standard",
"enabled": true
},
{
"name": "runNumber",
"order": 3,
"type": "standard",
"enabled": true
},
{
"name": "sourceFolder",
"order": 4,
"type": "standard",
"enabled": true
},
{
"name": "size",
"order": 5,
"type": "standard",
"enabled": true
},
{
"name": "creationTime",
"order": 6,
"type": "standard",
"enabled": true
},
{
"name": "type",
"order": 7,
"type": "standard",
"enabled": true
},
{
"name": "image",
"order": 8,
"type": "standard",
"enabled": true
},
{
"name": "metadata",
"order": 9,
"type": "standard",
"enabled": false
},
{
"name": "proposalId",
"order": 10,
"type": "standard",
"enabled": true
},
{
"name": "ownerGroup",
"order": 11,
"type": "standard",
"enabled": false
},
{
"name": "dataStatus",
"order": 12,
"type": "standard",
"enabled": false
}
],
"filters": [
{ "LocationFilter": true },
{ "PidFilter": true },
{ "GroupFilter": true },
{ "TypeFilter": true },
{ "KeywordFilter": true },
{ "DateRangeFilter": true },
{ "TextFilter": true }
],
"conditions": []
}
]
}
25 changes: 25 additions & 0 deletions src/datasets/datasets.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ import { JWTUser } from "src/auth/interfaces/jwt-user.interface";
import { LogbooksService } from "src/logbooks/logbooks.service";
import configuration from "src/config/configuration";
import { DatasetType } from "./dataset-type.enum";
import { CACHE_MANAGER, CacheInterceptor } from "@nestjs/cache-manager";

@ApiBearerAuth()
@ApiExtraModels(
Expand Down Expand Up @@ -888,6 +889,30 @@ export class DatasetsController {
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.",
})
@UseInterceptors(CacheInterceptor)
async metadataTypes(): Promise<any[]> {
const result = this.datasetsService.getScientificMetadataTypes();

return result;
}

// GET /datasets/findOne
@UseGuards(PoliciesGuard)
@CheckPolicies("datasets", (ability: AppAbility) =>
Expand Down
9 changes: 9 additions & 0 deletions src/datasets/datasets.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { LogbooksModule } from "src/logbooks/logbooks.module";
import { PoliciesService } from "src/policies/policies.service";
import { PoliciesModule } from "src/policies/policies.module";
import { ElasticSearchModule } from "src/elastic-search/elastic-search.module";
import { CacheModule } from "@nestjs/cache-manager";

@Module({
imports: [
Expand Down Expand Up @@ -62,6 +63,14 @@ import { ElasticSearchModule } from "src/elastic-search/elastic-search.module";
inject: [PoliciesService],
},
]),
CacheModule.register({
ttl: 3600000, // cache duration in seconds (1 hour)
max: 100, // maximum number of items in cache
// Uncomment and configure Redis as needed
// store: redisStore,
// host: 'localhost',
// port: 6379,
}),
],
exports: [DatasetsService],
controllers: [DatasetsController],
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