import { PrometheusClient } from '../infrastructure/prometheus_scraper' ; import { DataUsageByUser , DataUsageTimeframe } from '../model/metrics' ; export interface ManagerMetrics { getOutboundByteTransfer ( timeframe ) : Promise < DataUsageByUser > ; } export class PrometheusManagerMetrics implements ManagerMetrics { constructor ( private prometheusClient ) { } async getOutboundByteTransfer ( timeframe ) { const result = await this . prometheusClient . query ( `sum(increase(shadowsocks_data_bytes{dir=~"c<p|p>t"}[${ timeframe . hours }h])) by (access_key)` ) ; const usage = { } as { [ userId ] : number ; } ; for ( const entry of result . result ) { const bytes = Math . round ( parseFloat ( entry . value [ 1 ] ) ) ; if ( bytes === 0 ) { continue ; } usage [ entry . metric [ 'access_key' ] || '' ] = bytes ; } return { bytesTransferredByUserId : usage } ; } }