forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ui: move events endpoint to use sql-over-http
Part of: cockroachdb#89429 Addresses: cockroachdb#90272 (blocked from resolving by cockroachdb#80789) This change migrates the existing `/events` request to use the sql-over-http endpoint on apiV2, making this request tenant-scoped once the sql-over-http endpoint is scoped to tenants (this should be the case when cockroachdb#91323 is completed). Release note: None
- Loading branch information
Thomas Hardy
committed
Nov 11, 2022
1 parent
15369db
commit 5828597
Showing
7 changed files
with
122 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// Copyright 2022 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
import { | ||
executeInternalSql, | ||
SqlExecutionRequest, | ||
sqlResultsAreEmpty, | ||
SqlStatement, | ||
} from "./sqlApi"; | ||
import { withTimeout } from "./util"; | ||
import moment from "moment"; | ||
|
||
export const defaultAPIEventLimit = 1000; | ||
|
||
export type EventColumns = { | ||
timestamp: string; | ||
eventType: string; | ||
reportingID: string; | ||
info: string; | ||
uniqueID: string; | ||
}; | ||
|
||
type NonRedactedEventsRequest = { | ||
type?: string; | ||
limit?: number; | ||
offset?: number; | ||
}; | ||
|
||
export type EventsResponse = EventColumns[]; | ||
|
||
export const baseEventsQuery = `SELECT timestamp, \"eventType\", \"reportingID\", info, \"uniqueID\" FROM system.eventlog`; | ||
|
||
function buildEventStatement({ | ||
type, | ||
limit, | ||
offset, | ||
}: NonRedactedEventsRequest): SqlStatement { | ||
let placeholder = 1; | ||
const eventsStmt: SqlStatement = { | ||
sql: baseEventsQuery, | ||
arguments: [], | ||
}; | ||
if (type) { | ||
eventsStmt.sql += ` WHERE "eventType" = ` + type; | ||
eventsStmt.arguments.push(type); | ||
} | ||
eventsStmt.sql += ` ORDER BY timestamp DESC`; | ||
if (!limit || limit === 0) { | ||
limit = defaultAPIEventLimit; | ||
} | ||
if (limit > 0) { | ||
eventsStmt.sql += ` LIMIT $${placeholder}`; | ||
eventsStmt.arguments.push(limit); | ||
placeholder++; | ||
} | ||
if (offset && offset > 0) { | ||
eventsStmt.sql += ` OFFSET $${placeholder}`; | ||
eventsStmt.arguments.push(offset); | ||
} | ||
eventsStmt.sql += ";"; | ||
return eventsStmt; | ||
} | ||
|
||
// getEvents fetches events logs from the database. Callers of | ||
// getEvents from cluster-ui will need to pass a timeout argument for | ||
// promise timeout handling (callers from db-console already have promise | ||
// timeout handling as part of the cacheDataReducer). | ||
// Note that this endpoint is not able to redact event log information. | ||
export function getNonRedactedEvents( | ||
req: NonRedactedEventsRequest = {}, | ||
timeout?: moment.Duration, | ||
): Promise<EventsResponse> { | ||
const eventsStmt: SqlStatement = buildEventStatement(req); | ||
const eventsRequest: SqlExecutionRequest = { | ||
statements: [eventsStmt], | ||
execute: true, | ||
}; | ||
return withTimeout( | ||
executeInternalSql<EventColumns>(eventsRequest), | ||
timeout, | ||
).then(result => { | ||
// If request succeeded but query failed, throw error (caught by saga/cacheDataReducer). | ||
if (result.error) { | ||
throw result.error; | ||
} | ||
|
||
if (sqlResultsAreEmpty(result)) { | ||
return []; | ||
} | ||
return result.execution.txn_results[0].rows; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters