@ -8,32 +8,28 @@ import { queryByMetric } from 'grafana-datasource-kit';
import * as _ from 'lodash' ;
declare type UnitTime = {
unit : AnalyticUnit.AnalyticUnit ,
time : number
} ;
type MetricDataChunk = { values : [ number , number ] [ ] , columns : string [ ] } ;
const PULL_PERIOD_MS = 5000 ;
export class DataPuller {
private PULL_PERIOD_MS : number = 5000 ;
private _interval : number = 1000 ;
private _timer : any = null ;
private _unitTimes : { [ id : string ] : UnitTime } = { } ;
private _unitTimes : { [ analyticUnitId : string ] : number } = { } ;
constructor ( private analyticsService : AnalyticsService ) { } ;
constructor ( private analyticsService : AnalyticsService ) { } ;
public addUnit ( unit : AnalyticUnit.AnalyticUnit ) {
let time = unit . lastDetectionTime || Date . now ( ) ;
let unitTime : UnitTime = { unit , time } ;
this . _unitTimes [ unit . id ] = unitTime ;
public addUnit ( analyticUnit : AnalyticUnit.AnalyticUnit ) {
this . _runAnalyticUnitPuller ( analyticUnit ) ;
}
public deleteUnit ( id : AnalyticUnit.AnalyticUnitId ) {
delete this . _unitTimes [ id ] ;
public deleteUnit ( analyticUnitId : AnalyticUnit.AnalyticUnitId ) {
if ( _ . has ( this . _unitTimes , analyticUnitId ) ) {
delete this . _unitTimes [ analyticUnitId ] ;
}
}
private pullData ( unit : AnalyticUnit.AnalyticUnit , from : number , to : number ) {
if ( ! unit ) {
private async pullData ( unit : AnalyticUnit.AnalyticUnit , from : number , to : number ) : Promise < MetricDataChunk > {
if ( unit === undefined ) {
throw Error ( ` puller: can't pull undefined unit ` ) ;
}
return queryByMetric ( unit . metric , unit . panelUrl , from , to , HASTIC_API_KEY ) ;
@ -48,46 +44,65 @@ export class DataPuller {
}
//TODO: group analyticUnits by panelID and send same dataset for group
public runPuller() {
this . _timer = setTimeout ( this . puller . bind ( this ) , this . _interval ) ;
public async runPuller() {
const analyticUnits = await AnalyticUnit . findMany ( { alert : true } ) ;
_ . each ( analyticUnits , analyticUnit = > {
this . _runAnalyticUnitPuller ( analyticUnit ) ;
} ) ;
console . log ( 'Data puller runned' ) ;
}
public stopPuller() {
if ( this . _timer ) {
clearTimeout ( this . _timer ) ;
this . _timer = null ;
this . _interval = 0 ;
console . log ( 'Data puller stopped' ) ;
this . _unitTimes = { } ;
}
console . log ( 'Data puller already stopped' ) ;
private async _runAnalyticUnitPuller ( analyticUnit : AnalyticUnit.AnalyticUnit ) {
const time = analyticUnit . lastDetectionTime || Date . now ( ) ;
this . _unitTimes [ analyticUnit . id ] = time ;
const dataGenerator = this . getDataGenerator (
analyticUnit , PULL_PERIOD_MS
) ;
for await ( const data of dataGenerator ) {
if ( ! _ . has ( this . _unitTimes , analyticUnit . id ) ) {
break ;
}
private async puller() {
if ( data . values . length === 0 ) {
continue ;
}
if ( _ . isEmpty ( this . _unitTimes ) ) {
this . _interval = this . PULL_PERIOD_MS ;
this . _timer = setTimeout ( this . puller . bind ( this ) , this . _interval ) ;
return ;
const now = Date . now ( ) ;
let payload = { data , from : time , to : now , pattern : analyticUnit.type } ;
this . _unitTimes [ analyticUnit . id ] = now ;
this . pushData ( analyticUnit , payload ) ;
}
}
let now = Date . now ( ) ;
async * getDataGenerator ( analyticUnit : AnalyticUnit.AnalyticUnit , duration : number ) :
AsyncIterableIterator < MetricDataChunk > {
_ . forOwn ( this . _unitTimes , async value = > {
if ( ! value . unit . alert ) {
return ;
const getData = async ( ) = > {
try {
const time = this . _unitTimes [ analyticUnit . id ]
const now = Date . now ( ) ;
return await this . pullData ( analyticUnit , time , now ) ;
} catch ( err ) {
throw new Error ( ` Error while pulling data: ${ err . message } ` ) ;
}
let data = await this . pullData ( value . unit , value . time , now ) ;
if ( data . values . length === 0 ) {
return ;
}
let payload = { data , from : value . time , to : now } ;
value . time = now ;
this . pushData ( value . unit , payload ) ;
} ) ;
const timeout = async ( ) = > new Promise (
resolve = > setTimeout ( resolve , duration )
) ;
this . _timer = setTimeout ( this . puller . bind ( this ) , this . _interval ) ;
while ( true ) {
yield await getData ( ) ;
await timeout ( ) ;
}
}
}