Skip to content

Commit 679a45a

Browse files
AKRAM@il.ibm.comAkramBitar
authored andcommitted
Fix formating issues
Signed-off-by: AKRAM@il.ibm.com <akram@fhe3.haifa.ibm.com> Signed-off-by: AkramBitar <akram@il.ibm.com>
1 parent c62bcd3 commit 679a45a

File tree

2 files changed

+40
-40
lines changed

2 files changed

+40
-40
lines changed

platform/common/utils/metrics.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,19 @@ func ExponentialBucketTimeRange(start, end time.Duration, buckets int) []float64
4242
if buckets <= 1 {
4343
return []float64{roundToSignificantDigits(start.Seconds(), 6)}
4444
}
45-
45+
4646
interval := end - start
4747
if interval <= 0 {
4848
return []float64{roundToSignificantDigits(start.Seconds(), 6)}
4949
}
50-
50+
5151
// Calculate factor more safely using Pow instead of Exp(Log(...))
5252
// This ensures we generate exactly 'buckets' number of buckets
5353
factor := math.Pow(float64(interval)/precision, 1.0/float64(buckets-1))
54-
54+
5555
bs := make([]float64, 0, buckets)
5656
bs = append(bs, roundToSignificantDigits(start.Seconds(), 6))
57-
57+
5858
// Generate exactly buckets-1 additional buckets
5959
for i := 1; i < buckets; i++ {
6060
v := time.Duration(math.Pow(factor, float64(i)) * precision)
@@ -64,7 +64,7 @@ func ExponentialBucketTimeRange(start, end time.Duration, buckets int) []float64
6464
// Round to 6 significant digits to avoid ugly floating-point representations
6565
bs = append(bs, roundToSignificantDigits((start+v).Seconds(), 6))
6666
}
67-
67+
6868
return bs
6969
}
7070

@@ -74,16 +74,16 @@ func roundToSignificantDigits(value float64, sigDigits int) float64 {
7474
if value == 0 {
7575
return 0
7676
}
77-
77+
7878
// Determine the order of magnitude
7979
magnitude := math.Floor(math.Log10(math.Abs(value)))
80-
80+
8181
// Calculate the scaling factor to get sigDigits significant figures
8282
scale := math.Pow(10, float64(sigDigits-1)-magnitude)
83-
83+
8484
// Round to significant digits
8585
rounded := math.Round(value*scale) / scale
86-
86+
8787
// Additional cleanup: round to remove floating-point artifacts
8888
// This handles cases like 0.0016681000000000001 -> 0.0016681
8989
if rounded != 0 {
@@ -94,6 +94,6 @@ func roundToSignificantDigits(value float64, sigDigits int) float64 {
9494
rounded = math.Round(rounded*multiplier) / multiplier
9595
}
9696
}
97-
97+
9898
return rounded
9999
}

