mirror of
https://github.com/benjamin-wilson/public-pool.git
synced 2026-09-29 17:15:03 -07:00
user agent report
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
import { Global, Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
|
||||
import { UserAgentReportService } from './user-agent-report.service';
|
||||
import { UserAgentReportView } from './user-agent-report.view';
|
||||
|
||||
|
||||
|
||||
@Global()
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([UserAgentReportView])],
|
||||
providers: [UserAgentReportService],
|
||||
exports: [TypeOrmModule, UserAgentReportService],
|
||||
})
|
||||
export class UserAgentReportModule { }
|
||||
@@ -0,0 +1,35 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
|
||||
import { UserAgentReportView } from './user-agent-report.view';
|
||||
|
||||
@Injectable()
|
||||
export class UserAgentReportService {
|
||||
|
||||
constructor(
|
||||
@InjectRepository(UserAgentReportView)
|
||||
private userAgentReport: Repository<UserAgentReportView>,
|
||||
) {
|
||||
|
||||
}
|
||||
|
||||
public async getReport() {
|
||||
return await this.userAgentReport.find();
|
||||
}
|
||||
|
||||
public async refreshReport() {
|
||||
try {
|
||||
return await this.userAgentReport.query(`
|
||||
COMMIT;
|
||||
BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ;
|
||||
REFRESH MATERIALIZED VIEW user_agent_report_view;
|
||||
COMMIT;
|
||||
`);
|
||||
} catch (e) {
|
||||
|
||||
console.log(e)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
import { DataSource, ViewColumn, ViewEntity } from 'typeorm';
|
||||
|
||||
import { ClientEntity } from '../../client/client.entity';
|
||||
|
||||
@ViewEntity({
|
||||
materialized: true,
|
||||
expression: (dataSource: DataSource) =>
|
||||
dataSource
|
||||
.createQueryBuilder()
|
||||
.select('client.userAgent as "userAgent"')
|
||||
.addSelect('COUNT(client.userAgent)', 'count')
|
||||
.addSelect('MAX(client.bestDifficulty)', 'bestDifficulty')
|
||||
.addSelect('SUM(client.hashRate)', 'totalHashRate')
|
||||
.from(ClientEntity, 'client')
|
||||
.groupBy('client.userAgent')
|
||||
.orderBy('"totalHashRate"', 'DESC')
|
||||
})
|
||||
export class UserAgentReportView {
|
||||
@ViewColumn()
|
||||
userAgent: string;
|
||||
@ViewColumn()
|
||||
count: number;
|
||||
@ViewColumn()
|
||||
bestDifficulty: number;
|
||||
@ViewColumn()
|
||||
totalHashRate: number;
|
||||
}
|
||||
@@ -104,16 +104,16 @@ export class ClientService {
|
||||
return await this.clientRepository.softDelete({})
|
||||
}
|
||||
|
||||
public async getUserAgents() {
|
||||
const result = await this.clientRepository.createQueryBuilder('client')
|
||||
.select('client.userAgent as "userAgent"')
|
||||
.addSelect('COUNT(client.userAgent)', 'count')
|
||||
.addSelect('MAX(client.bestDifficulty)', 'bestDifficulty')
|
||||
.addSelect('SUM(client.hashRate)', 'totalHashRate')
|
||||
.groupBy('client.userAgent')
|
||||
.orderBy('"totalHashRate"', 'DESC')
|
||||
.getRawMany();
|
||||
return result;
|
||||
}
|
||||
// public async getUserAgents() {
|
||||
// const result = await this.clientRepository.createQueryBuilder('client')
|
||||
// .select('client.userAgent as "userAgent"')
|
||||
// .addSelect('COUNT(client.userAgent)', 'count')
|
||||
// .addSelect('MAX(client.bestDifficulty)', 'bestDifficulty')
|
||||
// .addSelect('SUM(client.hashRate)', 'totalHashRate')
|
||||
// .groupBy('client.userAgent')
|
||||
// .orderBy('"totalHashRate"', 'DESC')
|
||||
// .getRawMany();
|
||||
// return result;
|
||||
// }
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user