forked from XStreamRollz/XStreamRoll
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.controller.ts
More file actions
63 lines (58 loc) · 1.99 KB
/
Copy pathadmin.controller.ts
File metadata and controls
63 lines (58 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { CACHE_MANAGER, CacheInterceptor, CacheTTL } from "@nestjs/cache-manager"
import {
Controller,
Get,
Inject,
UseGuards,
UseInterceptors,
} from "@nestjs/common"
import {
ApiBearerAuth,
ApiForbiddenResponse,
ApiOkResponse,
ApiOperation,
ApiTags,
ApiUnauthorizedResponse,
} from "@nestjs/swagger"
import { Cache } from "cache-manager"
import { AdminStats, AdminStatsService } from "./admin-stats.service"
import { AdminGuard } from "../common/auth/admin.guard"
import { Roles } from "../common/auth/roles.guard"
const STATS_CACHE_TTL_MS = 60_000
@ApiTags("admin")
@Controller("admin")
@UseGuards(AdminGuard)
@Roles("admin")
export class AdminController {
constructor(
private readonly stats: AdminStatsService,
@Inject(CACHE_MANAGER) private readonly cache: Cache,
) {}
/**
* GET /admin/stats — protected, cached snapshot of platform-wide
* metrics. The 60-second TTL is enforced both by NestJS's
* CacheInterceptor (HTTP-level) and by an explicit cache.set inside
* the service body, so even handlers that bypass the interceptor
* stay within budget on aggregate-query cost.
*/
@Get("stats")
@UseInterceptors(CacheInterceptor)
@CacheTTL(STATS_CACHE_TTL_MS)
@ApiBearerAuth("bearer")
@ApiOperation({
summary: "Get platform-wide statistics",
description:
"Returns cached aggregate platform metrics: total users, total streams, active streams (status = 'active'), and stream events created within the last 24 hours. Admin role required.",
})
@ApiOkResponse({ description: "Admin platform statistics." })
@ApiUnauthorizedResponse({ description: "Authentication required." })
@ApiForbiddenResponse({ description: "Admin role required." })
async getStats(): Promise<AdminStats> {
const cacheKey = "admin:stats"
const cached = await this.cache.get<AdminStats>(cacheKey)
if (cached) return cached
const snapshot = await this.stats.compute()
await this.cache.set(cacheKey, snapshot, STATS_CACHE_TTL_MS)
return snapshot
}
}