Skip to content

Commit

Permalink
feat(back): dashboard stats
Browse files Browse the repository at this point in the history
  • Loading branch information
EdenComp committed Nov 5, 2023
1 parent f0236f6 commit 0d177f9
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
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

0 comments on commit 0d177f9

Please sign in to comment.