Skip to content

Commit

Permalink
chore: get canner endpoint based on the environment
Browse files Browse the repository at this point in the history
  • Loading branch information
onlyjackfrost committed May 29, 2023
1 parent 2b06d37 commit 0b0de36
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 32 deletions.
81 changes: 49 additions & 32 deletions packages/extension-driver-canner/src/lib/cannerAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,46 @@
import axios from 'axios';
import { PGOptions } from './cannerDataSource';
import { ConnectionOptions } from 'tls';
import { createEnvConfig } from './config';

const envConfig = createEnvConfig();

export class CannerAdapter {
public readonly host: string;
public readonly workspaceSqlName: string;
public readonly PAT: string | (() => string | Promise<string>);
public readonly ssl: boolean | ConnectionOptions;
private baseUrl: string | undefined;

constructor(options?: PGOptions) {
if (!options) {
throw new Error(`connection options is required`);
}
const { host, database, password } = options;
const { host, database, password, ssl = false } = options;
if (!host || !database || !password) {
throw new Error(`host, database and password are required`);
}
this.host = host;
this.workspaceSqlName = database;
this.PAT = password;
this.ssl = ssl;
}

private async prepare() {
if (this.baseUrl) {
return;
}
const response = await axios({
method: 'get',
maxBodyLength: Infinity,
url: `https://${this.host}/cluster-info`,
headers: {},
public async createAsyncQueryResultUrls(sql: string): Promise<string[]> {
const data = await this.workspaceRequest('post', '/v2/async-queries', {
data: {
sql,
timeout: 600,
noLimit: true,
},
});
const { restfulApiBaseEndpoint } = response.data;
if (!restfulApiBaseEndpoint) {
throw new Error(`restfulApiBaseEndpoint is not found`);
if (data.error.message) {
throw new Error(data.error.message);
}

this.baseUrl = restfulApiBaseEndpoint;
const { id: requestId } = data;
await this.waitAsyncQueryToFinish(requestId);
const urls = await this.getAsyncQueryResultUrls(requestId);
return urls;
}

private async workspaceRequest(
Expand All @@ -58,6 +63,35 @@ export class CannerAdapter {
return response.data;
}

private async prepare() {
if (this.baseUrl) {
return;
}
const response = await axios({
method: 'get',
maxBodyLength: Infinity,
url: `${this.getCannerUrl()}/cluster-info`,
headers: {},
});
const { restfulApiBaseEndpoint } = response.data;
if (!restfulApiBaseEndpoint) {
throw new Error(`restfulApiBaseEndpoint is not found`);
}

this.baseUrl = restfulApiBaseEndpoint;
}

private getCannerUrl() {
if (envConfig.isInternal) {
// use env to get the endpoint in k8s
return `http://${envConfig.webServiceHost}`;
} else {
// otherwise use the host user provided
const protocol = this.ssl ? 'https' : 'http';
return `${protocol}://${this.host}`;
}
}

private async waitAsyncQueryToFinish(requestId: string) {
let response = await this.workspaceRequest(
'get',
Expand All @@ -83,21 +117,4 @@ export class CannerAdapter {
);
return data.urls || [];
}

public async createAsyncQueryResultUrls(sql: string): Promise<string[]> {
const data = await this.workspaceRequest('post', '/v2/async-queries', {
data: {
sql,
timeout: 600,
noLimit: true,
},
});
if (data.error.message) {
throw new Error(data.error.message);
}
const { id: requestId } = data;
await this.waitAsyncQueryToFinish(requestId);
const urls = await this.getAsyncQueryResultUrls(requestId);
return urls;
}
}
13 changes: 13 additions & 0 deletions packages/extension-driver-canner/src/lib/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export interface IEnvConfig {
// indicates whether the extension is running in k8s
isInternal?: boolean;
// the host of the web service
webServiceHost?: string;
}

export const createEnvConfig = (): IEnvConfig => {
return {
isInternal: Boolean(process.env['IS_INTERNAL']) || false,
webServiceHost: process.env['WEB_SERVICE_HOST'],
} as IEnvConfig;
};

0 comments on commit 0b0de36

Please sign in to comment.