From 62e13e673f6922cb2e401cf2013172a566119241 Mon Sep 17 00:00:00 2001 From: Suman991 Date: Mon, 13 Apr 2026 17:43:52 +0530 Subject: [PATCH] added get stat api --- src/form/form.controller.ts | 7 +++++++ src/form/form.service.ts | 30 ++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/form/form.controller.ts b/src/form/form.controller.ts index c98e3ab..c74b02c 100644 --- a/src/form/form.controller.ts +++ b/src/form/form.controller.ts @@ -31,11 +31,18 @@ export class FormController { return this.formService.findAll(query); } + @Get('stats') + @ApiOperation({ summary: 'Get Stats' }) + async getStats() { + return await this.formService.getStats(); + } + @Get(':formId') @ApiOperation({ summary: 'Find a form' }) async find(@Param('formId') formId: string) { return await this.formService.find(formId); } + @Get(':formId/interface') @ApiOperation({ summary: 'Get interface for the form provided by llm' }) async getInterface(@Param('formId') formId: string) { diff --git a/src/form/form.service.ts b/src/form/form.service.ts index 50050aa..16dbb1e 100644 --- a/src/form/form.service.ts +++ b/src/form/form.service.ts @@ -93,6 +93,36 @@ export class FormService { return form; } + async getStats() { + const [totalForms, totalFields, fieldTypeBreakdown, formStatusBreakdown] = + await Promise.all([ + this.formModel.countDocuments(), + this.formModel.aggregate([ + { $project: { fieldCount: { $size: '$fields' } } }, + { $group: { _id: null, total: { $sum: '$fieldCount' } } }, + ]), + this.formModel.aggregate([ + { $unwind: '$fields' }, + { $group: { _id: '$fields.keyType', count: { $sum: 1 } } }, + ]), + this.formModel.aggregate([ + { + $group: { + _id: { + $cond: { + if: { $ifNull: ['$deletedAt', false] }, + then: 'deleted', + else: 'active', + }, + }, + count: { $sum: 1 }, + }, + }, + ]), + ]); + return { totalForms, totalFields, fieldTypeBreakdown, formStatusBreakdown }; + } + async findInterface(formId: string): Promise { const form = await this.formModel .findOne({ id: formId, deletedAt: null })