Ordisrespector-pool-ui/src/app/components/dashboard/dashboard.component.ts
2023-06-26 18:19:38 -04:00

158 lines
4.2 KiB
TypeScript

import { Component } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { map, Observable, shareReplay } from 'rxjs';
import { ClientService } from '../../services/client.service';
@Component({
selector: 'app-dashboard',
templateUrl: './dashboard.component.html',
styleUrls: ['./dashboard.component.scss']
})
export class DashboardComponent {
public address: string;
public clientInfo$: Observable<any>;
public chartData$: Observable<any>;
public chartOptions: any;
constructor(private clientService: ClientService, private route: ActivatedRoute) {
this.address = this.route.snapshot.params['address'];
this.clientInfo$ = this.clientService.getClientInfo(this.address).pipe(
shareReplay({ refCount: true, bufferSize: 1 })
)
const documentStyle = getComputedStyle(document.documentElement);
const textColor = documentStyle.getPropertyValue('--text-color');
const textColorSecondary = documentStyle.getPropertyValue('--text-color-secondary');
const surfaceBorder = documentStyle.getPropertyValue('--surface-border');
this.chartData$ = this.clientInfo$.pipe(
map((workerInfo: any) => {
const GROUP_SIZE = 24; //12 = 1 hour
let chartData: any[] = workerInfo.chartData;
let hourlyData = [];
for (let i = GROUP_SIZE; i < chartData.length; i += GROUP_SIZE) {
let sum = 0;
for (let j = GROUP_SIZE - 1; j >= 0; j--) {
sum += chartData[i - j].data;
}
sum = sum / GROUP_SIZE;
hourlyData.push({ y: sum, x: chartData[i].label });
}
const data = workerInfo.chartData.map((d: any) => { return { y: d.data, x: d.label } });
return {
labels: workerInfo.chartData.map((d: any) => d.label),
datasets: [
{
type: 'line',
label: '2 Hour',
data: hourlyData,
fill: false,
backgroundColor: documentStyle.getPropertyValue('--primary-color'),
borderColor: documentStyle.getPropertyValue('--primary-color'),
tension: .4
},
{
type: 'line',
label: '5 Minute',
data: data,
fill: false,
backgroundColor: documentStyle.getPropertyValue('--bluegray-700'),
borderColor: documentStyle.getPropertyValue('--bluegray-700'),
tension: .4
},
]
}
})
);
this.chartOptions = {
plugins: {
legend: {
labels: {
color: textColor
}
}
},
scales: {
x: {
type: 'time',
time: {
unit: 'hour', // Set the unit to 'minute'
},
ticks: {
color: textColorSecondary
},
grid: {
color: surfaceBorder,
drawBorder: false,
display: true
}
},
y: {
ticks: {
color: textColorSecondary,
callback: (value: number) => value + ' GH/s',
},
grid: {
color: surfaceBorder,
drawBorder: false
}
}
}
};
}
public getSessionCount(name: string, workers: any[]) {
const workersByName = workers.filter(w => w.name == name);
return workersByName.length;
}
public getTotalHashRate(name: string, workers: any[]) {
const workersByName = workers.filter(w => w.name == name);
const sum = workersByName.reduce((pre, cur, idx, arr) => {
return pre += cur.hashRate;
}, 0);
return Math.floor(sum);
}
public getBestDifficulty(name: string, workers: any[]) {
const workersByName = workers.filter(w => w.name == name);
const best = workersByName.reduce((pre, cur, idx, arr) => {
if (cur.bestDifficulty > pre) {
return cur.bestDifficulty;
}
return pre;
}, 0);
return best;
}
public getTotalUptime(name: string, workers: any[]) {
const now = new Date().getTime();
const workersByName = workers.filter(w => w.name == name);
const sum = workersByName.reduce((pre, cur, idx, arr) => {
return pre += now - new Date(cur.startTime).getTime();
}, 0);
return new Date(now - sum);
}
}