added get stat api

This commit is contained in:
Suman991 2026-04-13 17:43:52 +05:30
parent 1f4ddfa10b
commit 62e13e673f
2 changed files with 37 additions and 0 deletions

View File

@ -31,11 +31,18 @@ export class FormController {
return this.formService.findAll(query); return this.formService.findAll(query);
} }
@Get('stats')
@ApiOperation({ summary: 'Get Stats' })
async getStats() {
return await this.formService.getStats();
}
@Get(':formId') @Get(':formId')
@ApiOperation({ summary: 'Find a form' }) @ApiOperation({ summary: 'Find a form' })
async find(@Param('formId') formId: string) { async find(@Param('formId') formId: string) {
return await this.formService.find(formId); return await this.formService.find(formId);
} }
@Get(':formId/interface') @Get(':formId/interface')
@ApiOperation({ summary: 'Get interface for the form provided by llm' }) @ApiOperation({ summary: 'Get interface for the form provided by llm' })
async getInterface(@Param('formId') formId: string) { async getInterface(@Param('formId') formId: string) {

View File

@ -93,6 +93,36 @@ export class FormService {
return form; 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<string> { async findInterface(formId: string): Promise<string> {
const form = await this.formModel const form = await this.formModel
.findOne({ id: formId, deletedAt: null }) .findOne({ id: formId, deletedAt: null })