import mongoose from "mongoose" ; import { DataSourceInterface , DataSourceParams , DataSourceSettings , DataSourceType , } from "../../types/datasource" ; import { GoogleAnalyticsParams } from "../../types/integrations/googleanalytics" ; import { getOauth2Client } from "../integrations/GoogleAnalytics" ; import { encryptParams , testDataSourceConnection , } from "../services/datasource" ; import uniqid from "uniqid" ; import { usingFileConfig , getConfigDatasources } from "../init/config" ; const dataSourceSchema = new mongoose . Schema ( { id : String , name : String , organization : { type : String , index : true , } , dateCreated : Date , dateUpdated : Date , type : { type : String } , params : String , settings : { notebookRunQuery : String , queries : { experimentsQuery : String , pageviewsQuery : String , } , events : { experimentEvent : String , experimentIdProperty : String , variationIdProperty : String , pageviewEvent : String , urlProperty : String , } , experimentDimensions : [ String ] , default : { timestampColumn : String , userIdColumn : String , anonymousIdColumn : String , } , experiments : { table : String , timestampColumn : String , userIdColumn : String , anonymousIdColumn : String , experimentIdColumn : String , variationColumn : String , } , users : { table : String , userIdColumn : String , } , identifies : { table : String , userIdColumn : String , anonymousIdColumn : String , } , pageviews : { table : String , urlColumn : String , timestampColumn : String , userIdColumn : String , anonymousIdColumn : String , } , } , } ) ; dataSourceSchema . index ( { id : 1 , organization : 1 } , { unique : true } ) ; type DataSourceDocument = mongoose . Document & DataSourceInterface ; const DataSourceModel = mongoose . model < DataSourceDocument > ( "DataSource" , dataSourceSchema ) ; function toInterface ( doc ) { return doc . toJSON ( ) ; } export async function getDataSourcesByOrganization ( organization ) { if ( usingFileConfig ( ) ) { return getConfigDatasources ( organization ) ; } return ( await DataSourceModel . find ( { organization , } ) ) . map ( toInterface ) ; } export async function getDataSourceById ( id , organization ) { if ( usingFileConfig ( ) ) { return ( getConfigDatasources ( organization ) . filter ( ( d ) => d . id === id ) [ 0 ] || null ) ; } const doc = await DataSourceModel . findOne ( { id , organization , } ) ; return doc ? toInterface ( doc ) : null ; } export async function getOrganizationsWithDatasources ( ) { if ( usingFileConfig ( ) ) { return [ ] ; } return await DataSourceModel . distinct ( "organization" ) ; } export async function deleteDatasourceById ( id , organization ) { if ( usingFileConfig ( ) ) { throw new Error ( "Cannot delete. Data sources managed by config.yml" ) ; } await DataSourceModel . deleteOne ( { id , organization , } ) ; } export async function createDataSource ( organization , name , type , params , settings , id ? ) { if ( usingFileConfig ( ) ) { throw new Error ( "Cannot add. Data sources managed by config.yml" ) ; } id = id || uniqid ( "ds_" ) ; if ( type === "google_analytics" ) { const oauth2Client = getOauth2Client ( ) ; const { tokens } = await oauth2Client . getToken ( ( params as GoogleAnalyticsParams ) . refreshToken ) ; ( params as GoogleAnalyticsParams ) . refreshToken = tokens . refresh_token || "" ; } const datasource = { id , name , organization , type , settings , dateCreated : new Date ( ) , dateUpdated : new Date ( ) , params : encryptParams ( params ) , } ; await testDataSourceConnection ( datasource ) ; const model = await DataSourceModel . create ( datasource ) ; return toInterface ( model ) ; } export async function updateDataSource ( id , organization , updates ) { if ( usingFileConfig ( ) ) { throw new Error ( "Cannot update. Data sources managed by config.yml" ) ; } await DataSourceModel . updateOne ( { id , organization , } , { $set : updates , } ) ; }