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

Backend: Dashboard stats #224

Merged
merged 1 commit into from
Nov 5, 2023
Merged
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
20 changes: 20 additions & 0 deletions backend/back/src/workflows/dto/workflow-summary.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { ApiProperty } from "@nestjs/swagger";
import { IsNumber } from "class-validator";

export class WorkflowSummaryDto {
@ApiProperty()
@IsNumber()
workflowRuns!: number;

@ApiProperty()
@IsNumber()
workflowErrors!: number;

@ApiProperty()
@IsNumber()
workflows!: number;

@ApiProperty()
@IsNumber()
activeWorkflows!: number;
}
10 changes: 10 additions & 0 deletions backend/back/src/workflows/workflows.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
PickType,
} from "@nestjs/swagger";
import Workflow from "./entities/workflow.entity";
import { WorkflowSummaryDto } from "./dto/workflow-summary.dto";

@ApiBearerAuth()
@ApiTags("Workflows")
Expand All @@ -48,6 +49,15 @@ export class WorkflowsController {

constructor(private readonly workspacesService: WorkflowsService) {}

@ApiOkResponse({
description: "The workflows summary was successfully retrieved",
type: WorkflowSummaryDto,
})
@Get("summary")
async getWorkflowsSummary(@Req() { user: { id: ownerId } }: APIRequest) {
return await this.workspacesService.getWorkflowsSummary(ownerId);
}

@ApiOkResponse({
description: "The workflow was successfully created",
schema: {
Expand Down
3 changes: 2 additions & 1 deletion backend/back/src/workflows/workflows.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import { JobsModule } from "../jobs/jobs.module";
import { ConnectionsModule } from "../connections/connections.module";
import { ServicesModule } from "../services/services.module";
import { GrpcModule } from "../grpc/grpc.module";
import ActivityLog from "../activity/entities/activity-log.entity";

@Module({
imports: [
TypeOrmModule.forFeature([Workflow, WorkflowArea, Area, User]),
TypeOrmModule.forFeature([Workflow, WorkflowArea, Area, User, ActivityLog]),
forwardRef(() => GrpcModule),
forwardRef(() => JobsModule),
ConnectionsModule,
Expand Down
24 changes: 24 additions & 0 deletions backend/back/src/workflows/workflows.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import { ServiceName, ServicesService } from "../services/services.service";
import { OwnerJobParams, OwnerUniqueJobParams, UniqueJobParams } from "../types/jobParams";
import { plainToInstance } from "class-transformer";
import { GrpcService } from "../grpc/grpc.service";
import ActivityLog from "../activity/entities/activity-log.entity";
import { WorkflowSummaryDto } from "./dto/workflow-summary.dto";

@Injectable()
export class WorkflowsService {
Expand All @@ -35,6 +37,8 @@ export class WorkflowsService {
private readonly workflowAreaRepository: Repository<WorkflowArea>,
@InjectRepository(Area)
private readonly areaRepository: Repository<Area>,
@InjectRepository(ActivityLog)
private readonly activityRepository: Repository<ActivityLog>,
@Inject(forwardRef(() => JobsService))
private readonly jobsService: JobsService,
@Inject(forwardRef(() => GrpcService))
Expand All @@ -43,6 +47,26 @@ export class WorkflowsService {
private readonly servicesService: ServicesService,
) {}

async getWorkflowsSummary(ownerId: string): Promise<WorkflowSummaryDto> {
const workflowRuns = await this.activityRepository.count({
where: { type: "ran", workflow: { ownerId } },
relations: { workflow: true },
});
const workflowErrors = await this.activityRepository.count({
where: { type: "error", workflow: { ownerId } },
relations: { workflow: true },
});
const workflows = await this.workflowRepository.countBy({ ownerId });
const activeWorkflows = await this.workflowRepository.countBy({ ownerId, active: true });

return {
workflowRuns,
workflowErrors,
workflows,
activeWorkflows,
};
}

async getWorkflowByNameAndOwner(name: string, ownerId: string) {
return this.workflowRepository.findOneBy({
name,
Expand Down