github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/ui/src/util/events.ts (about) 1 // Copyright 2018 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 import * as protobuf from "protobufjs/minimal"; 12 13 import * as protos from "src/js/protos"; 14 import * as eventTypes from "src/util/eventTypes"; 15 16 type Event$Properties = protos.cockroach.server.serverpb.EventsResponse.IEvent; 17 18 /** 19 * getEventDescription returns a short summary of an event. 20 */ 21 export function getEventDescription(e: Event$Properties): string { 22 const info: EventInfo = protobuf.util.isset(e, "info") ? JSON.parse(e.info) : {}; 23 const targetId: number = e.target_id ? e.target_id.toNumber() : null; 24 25 switch (e.event_type) { 26 case eventTypes.CREATE_DATABASE: 27 return `Database Created: User ${info.User} created database ${info.DatabaseName}`; 28 case eventTypes.DROP_DATABASE: 29 const tableDropText = getDroppedObjectsText(info); 30 return `Database Dropped: User ${info.User} dropped database ${info.DatabaseName}. ${tableDropText}`; 31 case eventTypes.CREATE_TABLE: 32 return `Table Created: User ${info.User} created table ${info.TableName}`; 33 case eventTypes.DROP_TABLE: 34 return `Table Dropped: User ${info.User} dropped table ${info.TableName}`; 35 case eventTypes.TRUNCATE_TABLE: 36 return `Table Truncated: User ${info.User} truncated table ${info.TableName}`; 37 case eventTypes.ALTER_TABLE: 38 return `Schema Change: User ${info.User} began a schema change to alter table ${info.TableName} with ID ${info.MutationID}`; 39 case eventTypes.CREATE_INDEX: 40 return `Schema Change: User ${info.User} began a schema change to create an index ${info.IndexName} on table ${info.TableName} with ID ${info.MutationID}`; 41 case eventTypes.DROP_INDEX: 42 return `Schema Change: User ${info.User} began a schema change to drop index ${info.IndexName} on table ${info.TableName} with ID ${info.MutationID}`; 43 case eventTypes.ALTER_INDEX: 44 return `Schema Change: User ${info.User} began a schema change to alter index ${info.IndexName} on table ${info.TableName} with ID ${info.MutationID}`; 45 case eventTypes.CREATE_VIEW: 46 return `View Created: User ${info.User} created view ${info.ViewName}`; 47 case eventTypes.DROP_VIEW: 48 return `View Dropped: User ${info.User} dropped view ${info.ViewName}`; 49 case eventTypes.CREATE_SEQUENCE: 50 return `Sequence Created: User ${info.User} created sequence ${info.SequenceName}`; 51 case eventTypes.ALTER_SEQUENCE: 52 return `Sequence Altered: User ${info.User} altered sequence ${info.SequenceName}`; 53 case eventTypes.DROP_SEQUENCE: 54 return `Sequence Dropped: User ${info.User} dropped sequence ${info.SequenceName}`; 55 case eventTypes.REVERSE_SCHEMA_CHANGE: 56 return `Schema Change Reversed: Schema change with ID ${info.MutationID} was reversed.`; 57 case eventTypes.FINISH_SCHEMA_CHANGE: 58 return `Schema Change Completed: Schema change with ID ${info.MutationID} was completed.`; 59 case eventTypes.FINISH_SCHEMA_CHANGE_ROLLBACK: 60 return `Schema Change Rollback Completed: Rollback of schema change with ID ${info.MutationID} was completed.`; 61 case eventTypes.NODE_JOIN: 62 return `Node Joined: Node ${targetId} joined the cluster`; 63 case eventTypes.NODE_DECOMMISSIONED: 64 return `Node Decommissioned: Node ${targetId} was decommissioned`; 65 case eventTypes.NODE_RECOMMISSIONED: 66 return `Node Recommissioned: Node ${targetId} was recommissioned`; 67 case eventTypes.NODE_RESTART: 68 return `Node Rejoined: Node ${targetId} rejoined the cluster`; 69 case eventTypes.SET_CLUSTER_SETTING: 70 if (info.Value && info.Value.length > 0) { 71 return `Cluster Setting Changed: User ${info.User} set ${info.SettingName} to ${info.Value}`; 72 } 73 return `Cluster Setting Changed: User ${info.User} changed ${info.SettingName}`; 74 case eventTypes.SET_ZONE_CONFIG: 75 return `Zone Config Changed: User ${info.User} set the zone config for ${info.Target} to ${info.Config}`; 76 case eventTypes.REMOVE_ZONE_CONFIG: 77 return `Zone Config Removed: User ${info.User} removed the zone config for ${info.Target}`; 78 case eventTypes.CREATE_STATISTICS: 79 return `Table statistics refreshed for ${info.TableName}`; 80 default: 81 return `Unknown Event Type: ${e.event_type}, content: ${JSON.stringify(info, null, 2)}`; 82 } 83 } 84 85 // EventInfo corresponds to the `info` column of the `system.eventlog` table 86 // and the `info` field of the `server.serverpb.EventsResponse.Event` proto. 87 export interface EventInfo { 88 User: string; 89 DatabaseName?: string; 90 TableName?: string; 91 IndexName?: string; 92 MutationID?: string; 93 ViewName?: string; 94 SequenceName?: string; 95 SettingName?: string; 96 Value?: string; 97 Target?: string; 98 Config?: string; 99 Statement?: string; 100 // The following are three names for the same key (it was renamed twice). 101 // All ar included for backwards compatibility. 102 DroppedTables?: string[]; 103 DroppedTablesAndViews?: string[]; 104 DroppedSchemaObjects?: string[]; 105 } 106 107 export function getDroppedObjectsText(eventInfo: EventInfo): string { 108 const droppedObjects = 109 eventInfo.DroppedSchemaObjects || eventInfo.DroppedTablesAndViews || eventInfo.DroppedTables; 110 if (!droppedObjects) { 111 return ""; 112 } 113 if (droppedObjects.length === 0) { 114 return "No schema objects were dropped."; 115 } else if (droppedObjects.length === 1) { 116 return `1 schema object was dropped: ${droppedObjects[0]}`; 117 } 118 return `${droppedObjects.length} schema objects were dropped: ${droppedObjects.join(", ")}`; 119 }