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

ui: fix sql activity app filter on internal queries #114498

Merged
merged 1 commit into from
Nov 15, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
62 changes: 59 additions & 3 deletions pkg/ui/workspaces/cluster-ui/src/sqlActivity/util.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { mockStmtStats, Stmt } from "src/api/testUtils";
import { Filters } from "src/queryFilter/filter";
import Long from "long";
import { cockroach } from "@cockroachlabs/crdb-protobuf-client";
import { unset } from "../util";
import { INTERNAL_APP_NAME_PREFIX, unset } from "../util";

describe("filterStatementsData", () => {
function filterAndCheckStmts(
Expand Down Expand Up @@ -71,6 +71,25 @@ describe("filterStatementsData", () => {
filterAndCheckStmts(stmtsRaw, {}, "giraffe", expectedIDs);
});

it("should show non-internal statements when no app filters are applied", () => {
const stmtsRaw = [
{ id: 1, app: "hello" },
{ id: 2, app: "$ internal hello" },
{ id: 3, app: "$ internal app" },
{ id: 4, app: "world" },
{ id: 5, app: "great" },
].map(stmt =>
mockStmtStats({
id: Long.fromInt(stmt.id),
key: { key_data: { app: stmt.app } },
}),
);

const filters: Filters = {};
const expected = [1, 4, 5];
filterAndCheckStmts(stmtsRaw, filters, null, expected);
});

it.each([
{
stmts: [
Expand Down Expand Up @@ -188,10 +207,40 @@ describe("filterStatementsData", () => {
id: 7,
app: "elephants cannot jump", // Should not match.
},
{
id: 8,
app: "$ internal-my-app", // Should not match.
},
],
appName: "aaaaaaaaaaaaaaaaaaaaaaaa",
expectedIDs: [],
},
{
stmts: [
{
id: 1,
// Should match because it starts with INTERNAL_APP_NAME_PREFIX.
app: INTERNAL_APP_NAME_PREFIX + "-my-app",
},
{
id: 2,
// Should match because it starts with INTERNAL_APP_NAME_PREFIX.
app: INTERNAL_APP_NAME_PREFIX,
},
{
id: 3,
// Should not match.
app: "myApp" + INTERNAL_APP_NAME_PREFIX,
},
{
id: 4,
// Should match because it starts with INTERNAL_APP_NAME_PREFIX.
app: INTERNAL_APP_NAME_PREFIX + "myApp",
},
],
appName: INTERNAL_APP_NAME_PREFIX + ",aaaaaaaaaaaaa",
expectedIDs: [1, 2, 4],
},
])("should filter out statements not matching filter apps", tc => {
const stmtsRaw = tc.stmts.map(stmt =>
mockStmtStats({
Expand Down Expand Up @@ -321,7 +370,7 @@ describe("filterStatementsData", () => {
it("should filter out statements not matching ALL filters", () => {
const filters: Filters = {
database: "coolestDB",
app: "coolestApp",
app: "coolestApp, " + INTERNAL_APP_NAME_PREFIX,
timeNumber: "1",
timeUnit: "seconds",
};
Expand Down Expand Up @@ -364,6 +413,13 @@ describe("filterStatementsData", () => {
svcLatSecs: 1,
query: `select ${searchTerm}`,
},
{
id: 6,
db: "coolestDB",
app: INTERNAL_APP_NAME_PREFIX + "-cool-app",
svcLatSecs: 1,
query: `select * from ${searchTerm} where a = 1`,
},
].map(stmt =>
mockStmtStats({
id: Long.fromInt(stmt.id),
Expand All @@ -379,7 +435,7 @@ describe("filterStatementsData", () => {
}),
);

const expectedIDs = [5];
const expectedIDs = [5, 6];

filterAndCheckStmts(stmtsRaw, filters, searchTerm, expectedIDs);
});
Expand Down
21 changes: 14 additions & 7 deletions pkg/ui/workspaces/cluster-ui/src/sqlActivity/util.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
flattenStatementStats,
} from "src/util/appStats/appStats";
import { FixFingerprintHexValue } from "src/util/format";
import { unset } from "src/util/constants";
import { INTERNAL_APP_NAME_PREFIX, unset } from "src/util/constants";
import { createSelector } from "@reduxjs/toolkit";
import { SqlStatsResponse } from "src/api/statementsApi";
import { Filters, getTimeValueInSeconds } from "src/queryFilter";
Expand Down Expand Up @@ -72,6 +72,8 @@ export function filterStatementsData(
.map(app => app.trim())
.filter(appName => !!appName);

const includeInternalApps = !!appNames?.includes(INTERNAL_APP_NAME_PREFIX);

// Return statements filtered by the values selected on the filter and
// the search text. A statement must match all selected filters to be
// displayed on the table.
Expand All @@ -90,13 +92,18 @@ export function filterStatementsData(
return databases.length === 0 || databases.includes(statement.database);
}
})
.filter(
statement =>
!appNames?.length ||
appNames.includes(
.filter(statement => {
const isInternal = statement.applicationName?.startsWith(
INTERNAL_APP_NAME_PREFIX,
);
return (
(!appNames?.length && !isInternal) ||
(includeInternalApps && isInternal) ||
appNames?.includes(
statement.applicationName ? statement.applicationName : unset,
),
)
)
);
})
.filter(statement => (filters.fullScan ? statement.fullScan : true))
.filter(
statement =>
Expand Down