|
| 1 | +import { Injectable, Inject } from '@nestjs/common'; |
| 2 | +import { DATABASE_CONNECTION } from '../database/database.module'; |
| 3 | +import { NodePgDatabase } from 'drizzle-orm/node-postgres'; |
| 4 | +import { cities, regions, users, organizations, quests } from '../database/schema'; |
| 5 | +import { eq, and, ne, count } from 'drizzle-orm'; |
| 6 | + |
| 7 | +export interface StatisticsDto { |
| 8 | + citiesCount: number; |
| 9 | + regionsCount: number; |
| 10 | + usersCount: number; |
| 11 | + organizationsCount: number; |
| 12 | + totalQuests: number; |
| 13 | + activeQuests: number; |
| 14 | + completedQuests: number; |
| 15 | +} |
| 16 | + |
| 17 | +@Injectable() |
| 18 | +export class StatisticsService { |
| 19 | + private cache: StatisticsDto | null = null; |
| 20 | + private cacheTime: number = 0; |
| 21 | + private readonly CACHE_TTL = 60 * 1000; // 1 минута в миллисекундах |
| 22 | + |
| 23 | + constructor( |
| 24 | + @Inject(DATABASE_CONNECTION) |
| 25 | + private db: NodePgDatabase, |
| 26 | + ) {} |
| 27 | + |
| 28 | + async getStatistics(): Promise<StatisticsDto> { |
| 29 | + const now = Date.now(); |
| 30 | + |
| 31 | + // Проверяем кеш |
| 32 | + if (this.cache && (now - this.cacheTime) < this.CACHE_TTL) { |
| 33 | + return this.cache; |
| 34 | + } |
| 35 | + |
| 36 | + // Выполняем все запросы параллельно для оптимизации |
| 37 | + const [ |
| 38 | + citiesResult, |
| 39 | + regionsResult, |
| 40 | + usersResult, |
| 41 | + organizationsResult, |
| 42 | + totalQuestsResult, |
| 43 | + activeQuestsResult, |
| 44 | + completedQuestsResult, |
| 45 | + ] = await Promise.all([ |
| 46 | + // Количество городов (исключая удаленные) |
| 47 | + this.db |
| 48 | + .select({ count: count(cities.id) }) |
| 49 | + .from(cities) |
| 50 | + .where(ne(cities.recordStatus, 'DELETED')), |
| 51 | + |
| 52 | + // Количество регионов (исключая удаленные) |
| 53 | + this.db |
| 54 | + .select({ count: count(regions.id) }) |
| 55 | + .from(regions) |
| 56 | + .where(ne(regions.recordStatus, 'DELETED')), |
| 57 | + |
| 58 | + // Количество пользователей (исключая удаленные) |
| 59 | + this.db |
| 60 | + .select({ count: count(users.id) }) |
| 61 | + .from(users) |
| 62 | + .where(ne(users.recordStatus, 'DELETED')), |
| 63 | + |
| 64 | + // Количество организаций (исключая удаленные) |
| 65 | + this.db |
| 66 | + .select({ count: count(organizations.id) }) |
| 67 | + .from(organizations) |
| 68 | + .where(ne(organizations.recordStatus, 'DELETED')), |
| 69 | + |
| 70 | + // Всего квестов (исключая удаленные) |
| 71 | + this.db |
| 72 | + .select({ count: count(quests.id) }) |
| 73 | + .from(quests) |
| 74 | + .where(ne(quests.recordStatus, 'DELETED')), |
| 75 | + |
| 76 | + // Активные квесты (status = 'active', исключая удаленные) |
| 77 | + this.db |
| 78 | + .select({ count: count(quests.id) }) |
| 79 | + .from(quests) |
| 80 | + .where( |
| 81 | + and( |
| 82 | + eq(quests.status, 'active'), |
| 83 | + ne(quests.recordStatus, 'DELETED'), |
| 84 | + ), |
| 85 | + ), |
| 86 | + |
| 87 | + // Завершённые квесты (status = 'completed', исключая удаленные) |
| 88 | + this.db |
| 89 | + .select({ count: count(quests.id) }) |
| 90 | + .from(quests) |
| 91 | + .where( |
| 92 | + and( |
| 93 | + eq(quests.status, 'completed'), |
| 94 | + ne(quests.recordStatus, 'DELETED'), |
| 95 | + ), |
| 96 | + ), |
| 97 | + ]); |
| 98 | + |
| 99 | + const statistics: StatisticsDto = { |
| 100 | + citiesCount: citiesResult[0]?.count ?? 0, |
| 101 | + regionsCount: regionsResult[0]?.count ?? 0, |
| 102 | + usersCount: usersResult[0]?.count ?? 0, |
| 103 | + organizationsCount: organizationsResult[0]?.count ?? 0, |
| 104 | + totalQuests: totalQuestsResult[0]?.count ?? 0, |
| 105 | + activeQuests: activeQuestsResult[0]?.count ?? 0, |
| 106 | + completedQuests: completedQuestsResult[0]?.count ?? 0, |
| 107 | + }; |
| 108 | + |
| 109 | + // Обновляем кеш |
| 110 | + this.cache = statistics; |
| 111 | + this.cacheTime = now; |
| 112 | + |
| 113 | + return statistics; |
| 114 | + } |
| 115 | +} |
| 116 | + |
0 commit comments