mirror of
https://github.com/benjamin-wilson/public-pool.git
synced 2026-09-29 17:15:03 -07:00
msic
This commit is contained in:
@@ -1,13 +1,22 @@
|
||||
import { Column, Entity, ManyToOne, PrimaryGeneratedColumn } from 'typeorm';
|
||||
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
|
||||
|
||||
import { ClientEntity } from '../client/client.entity';
|
||||
import { TrackedEntity } from '../utils/TrackedEntity.entity';
|
||||
|
||||
@Entity()
|
||||
export class ClientStatisticsEntity {
|
||||
export class ClientStatisticsEntity extends TrackedEntity {
|
||||
|
||||
@PrimaryGeneratedColumn()
|
||||
id: number;
|
||||
|
||||
@Column({ length: 62, type: 'varchar' })
|
||||
address: string;
|
||||
|
||||
@Column()
|
||||
clientName: string;
|
||||
|
||||
@Column({ length: 8, type: 'varchar' })
|
||||
sessionId: string;
|
||||
|
||||
@Column({ type: 'datetime' })
|
||||
time: Date;
|
||||
|
||||
@@ -15,10 +24,6 @@ export class ClientStatisticsEntity {
|
||||
difficulty: number;
|
||||
|
||||
|
||||
@ManyToOne(
|
||||
() => ClientEntity,
|
||||
client => client.clientStatistics
|
||||
)
|
||||
client: ClientEntity;
|
||||
|
||||
|
||||
}
|
||||
@@ -19,19 +19,19 @@ export class ClientStatisticsService {
|
||||
return await this.clientStatisticsRepository.save(clientStatistic);
|
||||
}
|
||||
|
||||
public async getHashRate(clientId: string) {
|
||||
public async getHashRate(sessionId: string) {
|
||||
|
||||
const query = `
|
||||
SELECT
|
||||
(JULIANDAY(MAX(entry.time)) - JULIANDAY(MIN(entry.time))) * 24 * 60 * 60 AS timeDiff,
|
||||
SUM(entry.difficulty) AS difficultySum
|
||||
FROM
|
||||
client_statistics_entity AS entry
|
||||
WHERE
|
||||
entry.clientId = ?
|
||||
FROM
|
||||
client_statistics_entity AS entry
|
||||
WHERE
|
||||
entry.sessionId = ? AND entry.time > datetime("now", "-1 hour")
|
||||
`;
|
||||
|
||||
const result = await this.clientStatisticsRepository.query(query, [clientId]);
|
||||
const result = await this.clientStatisticsRepository.query(query, [sessionId]);
|
||||
|
||||
const timeDiff = result[0].timeDiff;
|
||||
const difficultySum = result[0].difficultySum;
|
||||
@@ -39,4 +39,32 @@ export class ClientStatisticsService {
|
||||
return (difficultySum * 4294967296) / (timeDiff * 1000000000);
|
||||
|
||||
}
|
||||
|
||||
public async getChartData(sessionId: string) {
|
||||
const query = `
|
||||
WITH result_set AS (
|
||||
SELECT
|
||||
MAX(time) AS label,
|
||||
(SUM(difficulty) * 4294967296) /
|
||||
((JULIANDAY(MAX(time)) - JULIANDAY(MIN(time))) * 24 * 60 * 60 * 1000000000) AS data
|
||||
FROM
|
||||
client_statistics_entity AS entry
|
||||
WHERE
|
||||
entry.sessionId = ? AND entry.time > datetime("now", "-1 day")
|
||||
GROUP BY
|
||||
strftime('%Y-%m-%d %H', time, 'localtime') || (strftime('%M', time, 'localtime') / 5)
|
||||
)
|
||||
SELECT *
|
||||
FROM result_set
|
||||
WHERE label <> (SELECT MAX(Label) FROM result_set);
|
||||
`;
|
||||
|
||||
const result = await this.clientStatisticsRepository.query(query, [sessionId]);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public async deleteAll() {
|
||||
return await this.clientStatisticsRepository.delete({})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user