import * as queryString from 'query-string' ; import { LEGEND } from './constants' ; import { AlarmsConfig , DataStream , DataStreamInfo , MessageOverrides , TableColumn } from '../../utils/dataTypes' ; import { Annotations , Axis , LegendConfig , XAnnotation } from '../../components/charts/common/types' ; export type SearchQueryParams = { alarms ? : AlarmsConfig ; messageOverrides ? : MessageOverrides ; width ? : number | string ; axis ? : Axis . Options ; height ? : number | string ; duration ? : number ; errMsg : string ; viewportStart : Date ; viewportEnd : Date ; componentTag : string ; legend : LegendConfig ; isEditing : boolean ; gestures : boolean ; dataStreamInfos : DataStreamInfo [ ] ; annotations ? : Annotations ; tableColumns : TableColumn [ ] ; dataStreams : DataStream [ ] ; asyncDataStreams : DataStream [ ] ; delayBeforeDataLoads : number ; hasError : boolean ; displayInfoNames : boolean ; } ; const parseBool = ( str ) : boolean => str === 'true' ; const deserializeAnnotations = ( str ) : Annotations => { const annotations = JSON . parse ( str ) as Annotations ; const { x } = annotations ; const deserializeXAnnotation = ( xAnnotation ) : XAnnotation => ( { ... xAnnotation , value : new Date ( xAnnotation . value ) , } ) ; return { ... annotations , x : x != null ? x . map ( deserializeXAnnotation ) : undefined , } ; } ; export const SCREEN_SIZE = { width : 700 , height : 450 , } ; export const constructSearchQuery = ( { viewportStart , viewportEnd , dataStreams , asyncDataStreams , alarms , dataStreamInfos , legend , tableColumns , annotations , messageOverrides , axis , width , height , ... serializableProps } ) : string => queryString . stringify ( { annotations : annotations && JSON . stringify ( annotations ) , legend : legend && JSON . stringify ( legend ) , dataStreamInfos : dataStreamInfos && JSON . stringify ( dataStreamInfos ) , dataStreams : dataStreams && JSON . stringify ( dataStreams ) , asyncDataStreams : asyncDataStreams && JSON . stringify ( asyncDataStreams ) , alarms : alarms && JSON . stringify ( alarms ) , viewportStart : viewportStart && viewportStart . toISOString ( ) , viewportEnd : viewportEnd && viewportEnd . toISOString ( ) , tableColumns : tableColumns && JSON . stringify ( tableColumns ) , messageOverrides : messageOverrides && JSON . stringify ( messageOverrides ) , axis : axis && JSON . stringify ( axis ) , width : width && JSON . stringify ( width ) , height : height && JSON . stringify ( height ) , ... serializableProps , } ) ; const deserializeDataStream = ( almostDataStream ) : DataStream => ( { ... almostDataStream , data : almostDataStream . data . map ( ( point ) => ( { x : Number ( point . x ) , y : point . y } ) ) , } as DataStream ) ; export const testCaseParameters = ( ) : SearchQueryParams => { const query = queryString . parse ( window . location . search ) as any ; return { alarms : query . alarms ? JSON . parse ( query . alarms ) : undefined , width : query . width ? JSON . parse ( query . width ) : undefined , axis : query . axis ? JSON . parse ( query . axis ) : undefined , height : query . height ? JSON . parse ( query . height ) : undefined , duration : query . duration ? JSON . parse ( query . duration ) : undefined , errMsg : query . errMsg , gestures : query . gestures != null ? parseBool ( query . gestures ) : true , delayBeforeDataLoads : query . delayBeforeDataLoads != null ? JSON . parse ( query . delayBeforeDataLoads ) : 0 , dataStreamInfos : query . dataStreamInfos != null ? JSON . parse ( query . dataStreamInfos ) : [ ] , tableColumns : query . tableColumns != null ? JSON . parse ( query . tableColumns ) : [ ] , legend : query . legend != null ? JSON . parse ( query . legend ) : LEGEND , messageOverrides : query . messageOverrides != null ? JSON . parse ( query . messageOverrides ) : undefined , componentTag : query . componentTag != null ? query . componentTag : 'sc-line-chart' , displayInfoNames : query . displayInfoNames ? parseBool ( query . displayInfoNames ) : false , annotations : query . annotations != null ? deserializeAnnotations ( query . annotations ) : undefined , isEditing : query . isEditing != null ? parseBool ( query . isEditing ) : false , hasError : query . hasError != null ? parseBool ( query . hasError ) : false , dataStreams : query . dataStreams != null ? JSON . parse ( query . dataStreams ) . map ( deserializeDataStream ) : [ ] , asyncDataStreams : query . asyncDataStreams != null ? JSON . parse ( query . asyncDataStreams ) . map ( deserializeDataStream ) : [ ] , viewportStart : query . viewportStart != null ? new Date ( query . viewportStart ) : new Date ( 2000 , 0 , 0 ) , viewportEnd : query . viewportEnd != null ? new Date ( query . viewportEnd ) : new Date ( 2000 , 0 , 1 ) , } ; } ;