import { PrometheusClient , QueryResultData } from '../../infrastructure/prometheus_scraper' ; import { DataUsageByUser } from '../../model/metrics' ; import { ShadowsocksAccessKey , ShadowsocksServer } from '../../model/shadowsocks_server' ; import { TextFile } from '../../model/text_file' ; export class InMemoryFile implements TextFile { private savedText : string ; constructor ( private exists ) { } readFileSync ( ) { if ( this . exists ) { return this . savedText ; } else { const err = new Error ( 'no such file or directory' ) ; ( err as any ) . code = 'ENOENT' ; throw err ; } } writeFileSync ( text ) { this . savedText = text ; this . exists = true ; } } export class FakeShadowsocksServer implements ShadowsocksServer { private accessKeys : ShadowsocksAccessKey [ ] = [ ] ; update ( keys ) { this . accessKeys = keys ; return Promise . resolve ( ) ; } getAccessKeys ( ) { return this . accessKeys ; } } export class FakePrometheusClient extends PrometheusClient { constructor ( public bytesTransferredById ) { super ( '' ) ; } async query ( query ) { const queryResultData = { result : [ ] } as QueryResultData ; for ( const accessKeyId of Object . keys ( this . bytesTransferredById ) ) { const bytesTransferred = this . bytesTransferredById [ accessKeyId ] || 0 ; queryResultData . result . push ( { metric : { 'access_key' : accessKeyId } , value : [ bytesTransferred , `${ bytesTransferred }` ] } ) ; } return queryResultData ; } }