|
|
@@ -3,11 +3,10 @@ import { Observable, interval } from 'rxjs'
|
|
|
import { ChatGPTAPI, ChatMessage } from '../chatapi'
|
|
|
import type { RequestProps } from './types'
|
|
|
import { chatReplyProcess } from './chatgpt'
|
|
|
-import { Repository, MoreThanOrEqual, LessThanOrEqual, And } from 'typeorm'
|
|
|
+import { In, Repository, MoreThanOrEqual, LessThanOrEqual, And } from 'typeorm'
|
|
|
import { ChatHistory } from './entities/chat.entity'
|
|
|
import { InjectRepository } from '@nestjs/typeorm'
|
|
|
import { TokenUsage } from './entities/token-usage.entity'
|
|
|
-import { format } from 'date-fns'
|
|
|
import { MembershipService } from '../membership/membership.service'
|
|
|
import { MemberType } from '../membership/entities/membership.entity'
|
|
|
import { get_encoding } from '@dqbd/tiktoken'
|
|
|
@@ -16,6 +15,9 @@ import { HttpService } from '@nestjs/axios'
|
|
|
import * as types from '../chatapi/types'
|
|
|
import { SysConfigService } from '../sys-config/sys-config.service'
|
|
|
import { ChatPdfService } from '../chat-pdf/chat-pdf.service'
|
|
|
+import { UsersService } from '../users/users.service'
|
|
|
+import { Role } from '../model/role.enum'
|
|
|
+import { startOfDay, endOfDay, addDays, format } from 'date-fns'
|
|
|
|
|
|
@Injectable()
|
|
|
export class ChatService {
|
|
|
@@ -28,7 +30,8 @@ export class ChatService {
|
|
|
private readonly membershipService: MembershipService,
|
|
|
private readonly httpService: HttpService,
|
|
|
private readonly sysConfigService: SysConfigService,
|
|
|
- private readonly chatPdfService: ChatPdfService
|
|
|
+ private readonly chatPdfService: ChatPdfService,
|
|
|
+ private readonly usersService: UsersService
|
|
|
) {}
|
|
|
|
|
|
public chat(req, res): Observable<any> {
|
|
|
@@ -270,4 +273,32 @@ export class ChatService {
|
|
|
date: And(MoreThanOrEqual(start || '2023-04-01'), LessThanOrEqual(end || date))
|
|
|
})
|
|
|
}
|
|
|
+
|
|
|
+ public async getCount(apiUserId: number, roles?: Role, date?: string) {
|
|
|
+ const userIds = await this.usersService.getUsers(apiUserId, roles)
|
|
|
+
|
|
|
+ return this.chatHistoryRepository.countBy({
|
|
|
+ userId: In(userIds),
|
|
|
+ role: 'user',
|
|
|
+ time: date
|
|
|
+ ? And(MoreThanOrEqual(startOfDay(new Date(date))), LessThanOrEqual(endOfDay(new Date(date))))
|
|
|
+ : undefined
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ public async getWeekCount(apiUserId: number) {
|
|
|
+ const date1 = new Date()
|
|
|
+ let dayInfo = {}
|
|
|
+ for (let i = 0; i < 7; i++) {
|
|
|
+ let date2 = format(addDays(date1, 0 - i), 'yyyy-MM-dd')
|
|
|
+ let api = await this.getCount(apiUserId, Role.Api, date2)
|
|
|
+ let user = await this.getCount(apiUserId, Role.User, date2)
|
|
|
+ dayInfo[format(addDays(date1, 0 - i), 'MM-dd')] = {
|
|
|
+ api: api,
|
|
|
+ user: user
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return dayInfo
|
|
|
+ }
|
|
|
}
|