platform/common/utils/metrics_test.go

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
func TestFix1_SaferCalculation_MaintainsExponentialSpacing(t *testing.T) {
1818
t.Run("Verify exponential spacing with constant ratio", func(t *testing.T) {
1919
buckets := ExponentialBucketTimeRange(0, 1*time.Second, 10)
20-
20+
2121
// Calculate ratios between consecutive buckets
2222
var ratios []float64
2323
for i := 2; i < len(buckets); i++ {
@@ -26,20 +26,20 @@ func TestFix1_SaferCalculation_MaintainsExponentialSpacing(t *testing.T) {
2626
ratios = append(ratios, ratio)
2727
}
2828
}
29-
29+
3030
// All ratios should be approximately equal (exponential spacing)
3131
if len(ratios) < 2 {
3232
t.Fatal("Not enough ratios to verify exponential spacing")
3333
}
34-
34+
3535
expectedRatio := ratios[0]
3636
for i, ratio := range ratios {
3737
diff := math.Abs(ratio - expectedRatio)
3838
if diff > 0.01 { // Allow 1% tolerance
3939
t.Errorf("Ratio %d: %.6f differs from expected %.6f by %.6f", i, ratio, expectedRatio, diff)
4040
}
4141
}
42-
42+
4343
t.Logf("✓ Exponential spacing confirmed: constant ratio = %.6f", expectedRatio)
4444
t.Logf(" Buckets: %v", buckets)
4545
})
@@ -59,11 +59,11 @@ func TestFix2_GuaranteedExactBucketCount(t *testing.T) {
5959
{"2 buckets", 0, 1 * time.Second, 2},
6060
{"20 buckets", 0, 10 * time.Second, 20},
6161
}
62-
62+
6363
for _, tc := range testCases {
6464
t.Run(tc.name, func(t *testing.T) {
6565
result := ExponentialBucketTimeRange(tc.start, tc.end, tc.buckets)
66-
66+
6767
if len(result) != tc.buckets {
6868
t.Errorf("Expected exactly %d buckets, got %d", tc.buckets, len(result))
6969
t.Errorf("Buckets: %v", result)
@@ -78,33 +78,33 @@ func TestFix2_GuaranteedExactBucketCount(t *testing.T) {
7878
func TestFix3_IndependentCalculation_NoErrorAccumulation(t *testing.T) {
7979
t.Run("Verify monotonically increasing values", func(t *testing.T) {
8080
buckets := ExponentialBucketTimeRange(0, 5*time.Second, 15)
81-
81+
8282
for i := 1; i < len(buckets); i++ {
8383
if buckets[i] <= buckets[i-1] {
84-
t.Errorf("Bucket %d (%.10f) is not greater than bucket %d (%.10f)",
84+
t.Errorf("Bucket %d (%.10f) is not greater than bucket %d (%.10f)",
8585
i, buckets[i], i-1, buckets[i-1])
8686
}
8787
}
88-
88+
8989
t.Logf("✓ All buckets monotonically increasing")
9090
})
91-
91+
9292
t.Run("Verify first and last buckets match start and end", func(t *testing.T) {
9393
start := 0 * time.Second
9494
end := 1 * time.Second
9595
buckets := ExponentialBucketTimeRange(start, end, 10)
96-
96+
9797
if buckets[0] != start.Seconds() {
9898
t.Errorf("First bucket %.10f != start %.10f", buckets[0], start.Seconds())
9999
}
100-
100+
101101
// Last bucket should be close to end (within rounding)
102102
diff := math.Abs(buckets[len(buckets)-1] - end.Seconds())
103103
if diff > 0.01 {
104-
t.Errorf("Last bucket %.10f differs from end %.10f by %.10f",
104+
t.Errorf("Last bucket %.10f differs from end %.10f by %.10f",
105105
buckets[len(buckets)-1], end.Seconds(), diff)
106106
}
107-
107+
108108
t.Logf("✓ First bucket = %.10f (start)", buckets[0])
109109
t.Logf("✓ Last bucket = %.10f (end = %.10f)", buckets[len(buckets)-1], end.Seconds())
110110
})
@@ -114,29 +114,29 @@ func TestFix3_IndependentCalculation_NoErrorAccumulation(t *testing.T) {
114114
func TestFix4_RoundingProducesCleanValues(t *testing.T) {
115115
t.Run("Verify Prometheus output format is clean", func(t *testing.T) {
116116
buckets := ExponentialBucketTimeRange(0, 1*time.Second, 10)
117-
117+
118118
for i, v := range buckets {
119119
// Format as Prometheus would (%g format)
120120
prometheusStr := fmt.Sprintf("%g", v)
121-
121+
122122
// Check for ugly patterns
123123
if len(prometheusStr) > 12 && v > 0 && v < 10 {
124124
t.Errorf("Bucket %d has potentially ugly Prometheus output: le=\"%s\"", i, prometheusStr)
125125
}
126-
126+
127127
t.Logf("Bucket %d: le=\"%s\" ✓", i, prometheusStr)
128128
}
129129
})
130-
130+
131131
t.Run("Compare internal vs Prometheus representation", func(t *testing.T) {
132132
buckets := ExponentialBucketTimeRange(0, 100*time.Millisecond, 10)
133-
133+
134134
t.Logf("\nInternal vs Prometheus representation:")
135135
for i, v := range buckets {
136136
internal := fmt.Sprintf("%.17g", v)
137137
prometheus := fmt.Sprintf("%g", v)
138138
t.Logf(" Bucket %d: internal=%.17g, prometheus=le=\"%s\"", i, v, prometheus)
139-
139+
140140
// Prometheus format should be shorter/cleaner
141141
if len(prometheus) > len(internal) {
142142
t.Errorf("Prometheus format longer than internal for bucket %d", i)
@@ -162,7 +162,7 @@ func TestFix5_EdgeCaseHandling(t *testing.T) {
162162
{"Start greater than end", 5 * time.Second, 1 * time.Second, 10, 1, false},
163163
{"Normal case", 0, 1 * time.Second, 10, 10, false},
164164
}
165-
165+
166166
for _, tc := range testCases {
167167
t.Run(tc.name, func(t *testing.T) {
168168
defer func() {
@@ -172,15 +172,15 @@ func TestFix5_EdgeCaseHandling(t *testing.T) {
172172
}
173173
}
174174
}()
175-
175+
176176
result := ExponentialBucketTimeRange(tc.start, tc.end, tc.buckets)
177-
177+
178178
if len(result) != tc.expectedLength {
179179
t.Errorf("Expected %d buckets, got %d", tc.expectedLength, len(result))
180180
} else {
181181
t.Logf("✓ Handled edge case correctly: got %d bucket(s)", len(result))
182182
}
183-
183+
184184
t.Logf(" Result: %v", result)
185185
})
186186
}
@@ -190,27 +190,27 @@ func TestFix5_EdgeCaseHandling(t *testing.T) {
190190
func TestAllFixes_Comprehensive(t *testing.T) {
191191
t.Run("10 buckets from 0 to 1 second", func(t *testing.T) {
192192
buckets := ExponentialBucketTimeRange(0, 1*time.Second, 10)
193-
193+
194194
// Fix #2: Exact count
195195
if len(buckets) != 10 {
196196
t.Errorf("Expected 10 buckets, got %d", len(buckets))
197197
}
198-
198+
199199
// Fix #3: Monotonic
200200
for i := 1; i < len(buckets); i++ {
201201
if buckets[i] <= buckets[i-1] {
202202
t.Errorf("Not monotonic at index %d", i)
203203
}
204204
}
205-
205+
206206
// Fix #1: Exponential spacing
207207
var ratios []float64
208208
for i := 2; i < len(buckets); i++ {
209209
if buckets[i-1] > 0 {
210210
ratios = append(ratios, buckets[i]/buckets[i-1])
211211
}
212212
}
213-
213+
214214
if len(ratios) > 1 {
215215
avgRatio := ratios[0]
216216
for _, r := range ratios {
@@ -219,15 +219,15 @@ func TestAllFixes_Comprehensive(t *testing.T) {
219219
}
220220
}
221221
}
222-
222+
223223
// Fix #4: Clean Prometheus output
224224
for i, v := range buckets {
225225
prometheusStr := fmt.Sprintf("%g", v)
226226
if len(prometheusStr) > 12 && v > 0 && v < 10 {
227227
t.Errorf("Bucket %d has ugly output: %s", i, prometheusStr)
228228
}
229229
}
230-
230+
231231
t.Logf("✓ All fixes verified")
232232
t.Logf(" Buckets: %v", buckets)
233233
t.Logf(" Exponential ratio: %.6f", ratios[0])

0 commit comments

Comments
 (0)