🌳
pt0/deployF/k8sF/fmtDeleteSummaryAI.mts
1import * as _ from 'lodash-es'
3type ResourceResult = {
4 cluster_name: string
5 action: string
6 name: string
7 kind: string
8}
10type ClusterLine = { cluster: string, line: string }
11type ClusterActionLine = ClusterLine & { action: string }
13const kindShortNames: Record<string, string> = {
14 PersistentVolumeClaim: 'pvc', Secret: 'secret', Deployment: 'deployment',
15 Service: 'service', Ingress: 'ingress', ConfigMap: 'configmap', Job: 'job',
17export const shortKind = (kind: string) => kindShortNames[kind] || kind.toLowerCase()
19const actionLabels: Record<string, string> = { cantupdate: 'skip' }
20export const actionLabel = (action: string) => actionLabels[action] || action
22// Format: "name1, name2 (secret), name3 (deployment)" - groups consecutive same-kind
23const fmtNameKindList = (items: ResourceResult[]) => {
24 const byName = _.groupBy(items, 'name')
25 return _.map(byName, (nameItems, name) => {
26 const kinds = _.map(nameItems, r => shortKind(r.kind))
27 const kindCounts = _.countBy(kinds)
28 const kindStr = _.map(kindCounts, (count, kind) => count > 1 ? `${count}×${kind}` : kind).join(', ')
29 return `${name} (${kindStr})`
30 }).join(', ')
33// For delete: returns {deleted: [{cluster, line}], skipdel: [{cluster, line}]}
34export const fmtDeleteSummary = (results: (ResourceResult | null | undefined)[]) => {
35 const validResults = _.compact(results)
36 if (!validResults.length) return {deleted: [] as ClusterLine[], skipdel: [] as ClusterLine[]}
38 const byAction = _.groupBy(validResults, 'action')
39 const out: {deleted: ClusterLine[], skipdel: ClusterLine[]} = {deleted: [], skipdel: []}
41 for (const [action, actionResults] of Object.entries(byAction)) {
42 const key = action === 'skipdel' ? 'skipdel' : 'deleted'
43 const byCluster = _.groupBy(actionResults, 'cluster_name')
44 for (const [cluster, clusterResults] of Object.entries(byCluster)) {
45 out[key].push({cluster, line: fmtNameKindList(clusterResults)})
46 }
47 }
48 return out
51export const fmtApplySummary = (results: (ResourceResult | null | undefined)[]): ClusterActionLine[] => {
52 const validResults = _.compact(results)
53 if (!validResults.length) return []
55 const out: ClusterActionLine[] = []
56 const byCluster = _.groupBy(validResults, 'cluster_name')
57 for (const [cluster, clusterResults] of Object.entries(byCluster)) {
58 const byAction = _.groupBy(clusterResults, 'action')
59 for (const [action, actionResults] of Object.entries(byAction)) {
60 out.push({cluster, action, line: fmtNameKindList(actionResults)})
61 }
62 }
63 return out