import * as Boom from 'boom' ; import { Request , RequestQuery } from 'hapi' ; import { WatcherModel , IWatcherModel } from '../../../_core/Watcher/model' ; import { IWatcherConfig , Watcher } from '../../../_core/Watcher/Watcher' ; import { watcherClasses } from '../../../_core/Watcher/exports' ; import { logger } from '../../../logger' ; import { restartWatchers , watcherConfigurationExist , stopWatchers } from './helpers' ; import { MEASUREMENT_OHLC } from '../../../_core/Influx/constants' ; export class export class Watchers { public static runningWatchers : Watcher [ ] = [ ] ; static param { Request } request returns { Promise < any > } memberof public static async restartAllWatchers ( request ) { try { await restartWatchers ( ( < any > request . server . app ) . influx ) ; return { msg : '' } ; } catch ( error ) { logger . error ( error ) ; throw Boom . internal ( error ) ; } } public static async stopAllWatchers ( request ) { try { await stopWatchers ( ) ; return { msg : '' } ; } catch ( error ) { logger . error ( error ) ; throw Boom . internal ( error ) ; } } public static async getWatchers ( ) { try { const watchers = await WatcherModel . find ( ) ; return watchers . map ( ( w ) => < IWatcherConfig > w . _doc ) ; } catch ( error ) { logger . error ( error ) ; throw Boom . internal ( error ) ; } } public static async deleteWatchers ( request ) { try { for ( const watcher of Watchers . runningWatchers ) { await watcher . stop ( ) ; } await WatcherModel . collection . drop ( ) ; Watchers . runningWatchers = [ ] ; return { msg : '' } ; } catch ( error ) { logger . error ( error ) ; return Boom . internal ( error ) ; } } public static async getWatcher ( request ) { try { const { id } = request . params ; const watcher = await WatcherModel . findOne ( { id } ) ; if ( ! watcher ) { return Boom . notFound ( `Watcher ${ id } not found` ) ; } return watcher ; } catch ( error ) { logger . error ( error ) ; throw Boom . internal ( error ) ; } } public static async createWatcher ( request ) { try { const watcherConfig = < IWatcherConfig > request . payload ; if ( watcherConfig . id ) { return Boom . badRequest ( 'Cannot create a new watcher with an id' ) ; } if ( ! ( < any > watcherClasses ) [ watcherConfig . type ] ) { return Boom . badRequest ( `Watcher type ${ watcherConfig . type } doesn't exists` ) ; } if ( await watcherConfigurationExist ( watcherConfig ) ) { return Boom . badRequest ( `Watcher configuration already exist` ) ; } const watcher = new ( < any > watcherClasses ) [ watcherConfig . type ] ( watcherConfig ) ; try { await watcher . init ( ) ; } catch ( error ) { return Boom . badRequest ( error ) ; } watcher . setInflux ( ( < any > request . server . app ) . influx ) ; watcher . run ( ) . catch ( error => { throw error ; } ) ; Watchers . runningWatchers . push ( watcher ) ; return { id : watcher . id } ; } catch ( error ) { logger . error ( error ) ; return Boom . internal ( error ) ; } } public static async startWatcher ( request ) { try { const { id } = request . params ; const running = Watchers . runningWatchers . find ( w => w . id === id ) ; if ( running ) return { msg : 'Watcher already running' } ; const watcher = await WatcherModel . findOne ( { id } ) ; if ( ! watcher ) return Boom . notFound ( `Watcher ${ id } not found` ) ; const instance = < Watcher > new ( < any > watcherClasses ) [ watcher . type ] ( ( < any > watcher ) . _doc ) ; await instance . init ( ) ; instance . setInflux ( ( < any > request . server . app ) . influx ) ; instance . run ( ) . catch ( error => { throw error ; } ) ; Watchers . runningWatchers . push ( instance ) ; return { msg : `Watcher ${ id } started` } ; } catch ( error ) { logger . error ( error ) ; return Boom . internal ( error ) ; } } public static async stopWatcher ( request ) { try { const { id } = request . params ; const running = Watchers . runningWatchers . find ( w => w . id === id ) ; if ( running ) { await running . stop ( ) ; const runningIdx = Watchers . runningWatchers . map ( w => w . id ) . indexOf ( running . id ) ; Watchers . runningWatchers . splice ( runningIdx , 1 ) ; } return { msg : `Watcher ${ id } stopped` } ; } catch ( error ) { logger . error ( error ) ; return Boom . internal ( error ) ; } } public static async deleteWatcher ( request ) { try { const { id } = request . params ; const { flush } = request . query as RequestQuery ; const running = Watchers . runningWatchers . find ( w => w . id === id ) ; if ( running ) { await running . stop ( Boolean ( flush ) === true ? true : false ) ; const runningIdx = Watchers . runningWatchers . map ( w => w . id ) . indexOf ( running . id ) ; Watchers . runningWatchers . splice ( runningIdx , 1 ) ; } await WatcherModel . findOneAndDelete ( { id } ) ; return { msg : `Watcher ${ id } stopped` } ; } catch ( error ) { logger . error ( error ) ; return Boom . internal ( error ) ; } } }