Skip to content

Commit

Permalink
ui: add checks for values
Browse files Browse the repository at this point in the history
Fixes #99655
Fixes #99538
Fixes #99539

Add checks to usages that could cause
`Cannot read properties of undefined`.

Release note: None
  • Loading branch information
maryliag committed Mar 29, 2023
1 parent aec78f3 commit 2534915
Show file tree
Hide file tree
Showing 26 changed files with 63 additions and 57 deletions.
3 changes: 3 additions & 0 deletions pkg/ui/workspaces/cluster-ui/src/api/schemaInsightsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ function createIndexRecommendationsToSchemaInsight(

txn_result.rows.forEach(row => {
row.index_recommendations.forEach(rec => {
if (!rec.includes(" : ")) {
return;
}
const recSplit = rec.split(" : ");
const recType = recSplit[0];
const recQuery = recSplit[1];
Expand Down
2 changes: 1 addition & 1 deletion pkg/ui/workspaces/cluster-ui/src/api/txnInsightsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ function formatTxnInsightsRow(row: TxnInsightsResponseRow): TxnInsightEvent {
transactionExecutionID: row.txn_id,
transactionFingerprintID: row.txn_fingerprint_id,
implicitTxn: row.implicit_txn,
query: row.query.split(" ; ").join("\n"),
query: row.query?.split(" ; ").join("\n") || "",
startTime,
endTime,
elapsedTimeMillis: endTime.diff(startTime, "milliseconds"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,9 +378,9 @@ export class DatabaseDetailsPage extends React.Component<
const { search, tables, filters, nodeRegions } = this.props;

const regionsSelected =
filters.regions.length > 0 ? filters.regions.split(",") : [];
filters.regions?.length > 0 ? filters.regions.split(",") : [];
const nodesSelected =
filters.nodes.length > 0 ? filters.nodes.split(",") : [];
filters.nodes?.length > 0 ? filters.nodes.split(",") : [];

return tables
.filter(table => (search ? filterBySearchQuery(table, search) : true))
Expand All @@ -392,13 +392,11 @@ export class DatabaseDetailsPage extends React.Component<
let foundNode = nodesSelected.length == 0;

table.details.nodes?.forEach(node => {
if (
foundRegion ||
regionsSelected.includes(nodeRegions[node.toString()])
) {
const n = node?.toString() || "";
if (foundRegion || regionsSelected.includes(nodeRegions[n])) {
foundRegion = true;
}
if (foundNode || nodesSelected.includes("n" + node.toString())) {
if (foundNode || nodesSelected.includes("n" + n)) {
foundNode = true;
}
if (foundNode && foundRegion) return true;
Expand Down Expand Up @@ -738,7 +736,7 @@ export class DatabaseDetailsPage extends React.Component<
hideAppNames={true}
regions={regions}
hideTimeLabel={true}
nodes={nodes.map(n => "n" + n.toString())}
nodes={nodes.map(n => "n" + n?.toString())}
activeFilters={activeFilters}
filters={defaultFilters}
onSubmitFilters={this.onSubmitFilters}
Expand Down
14 changes: 6 additions & 8 deletions pkg/ui/workspaces/cluster-ui/src/databasesPage/databasesPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -424,9 +424,9 @@ export class DatabasesPage extends React.Component<

// The regions and nodes selected from the filter dropdown.
const regionsSelected =
filters.regions.length > 0 ? filters.regions.split(",") : [];
filters.regions?.length > 0 ? filters.regions.split(",") : [];
const nodesSelected =
filters.nodes.length > 0 ? filters.nodes.split(",") : [];
filters.nodes?.length > 0 ? filters.nodes.split(",") : [];

return databases
.filter(db => (search ? filterBySearchQuery(db, search) : true))
Expand All @@ -438,13 +438,11 @@ export class DatabasesPage extends React.Component<
let foundNode = nodesSelected.length == 0;

db.nodes?.forEach(node => {
if (
foundRegion ||
regionsSelected.includes(nodeRegions[node.toString()])
) {
const n = node?.toString() || "";
if (foundRegion || regionsSelected.includes(nodeRegions[n])) {
foundRegion = true;
}
if (foundNode || nodesSelected.includes("n" + node.toString())) {
if (foundNode || nodesSelected.includes("n" + n)) {
foundNode = true;
}
if (foundNode && foundRegion) return true;
Expand Down Expand Up @@ -617,7 +615,7 @@ export class DatabasesPage extends React.Component<
hideAppNames={true}
regions={regions}
hideTimeLabel={true}
nodes={nodes.map(n => "n" + n.toString())}
nodes={nodes.map(n => "n" + n?.toString())}
activeFilters={activeFilters}
filters={defaultFilters}
onSubmitFilters={this.onSubmitFilters}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export function getHighlightedText(
})
.join("|");
const parts = isOriginalText
? text.split(new RegExp(`(${search})`, "gi"))
? text?.split(new RegExp(`(${search})`, "gi"))
: rebaseText(text, highlight).split(new RegExp(`(${search})`, "gi"));
const highlightClass = hasDarkBkg ? "_text-bold-light" : "_text-bold";
return parts.map((part, i) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,12 @@ export const MultiSelectCheckbox = (props: MultiSelectCheckboxProps) => {
field: string,
parent: any,
) => {
const selected = selectedOptions
.map(function (option: SelectOption) {
return option.label;
})
.toString();
const selected =
selectedOptions
?.map(function (option: SelectOption) {
return option.label;
})
.toString() || "";
parent.setState({
filters: {
...parent.state.filters,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export const selectContentionDetailsForStatement = createSelector(
export const selectAppName = createSelector(
(state: AppState) => state.adminUI?.sessions,
response => {
if (!response.data) return null;
if (!response?.data) return null;
return response.data.internal_app_name_prefix;
},
);
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const selectSessionsData = createSelector(
export const selectSessions = createSelector(
(state: AppState) => state.adminUI?.sessions,
(state: SessionsState) => {
if (!state.data) {
if (!state?.data) {
return null;
}
return state.data.sessions.map(session => {
Expand All @@ -48,7 +48,7 @@ export const selectSessions = createSelector(
export const selectAppName = createSelector(
(state: AppState) => state.adminUI?.sessions,
(state: SessionsState) => {
if (!state.data) {
if (!state?.data) {
return null;
}
return state.data.internal_app_name_prefix;
Expand All @@ -64,7 +64,7 @@ export const selectColumns = createSelector(
localStorageSelector,
localStorage =>
localStorage["showColumns/SessionsPage"]
? localStorage["showColumns/SessionsPage"].split(",")
? localStorage["showColumns/SessionsPage"]?.split(",")
: null,
);

Expand Down
8 changes: 4 additions & 4 deletions pkg/ui/workspaces/cluster-ui/src/sqlActivity/util.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@ export function filteredStatementsData(
): AggregateStatistics[] {
const timeValue = getTimeValueInSeconds(filters);
const sqlTypes =
filters.sqlType.length > 0
filters.sqlType?.length > 0
? filters.sqlType.split(",").map(function (sqlType: string) {
// Adding "Type" to match the value on the Statement
// Possible values: TypeDDL, TypeDML, TypeDCL and TypeTCL
return "Type" + sqlType;
})
: [];
const databases =
filters.database.length > 0 ? filters.database.split(",") : [];
filters.database?.length > 0 ? filters.database.split(",") : [];
if (databases.includes(unset)) {
databases.push("");
}
const regions = filters.regions.length > 0 ? filters.regions.split(",") : [];
const nodes = filters.nodes.length > 0 ? filters.nodes.split(",") : [];
const regions = filters.regions?.length > 0 ? filters.regions.split(",") : [];
const nodes = filters.nodes?.length > 0 ? filters.nodes.split(",") : [];

// Return statements filtered by the values selected on the filter and
// the search text. A statement must match all selected filters to be
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,9 @@ function formatIdxRecommendations(
for (let i = 0; i < idxRecs.length; i++) {
const rec = idxRecs[i];
let idxType: InsightType;
if (!rec?.includes(" : ")) {
continue;
}
const t = rec.split(" : ")[0];
switch (t) {
case "creation":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const selectStatementsLastUpdated = createSelector(
// selectApps returns the array of all apps with statement statistics present
// in the data.
export const selectApps = createSelector(sqlStatsSelector, sqlStatsState => {
if (!sqlStatsState.data || !sqlStatsState.valid) {
if (!sqlStatsState?.data || !sqlStatsState?.valid) {
return [];
}

Expand Down Expand Up @@ -88,7 +88,7 @@ export const selectApps = createSelector(sqlStatsSelector, sqlStatsState => {

// selectDatabases returns the array of all databases in the cluster.
export const selectDatabases = createSelector(databasesListSelector, state => {
if (!state.data) {
if (!state?.data) {
return [];
}

Expand All @@ -102,7 +102,7 @@ export const selectDatabases = createSelector(databasesListSelector, state => {
export const selectTotalFingerprints = createSelector(
sqlStatsSelector,
state => {
if (!state.data) {
if (!state?.data) {
return 0;
}
const aggregated = aggregateStatementStats(state.data.statements);
Expand All @@ -113,7 +113,7 @@ export const selectTotalFingerprints = createSelector(
// selectLastReset returns a string displaying the last time the statement
// statistics were reset.
export const selectLastReset = createSelector(sqlStatsSelector, state => {
if (!state.data) {
if (!state?.data) {
return "";
}

Expand Down Expand Up @@ -144,7 +144,7 @@ export const selectStatements = createSelector(
diagnosticsReportsPerStatement,
): AggregateStatistics[] => {
// State is valid if we successfully fetched data, and the data has not yet been invalidated.
if (!state.data || !state.valid) {
if (!state?.data || !state?.valid) {
return null;
}
let statements = flattenStatementStats(state.data.statements);
Expand Down Expand Up @@ -229,7 +229,7 @@ export const selectColumns = createSelector(
// return array of columns if user have customized it or `null` otherwise
localStorage =>
localStorage["showColumns/StatementsPage"]
? localStorage["showColumns/StatementsPage"].split(",")
? localStorage["showColumns/StatementsPage"]?.split(",")
: null,
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ export class StatementsPage extends React.Component<
// hiding columns that won't be displayed for tenants.
const columns = makeStatementsColumns(
statements,
filters.app.split(","),
filters.app?.split(","),
this.props.stmtsTotalRuntimeSecs,
"statement",
isTenant,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const selectColumns = createSelector(
localStorageSelector,
localStorage =>
localStorage["showColumns/StatementInsightsPage"]
? localStorage["showColumns/StatementInsightsPage"].split(",")
? localStorage["showColumns/StatementInsightsPage"]?.split(",")
: null,
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ export const selectColumns = createSelector(
// return array of columns if user have customized it or `null` otherwise
localStorage =>
localStorage["showColumns/JobsPage"]
? localStorage["showColumns/JobsPage"].split(",")
? localStorage["showColumns/JobsPage"]?.split(",")
: null,
);
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const selectSession = createSelector(
(state: AppState) => state.adminUI?.sessions,
(_state: AppState, props: RouteComponentProps) => props,
(state: SessionsState, props: RouteComponentProps<any>) => {
if (!state.data) {
if (!state?.data) {
return null;
}
const sessionID = getMatchParamByName(props.match, sessionAttr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const selectTxnColumns = createSelector(
// return array of columns if user have customized it or `null` otherwise
localStorage =>
localStorage["showColumns/TransactionPage"]
? localStorage["showColumns/TransactionPage"].split(",")
? localStorage["showColumns/TransactionPage"]?.split(",")
: null,
);

Expand Down
4 changes: 2 additions & 2 deletions pkg/ui/workspaces/cluster-ui/src/transactionsPage/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ export const filterTransactions = (
activeFilters: 0,
};
const timeValue = getTimeValueInSeconds(filters);
const regions = filters.regions.length > 0 ? filters.regions.split(",") : [];
const nodes = filters.nodes.length > 0 ? filters.nodes.split(",") : [];
const regions = filters.regions?.length > 0 ? filters.regions.split(",") : [];
const nodes = filters.nodes?.length > 0 ? filters.nodes.split(",") : [];

const activeFilters = calculateActiveFilters(filters);

Expand Down
2 changes: 1 addition & 1 deletion pkg/ui/workspaces/cluster-ui/src/util/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ function add(a: string, b: string): string {
// to an int64 (in string form).
export function HexStringToInt64String(s: string): string {
let dec = "0";
s.split("").forEach(function (chr: string) {
s?.split("").forEach(function (chr: string) {
const n = parseInt(chr, 16);
for (let t = 8; t; t >>= 1) {
dec = add(dec, dec);
Expand Down
2 changes: 1 addition & 1 deletion pkg/ui/workspaces/cluster-ui/src/util/formatNumber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import { isNumber } from "lodash";

function numberToString(n: number) {
return n.toString();
return n?.toString() || "";
}

export function formatNumberForDisplay(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ export const selectIndexStatsKeys = createSelector(
);

export const KeyToTableRequest = (key: string): TableIndexStatsRequest => {
if (!key?.includes("/")) {
return new TableIndexStatsRequest({ database: "", table: "" });
}
const s = key.split("/");
const database = s[0];
const table = s[1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export const selectRecentStatement = createSelector(
export const selectAppName = createSelector(
(state: AdminUIState) => state.cachedData.sessions,
(state?: CachedDataReducerState<SessionsResponseMessage>) => {
if (!state.data) {
if (!state?.data) {
return null;
}
return state.data.internal_app_name_prefix;
Expand Down
2 changes: 1 addition & 1 deletion pkg/ui/workspaces/db-console/src/util/highlightedText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default function getHighlightedText(
})
.join("|");
const parts = isOriginalText
? text.split(new RegExp(`(${search})`, "gi"))
? text?.split(new RegExp(`(${search})`, "gi"))
: rebaseText(text, highlight).split(new RegExp(`(${search})`, "gi"));
const highlightClass = hasDarkBkg ? "_text-bold-light" : "_text-bold";
return parts.map((part, i) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const selectSession = createSelector(
state: CachedDataReducerState<SessionsResponseMessage>,
props: RouteComponentProps<any>,
) => {
if (!state.data) {
if (!state?.data) {
return null;
}
const sessionID = getMatchParamByName(props.match, sessionAttr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const selectSessions = createSelector(
state: CachedDataReducerState<SessionsResponseMessage>,
_: RouteComponentProps<any>,
) => {
if (!state.data) {
if (!state?.data) {
return null;
}
return state.data.sessions.map(session => {
Expand All @@ -64,7 +64,7 @@ export const selectAppName = createSelector(
state: CachedDataReducerState<SessionsResponseMessage>,
_: RouteComponentProps<any>,
) => {
if (!state.data) {
if (!state?.data) {
return null;
}
return state.data.internal_app_name_prefix;
Expand Down
Loading

0 comments on commit 2534915

Please sign in to comment.