Skip to content
This repository has been archived by the owner on Sep 4, 2024. It is now read-only.

Commit

Permalink
run queries
Browse files Browse the repository at this point in the history
  • Loading branch information
boristane committed Mar 26, 2022
1 parent fd7870e commit 45cd7f1
Show file tree
Hide file tree
Showing 10 changed files with 174 additions and 9 deletions.
22 changes: 22 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@
"base64url": "^3.0.1",
"chalk": "^4.1.2",
"cli-table3": "^0.6.1",
"dayjs": "^1.11.0",
"dotenv": "^16.0.0",
"enquirer": "^2.3.6",
"figlet": "^1.5.2",
"ora": "^5.4.1",
"parse-duration": "^1.0.2",
"yaml": "^1.10.2",
"yargs": "^17.3.1",
"yup": "^0.32.11"
Expand Down
2 changes: 1 addition & 1 deletion src/commands/apply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export async function handler(argv: Arguments<Options>) {
switch (subcommand) {
case subCommand.check: {
if (!id) {
throw new Error("Id missing");
throw new Error("id missing");
}
await handlers.check(application, id, !!json);
break;
Expand Down
2 changes: 1 addition & 1 deletion src/commands/apply/outputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function check(deployment: Deployment, json: boolean) {
process.stdout.write(JSON.stringify({ deployment }, null, 4));
return;
}
var table = new Table({
const table = new Table({
chars: tableChars,
head: ["id", "application", "Status", "Created", "Updated"].map((e) => `${chalk.bold(chalk.cyan(e))}`),
});
Expand Down
16 changes: 11 additions & 5 deletions src/commands/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ export const builder: CommandBuilder<Options, Options> = (yargs) => {
.options({
...baseOptions,
application: { type: "string", desc: "application name", alias: "a" },
id: { type: "string", desc: "id", },
from: { type: "string", desc: "start of the query", },
to: { type: "string", desc: "end of the query", },
})
.positional("subcommand", {
type: "string",
choices: ["list", "create"],
choices: ["list", "run"],
})
.example([
["$0 queries <subcommand>"],
Expand All @@ -24,16 +27,19 @@ export const builder: CommandBuilder<Options, Options> = (yargs) => {
};

export async function handler(argv: Arguments<Options>) {
const { subcommand, profile = "default", json, application } = argv;
const { subcommand, profile = "default", json, application, id, from, to } = argv;
spinner.init(!!argv.quiet);
await authenticate(profile);

switch (subcommand) {
case subCommand.list:
await handlers.list(!!json, application);
break;
case subCommand.create:
console.log("create");
case subCommand.run:
if (!id || !from || !to) {
throw new Error("id missing");
}
await handlers.createRun(!!json, id, from, to);
break;
default:
process.exit(1);
Expand All @@ -42,5 +48,5 @@ export async function handler(argv: Arguments<Options>) {

export enum subCommand {
list = "list",
create = "create",
run = "run",
}
20 changes: 20 additions & 0 deletions src/commands/queries/handlers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import spinner from "../../services/spinner/index";
import api from "../../services/api/api";
import outputs from "./outputs";
import parse from 'parse-duration'
import dayjs from "dayjs";

async function list(json: boolean, application?: string) {
const s = spinner.get();
Expand All @@ -10,6 +12,24 @@ async function list(json: boolean, application?: string) {
outputs.list(queries, json);
}

async function createRun(json: boolean, id: string, from: string, to: string) {
const now = dayjs();
const f = now.subtract(parse(from), "milliseconds");
const t = to === "now" ? now : now.subtract(parse(to), "milliseconds");
const s = spinner.get();
s.start("Running the query");
const { queryRun, calculations: { aggregates, bins } } = await api.queryRunCreate({
queryId: id,
timeframe: {
from: f.valueOf(),
to: t.valueOf(),
}
});
s.succeed();
outputs.getQueryRun(queryRun, aggregates, bins, [], json);
}

export default {
list,
createRun,
};
34 changes: 32 additions & 2 deletions src/commands/queries/outputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@ import Table from "cli-table3";
import { EOL } from "os";
import { tableChars } from "../../shared";
import chalk from "chalk";
import { Bin, QueryRun } from "../../services/api/paths/query-runs";
import dayjs from "dayjs";

const { BASELIME_DOMAIN = "baselime.io" } = process.env;

function list(queries: Query[], json: boolean) {
if (json) {
process.stdout.write(JSON.stringify({ queries }, null, 4));
return;
}
var table = new Table({
const table = new Table({
chars: tableChars,
head: ["id", "application", "ref", "Name", "Created"].map((e) => `${chalk.bold(chalk.cyan(e))}`),
head: ["id", "application", "ref", "name", "created"].map((e) => `${chalk.bold(chalk.cyan(e))}`),
});
queries.forEach((query) => {
table.push([query.id, query.application, query.ref, query.name, query.created]);
Expand All @@ -22,6 +26,32 @@ function list(queries: Query[], json: boolean) {
);
}

function getQueryRun(queryRun: QueryRun, aggregates: Record<string, any>, bins: Bin[], events: Event[], json: boolean) {
if (json) {
process.stdout.write(JSON.stringify({ queryRun, aggregates, bins, events }, null, 4));
return;
}
const runTable = new Table({
chars: tableChars,
head: ["id", "queryId", "from", "to", "status", "created"].map((e) => `${chalk.bold(chalk.cyan(e))}`),
});

runTable.push([queryRun.id, queryRun.queryId, `${dayjs(queryRun.timeframe.from).format()}`, `${dayjs(queryRun.timeframe.to).format()}`, queryRun.status, queryRun.created]);

const aggregatesTable = new Table({
chars: tableChars,
head: ["aggregate", "value"].map((e) => `${chalk.bold(chalk.cyan(e))}`),
});

Object.keys(aggregates).forEach((key: string) => {
aggregatesTable.push([key, aggregates[key]]);
});
process.stdout.write(`${EOL}${runTable.toString()}${EOL}`);
process.stdout.write(`${EOL}${aggregatesTable.toString()}${EOL}`);
process.stdout.write(`${EOL}Follow this url: https://console.${BASELIME_DOMAIN}/workspaces/${queryRun.workspaceId}/envs/${queryRun.environmentId}/queries/${queryRun.queryId}/${queryRun.id}${EOL}`)
}

export default {
list,
getQueryRun,
};
3 changes: 3 additions & 0 deletions src/commands/queries/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ import type { BaseOptions } from "../../shared";

export interface Options extends BaseOptions {
application?: string;
id?: string;
from?: string;
to?: string;
}
2 changes: 2 additions & 0 deletions src/services/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import auth from "./paths/auth";
import queries from "./paths/queries";
import alerts from "./paths/alerts";
import polaris from "./paths/polaris";
import queryRuns from "./paths/query-runs";

export default {
...auth,
...queries,
...alerts,
...polaris,
...queryRuns,
};
80 changes: 80 additions & 0 deletions src/services/api/paths/query-runs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { client } from "../clients";

export interface QueryRun {
id: number;
queryId: string,
workspaceId: string;
environmentId: string;
timeframe: {
from: number;
to: number;
};
userId: string;
status: "STARTED" | "COMPLETED";
granularity: number;
created?: string;
updated?: string;
}

export interface Event {
_namespace: string;
_timestamp: number;
_source: string;
_logId: string;
_baselimeId: string;
_now?: number;
_stream: string;
[key: string]: any;
}

export interface Bin {
bin: string;
[key: string]: number | string;
}

export interface queryRunGetParams {
queryId: string;
id: string;
events?: boolean;
from?: number;
to?: number;
limit?: number;
offset?: number;
}

export interface queryRunCreateParams {
queryId: string;
timeframe: {
from: number;
to: number;
}
}

async function queryRunsList(queryId: string): Promise<QueryRun[]> {
const res = (await client.get(`/query-runs/${queryId}`)).data;
return res.queryRuns;
}

async function queryRunGet(params: queryRunGetParams): Promise<{ queryRun: QueryRun[]; events: Event[]; calculations: Record<string, any>; count: number }> {
const res = (await client.get(`/query-runs/${params.queryId}/${params.id}`, {
params: {
events: params.events,
from: params.from,
to: params.to,
limit: params.limit,
offset: params.offset,
}
})).data;
return res;
}

async function queryRunCreate(params: queryRunCreateParams): Promise<{ queryRun: QueryRun; calculations: { aggregates: Record<string, any>; bins: Bin[] }; }> {
const res = (await client.post(`/query-runs/`, params)).data;
return res;
}

export default {
queryRunsList,
queryRunGet,
queryRunCreate,
};

0 comments on commit 45cd7f1

Please sign in to comment.