|
| 1 | +import { DataSourcePlugin } from "openblocks-sdk/dataSource"; |
| 2 | +import dataSourceConfig, { DataSourceDataType } from "./dataSourceConfig"; |
| 3 | +import queryConfig, { ActionDataType } from "./queryConfig"; |
| 4 | +import { |
| 5 | + GetQueryExecutionCommandOutput, |
| 6 | + QueryExecutionState, |
| 7 | + ResultSet, |
| 8 | + AthenaClient, |
| 9 | + GetQueryExecutionCommand, |
| 10 | + GetQueryResultsCommand, |
| 11 | + StartQueryExecutionCommand, |
| 12 | +} from "@aws-sdk/client-athena"; |
| 13 | +import _ from "lodash"; |
| 14 | +import { ServiceError } from "../../common/error"; |
| 15 | + |
| 16 | +function parseResultSet(resultSet?: ResultSet) { |
| 17 | + if (!resultSet) { |
| 18 | + return []; |
| 19 | + } |
| 20 | + const rows: any[] = []; |
| 21 | + const [header, ...data] = resultSet.Rows || []; |
| 22 | + const columns = header.Data?.map((i) => i.VarCharValue) || []; |
| 23 | + data?.forEach((row) => { |
| 24 | + const entries: [string, string][] = []; |
| 25 | + row.Data?.forEach((data, idx) => { |
| 26 | + const column = columns[idx]; |
| 27 | + if (!column) { |
| 28 | + entries.push([`_col_${idx}`, data.VarCharValue || ""]); |
| 29 | + } else { |
| 30 | + entries.push([column, data.VarCharValue || ""]); |
| 31 | + } |
| 32 | + }); |
| 33 | + rows.push(Object.fromEntries(entries)); |
| 34 | + }); |
| 35 | + return rows; |
| 36 | +} |
| 37 | + |
| 38 | +// fixme: should use the timeout config filled in frontend |
| 39 | +const timeout = 1000 * 60; // 60s |
| 40 | + |
| 41 | +const athenaPlugin: DataSourcePlugin<ActionDataType, DataSourceDataType> = { |
| 42 | + id: "athena", |
| 43 | + name: "Athena", |
| 44 | + category: "api", |
| 45 | + icon: "athena.svg", |
| 46 | + dataSourceConfig, |
| 47 | + queryConfig, |
| 48 | + run: async function (actionData, dataSourceConfig): Promise<any> { |
| 49 | + const { accessKey, secretKey, region } = dataSourceConfig; |
| 50 | + const client = new AthenaClient({ |
| 51 | + credentials: { |
| 52 | + accessKeyId: accessKey, |
| 53 | + secretAccessKey: secretKey, |
| 54 | + }, |
| 55 | + region, |
| 56 | + }); |
| 57 | + if (actionData.actionName === "Query") { |
| 58 | + const startRet = await client.send( |
| 59 | + new StartQueryExecutionCommand({ |
| 60 | + QueryString: actionData.queryString, |
| 61 | + ResultConfiguration: { |
| 62 | + OutputLocation: dataSourceConfig.s3Location, |
| 63 | + }, |
| 64 | + }) |
| 65 | + ); |
| 66 | + |
| 67 | + const start = Date.now(); |
| 68 | + |
| 69 | + let execution: GetQueryExecutionCommandOutput | undefined = undefined; |
| 70 | + waitLoop: while (Date.now() - start < timeout) { |
| 71 | + execution = await client.send( |
| 72 | + new GetQueryExecutionCommand({ QueryExecutionId: startRet.QueryExecutionId }) |
| 73 | + ); |
| 74 | + const { State, StateChangeReason } = execution.QueryExecution?.Status || {}; |
| 75 | + switch (State) { |
| 76 | + case QueryExecutionState.RUNNING: |
| 77 | + case QueryExecutionState.QUEUED: |
| 78 | + await new Promise((r) => setTimeout(r, 2000)); |
| 79 | + break; |
| 80 | + case QueryExecutionState.CANCELLED: |
| 81 | + case QueryExecutionState.FAILED: |
| 82 | + throw new ServiceError(`query execution state: ${State}, reason: ${StateChangeReason}`); |
| 83 | + case QueryExecutionState.SUCCEEDED: |
| 84 | + break waitLoop; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + const result = await client.send( |
| 89 | + new GetQueryResultsCommand({ QueryExecutionId: startRet.QueryExecutionId }) |
| 90 | + ); |
| 91 | + |
| 92 | + return parseResultSet(result.ResultSet); |
| 93 | + } |
| 94 | + }, |
| 95 | +}; |
| 96 | + |
| 97 | +export default athenaPlugin; |
0 commit comments