Some checks failed
Deploy / deploy (push) Has been cancelled
Full-stack web application for Telegram management - Frontend: Vue 3 + Vben Admin - Backend: NestJS - Features: User management, group broadcast, statistics 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
249 lines
6.4 KiB
TypeScript
249 lines
6.4 KiB
TypeScript
import {
|
|
Controller,
|
|
Get,
|
|
Post,
|
|
Body,
|
|
Query,
|
|
UseGuards,
|
|
Req,
|
|
HttpCode,
|
|
HttpStatus,
|
|
} from '@nestjs/common';
|
|
import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger';
|
|
import { Request } from 'express';
|
|
|
|
import { JwtAuthGuard } from '@common/guards/jwt-auth.guard';
|
|
import { RolesGuard } from '@common/guards/roles.guard';
|
|
import { Roles } from '@common/decorators/roles.decorator';
|
|
import { ApiOkResponse, ApiCreatedResponse } from '@common/decorators/api-response.decorator';
|
|
|
|
import { AnalyticsService } from '../services/analytics.service';
|
|
import {
|
|
AnalyticsQueryDto,
|
|
CreateAnalyticsRecordDto,
|
|
BatchAnalyticsQueryDto,
|
|
UserActivityQueryDto,
|
|
PerformanceQueryDto,
|
|
ErrorAnalyticsQueryDto,
|
|
} from '../dto/analytics-query.dto';
|
|
|
|
@ApiTags('Analytics - 分析统计')
|
|
@Controller('analytics')
|
|
@UseGuards(JwtAuthGuard, RolesGuard)
|
|
export class AnalyticsController {
|
|
constructor(private readonly analyticsService: AnalyticsService) {}
|
|
|
|
/**
|
|
* 记录分析事件
|
|
*/
|
|
@Post('record')
|
|
@ApiOperation({ summary: '记录分析事件' })
|
|
@ApiCreatedResponse('分析事件记录成功')
|
|
@HttpCode(HttpStatus.CREATED)
|
|
async recordEvent(
|
|
@Body() createDto: CreateAnalyticsRecordDto,
|
|
@Req() request: Request,
|
|
) {
|
|
const record = await this.analyticsService.recordEvent(createDto, request);
|
|
return {
|
|
id: record.id,
|
|
eventType: record.eventType,
|
|
eventName: record.eventName,
|
|
timestamp: record.timestamp,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 批量记录分析事件
|
|
*/
|
|
@Post('record/batch')
|
|
@ApiOperation({ summary: '批量记录分析事件' })
|
|
@ApiCreatedResponse('批量分析事件记录成功')
|
|
@HttpCode(HttpStatus.CREATED)
|
|
async recordBatchEvents(
|
|
@Body() events: CreateAnalyticsRecordDto[],
|
|
@Req() request: Request,
|
|
) {
|
|
const records = await this.analyticsService.recordEvents(events, request);
|
|
return {
|
|
count: records.length,
|
|
events: records.map(record => ({
|
|
id: record.id,
|
|
eventType: record.eventType,
|
|
eventName: record.eventName,
|
|
timestamp: record.timestamp,
|
|
})),
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 查询分析数据
|
|
*/
|
|
@Get('query')
|
|
@ApiOperation({ summary: '查询分析数据' })
|
|
@ApiOkResponse('分析数据查询成功')
|
|
@Roles('admin', 'operator')
|
|
async queryAnalytics(@Query() queryDto: AnalyticsQueryDto) {
|
|
const result = await this.analyticsService.queryAnalytics(queryDto);
|
|
return {
|
|
query: queryDto,
|
|
data: result,
|
|
count: Array.isArray(result) ? result.length : 1,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 批量查询分析数据
|
|
*/
|
|
@Post('query/batch')
|
|
@ApiOperation({ summary: '批量查询分析数据' })
|
|
@ApiOkResponse('批量分析数据查询成功')
|
|
@Roles('admin', 'operator')
|
|
async batchQueryAnalytics(@Body() batchQueryDto: BatchAnalyticsQueryDto) {
|
|
const results = await Promise.all(
|
|
batchQueryDto.queries.map(query =>
|
|
this.analyticsService.queryAnalytics(query)
|
|
)
|
|
);
|
|
|
|
return {
|
|
queries: batchQueryDto.queries,
|
|
results: results.map((data, index) => ({
|
|
query: batchQueryDto.queries[index],
|
|
data,
|
|
count: Array.isArray(data) ? data.length : 1,
|
|
})),
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 获取实时指标
|
|
*/
|
|
@Get('realtime')
|
|
@ApiOperation({ summary: '获取实时指标' })
|
|
@ApiOkResponse('实时指标获取成功')
|
|
@Roles('admin', 'operator')
|
|
async getRealtimeMetrics() {
|
|
return await this.analyticsService.getRealtimeMetrics();
|
|
}
|
|
|
|
/**
|
|
* 获取热门事件
|
|
*/
|
|
@Get('top-events')
|
|
@ApiOperation({ summary: '获取热门事件' })
|
|
@ApiOkResponse('热门事件获取成功')
|
|
@Roles('admin', 'operator')
|
|
async getTopEvents(@Query('limit') limit: number = 10) {
|
|
const events = await this.analyticsService.getTopEvents(limit);
|
|
return {
|
|
limit,
|
|
events,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 获取用户活动分析
|
|
*/
|
|
@Get('user-activity')
|
|
@ApiOperation({ summary: '获取用户活动分析' })
|
|
@ApiOkResponse('用户活动分析获取成功')
|
|
@Roles('admin', 'operator')
|
|
async getUserActivityAnalytics(@Query() queryDto: UserActivityQueryDto) {
|
|
const result = await this.analyticsService.getUserActivityAnalytics(
|
|
queryDto.startDate,
|
|
queryDto.endDate,
|
|
queryDto.userId,
|
|
);
|
|
|
|
return {
|
|
query: queryDto,
|
|
data: result,
|
|
count: result.length,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 获取性能指标分析
|
|
*/
|
|
@Get('performance')
|
|
@ApiOperation({ summary: '获取性能指标分析' })
|
|
@ApiOkResponse('性能指标分析获取成功')
|
|
@Roles('admin', 'operator')
|
|
async getPerformanceAnalytics(@Query() queryDto: PerformanceQueryDto) {
|
|
const result = await this.analyticsService.getPerformanceAnalytics(
|
|
queryDto.startDate,
|
|
queryDto.endDate,
|
|
queryDto.metricName,
|
|
);
|
|
|
|
return {
|
|
query: queryDto,
|
|
data: result,
|
|
count: result.length,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 获取错误分析
|
|
*/
|
|
@Get('errors')
|
|
@ApiOperation({ summary: '获取错误分析' })
|
|
@ApiOkResponse('错误分析获取成功')
|
|
@Roles('admin', 'operator')
|
|
async getErrorAnalytics(@Query() queryDto: ErrorAnalyticsQueryDto) {
|
|
const result = await this.analyticsService.getErrorAnalytics(
|
|
queryDto.startDate,
|
|
queryDto.endDate,
|
|
);
|
|
|
|
return {
|
|
query: queryDto,
|
|
...result,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 获取分析概览
|
|
*/
|
|
@Get('overview')
|
|
@ApiOperation({ summary: '获取分析概览' })
|
|
@ApiOkResponse('分析概览获取成功')
|
|
@Roles('admin', 'operator')
|
|
async getAnalyticsOverview(
|
|
@Query('startDate') startDate: string,
|
|
@Query('endDate') endDate: string,
|
|
) {
|
|
const [
|
|
realtimeMetrics,
|
|
userActivity,
|
|
performance,
|
|
errors,
|
|
topEvents,
|
|
] = await Promise.all([
|
|
this.analyticsService.getRealtimeMetrics(),
|
|
this.analyticsService.getUserActivityAnalytics(startDate, endDate),
|
|
this.analyticsService.getPerformanceAnalytics(startDate, endDate),
|
|
this.analyticsService.getErrorAnalytics(startDate, endDate),
|
|
this.analyticsService.getTopEvents(5),
|
|
]);
|
|
|
|
return {
|
|
period: { startDate, endDate },
|
|
realtime: realtimeMetrics,
|
|
userActivity: {
|
|
data: userActivity,
|
|
count: userActivity.length,
|
|
},
|
|
performance: {
|
|
data: performance,
|
|
count: performance.length,
|
|
},
|
|
errors,
|
|
topEvents: {
|
|
data: topEvents,
|
|
count: topEvents.length,
|
|
},
|
|
};
|
|
}
|
|
} |