github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/ui/src/util/proto.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 _ from "lodash"; 12 13 import * as protos from "src/js/protos"; 14 15 export type INodeStatus = protos.cockroach.server.status.statuspb.INodeStatus; 16 const nodeStatus: INodeStatus = null; 17 export type StatusMetrics = typeof nodeStatus.metrics; 18 19 /** 20 * AccumulateMetrics is a convenience function which accumulates the values 21 * in multiple metrics collections. Values from all provided StatusMetrics 22 * collections are accumulated into the first StatusMetrics collection 23 * passed. 24 */ 25 export function AccumulateMetrics(dest: StatusMetrics, ...srcs: StatusMetrics[]): void { 26 srcs.forEach((s: StatusMetrics) => { 27 _.forEach(s, (val: number, key: string) => { 28 if (_.has(dest, key)) { 29 dest[key] = dest[key] + val; 30 } else { 31 dest[key] = val; 32 } 33 }); 34 }); 35 } 36 37 /** 38 * RollupStoreMetrics accumulates all store-level metrics into the top level 39 * metrics collection of the supplied NodeStatus object. This is convenient 40 * for all current usages of NodeStatus in the UI. 41 */ 42 export function RollupStoreMetrics(ns: INodeStatus): void { 43 AccumulateMetrics(ns.metrics, ..._.map(ns.store_statuses, (ss) => ss.metrics)); 44 } 45 46 /** 47 * MetricConstants contains the name of several stats provided by 48 * CockroachDB. 49 */ 50 export namespace MetricConstants { 51 // Store level metrics. 52 export const replicas: string = "replicas"; 53 export const raftLeaders: string = "replicas.leaders"; 54 export const leaseHolders: string = "replicas.leaseholders"; 55 export const ranges: string = "ranges"; 56 export const unavailableRanges: string = "ranges.unavailable"; 57 export const underReplicatedRanges: string = "ranges.underreplicated"; 58 export const liveBytes: string = "livebytes"; 59 export const keyBytes: string = "keybytes"; 60 export const valBytes: string = "valbytes"; 61 export const totalBytes: string = "totalbytes"; 62 export const intentBytes: string = "intentbytes"; 63 export const liveCount: string = "livecount"; 64 export const keyCount: string = "keycount"; 65 export const valCount: string = "valcount"; 66 export const intentCount: string = "intentcount"; 67 export const intentAge: string = "intentage"; 68 export const gcBytesAge: string = "gcbytesage"; 69 export const lastUpdateNano: string = "lastupdatenanos"; 70 export const capacity: string = "capacity"; 71 export const availableCapacity: string = "capacity.available"; 72 export const usedCapacity: string = "capacity.used"; 73 export const sysBytes: string = "sysbytes"; 74 export const sysCount: string = "syscount"; 75 76 // Node level metrics. 77 export const userCPUPercent: string = "sys.cpu.user.percent"; 78 export const sysCPUPercent: string = "sys.cpu.sys.percent"; 79 export const allocBytes: string = "sys.go.allocbytes"; 80 export const sqlConns: string = "sql.conns"; 81 export const rss: string = "sys.rss"; 82 } 83 84 /** 85 * TotalCPU computes the total CPU usage accounted for in a NodeStatus. 86 */ 87 export function TotalCpu(status: INodeStatus): number { 88 const metrics = status.metrics; 89 return metrics[MetricConstants.sysCPUPercent] + metrics[MetricConstants.userCPUPercent]; 90 } 91 92 /** 93 * BytesUsed computes the total byte usage accounted for in a NodeStatus. 94 */ 95 const aggregateByteKeys = [ 96 MetricConstants.liveBytes, 97 MetricConstants.intentBytes, 98 MetricConstants.sysBytes, 99 ]; 100 101 export function BytesUsed(s: INodeStatus): number { 102 const usedCapacity = s.metrics[MetricConstants.usedCapacity]; 103 if (usedCapacity !== 0) { 104 return usedCapacity; 105 } 106 return _.sumBy(aggregateByteKeys, (key: string) => { 107 return s.metrics[key]; 108 }); 109 }