| 1 | /* |
| 2 | * @Author: nevin |
| 3 | * @Date: 2024-06-17 19:19:15 |
| 4 | * @LastEditTime: 2024-09-05 15:19:25 |
| 5 | * @LastEditors: nevin |
| 6 | * @Description: Tracing tracing |
| 7 | */ |
| 8 | import { Injectable } from '@nestjs/common'; |
| 9 | import { InjectModel } from '@nestjs/mongoose'; |
| 10 | import { Model } from 'mongoose'; |
| 11 | import { Tracing } from 'src/db/schema/tracing.schema'; |
| 12 | |
| 13 | export enum TracingTag { |
| 14 | AccountAdd = 'AccountAdd', // 账号添加 |
| 15 | VideoPul = 'VideoPul', // 视频发布 |
| 16 | OpenProjectUse = 'OpenProjectUse', // 开源项目调用 |
| 17 | } |
| 18 | |
| 19 | @Injectable() |
| 20 | export class TracingService { |
| 21 | constructor( |
| 22 | @InjectModel(Tracing.name) |
| 23 | private readonly tracingModel: Model<Tracing>, |
| 24 | ) {} |
| 25 | |
| 26 | async create(newData: Partial<Tracing>) { |
| 27 | return await this.tracingModel.create(newData); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * 账号总数 |
| 32 | * @param time |
| 33 | * @returns |
| 34 | */ |
| 35 | async getTracingAccountCount() { |
| 36 | try { |
| 37 | const result = await this.tracingModel.aggregate([ |
| 38 | { |
| 39 | $match: { |
| 40 | tag: TracingTag.AccountAdd, |
| 41 | }, |
| 42 | }, |
| 43 | { |
| 44 | $group: { |
| 45 | _id: '$accountId', // 按账号ID |
| 46 | }, |
| 47 | }, |
| 48 | { |
| 49 | $count: 'total', // 统计分组数量 |
| 50 | }, |
| 51 | ]); |
| 52 | |
| 53 | return result.length > 0 ? result[0].total : 0; |
| 54 | } catch (error) { |
| 55 | console.error('Error counting documents:', error); |
| 56 | return 0; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * 时间段内发视频的用户总数 |
| 62 | * @param time |
| 63 | * @returns |
| 64 | */ |
| 65 | async getTracingVideoUserCount(time?: [Date, Date]) { |
| 66 | try { |
| 67 | const startTime = time ? time[0] : new Date('2024-01-01'); |
| 68 | const endTime = time ? time[1] : new Date(); |
| 69 | |
| 70 | const result = await this.tracingModel.aggregate([ |
| 71 | { |
| 72 | $match: { |
| 73 | tag: TracingTag.VideoPul, |
| 74 | time: { |
| 75 | $gte: startTime, |
| 76 | $lte: endTime, |
| 77 | }, |
| 78 | }, |
| 79 | }, |
| 80 | { |
| 81 | $group: { |
| 82 | _id: '$userId', // 按用户ID分组 |
| 83 | }, |
| 84 | }, |
| 85 | { |
| 86 | $count: 'total', // 统计分组数量 |
| 87 | }, |
| 88 | ]); |
| 89 | |
| 90 | return result.length > 0 ? result[0].total : 0; |
| 91 | } catch (error) { |
| 92 | console.error('Error counting documents:', error); |
| 93 | return 0; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * 时间段内发视频的总数 |
| 99 | * @param time |
| 100 | * @returns |
| 101 | */ |
| 102 | async getTracingVideoCount(time?: [Date, Date]) { |
| 103 | try { |
| 104 | const startTime = time ? time[0] : new Date('2024-01-01'); |
| 105 | const endTime = time ? time[1] : new Date(); |
| 106 | |
| 107 | const result = await this.tracingModel.aggregate([ |
| 108 | { |
| 109 | $match: { |
| 110 | tag: TracingTag.VideoPul, |
| 111 | time: { |
| 112 | $gte: startTime, |
| 113 | $lte: endTime, |
| 114 | }, |
| 115 | }, |
| 116 | }, |
| 117 | { |
| 118 | $count: 'total', // 统计文档数量 |
| 119 | }, |
| 120 | ]); |
| 121 | |
| 122 | return result.length > 0 ? result[0].total : 0; |
| 123 | } catch (error) { |
| 124 | console.error('Error counting documents:', error); |
| 125 | return 0; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * 时间段内开源项目调用总数 |
| 131 | * @param time |
| 132 | * @returns |
| 133 | */ |
| 134 | async getTracingOpenProjectCount(time?: [Date, Date]) { |
| 135 | try { |
| 136 | const startTime = time ? time[0] : new Date('2024-01-01'); |
| 137 | const endTime = time ? time[1] : new Date(); |
| 138 | |
| 139 | const result = await this.tracingModel.aggregate([ |
| 140 | { |
| 141 | $match: { |
| 142 | tag: TracingTag.OpenProjectUse, |
| 143 | time: { |
| 144 | $gte: startTime, |
| 145 | $lte: endTime, |
| 146 | }, |
| 147 | }, |
| 148 | }, |
| 149 | { |
| 150 | $count: 'total', // 统计文档数量 |
| 151 | }, |
| 152 | ]); |
| 153 | |
| 154 | return result.length > 0 ? result[0].total : 0; |
| 155 | } catch (error) { |
| 156 | console.error('Error counting documents:', error); |
| 157 | return 0; |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 |