github.com/turbot/steampipe@v1.7.0-rc.0.0.20240517123944-7cef272d4458/ui/dashboard/src/components/dashboards/check/common/node/HierarchyNode.ts (about) 1 import { 2 CheckNode, 3 CheckNodeStatus, 4 CheckNodeType, 5 CheckSeveritySummary, 6 CheckSummary, 7 } from "../index"; 8 9 class HierarchyNode implements CheckNode { 10 private readonly _type: CheckNodeType; 11 private readonly _name: string; 12 private readonly _title: string; 13 private readonly _sort: string; 14 private readonly _children: CheckNode[]; 15 16 constructor( 17 type: CheckNodeType, 18 name: string, 19 title: string, 20 sort: string, 21 children: CheckNode[] 22 ) { 23 this._type = type; 24 this._name = name; 25 this._title = title; 26 this._sort = sort; 27 this._children = children; 28 } 29 30 get type(): CheckNodeType { 31 return this._type; 32 } 33 34 get name(): string { 35 return this._name; 36 } 37 38 get title(): string { 39 return this._title; 40 } 41 42 get sort(): string { 43 return this._sort; 44 } 45 46 get children(): CheckNode[] { 47 return this._children; 48 } 49 50 get summary(): CheckSummary { 51 const summary = { 52 alarm: 0, 53 ok: 0, 54 info: 0, 55 skip: 0, 56 error: 0, 57 }; 58 for (const child of this._children) { 59 const nestedSummary = child.summary; 60 summary.alarm += nestedSummary.alarm; 61 summary.ok += nestedSummary.ok; 62 summary.info += nestedSummary.info; 63 summary.skip += nestedSummary.skip; 64 summary.error += nestedSummary.error; 65 } 66 return summary; 67 } 68 69 get severity_summary(): CheckSeveritySummary { 70 const summary = {}; 71 for (const child of this._children) { 72 for (const [severity, count] of Object.entries(child.severity_summary)) { 73 if (!summary[severity]) { 74 summary[severity] = count; 75 } else { 76 summary[severity] += count; 77 } 78 } 79 } 80 return summary; 81 } 82 83 get status(): CheckNodeStatus { 84 for (const child of this._children) { 85 if (child.status === "running") { 86 return "running"; 87 } 88 } 89 return "complete"; 90 } 91 92 merge(other: CheckNode) { 93 // merge(other) -> iterate children of other -> if child exists on me, call me_child.merge(other_child), else add to end of children 94 for (const otherChild of other.children || []) { 95 // Check for existing child with this name 96 const matchingSelfChild = this.children.find( 97 (selfChild) => selfChild.name === otherChild.name 98 ); 99 100 if (matchingSelfChild) { 101 if (!matchingSelfChild.merge) { 102 continue; 103 } 104 // If there's a matching child, merge that child in 105 matchingSelfChild.merge(otherChild); 106 } else { 107 // Else append to my children 108 this.children.push(otherChild); 109 } 110 } 111 } 112 } 113 114 export default HierarchyNode;