|
| 1 | +package phases |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "os" |
| 6 | + "slices" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/databricks/cli/bundle" |
| 10 | + "github.com/databricks/cli/bundle/statemgmt/resourcestate" |
| 11 | + "github.com/databricks/cli/libs/log" |
| 12 | + "github.com/databricks/cli/libs/telemetry/protos" |
| 13 | +) |
| 14 | + |
| 15 | +// directEngine is the only engine for which resources_metadata is populated. |
| 16 | +const directEngine = "direct" |
| 17 | + |
| 18 | +// collectResourcesMetadata builds a BundleResourcesMetadata from the per-resource |
| 19 | +// state sizes captured during the deploy. |
| 20 | +// |
| 21 | +// Only direct deploys are measured. b.Metrics.ResourceState is the direct |
| 22 | +// engine's finalized state, populated in deployCore from the WAL replay the |
| 23 | +// deploy already performs; each entry carries StateSizeBytes (len of the JSON |
| 24 | +// blob stored in resources.json). So no marshalling, file read, or JSON parsing |
| 25 | +// happens here — sizes are read straight off the in-memory map. The whole-file |
| 26 | +// size comes from a single os.Stat (no parse). Returns nil for terraform |
| 27 | +// deploys (ResourceState is nil) and when no resources are in state. |
| 28 | +func collectResourcesMetadata(ctx context.Context, b *bundle.Bundle) *protos.BundleResourcesMetadata { |
| 29 | + state := b.Metrics.ResourceState |
| 30 | + if len(state) == 0 { |
| 31 | + return nil |
| 32 | + } |
| 33 | + |
| 34 | + resources := resourceMetadataFromState(state) |
| 35 | + if len(resources) == 0 { |
| 36 | + return nil |
| 37 | + } |
| 38 | + |
| 39 | + return &protos.BundleResourcesMetadata{ |
| 40 | + StateEngine: directEngine, |
| 41 | + StateFileSizeBytes: directStateFileSize(ctx, b), |
| 42 | + Resources: resources, |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +// resourceMetadataFromState groups the deployment state by resource type and |
| 47 | +// computes per-type count and size statistics from each entry's StateSizeBytes. |
| 48 | +func resourceMetadataFromState(state resourcestate.ExportedResourcesMap) []protos.ResourceMetadata { |
| 49 | + counts := make(map[string]int64) |
| 50 | + sizesByType := make(map[string][]int64) |
| 51 | + for key, rs := range state { |
| 52 | + t := resourceTypeFromKey(key) |
| 53 | + if t == "" { |
| 54 | + continue |
| 55 | + } |
| 56 | + counts[t]++ |
| 57 | + sizesByType[t] = append(sizesByType[t], int64(rs.StateSizeBytes)) |
| 58 | + } |
| 59 | + |
| 60 | + types := make([]string, 0, len(counts)) |
| 61 | + for t := range counts { |
| 62 | + types = append(types, t) |
| 63 | + } |
| 64 | + slices.Sort(types) |
| 65 | + |
| 66 | + resources := make([]protos.ResourceMetadata, 0, len(types)) |
| 67 | + for _, t := range types { |
| 68 | + sizes := sizesByType[t] |
| 69 | + slices.Sort(sizes) |
| 70 | + resources = append(resources, protos.ResourceMetadata{ |
| 71 | + ResourceType: t, |
| 72 | + Count: counts[t], |
| 73 | + StateSizeMaxBytes: statMax(sizes), |
| 74 | + StateSizeMeanBytes: statMean(sizes), |
| 75 | + StateSizeMedianBytes: statMedian(sizes), |
| 76 | + }) |
| 77 | + } |
| 78 | + return resources |
| 79 | +} |
| 80 | + |
| 81 | +// directStateFileSize returns the size in bytes of the direct engine's |
| 82 | +// resources.json via a single stat (no read/parse), or 0 if it can't be stat'd. |
| 83 | +func directStateFileSize(ctx context.Context, b *bundle.Bundle) int64 { |
| 84 | + _, localPath := b.StateFilenameDirect(ctx) |
| 85 | + info, err := os.Stat(localPath) |
| 86 | + if err != nil { |
| 87 | + log.Debugf(ctx, "resources-metadata telemetry: cannot stat direct state at %s: %s", localPath, err) |
| 88 | + return 0 |
| 89 | + } |
| 90 | + return info.Size() |
| 91 | +} |
| 92 | + |
| 93 | +// resourceTypeFromKey extracts the resource type from a direct-engine state |
| 94 | +// key. Keys are "resources.<type>.<name>", or "resources.<type>.<name>.<sub>" |
| 95 | +// for sub-resources like permissions / grants / secret_acls. Sub-resources are |
| 96 | +// tracked under the sub-resource type so they aggregate across resource |
| 97 | +// families. Returns "" for keys that don't match. |
| 98 | +func resourceTypeFromKey(key string) string { |
| 99 | + parts := strings.SplitN(key, ".", 4) |
| 100 | + if len(parts) < 3 || parts[0] != "resources" { |
| 101 | + return "" |
| 102 | + } |
| 103 | + if len(parts) == 4 { |
| 104 | + return parts[3] |
| 105 | + } |
| 106 | + return parts[1] |
| 107 | +} |
| 108 | + |
| 109 | +func statMax(sortedSizes []int64) int64 { |
| 110 | + if len(sortedSizes) == 0 { |
| 111 | + return 0 |
| 112 | + } |
| 113 | + return sortedSizes[len(sortedSizes)-1] |
| 114 | +} |
| 115 | + |
| 116 | +func statMean(sortedSizes []int64) int64 { |
| 117 | + if len(sortedSizes) == 0 { |
| 118 | + return 0 |
| 119 | + } |
| 120 | + var total int64 |
| 121 | + for _, s := range sortedSizes { |
| 122 | + total += s |
| 123 | + } |
| 124 | + return total / int64(len(sortedSizes)) |
| 125 | +} |
| 126 | + |
| 127 | +func statMedian(sortedSizes []int64) int64 { |
| 128 | + if len(sortedSizes) == 0 { |
| 129 | + return 0 |
| 130 | + } |
| 131 | + return sortedSizes[(len(sortedSizes)-1)/2] |
| 132 | +} |
0 commit comments