Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Optional Large Tier Execution Performance #27

Merged
merged 4 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@ const { DUNE_API_KEY } = process.env;

const client = new DuneClient(DUNE_API_KEY ?? "");
const queryID = 1215383;
const parameters = [
QueryParameter.text("TextField", "Plain Text"),
QueryParameter.number("NumberField", 3.1415926535),
QueryParameter.date("DateField", "2022-05-04 00:00:00"),
QueryParameter.enum("ListField", "Option 1"),
];
const params = {
query_parameters = [
QueryParameter.text("TextField", "Plain Text"),
QueryParameter.number("NumberField", 3.1415926535),
QueryParameter.date("DateField", "2022-05-04 00:00:00"),
QueryParameter.enum("ListField", "Option 1"),
]
};

client
.runQuery(queryID, parameters)
.runQuery(queryID, params)
.then((executionResult) => console.log(executionResult.result?.rows));

// should look like
Expand Down
16 changes: 13 additions & 3 deletions src/api/execution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,25 @@ import {
import log from "loglevel";
import { logPrefix } from "../utils";
import { Router } from "./router";
import { ExecutionParams, ExecutionPerformance } from "../types/requestPayload";

// This class implements all the routes defined in the Dune API Docs: https://dune.com/docs/api/
export class ExecutionClient extends Router {
async executeQuery(
queryID: number,
parameters?: QueryParameter[],
params?: ExecutionParams,
): Promise<ExecutionResponse> {
// Extract possible ExecutionParams
let query_parameters: QueryParameter[] = [];
let performance = ExecutionPerformance.Medium;
if (params !== undefined) {
query_parameters = params.query_parameters ? params.query_parameters : [];
performance = performance ? performance : ExecutionPerformance.Medium;
}

const response = await this._post<ExecutionResponse>(`query/${queryID}/execute`, {
query_parameters: parameters ? parameters : [],
query_parameters,
performance,
});
log.debug(logPrefix, `execute response ${JSON.stringify(response)}`);
return response as ExecutionResponse;
Expand Down Expand Up @@ -51,7 +61,7 @@ export class ExecutionClient extends Router {
queryID: number,
parameters?: QueryParameter[],
): Promise<ExecutionResponse> {
return this.executeQuery(queryID, parameters);
return this.executeQuery(queryID, {query_parameters: parameters});
}

/**
Expand Down
11 changes: 6 additions & 5 deletions src/api/extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import log from "loglevel";
import { logPrefix } from "../utils";
import { ExecutionClient } from "./execution";
import { POLL_FREQUENCY_SECONDS, THREE_MONTHS_IN_HOURS } from "../constants";
import { ExecutionParams, ExecutionPerformance } from "../types/requestPayload";

const TERMINAL_STATES = [
ExecutionState.CANCELLED,
Expand All @@ -14,16 +15,16 @@ const TERMINAL_STATES = [
export class ExtendedClient extends ExecutionClient {
async runQuery(
queryID: number,
parameters?: QueryParameter[],
params?: ExecutionParams,
pingFrequency: number = POLL_FREQUENCY_SECONDS,
): Promise<ResultsResponse> {
log.info(
logPrefix,
`refreshing query https://dune.com/queries/${queryID} with parameters ${JSON.stringify(
parameters,
params,
)}`,
);
const { execution_id: jobID } = await this.execute(queryID, parameters);
const { execution_id: jobID } = await this.executeQuery(queryID, params);
let { state } = await this.getExecutionStatus(jobID);
while (!TERMINAL_STATES.includes(state)) {
log.info(
Expand Down Expand Up @@ -57,7 +58,7 @@ export class ExtendedClient extends ExecutionClient {
logPrefix,
`results (from ${lastRun}) older than ${maxAgeHours} hours, re-running query.`,
);
results = await this.runQuery(queryId, parameters);
results = await this.runQuery(queryId, {query_parameters: parameters});
}
return results;
}
Expand All @@ -70,6 +71,6 @@ export class ExtendedClient extends ExecutionClient {
parameters?: QueryParameter[],
pingFrequency: number = 1,
): Promise<ResultsResponse> {
return this.runQuery(queryID, parameters, pingFrequency);
return this.runQuery(queryID, {query_parameters: parameters}, pingFrequency);
}
}
6 changes: 1 addition & 5 deletions src/api/query.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
// Assuming the existence of these imports based on your Python code
import { Router } from "./router";
import {
DuneQuery,
QueryParameter,
CreateQueryResponse,
} from "../types";
import { DuneQuery, QueryParameter, CreateQueryResponse } from "../types";
import { CreateQueryPayload, UpdateQueryPayload } from "../types/requestPayload";

export class QueryAPI extends Router {
Expand Down
5 changes: 1 addition & 4 deletions src/api/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ import { DuneError } from "../types";
import fetch from "cross-fetch";
import log from "loglevel";
import { logPrefix } from "../utils";
import {
RequestPayload,
payloadJSON,
} from "../types/requestPayload";
import { RequestPayload, payloadJSON } from "../types/requestPayload";

const BASE_URL = "https://api.dune.com/api/v1";

Expand Down
11 changes: 11 additions & 0 deletions src/types/requestPayload.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { QueryParameter } from "./queryParameter";

export interface ExecutionParams {
query_parameters?: QueryParameter[];
performance?: ExecutionPerformance;
}

export enum ExecutionPerformance {
Medium = "medium",
Large = "large",
}

export type RequestPayload =
| ExecuteQueryPayload
| UpdateQueryPayload
Expand All @@ -24,6 +34,7 @@ export function payloadJSON(payload?: RequestPayload): string {

export interface ExecuteQueryPayload {
query_parameters?: QueryParameter[];
performance: string;
}

export interface UpdateQueryPayload {
Expand Down
18 changes: 15 additions & 3 deletions tests/e2e/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
QueryAPI,
} from "../../src/";
import log from "loglevel";
import { ExecutionPerformance } from "../../src/types/requestPayload";

const { DUNE_API_KEY } = process.env;
const apiKey: string = DUNE_API_KEY ? DUNE_API_KEY : "No API Key";
Expand Down Expand Up @@ -71,7 +72,17 @@ describe("DuneClient: native routes", () => {
QueryParameter.enum("ListField", "Option 1"),
];
// Execute and check state
const execution = await client.executeQuery(queryID, parameters);
const execution = await client.executeQuery(queryID, {
query_parameters: parameters,
});
expect(execution.execution_id).is.not.null;
});

it("execute with Large tier performance", async () => {
const client = new DuneClient(apiKey);
const execution = await client.executeQuery(1215383, {
performance: ExecutionPerformance.Large,
});
expect(execution.execution_id).is.not.null;
});

Expand Down Expand Up @@ -180,9 +191,10 @@ describe("DuneClient: Errors", () => {
const client = new DuneClient(apiKey);
const queryID = 1215383;
const invalidParameterName = "Invalid Parameter Name";
const parameters = [QueryParameter.text(invalidParameterName, "")];
await expectAsyncThrow(
client.executeQuery(queryID, parameters),
client.executeQuery(queryID, {
query_parameters: [QueryParameter.text(invalidParameterName, "")],
}),
`unknown parameters (${invalidParameterName})`,
);
});
Expand Down