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 Clickhouse Operators #43

Merged
merged 1 commit into from
Oct 23, 2023
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
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const DEFAULT_PASSWORD = "";
export const DEFAULT_MAX_LIMIT = 500;
export const DEFAULT_VERBOSE = false;
export const APP_NAME = pkg.name;
export const DEFAULT_SORT_BY = "DESC";

// parse command line options
const opts = program
Expand Down
42 changes: 30 additions & 12 deletions src/fetch/openapi.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pkg from "../../package.json" assert { type: "json" };

import { OpenApiBuilder, SchemaObject, ExampleObject } from "openapi3-ts/oas31";
import { OpenApiBuilder, SchemaObject, ExampleObject, ParameterObject } from "openapi3-ts/oas31";
import { config } from "../config";
import { getBlock } from "../queries";
import { registry } from "../prometheus";
Expand All @@ -18,15 +18,15 @@ const chains = await supportedChainsQuery();
const block_example = (await makeQuery(await getBlock( new URLSearchParams({limit: "2"})))).data;

const timestampSchema: SchemaObject = { anyOf: [
{type: "number" },
{type: "number"},
{type: "string", format: "date"},
{type: "string", format: "date-time"}
]
};
const timestampExamples: ExampleObject = {
unix: { summary: "Unix Timestamp (milliseconds)", value: Number(new Date('2023-10-18T00:00:00Z')) },
date: { summary: 'Full-date notation', value: '2023-10-18' },
datetime: { summary: 'Date-time notation', value: '2023-10-18T00:00:00Z' },
unix: { summary: `Unix Timestamp (milliseconds)` },
date: { summary: `Full-date notation`, value: '2023-10-18' },
datetime: { summary: `Date-time notation`, value: '2023-10-18T00:00:00Z'},
}

export default new OpenApiBuilder()
Expand Down Expand Up @@ -95,21 +95,39 @@ export default new OpenApiBuilder()
in: "query",
required: false,
schema: { type: "boolean" },
example: true,
},
{
name: "limit",
name: "sort_by",
in: "query",
description: "Used to specify the number of records to return.",
description: "Sort by `block_number`",
required: false,
schema: { default: 1, type: "number", maximum: config.maxLimit, minimum: 1 },
schema: {enum: ['ASC', 'DESC'] },
},
...["greater_or_equals_by_timestamp", "greater_by_timestamp", "less_or_equals_by_timestamp", "less_by_timestamp"].map(name => {
return {
name,
in: "query",
description: "Filter " + name.replace(/_/g, " "),
required: false,
schema: timestampSchema,
examples: timestampExamples,
} as ParameterObject
}),
...["greater_or_equals_by_block_number", "greater_by_block_number", "less_or_equals_by_block_number", "less_by_block_number"].map(name => {
return {
name,
in: "query",
description: "Filter " + name.replace(/_/g, " "),
required: false,
schema: { type: "number" },
} as ParameterObject
}),
{
name: "sort_by",
name: "limit",
in: "query",
description: "Sort by `block_number`",
description: "Used to specify the number of records to return.",
required: false,
schema: {enum: ['ASC', 'DESC'], default: 'DESC'},
schema: { type: "number", maximum: config.maxLimit, minimum: 1 },
},
],
responses: {
Expand Down
48 changes: 34 additions & 14 deletions src/queries.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { config } from './config';
import { DEFAULT_SORT_BY, config } from './config';
import { parseLimit, parseTimestamp } from './utils';

export interface Block {
Expand All @@ -9,24 +9,44 @@ export interface Block {
}

export function getBlock(searchParams: URLSearchParams) {
// URL Params
const chain = searchParams.get("chain");
const block_number = searchParams.get("block_number");
const block_id = searchParams.get("block_id");
const timestamp = parseTimestamp(searchParams.get("timestamp"));
const limit = parseLimit(searchParams.get("limit"));
// TO-DO: lessOrEquals, greaterOrEquals, less & greater block number & timestamp
// TO-DO: Modulo block number (ex: search by every 1M blocks)

// SQL Query
let query = `SELECT * FROM ${config.table}`;
let query = `SELECT block_id, block_number, chain, timestamp FROM ${config.table}`;
const where = [];
if ( chain ) where.push(`chain == '${chain}'`);
if ( block_id ) where.push(`block_id == '${block_id}'`);
if ( block_number ) where.push(`block_number == '${block_number}'`);
if ( timestamp ) where.push(`timestamp == '${timestamp}'`);

// Clickhouse Operators
// https://clickhouse.com/docs/en/sql-reference/operators
const operators = [
["greater_or_equals", ">="],
["greater", ">"],
["less_or_equals", "<="],
["less", "<"],
]
for ( const [key, operator] of operators ) {
const block_number = searchParams.get(`${key}_by_block_number`);
const timestamp = parseTimestamp(searchParams.get(`${key}_by_timestamp`));
if (block_number) where.push(`block_number ${operator} '${block_number}'`);
if (timestamp) where.push(`timestamp ${operator} '${timestamp}'`);
}

// equals
const chain = searchParams.get("chain");
const block_id = searchParams.get("block_id");
const block_number = searchParams.get('block_number');
const timestamp = parseTimestamp(searchParams.get('timestamp'));
if (chain) where.push(`chain == '${chain}'`);
if (block_id) where.push(`block_id == '${block_id}'`);
if (block_number) where.push(`block_number == '${block_number}'`);
if (timestamp) where.push(`timestamp == '${timestamp}'`);

// Join WHERE statements with AND
if ( where.length ) query += ` WHERE (${where.join(' AND ')})`;
query += ' ORDER BY block_number DESC'

// Sort and Limit
const limit = parseLimit(searchParams.get("limit"));
const sort_by = searchParams.get("sort_by");
query += ` ORDER BY block_number ${sort_by ?? DEFAULT_SORT_BY}`
query += ` LIMIT ${limit}`
return query;
}
Expand Down
Loading