Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import {useQuery} from "@tanstack/react-query";
import {Typography} from "antd";

import {formatSizeInBytes} from "../../../../IngestPage/Jobs/units";
import useSearchStore from "../../../SearchState/index";
import {SEARCH_UI_STATE} from "../../../SearchState/typings";
import {fetchQuerySpeed} from "./utils";


const {Text} = Typography;
const SPEED_DEFAULT = {latency: 0, speed: 0};

/**
* Displays the query latency and speed.
*
* @return
*/
const QuerySpeed = () => {
const searchUiState = useSearchStore((state) => state.searchUiState);
const searchJobId = useSearchStore((state) => state.searchJobId);
const cachedDataset = useSearchStore((state) => state.cachedDataset);

const {data = SPEED_DEFAULT} = useQuery({
queryKey: [
"speed",
searchJobId,
cachedDataset,
],
queryFn: async () => {
if (null === searchJobId) {
return SPEED_DEFAULT;
}
const {bytes, duration} = await fetchQuerySpeed(cachedDataset ?? "", searchJobId);
return {latency: duration, speed: bytes / duration};
},
enabled: SEARCH_UI_STATE.DONE === searchUiState,
refetchInterval: false,
});
const {latency, speed} = data;
return (
<Text type={"secondary"}>
{` in ${latency.toFixed(3)} seconds (${formatSizeInBytes(speed)}/s)`}
</Text>
);
};

export default QuerySpeed;
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import {querySql} from "../../../../../api/sql";
import {settings} from "../../../../../settings";


/**
* Builds a SQL query string to retrieve the total uncompressed bytes and duration
* for a specific query job within a given dataset.
*
* @param datasetName
* @param jobId
* @return
*/
const buildQuerySpeedSql = (datasetName: string, jobId: string) => {
const tableName = "" === datasetName ?
settings.SqlDbClpArchivesTableName :
`${settings.SqlDbClpTablePrefix}${datasetName}_archives`;

return `WITH qt AS (

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the query lgtm

SELECT job_id, archive_id
FROM query_tasks
WHERE
archive_id IS NOT NULL
AND job_id = ${jobId}
),
totals AS (
SELECT
qt.job_id,
SUM(ca.uncompressed_size) AS total_uncompressed_bytes
FROM qt
JOIN ${tableName} ca
ON qt.archive_id = ca.id
)
SELECT
CAST(totals.total_uncompressed_bytes AS double) AS bytes,
qj.duration AS duration
FROM query_jobs qj
Comment thread
hoophalab marked this conversation as resolved.
Resolved
JOIN totals
ON totals.job_id = qj.id`;
};

interface QuerySpeedResp {
bytes: number;
duration: number;
}

/**
* Fetches the query speed data (bytes and duration) for a specific job ID
* within a given dataset by executing a SQL query.
*
* @param datasetName
* @param jobId
* @return
*/
const fetchQuerySpeed = async (datasetName: string, jobId: string): Promise<QuerySpeedResp> => {
const resp = await querySql<QuerySpeedResp[]>(buildQuerySpeedSql(datasetName, jobId));
const [data] = resp.data;
if ("undefined" === typeof data) {
throw new Error("Invalid query speed.");
}

return data;
};
Comment thread
hoophalab marked this conversation as resolved.
Resolved

export {fetchQuerySpeed};
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {PRESTO_SQL_INTERFACE} from "../../SearchState/Presto/typings";
import {SEARCH_UI_STATE} from "../../SearchState/typings";
import OpenQueryDrawerButton from "./OpenQueryDrawerButton";
import QueryDrawer from "./QueryDrawer";
import QuerySpeed from "./QuerySpeed";
import Results from "./Results";


Expand Down Expand Up @@ -90,6 +91,7 @@ const QueryStatus = () => {
<Results/>
{" "}
<Text type={"secondary"}>results</Text>
{CLP_QUERY_ENGINES.PRESTO !== SETTINGS_QUERY_ENGINE && <QuerySpeed/>}
</>
)}
{searchUiState === SEARCH_UI_STATE.FAILED && (
Expand Down
Loading