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

feat: add QUERY_TECHNICAL_MARK to all UI queries #1992

Merged
merged 2 commits into from
Mar 10, 2025
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
Expand Up @@ -68,7 +68,7 @@ export function TenantStorage({tenantName, metrics}: TenantStorageProps) {
<React.Fragment>
<TenantDashboard database={tenantName} charts={storageDashboardConfig} />
<InfoViewer className={b('storage-info')} title="Storage details" info={info} />
<TopTables path={tenantName} />
<TopTables database={tenantName} />
<TopGroups tenant={tenantName} />
</React.Fragment>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ import i18n from '../i18n';
import '../TenantOverview.scss';

interface TopTablesProps {
path: string;
database: string;
}

const TOP_TABLES_COLUMNS_WIDTH_LS_KEY = 'topTablesTableColumnsWidth';

export function TopTables({path}: TopTablesProps) {
export function TopTables({database}: TopTablesProps) {
const location = useLocation();

const [autoRefreshInterval] = useAutoRefreshInterval();

const {currentData, error, isFetching} = topTablesApi.useGetTopTablesQuery(
{path},
{database},
{pollingInterval: autoRefreshInterval},
);
const loading = isFetching && currentData === undefined;
Expand Down
2 changes: 1 addition & 1 deletion src/store/reducers/cluster/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export const clusterApi = api.injectEndpoints({
}

try {
const query = createSelectClusterGroupsQuery(clusterRoot);
const query = createSelectClusterGroupsQuery();

// Normally query request should be fulfilled within 300-400ms even on very big clusters
// Table with stats is supposed to be very small (less than 10 rows)
Expand Down
9 changes: 5 additions & 4 deletions src/store/reducers/cluster/utils.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import type {TClusterInfoV2, TStorageStats} from '../../../types/api/cluster';
import type {ExecuteQueryResponse, KeyValueRow} from '../../../types/api/query';
import {QUERY_TECHNICAL_MARK} from '../../../utils/constants';
import {parseQueryAPIResponse} from '../../../utils/query';

import type {ClusterGroupsStats} from './types';

export const createSelectClusterGroupsQuery = (clusterRoot: string) => {
return `
export const createSelectClusterGroupsQuery = () => {
return `${QUERY_TECHNICAL_MARK}
SELECT
PDiskFilter,
ErasureSpecies,
CurrentAvailableSize,
CurrentAllocatedSize,
CurrentGroupsCreated,
AvailableGroupsToCreate
FROM \`${clusterRoot}/.sys/ds_storage_stats\`
ORDER BY CurrentGroupsCreated DESC;
FROM \`.sys/ds_storage_stats\`
ORDER BY CurrentGroupsCreated DESC;
`;
};

Expand Down
51 changes: 27 additions & 24 deletions src/store/reducers/executeTopQueries/executeTopQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {SortOrder} from '@gravity-ui/react-data-table';
import {createSlice} from '@reduxjs/toolkit';
import type {PayloadAction} from '@reduxjs/toolkit';

import {QUERY_TECHNICAL_MARK} from '../../../utils/constants';
import {prepareOrderByFromTableSort} from '../../../utils/hooks/useTableSort';
import {isQueryErrorResponse, parseQueryAPIResponse} from '../../../utils/query';
import {api} from '../api';
Expand All @@ -12,8 +13,6 @@ import {getFiltersConditions} from './utils';

const initialState: TopQueriesFilters = {};

const QUERY_TECHNICAL_MARK = '/*UI-QUERY-EXCLUDE*/';

const slice = createSlice({
name: 'executeTopQueries',
initialState,
Expand All @@ -27,13 +26,13 @@ const slice = createSlice({
export const {setTopQueriesFilters} = slice.actions;
export default slice.reducer;

const getQueryText = (path: string, filters?: TopQueriesFilters, sortOrder?: SortOrder[]) => {
const filterConditions = getFiltersConditions(path, filters);
const getQueryText = (filters?: TopQueriesFilters, sortOrder?: SortOrder[]) => {
const filterConditions = getFiltersConditions(filters);

const orderBy = prepareOrderByFromTableSort(sortOrder);

return `
SELECT ${QUERY_TECHNICAL_MARK}
return `${QUERY_TECHNICAL_MARK}
SELECT
CPUTime as CPUTimeUs,
QueryText,
IntervalEnd,
Expand All @@ -42,13 +41,32 @@ SELECT ${QUERY_TECHNICAL_MARK}
ReadBytes,
UserSID,
Duration
FROM \`${path}/.sys/top_queries_by_cpu_time_one_hour\`
FROM \`.sys/top_queries_by_cpu_time_one_hour\`
WHERE ${filterConditions || 'true'} AND QueryText NOT LIKE '%${QUERY_TECHNICAL_MARK}%'
${orderBy}
LIMIT 100
`;
};

function getRunningQueriesText(filters?: TopQueriesFilters, sortOrder?: SortOrder[]) {
const filterConditions = filters?.text
? `Query ILIKE '%${filters.text}%' OR UserSID ILIKE '%${filters.text}%'`
: '';

const orderBy = prepareOrderByFromTableSort(sortOrder);

return `${QUERY_TECHNICAL_MARK}
SELECT
UserSID,
QueryStartAt,
Query as QueryText,
ApplicationName
FROM \`.sys/query_sessions\`
WHERE ${filterConditions || 'true'} AND Query NOT LIKE '%${QUERY_TECHNICAL_MARK}%'
${orderBy}
LIMIT 100`;
}

interface TopQueriesRequestParams {
database: string;
filters?: TopQueriesFilters;
Expand All @@ -68,7 +86,7 @@ export const topQueriesApi = api.injectEndpoints({
try {
const response = await window.api.viewer.sendQuery(
{
query: getQueryText(database, preparedFilters, sortOrder),
query: getQueryText(preparedFilters, sortOrder),
database,
action: 'execute-scan',
},
Expand Down Expand Up @@ -102,24 +120,9 @@ export const topQueriesApi = api.injectEndpoints({
getRunningQueries: build.query({
queryFn: async ({database, filters, sortOrder}: TopQueriesRequestParams, {signal}) => {
try {
const filterConditions = filters?.text
? `Query ILIKE '%${filters.text}%' OR UserSID ILIKE '%${filters.text}%'`
: '';

const orderBy = prepareOrderByFromTableSort(sortOrder);

const queryText = `SELECT ${QUERY_TECHNICAL_MARK}
UserSID, QueryStartAt, Query as QueryText, ApplicationName
FROM
\`.sys/query_sessions\`
WHERE
${filterConditions || 'true'} AND Query NOT LIKE '%${QUERY_TECHNICAL_MARK}%'
${orderBy}
LIMIT 100`;

const response = await window.api.viewer.sendQuery(
{
query: queryText,
query: getRunningQueriesText(filters, sortOrder),
database,
action: 'execute-scan',
},
Expand Down
8 changes: 4 additions & 4 deletions src/store/reducers/executeTopQueries/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import type {TopQueriesFilters} from './types';
const endTimeColumn = 'EndTime';
const intervalEndColumn = 'IntervalEnd';

const getMaxIntervalSubquery = (path: string) => `(
const getMaxIntervalSubquery = () => `(
SELECT
MAX(${intervalEndColumn})
FROM \`${path}/.sys/top_queries_by_cpu_time_one_hour\`
FROM \`.sys/top_queries_by_cpu_time_one_hour\`
)`;

export function getFiltersConditions(path: string, filters?: TopQueriesFilters) {
export function getFiltersConditions(filters?: TopQueriesFilters) {
const conditions: string[] = [];
const to = dateTimeParse(Number(filters?.to) || filters?.to)?.valueOf();
const from = dateTimeParse(Number(filters?.from) || filters?.from)?.valueOf();
Expand All @@ -33,7 +33,7 @@ export function getFiltersConditions(path: string, filters?: TopQueriesFilters)

// If there is no filters, return queries, that were executed in the last hour
if (!from && !to) {
conditions.push(`${intervalEndColumn} IN ${getMaxIntervalSubquery(path)}`);
conditions.push(`${intervalEndColumn} IN ${getMaxIntervalSubquery()}`);
}

if (filters?.text) {
Expand Down
25 changes: 12 additions & 13 deletions src/store/reducers/shardsWorkload/shardsWorkload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {SortOrder} from '@gravity-ui/react-data-table';
import {createSlice} from '@reduxjs/toolkit';
import type {PayloadAction} from '@reduxjs/toolkit';

import {QUERY_TECHNICAL_MARK} from '../../../utils/constants';
import {prepareOrderByFromTableSort} from '../../../utils/hooks/useTableSort';
import {isQueryErrorResponse, parseQueryAPIResponse} from '../../../utils/query';
import {api} from '../api';
Expand Down Expand Up @@ -37,13 +38,11 @@ function getFiltersConditions(filters?: ShardsWorkloadFilters) {

function createShardQueryHistorical(
path: string,
database: string,
filters?: ShardsWorkloadFilters,
sortOrder?: SortOrder[],
tenantName?: string,
) {
const pathSelect = tenantName
? `CAST(SUBSTRING(CAST(Path AS String), ${tenantName.length}) AS Utf8) AS Path`
: 'Path';
const pathSelect = `CAST(SUBSTRING(CAST(Path AS String), ${database.length}) AS Utf8) AS Path`;

let where = `Path='${path}' OR Path LIKE '${path}/%'`;

Expand All @@ -54,7 +53,8 @@ function createShardQueryHistorical(

const orderBy = prepareOrderByFromTableSort(sortOrder);

return `SELECT
return `${QUERY_TECHNICAL_MARK}
SELECT
${pathSelect},
TabletId,
CPUCores,
Expand All @@ -69,14 +69,13 @@ ${orderBy}
LIMIT 20`;
}

function createShardQueryImmediate(path: string, sortOrder?: SortOrder[], tenantName?: string) {
const pathSelect = tenantName
? `CAST(SUBSTRING(CAST(Path AS String), ${tenantName.length}) AS Utf8) AS Path`
: 'Path';
function createShardQueryImmediate(path: string, database: string, sortOrder?: SortOrder[]) {
const pathSelect = `CAST(SUBSTRING(CAST(Path AS String), ${database.length}) AS Utf8) AS Path`;

const orderBy = prepareOrderByFromTableSort(sortOrder);

return `SELECT
return `${QUERY_TECHNICAL_MARK}
SELECT
${pathSelect},
TabletId,
CPUCores,
Expand Down Expand Up @@ -110,7 +109,7 @@ export const {setShardsQueryFilters} = slice.actions;
export default slice.reducer;

interface SendShardQueryParams {
database?: string;
database: string;
path?: string;
sortOrder?: SortOrder[];
filters?: ShardsWorkloadFilters;
Expand All @@ -128,12 +127,12 @@ export const shardApi = api.injectEndpoints({
{
query:
filters?.mode === EShardsWorkloadMode.Immediate
? createShardQueryImmediate(path, sortOrder, database)
? createShardQueryImmediate(path, database, sortOrder)
: createShardQueryHistorical(
path,
database,
Copy link
Collaborator

Choose a reason for hiding this comment

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

As I see here path and database can exist simulteneously
Could you please explain the real difference (as somewhere we change path to database and somewhere not)

Copy link
Member Author

Choose a reason for hiding this comment

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

Example: we look at shards of a table db1/table1
Database is db1, it should be used as a request param, system tables in db1/.sys/, but the result should be filtered, so only shards of current path db1/table1 will be shown

Copy link
Member Author

@artemmufazalov artemmufazalov Mar 7, 2025

Choose a reason for hiding this comment

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

path - path to schema object
database - database, where this schema object exists

filters,
sortOrder,
database,
),
database,
action: queryAction,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
import {TENANT_OVERVIEW_TABLES_LIMIT} from '../../../../utils/constants';
import {QUERY_TECHNICAL_MARK, TENANT_OVERVIEW_TABLES_LIMIT} from '../../../../utils/constants';
import {isQueryErrorResponse, parseQueryAPIResponse} from '../../../../utils/query';
import {api} from '../../api';

const getQueryText = (path: string) => {
return `
const getQueryText = () => {
return `${QUERY_TECHNICAL_MARK}
SELECT
Path, SUM(DataSize) as Size
FROM \`${path}/.sys/partition_stats\`
FROM \`.sys/partition_stats\`
GROUP BY Path
ORDER BY Size DESC
LIMIT ${TENANT_OVERVIEW_TABLES_LIMIT}
ORDER BY Size DESC
LIMIT ${TENANT_OVERVIEW_TABLES_LIMIT}
`;
};

export const topTablesApi = api.injectEndpoints({
endpoints: (builder) => ({
getTopTables: builder.query({
queryFn: async ({path}: {path: string}, {signal}) => {
queryFn: async ({database}: {database: string}, {signal}) => {
try {
const response = await window.api.viewer.sendQuery(
{
query: getQueryText(path),
database: path,
query: getQueryText(),
database,
action: 'execute-scan',
},
{signal, withRetries: true},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import {TENANT_OVERVIEW_TABLES_LIMIT} from '../../../../utils/constants';
import {QUERY_TECHNICAL_MARK, TENANT_OVERVIEW_TABLES_LIMIT} from '../../../../utils/constants';
import {isQueryErrorResponse, parseQueryAPIResponse} from '../../../../utils/query';
import {api} from '../../api';

function createShardQuery(path: string, tenantName?: string) {
const pathSelect = tenantName
? `CAST(SUBSTRING(CAST(Path AS String), ${tenantName.length}) AS Utf8) AS Path`
: 'Path';

return `SELECT
function createShardQuery(path: string, database: string) {
const pathSelect = `CAST(SUBSTRING(CAST(Path AS String), ${database.length}) AS Utf8) AS Path`;
return `${QUERY_TECHNICAL_MARK}
SELECT
${pathSelect},
TabletId,
CPUCores,
Expand Down
2 changes: 2 additions & 0 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ export const TENANT_OVERVIEW_TABLES_LIMIT = 5;

export const EMPTY_DATA_PLACEHOLDER = '—';

export const QUERY_TECHNICAL_MARK = '/*UI-QUERY-EXCLUDE*/';

// ==== Titles ====
export const DEVELOPER_UI_TITLE = 'Developer UI';
export const CLUSTER_DEFAULT_TITLE = 'Cluster';
Expand Down
Loading