mirror of
https://github.com/benjamin-wilson/public-pool.git
synced 2026-09-29 17:15:03 -07:00
separate home graph into it's own table
This commit is contained in:
@@ -85,6 +85,34 @@ export class ClientStatisticsService {
|
||||
|
||||
}
|
||||
|
||||
public async getChartDataForSiteSince(date: Date) {
|
||||
|
||||
const query = `
|
||||
SELECT
|
||||
time AS label,
|
||||
ROUND(((SUM(shares) * 4294967296) / 600)) AS data
|
||||
FROM
|
||||
client_statistics_entity AS entry
|
||||
WHERE
|
||||
entry.time > ${date.getTime()}
|
||||
GROUP BY
|
||||
time
|
||||
ORDER BY
|
||||
time
|
||||
LIMIT 144;
|
||||
`;
|
||||
|
||||
const result: any[] = await this.clientStatisticsRepository.query(query);
|
||||
|
||||
|
||||
return result.map(res => {
|
||||
res.label = new Date(parseInt(res.label)).toISOString();
|
||||
return res;
|
||||
}).slice(0, result.length - 1)
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
// public async getHashRateForAddress(address: string) {
|
||||
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
|
||||
|
||||
@Entity()
|
||||
export class HomeGraphEntity {
|
||||
|
||||
@PrimaryGeneratedColumn()
|
||||
id: number;
|
||||
|
||||
@Column({ type: 'bigint' })
|
||||
label: number;
|
||||
|
||||
@Column({ type: 'bigint' })
|
||||
data: number;
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import { Global, Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
|
||||
import { HomeGraphEntity } from './home-graph.entity';
|
||||
import { HomeGraphService } from './home-graph.service';
|
||||
|
||||
|
||||
|
||||
@Global()
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([HomeGraphEntity])],
|
||||
providers: [HomeGraphService],
|
||||
exports: [TypeOrmModule, HomeGraphService],
|
||||
})
|
||||
export class HomeGraphModule { }
|
||||
@@ -0,0 +1,45 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
|
||||
import { HomeGraphEntity } from './home-graph.entity';
|
||||
|
||||
@Injectable()
|
||||
export class HomeGraphService {
|
||||
constructor(
|
||||
@InjectRepository(HomeGraphEntity) private homeGraphRepository: Repository<HomeGraphEntity>
|
||||
) {
|
||||
|
||||
}
|
||||
|
||||
public async getLatestTime(): Promise<Date> {
|
||||
const result = await this.homeGraphRepository.createQueryBuilder('entity')
|
||||
.select('MAX(entity.label)', 'maxNumber')
|
||||
.getRawOne();
|
||||
|
||||
if (result.maxNumber == null) {
|
||||
return new Date(new Date().getTime() - (24 * 60 * 60 * 1000));
|
||||
}
|
||||
|
||||
return new Date(parseInt(result.maxNumber));
|
||||
}
|
||||
|
||||
public async save(graph: { label: number, data: number }[]) {
|
||||
await this.homeGraphRepository.insert(graph);
|
||||
}
|
||||
|
||||
public async getChartDataForSite(limit: number = 144 * 7) {
|
||||
const records = await this.homeGraphRepository
|
||||
.createQueryBuilder('homeGraph')
|
||||
.orderBy('homeGraph.id', 'DESC')
|
||||
.limit(limit)
|
||||
.getRawMany();
|
||||
|
||||
return records.map(res => {
|
||||
return {
|
||||
label: new Date(parseInt(res.homeGraph_label)).toISOString(),
|
||||
data: res.homeGraph_data
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -25,4 +25,19 @@ export class RpcBlockService {
|
||||
public saveBlock(blockHeight: number, data: string) {
|
||||
return this.rpcBlockRepository.update(blockHeight, { data })
|
||||
}
|
||||
|
||||
public async deleteOldBlocks() {
|
||||
const result = await this.rpcBlockRepository.createQueryBuilder('entity')
|
||||
.select('MAX(entity.blockHeight)', 'maxNumber')
|
||||
.getRawOne();
|
||||
|
||||
const newestBlock = result ? result.maxNumber : null;
|
||||
|
||||
await this.rpcBlockRepository.createQueryBuilder()
|
||||
.delete()
|
||||
.where('"blockHeight" < :newestBlock', { newestBlock })
|
||||
.execute();
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user