From c177c6c121df308ccac322827324a438113b5925 Mon Sep 17 00:00:00 2001 From: hexastack Date: Tue, 26 Nov 2024 17:48:02 +0100 Subject: [PATCH] feat: add services and refactor repositories --- .../repositories/nlp-experiment.repository.ts | 2 +- ...y.ts => nlp-parameter-value.repository.ts} | 0 .../repositories/nlp-parameter.repository.ts | 2 +- api/src/nlp/schemas/nlp-metric.schema.ts | 2 +- .../nlp/services/nlp-experiment.service.ts | 31 +++++--- api/src/nlp/services/nlp-metric-service.ts | 29 +++++++ .../nlp/services/nlp-metric-value.service.ts | 74 ++++++++++++++++++ .../services/nlp-parameter-value.service.ts | 76 +++++++++++++++++++ api/src/nlp/services/nlp-parameter.service.ts | 29 +++++++ api/src/utils/generics/base-repository.ts | 4 + 10 files changed, 235 insertions(+), 14 deletions(-) rename api/src/nlp/repositories/{nlp-parameter.value.repository.ts => nlp-parameter-value.repository.ts} (100%) create mode 100644 api/src/nlp/services/nlp-metric-service.ts create mode 100644 api/src/nlp/services/nlp-metric-value.service.ts create mode 100644 api/src/nlp/services/nlp-parameter-value.service.ts create mode 100644 api/src/nlp/services/nlp-parameter.service.ts diff --git a/api/src/nlp/repositories/nlp-experiment.repository.ts b/api/src/nlp/repositories/nlp-experiment.repository.ts index 6aa47541..2b7d96e0 100644 --- a/api/src/nlp/repositories/nlp-experiment.repository.ts +++ b/api/src/nlp/repositories/nlp-experiment.repository.ts @@ -25,8 +25,8 @@ import { NlpModelService } from '../services/nlp-model.service'; import { NlpDatasetRepository } from './nlp-dataset.repository'; import { NlpMetricValueRepository } from './nlp-metric-value.repository'; import { NlpMetricRepository } from './nlp-metric.repository'; +import { NlpParameterValueRepository } from './nlp-parameter-value.repository'; import { NlpParameterRepository } from './nlp-parameter.repository'; -import { NlpParameterValueRepository } from './nlp-parameter.value.repository'; @Injectable() export class NlpExperimentRepository extends BaseRepository< diff --git a/api/src/nlp/repositories/nlp-parameter.value.repository.ts b/api/src/nlp/repositories/nlp-parameter-value.repository.ts similarity index 100% rename from api/src/nlp/repositories/nlp-parameter.value.repository.ts rename to api/src/nlp/repositories/nlp-parameter-value.repository.ts diff --git a/api/src/nlp/repositories/nlp-parameter.repository.ts b/api/src/nlp/repositories/nlp-parameter.repository.ts index e4a07b91..7c83938c 100644 --- a/api/src/nlp/repositories/nlp-parameter.repository.ts +++ b/api/src/nlp/repositories/nlp-parameter.repository.ts @@ -23,7 +23,7 @@ import { import { NlpDatasetRepository } from './nlp-dataset.repository'; import { NlpExperimentRepository } from './nlp-experiment.repository'; -import { NlpParameterValueRepository } from './nlp-parameter.value.repository'; +import { NlpParameterValueRepository } from './nlp-parameter-value.repository'; @Injectable() export class NlpParameterRepository extends BaseRepository< diff --git a/api/src/nlp/schemas/nlp-metric.schema.ts b/api/src/nlp/schemas/nlp-metric.schema.ts index 87d7576d..985002f2 100644 --- a/api/src/nlp/schemas/nlp-metric.schema.ts +++ b/api/src/nlp/schemas/nlp-metric.schema.ts @@ -27,7 +27,7 @@ export class NlpMetricStub extends BaseSchema { name: string; /** - * The associated Experiment + * The associated Experiments */ @Prop([ { diff --git a/api/src/nlp/services/nlp-experiment.service.ts b/api/src/nlp/services/nlp-experiment.service.ts index 32180cfd..feeb2522 100644 --- a/api/src/nlp/services/nlp-experiment.service.ts +++ b/api/src/nlp/services/nlp-experiment.service.ts @@ -9,48 +9,57 @@ import { Injectable } from '@nestjs/common'; import { EventEmitter2 } from '@nestjs/event-emitter'; +import { BaseService } from '@/utils/generics/base-service'; + import { NlpExperimentDto } from '../dto/nlp-experiment.dto'; import { NlpExperimentRepository } from '../repositories/nlp-experiment.repository'; import { NlpExperiment, NlpExperimentFull, + NlpExperimentPopulate, } from '../schemas/nlp-experiment.schema'; import { NlpModelService } from './nlp-model.service'; @Injectable() -export class NlpExperimentService { +export class NlpExperimentService extends BaseService< + NlpExperiment, + NlpExperimentPopulate, + NlpExperimentFull +> { constructor( - private readonly nlpExperimentRepository: NlpExperimentRepository, + readonly repository: NlpExperimentRepository, private readonly eventEmitter: EventEmitter2, private readonly nlpModelService: NlpModelService, - ) {} + ) { + super(repository); + } /** * Create a new NLP experiment. * @param nlpExperimentDto - Data transfer object for creating an experiment - * @returns {Promise} - Created experiment details + * @returns {Promise} - Created experiment details */ async createExperiment(nlpExperimentDto: NlpExperimentDto): Promise { const experiment = new NlpExperiment(); Object.assign(experiment, nlpExperimentDto); // Create the experiment in the repository - await this.nlpExperimentRepository.create(experiment); + await this.repository.create(experiment); } /** * Update an existing NLP experiment. * @param id - The ID of the experiment to update * @param nlpExperimentDto - The data to update the experiment with - * @returns {Promise} - Updated experiment details + * @returns {Promise} - Updated experiment details */ async updateExperiment( id: string, nlpExperimentDto: NlpExperimentDto, ): Promise { // Perform the update in the repository - await this.nlpExperimentRepository.updateOne(id, nlpExperimentDto); + await this.repository.updateOne(id, nlpExperimentDto); } /** @@ -60,15 +69,15 @@ export class NlpExperimentService { */ async deleteCascadeExperiment(id: string): Promise { // Perform the delete operation in the repository - await this.nlpExperimentRepository.deleteOne(id); + await this.repository.deleteOne(id); } /** * Get all completed NLP experiments. - * @returns {Promise} - List of completed experiments + * @returns {Promise} - List of completed experiments */ async getCompletedExperiments(): Promise { - const experiments = await this.nlpExperimentRepository.find({ + const experiments = await this.repository.find({ isCompleted: true, }); @@ -81,7 +90,7 @@ export class NlpExperimentService { * @returns {Promise} - The experiment, or null if not found */ async getExperimentById(id: string): Promise { - const experiment = await this.nlpExperimentRepository.findOne(id); + const experiment = await this.repository.findOne(id); return experiment; } diff --git a/api/src/nlp/services/nlp-metric-service.ts b/api/src/nlp/services/nlp-metric-service.ts new file mode 100644 index 00000000..807a7e98 --- /dev/null +++ b/api/src/nlp/services/nlp-metric-service.ts @@ -0,0 +1,29 @@ +/* + * Copyright © 2024 Hexastack. All rights reserved. + * + * Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms: + * 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission. + * 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file). + */ + +import { Injectable } from '@nestjs/common'; + +import { BaseService } from '@/utils/generics/base-service'; + +import { NlpMetricRepository } from '../repositories/nlp-metric.repository'; +import { + NlpMetric, + NlpMetricFull, + NlpMetricPopulate, +} from '../schemas/nlp-metric.schema'; + +@Injectable() +export class NlpMetricService extends BaseService< + NlpMetric, + NlpMetricPopulate, + NlpMetricFull +> { + constructor(readonly repository: NlpMetricRepository) { + super(repository); + } +} diff --git a/api/src/nlp/services/nlp-metric-value.service.ts b/api/src/nlp/services/nlp-metric-value.service.ts new file mode 100644 index 00000000..85262a7b --- /dev/null +++ b/api/src/nlp/services/nlp-metric-value.service.ts @@ -0,0 +1,74 @@ +/* + * Copyright © 2024 Hexastack. All rights reserved. + * + * Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms: + * 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission. + * 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file). + */ + +import { Injectable } from '@nestjs/common'; +import { EventEmitter2 } from '@nestjs/event-emitter'; + +import { BaseService } from '@/utils/generics/base-service'; + +import { NlpMetricValueDto } from '../dto/nlp-metric-value.dto'; +import { NlpMetricValueRepository } from '../repositories/nlp-metric-value.repository'; +import { + NlpMetricValue, + NlpMetricValueFull, + NlpMetricValuePopulate, +} from '../schemas/nlp-metric-value.schema'; + +import { NlpModelService } from './nlp-model.service'; + +@Injectable() +export class NlpMetricValueService extends BaseService< + NlpMetricValue, + NlpMetricValuePopulate, + NlpMetricValueFull +> { + constructor( + readonly repository: NlpMetricValueRepository, + private readonly eventEmitter: EventEmitter2, + private readonly nlpModelService: NlpModelService, + ) { + super(repository); + } + + /** + * Create a new NLP MetricValue. + * @param nlpMetricValueDto - Data transfer object for creating an MetricValue + * @returns {Promise} - Created MetricValue details + */ + async createMetricValue(nlpMetricValueDto: NlpMetricValueDto): Promise { + const MetricValue = new NlpMetricValue(); + Object.assign(MetricValue, nlpMetricValueDto); + + // Create the MetricValue in the repository + await this.repository.create(MetricValue); + } + + /** + * Update an existing NLP MetricValue. + * @param id - The ID of the MetricValue to update + * @param nlpMetricValueDto - The data to update the MetricValue with + * @returns {Promise} - Updated MetricValue details + */ + async updateMetricValue( + id: string, + nlpMetricValueDto: NlpMetricValueDto, + ): Promise { + // Perform the update in the repository + await this.repository.updateOne(id, nlpMetricValueDto); + } + + /** + * Delete an NLP MetricValue and its related data. + * @param id - The ID of the MetricValue to delete + * @returns {Promise} - A promise indicating completion + */ + async deleteCascadeMetricValue(id: string): Promise { + // Perform the delete operation in the repository + await this.repository.deleteOne(id); + } +} diff --git a/api/src/nlp/services/nlp-parameter-value.service.ts b/api/src/nlp/services/nlp-parameter-value.service.ts new file mode 100644 index 00000000..2173e8cc --- /dev/null +++ b/api/src/nlp/services/nlp-parameter-value.service.ts @@ -0,0 +1,76 @@ +/* + * Copyright © 2024 Hexastack. All rights reserved. + * + * Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms: + * 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission. + * 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file). + */ + +import { Injectable } from '@nestjs/common'; +import { EventEmitter2 } from '@nestjs/event-emitter'; + +import { BaseService } from '@/utils/generics/base-service'; + +import { NlpParameterValueDto } from '../dto/nlp-parameter-value.dto'; +import { NlpParameterValueRepository } from '../repositories/nlp-parameter-value.repository'; +import { + NlpParameterValue, + NlpParameterValueFull, + NlpParameterValuePopulate, +} from '../schemas/nlp-parameter-value.schema'; + +import { NlpModelService } from './nlp-model.service'; + +@Injectable() +export class NlpParameterValueService extends BaseService< + NlpParameterValue, + NlpParameterValuePopulate, + NlpParameterValueFull +> { + constructor( + readonly repository: NlpParameterValueRepository, + private readonly eventEmitter: EventEmitter2, + private readonly nlpModelService: NlpModelService, + ) { + super(repository); + } + + /** + * Create a new NLP MetricValue. + * @param NlpParameterValueDto - Data transfer object for creating an MetricValue + * @returns {Promise} - Created MetricValue details + */ + async createMetricValue( + NlpParameterValueDto: NlpParameterValueDto, + ): Promise { + const MetricValue = new NlpParameterValue(); + Object.assign(MetricValue, NlpParameterValueDto); + + // Create the MetricValue in the repository + await this.repository.create(MetricValue); + } + + /** + * Update an existing NLP MetricValue. + * @param id - The ID of the MetricValue to update + * @param NlpParameterValueDto - The data to update the MetricValue with + * @returns {Promise} - Updated MetricValue details + */ + async updateMetricValue( + id: string, + NlpParameterValueDto: NlpParameterValueDto, + ): Promise { + // Perform the update in the repository + await this.repository.updateOne(id, NlpParameterValueDto); + } + + /** + * Delete an NLP MetricValue and its related data. + * @param id - The ID of the MetricValue to delete + * @returns {Promise} - A promise indicating completion + */ + async deleteCascadeMetricValue(id: string): Promise { + // Perform the delete operation in the repository + await this.repository.deleteOne(id); + } +} diff --git a/api/src/nlp/services/nlp-parameter.service.ts b/api/src/nlp/services/nlp-parameter.service.ts new file mode 100644 index 00000000..64387c9c --- /dev/null +++ b/api/src/nlp/services/nlp-parameter.service.ts @@ -0,0 +1,29 @@ +/* + * Copyright © 2024 Hexastack. All rights reserved. + * + * Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms: + * 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission. + * 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file). + */ + +import { Injectable } from '@nestjs/common'; + +import { BaseService } from '@/utils/generics/base-service'; + +import { NlpParameterRepository } from '../repositories/nlp-parameter.repository'; +import { + NlpParameter, + NlpParameterFull, + NlpParameterPopulate, +} from '../schemas/nlp-parameter.schema'; + +@Injectable() +export class NlpParameterService extends BaseService< + NlpParameter, + NlpParameterPopulate, + NlpParameterFull +> { + constructor(readonly repository: NlpParameterRepository) { + super(repository); + } +} diff --git a/api/src/utils/generics/base-repository.ts b/api/src/utils/generics/base-repository.ts index 00f205ce..9fafe05b 100644 --- a/api/src/utils/generics/base-repository.ts +++ b/api/src/utils/generics/base-repository.ts @@ -437,4 +437,8 @@ export abstract class BaseRepository< ) { // Nothing ... } + + async aggregate(pipeline: any[]): Promise { + return this.model.aggregate(pipeline).exec(); + } }