wip: evaluation/tests endpoint

This commit is contained in:
Eugene Molodkin 2024-11-04 18:51:27 +01:00
parent d450334d6b
commit 12f33cc401
No known key found for this signature in database
4 changed files with 65 additions and 20 deletions

View file

@ -4,7 +4,6 @@ import {
Generated, Generated,
Index, Index,
ManyToOne, ManyToOne,
OneToOne,
PrimaryColumn, PrimaryColumn,
RelationId, RelationId,
} from '@n8n/typeorm'; } from '@n8n/typeorm';
@ -56,6 +55,6 @@ export class TestDefinition extends WithTimestamps {
* Relation to the annotation tag associated with the test * Relation to the annotation tag associated with the test
* This tag will be used to select the test cases to run from previous executions * This tag will be used to select the test cases to run from previous executions
*/ */
@OneToOne('AnnotationTagEntity', 'test') @ManyToOne('AnnotationTagEntity', 'test')
annotationTag: AnnotationTagEntity; annotationTag: AnnotationTagEntity;
} }

View file

@ -1,11 +1,59 @@
import { DataSource, Repository } from '@n8n/typeorm'; import type { FindManyOptions, FindOptionsSelect, FindOptionsWhere } from '@n8n/typeorm';
import { DataSource, In, Repository } from '@n8n/typeorm';
import { Service } from 'typedi'; import { Service } from 'typedi';
import { TestEntity } from '@/databases/entities/test-entity'; import { TestEntity } from '@/databases/entities/test-entity';
import type { ListQuery } from '@/requests';
@Service() @Service()
export class TestRepository extends Repository<TestEntity> { export class TestRepository extends Repository<TestEntity> {
constructor(dataSource: DataSource) { constructor(dataSource: DataSource) {
super(TestEntity, dataSource.manager); super(TestEntity, dataSource.manager);
} }
async getMany(sharedWorkflowIds: string[], options?: ListQuery.Options) {
if (sharedWorkflowIds.length === 0) return { tests: [], count: 0 };
const where: FindOptionsWhere<TestEntity> = {
...options?.filter,
workflow: {
id: In(sharedWorkflowIds),
},
};
type Select = FindOptionsSelect<TestEntity>;
const select: Select = {
id: true,
name: true,
createdAt: true,
updatedAt: true,
};
const relations: string[] = ['workflow', 'evaluationWorkflow', 'annotationTag'];
const isDefaultSelect = options?.select === undefined;
const findManyOptions: FindManyOptions<TestEntity> = {
select: { ...select, id: true },
where,
};
if (isDefaultSelect || options?.select?.updatedAt === true) {
findManyOptions.order = { updatedAt: 'ASC' };
}
if (relations.length > 0) {
findManyOptions.relations = relations;
}
if (options?.take) {
findManyOptions.skip = options.skip;
findManyOptions.take = options.take;
}
const [tests, count] = await this.findAndCount(findManyOptions);
return { tests, count };
}
} }

View file

@ -1,7 +1,5 @@
// import type { Scope } from '@n8n/permissions';
// import type { User } from '@/databases/entities/user';
import { Get, /*Patch, Post,*/ RestController } from '@/decorators'; import { Get, /*Patch, Post,*/ RestController } from '@/decorators';
import { listQueryMiddleware } from '@/middlewares';
import { TestsService } from './tests.service'; import { TestsService } from './tests.service';
import { TestsRequest } from './tests.types'; import { TestsRequest } from './tests.types';
@ -21,13 +19,9 @@ export class TestsController {
// } // }
// } // }
@Get('/') @Get('/', { middlewares: listQueryMiddleware })
async getMany(_req: TestsRequest.GetMany) { async getMany(req: TestsRequest.GetMany) {
return await this.testsService.getMany( return await this.testsService.getMany(req.user, req.listQueryOptions);
/*req.user, {
listQueryOptions: req.listQueryOptions,
}*/
);
} }
// @Get('/:id') // @Get('/:id')

View file

@ -1,14 +1,18 @@
import { Service } from 'typedi'; import { Service } from 'typedi';
import type { TestEntity } from '@/databases/entities/test-entity'; import type { TestEntity } from '@/databases/entities/test-entity';
// import type { User } from '@/databases/entities/user'; import type { User } from '@/databases/entities/user';
import { TestRepository } from '@/databases/repositories/test.repository'; import { TestRepository } from '@/databases/repositories/test.repository';
import { validateEntity } from '@/generic-helpers'; import { validateEntity } from '@/generic-helpers';
// import type { ListQuery } from '@/requests'; import type { ListQuery } from '@/requests';
import { WorkflowSharingService } from '@/workflows/workflow-sharing.service';
@Service() @Service()
export class TestsService { export class TestsService {
constructor(private testRepository: TestRepository) {} constructor(
private testRepository: TestRepository,
private readonly workflowSharingService: WorkflowSharingService,
) {}
// toEntity(attrs: { name: string; id?: string }) { // toEntity(attrs: { name: string; id?: string }) {
// attrs.name = attrs.name.trim(); // attrs.name = attrs.name.trim();
@ -26,11 +30,11 @@ export class TestsService {
return await this.testRepository.delete(id); return await this.testRepository.delete(id);
} }
async getMany(/*user: User, options: ListQuery.Options*/) { async getMany(user: User, options: ListQuery.Options) {
const allTests = await this.testRepository.find({ const sharedWorkflowIds = await this.workflowSharingService.getSharedWorkflowIds(user, {
select: ['id', 'name', 'createdAt', 'updatedAt'], scopes: ['workflow:read'],
}); });
return { allTests, count: allTests.length }; return await this.testRepository.getMany(sharedWorkflowIds, options);
} }
} }