-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Trigger metabase db sync on data sync to populate filters (#242)
* Trigger metabase db sync on data sync to populate filters * Inject metabase credentials from .env file Co-authored-by: Yandry Perez Clemente <[email protected]>
- Loading branch information
1 parent
3ccfe5f
commit 535a101
Showing
9 changed files
with
161 additions
and
13 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
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 |
---|---|---|
|
@@ -6,11 +6,14 @@ import {makeBitbucketCommand, runBitbucket} from './bitbucket/run'; | |
import {makeGithubCommand, runGithub} from './github/run'; | ||
import {makeGitlabCommand, runGitlab} from './gitlab/run'; | ||
import {makeJiraCommand, runJira} from './jira/run'; | ||
import {Metabase} from './metabase/metabase-client'; | ||
import {display, terminalLink} from './utils'; | ||
import {runSelect} from './utils/prompts'; | ||
|
||
const DEFAULT_AIRBYTE_URL = 'http://localhost:8000'; | ||
const DEFAULT_METABASE_URL = 'http://localhost:3000'; | ||
const DEFAULT_METABASE_USER = '[email protected]'; | ||
const DEFAULT_METABASE_PASSWORD = 'admin'; | ||
|
||
export function wrapApiError(cause: unknown, msg: string): Error { | ||
// Omit verbose axios error | ||
|
@@ -33,6 +36,12 @@ export async function main(): Promise<void> { | |
.command('pick-source', {isDefault: true, hidden: true}) | ||
.action(async (options) => { | ||
const airbyte = new Airbyte(options.airbyteUrl); | ||
const metabase = await Metabase.fromConfig({ | ||
url: options.metabaseUrl, | ||
username: options.metabaseUsername, | ||
password: options.metabasePassword, | ||
}); | ||
|
||
let done = false; | ||
while (!done) { | ||
const source = await runSelect({ | ||
|
@@ -48,16 +57,16 @@ export async function main(): Promise<void> { | |
}); | ||
switch (source) { | ||
case 'GitHub (Cloud)': | ||
await runGithub({airbyte}); | ||
await runGithub({airbyte, metabase}); | ||
break; | ||
case 'GitLab (Cloud / Server)': | ||
await runGitlab({airbyte}); | ||
await runGitlab({airbyte, metabase}); | ||
break; | ||
case 'Bitbucket (Cloud / Server)': | ||
await runBitbucket({airbyte}); | ||
await runBitbucket({airbyte, metabase}); | ||
break; | ||
case 'Jira (Cloud)': | ||
await runJira({airbyte}); | ||
await runJira({airbyte, metabase}); | ||
break; | ||
case 'I\'m done!': | ||
done = true; | ||
|
@@ -69,6 +78,16 @@ export async function main(): Promise<void> { | |
cmd.option('--airbyte-url <string>', 'Airbyte URL', DEFAULT_AIRBYTE_URL); | ||
cmd | ||
.option('--metabase-url <string>', 'Metabase URL', DEFAULT_METABASE_URL) | ||
.option( | ||
'--metabase-username <string>', | ||
'Metabase username', | ||
DEFAULT_METABASE_USER | ||
) | ||
.option( | ||
'--metabase-password <string>', | ||
'Metabase password', | ||
DEFAULT_METABASE_PASSWORD | ||
) | ||
.hook('postAction', async (thisCommand) => { | ||
display( | ||
`Check out your metrics in ${await terminalLink( | ||
|
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import axios, {AxiosInstance} from 'axios'; | ||
import {VError} from 'verror'; | ||
|
||
export function wrapApiError(cause: unknown, msg: string): Error { | ||
// Omit verbose axios error | ||
const truncated = new VError((cause as Error).message); | ||
return new VError(truncated, msg); | ||
} | ||
|
||
export interface MetabaseConfig { | ||
readonly url: string; | ||
readonly username: string; | ||
readonly password: string; | ||
} | ||
|
||
export class Metabase { | ||
constructor(private readonly api: AxiosInstance) {} | ||
|
||
static async fromConfig(cfg: MetabaseConfig): Promise<Metabase> { | ||
const token = await Metabase.sessionToken(cfg); | ||
const api = axios.create({ | ||
baseURL: `${cfg.url}/api`, | ||
headers: { | ||
'X-Metabase-Session': token, | ||
}, | ||
}); | ||
return new Metabase(api); | ||
} | ||
|
||
private static async sessionToken(cfg: MetabaseConfig): Promise<string> { | ||
const {url, username, password} = cfg; | ||
try { | ||
const {data} = await axios.post(`${url}/api/session`, { | ||
username, | ||
password, | ||
}); | ||
return data.id; | ||
} catch (err) { | ||
throw wrapApiError(err, 'failed to get session token'); | ||
} | ||
} | ||
|
||
async forceSync(): Promise<any> { | ||
try { | ||
// Objective is to get filter values populated | ||
// Faros is always has DB id 2 | ||
// note that API call rescan_value was not sufficient | ||
// hence the call to sync_schema instead | ||
// https://www.metabase.com/docs/latest/api/database | ||
const {data} = await this.api.post('database/2/sync_schema'); | ||
return data; | ||
} catch (err) { | ||
throw wrapApiError(err, 'unable to trigger rescan'); | ||
} | ||
} | ||
} |
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