import { AES , enc } from "crypto-js" ; import { ENCRYPTION_KEY } from "../util/secrets" ; import GoogleAnalytics from "../integrations/GoogleAnalytics" ; import Athena from "../integrations/Athena" ; import Presto from "../integrations/Presto" ; import Redshift from "../integrations/Redshift" ; import Snowflake from "../integrations/Snowflake" ; import Postgres from "../integrations/Postgres" ; import { SourceIntegrationInterface } from "../types/Integration" ; import BigQuery from "../integrations/BigQuery" ; import ClickHouse from "../integrations/ClickHouse" ; import Mixpanel from "../integrations/Mixpanel" ; import { DataSourceInterface , DataSourceParams } from "../../types/datasource" ; import Mysql from "../integrations/Mysql" ; export function decryptDataSourceParams < T = DataSourceParams > ( encrypted ) { return JSON . parse ( AES . decrypt ( encrypted , ENCRYPTION_KEY ) . toString ( enc . Utf8 ) ) ; } export function encryptParams ( params ) { return AES . encrypt ( JSON . stringify ( params ) , ENCRYPTION_KEY ) . toString ( ) ; } export function getNonSensitiveParams ( integration ) { const ret = { ... integration . params } ; integration . getSensitiveParamKeys ( ) . forEach ( ( k ) => { if ( ret [ k ] ) { ret [ k ] = "" ; } } ) ; return ret ; } export function mergeParams ( integration , newParams ) { const secretKeys = integration . getSensitiveParamKeys ( ) ; Object . keys ( newParams ) . forEach ( ( k ) => { if ( secretKeys . includes ( k ) && ! newParams [ k ] ) return ; integration . params [ k ] = newParams [ k ] ; } ) ; } export function getSourceIntegrationObject ( datasource ) { const { type , params , settings } = datasource ; let obj ; if ( type === "athena" ) { obj = new Athena ( params , settings ) ; } else if ( type === "redshift" ) { obj = new Redshift ( params , settings ) ; } else if ( type === "google_analytics" ) { obj = new GoogleAnalytics ( params , settings ) ; } else if ( type === "snowflake" ) { obj = new Snowflake ( params , settings ) ; } else if ( type === "postgres" ) { obj = new Postgres ( params , settings ) ; } else if ( type === "mysql" ) { obj = new Mysql ( params , settings ) ; } else if ( type === "bigquery" ) { obj = new BigQuery ( params , settings ) ; } else if ( type === "clickhouse" ) { obj = new ClickHouse ( params , settings ) ; } else if ( type === "mixpanel" ) { obj = new Mixpanel ( params , settings ) ; } else if ( type === "presto" ) { obj = new Presto ( params , settings ) ; } else { throw new Error ( "Unknown data source type: " + type ) ; } obj . organization = datasource . organization ; obj . datasource = datasource . id ; return obj ; } export async function testDataSourceConnection ( datasource ) { const integration = getSourceIntegrationObject ( datasource ) ; await integration . testConnection ( ) ; }