import { decryptDataSourceParams } from "../services/datasource" ; import { ClickHouse as ClickHouseClient } from "clickhouse" ; import SqlIntegration from "./SqlIntegration" ; import { ClickHouseConnectionParams } from "../../types/integrations/clickhouse" ; export default class ClickHouse extends SqlIntegration { params : ClickHouseConnectionParams ; setParams ( encryptedParams ) { this . params = decryptDataSourceParams < ClickHouseConnectionParams > ( encryptedParams ) ; if ( this . params . user ) { this . params . username = this . params . user ; delete this . params . user ; } if ( this . params . host ) { this . params . url = this . params . host ; delete this . params . host ; } } getSensitiveParamKeys ( ) { return [ "password" ] ; } async runQuery ( sql ) { const client = new ClickHouseClient ( { url : this . params . url , port : this . params . port , basicAuth : this . params . username ? { username : this . params . username , password : this . params . password , } : null , format : "json" , debug : false , raw : false , config : { database : this . params . database , } , reqParams : { headers : { "x-clickhouse-format" : "JSON" , } , } , } ) ; return Array . from ( await client . query ( sql ) . toPromise ( ) ) ; } toTimestamp ( date ) { return `toDateTime('${ date . toISOString ( ) . substr ( 0 , 19 ) . replace ( "T" , " " ) }')` ; } addHours ( col , hours ) { return `dateAdd(hour, ${ hours }, ${ col })` ; } subtractHalfHour ( col ) { return `dateSub(minute, 30, ${ col })` ; } regexMatch ( col , regex ) { return `match(${ col }, '${ regex . replace ( /\\/g , "\\\\" ) }')` ; } percentile ( col , percentile ) { return `quantile(${ percentile })(${ col })` ; } dateTrunc ( col ) { return `dateTrunc('day', ${ col })` ; } dateDiff ( startCol , endCol ) { return `dateDiff('day', ${ startCol }, ${ endCol })` ; } stddev ( col ) { return `stddevSamp(${ col })` ; } formatDate ( col ) { return `formatDateTime(${ col }, '%F')` ; } ifElse ( condition , ifTrue , ifFalse ) { return `if(${ condition }, ${ ifTrue }, ${ ifFalse })` ; } castToString ( col ) { return `toString(${ col })` ; } }