Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(feat) [nan-591] insert into bigQuery (#1903)
## Describe your changes Add in BigQuery data ingestion from sync, actions, and webhooks ## Issue ticket number and link NAN-591 ## Checklist before requesting a review (skip if just adding/editing APIs & templates) - [ ] I added tests, otherwise the reason is: - [ ] I added observability, otherwise the reason is: - [ ] I added analytics, otherwise the reason is:
- Loading branch information
1 parent
8a3e0f9
commit 98bb60c
Showing
20 changed files
with
643 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
tsconfig.tsbuildinfo | ||
dist/* | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { BigQuery } from '@google-cloud/bigquery'; | ||
import type { BigQuery as BigQueryType } from '@google-cloud/bigquery'; | ||
import { getLogger } from '@nangohq/utils/dist/logger.js'; | ||
import { isCloud } from '@nangohq/utils/dist/environment/detection.js'; | ||
|
||
const logger = getLogger('BigQueryClient'); | ||
|
||
interface RunScriptRow { | ||
executionType: string; | ||
internalConnectionId: number | undefined; | ||
connectionId: string; | ||
accountId: number | undefined; | ||
scriptName: string; | ||
scriptType: string; | ||
environmentId: number; | ||
providerConfigKey: string; | ||
status: string; | ||
syncId: string; | ||
content: string; | ||
runTimeInSeconds: number; | ||
createdAt: number; | ||
} | ||
|
||
class BigQueryClient { | ||
private client: BigQuery; | ||
private datasetName: string; | ||
private tableName: string; | ||
|
||
constructor({ datasetName, tableName }: { datasetName: string; tableName: string }) { | ||
this.client = new BigQuery(); | ||
this.tableName = tableName; | ||
this.datasetName = datasetName; | ||
} | ||
|
||
static async createInstance({ datasetName, tableName }: { datasetName?: string; tableName: string }) { | ||
const instance = new BigQueryClient({ | ||
datasetName: datasetName || 'raw', | ||
tableName | ||
}); | ||
await instance.initialize(); | ||
return instance; | ||
} | ||
|
||
private async initialize() { | ||
try { | ||
if (isCloud) { | ||
await this.createDataSet(); | ||
await this.createTable(); | ||
} | ||
} catch (e) { | ||
logger.error('Error initializing', e); | ||
} | ||
} | ||
|
||
private async createDataSet() { | ||
const dataset = this.client.dataset(this.datasetName); | ||
const [exists] = await dataset.exists(); | ||
if (!exists) { | ||
await this.client.createDataset(this.datasetName); | ||
} | ||
} | ||
|
||
private async createTable() { | ||
const table = this.client.dataset(this.datasetName).table(this.tableName); | ||
const [exists] = await table.exists(); | ||
if (!exists) { | ||
await table.create({ | ||
schema: { | ||
fields: [ | ||
{ name: 'executionType', type: 'STRING' }, | ||
{ name: 'internalConnectionId', type: 'INTEGER' }, | ||
{ name: 'connectionId', type: 'STRING' }, | ||
{ name: 'accountId', type: 'INTEGER' }, | ||
{ name: 'scriptName', type: 'STRING' }, | ||
{ name: 'scriptType', type: 'STRING' }, | ||
{ name: 'environmentId', type: 'INTEGER' }, | ||
{ name: 'providerConfigKey', type: 'STRING' }, | ||
{ name: 'status', type: 'STRING' }, | ||
{ name: 'syncId', type: 'STRING' }, | ||
{ name: 'content', type: 'STRING' }, | ||
{ name: 'runTimeInSeconds', type: 'FLOAT' }, | ||
{ name: 'createdAt', type: 'INTEGER' } | ||
] | ||
} | ||
}); | ||
} | ||
} | ||
|
||
public async insert(data: RunScriptRow, tableName?: string) { | ||
const table = tableName || this.tableName; | ||
try { | ||
if (isCloud) { | ||
await this.client.dataset(this.datasetName).table(table).insert(data); | ||
} | ||
} catch (e) { | ||
logger.error('Error inserting into BigQuery', e); | ||
} | ||
} | ||
} | ||
|
||
export { BigQueryClient, BigQueryType }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"name": "@nangohq/data-ingestion", | ||
"version": "1.0.0", | ||
"description": "Package to ingest Nango data for analytics", | ||
"type": "module", | ||
"main": "dist/index.js", | ||
"typings": "dist/index.d.ts", | ||
"scripts": { | ||
"build": "rimraf ./dist && tsc" | ||
}, | ||
"keywords": [], | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/NangoHQ/nango.git", | ||
"directory": "packages/utils" | ||
}, | ||
"license": "SEE LICENSE IN LICENSE FILE IN GIT REPOSITORY", | ||
"dependencies": { | ||
"@google-cloud/bigquery": "7.5.1", | ||
"@nangohq/utils": "file:../utils" | ||
}, | ||
"devDependencies": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"rootDir": "lib", | ||
"outDir": "dist" | ||
}, | ||
"include": ["lib/**/*"], | ||
"references": [ | ||
{ | ||
"path": "../utils" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,9 @@ | |
}, | ||
{ | ||
"path": "../utils" | ||
}, | ||
{ | ||
"path": "../data-ingestion" | ||
} | ||
], | ||
"include": ["lib/**/*"] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"watch": ["lib", "../shared/dist", "../utils/dist", "../../.env"], | ||
"ext": "ts,json", | ||
"ext": "js,ts,json", | ||
"ignore": ["lib/**/*.test.ts"], | ||
"exec": "tsc && tsx -r dotenv/config lib/app.ts dotenv_config_path=./../../.env" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"watch": ["lib", "../shared/dist", "../utils/dist", "../../.env"], | ||
"ext": "ts,json", | ||
"ext": "js,ts,json", | ||
"ignore": ["lib/**/*.test.ts"], | ||
"exec": "tsc && tsx -r dotenv/config lib/app.ts dotenv_config_path=./../../.env" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"watch": ["lib", "../shared/dist", "../utils/dist", "../../.env", "../shared/providers.yaml"], | ||
"ext": "ts,json", | ||
"ext": "js,ts,json", | ||
"ignore": ["src/**/*.spec.ts"], | ||
"exec": "tsx -r dotenv/config lib/server.ts Dotenv_config_path=./../../.env" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.