-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: aggregation functions support for metrics controller (#1802)
Signed-off-by: Rakshit Gondwal <rakshitgondwal3@gmail.com> Signed-off-by: Rakshit Gondwal <98955085+rakshitgondwal@users.noreply.github.com>
- Loading branch information
1 parent
68b411e
commit 678c4c9
Showing
5 changed files
with
400 additions
and
5 deletions.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
metrics-operator/controllers/common/aggregation/aggregation.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package aggregation | ||
|
||
import ( | ||
"math" | ||
"sort" | ||
) | ||
|
||
func CalculateMax(values []float64) float64 { | ||
if len(values) == 0 { | ||
return 0.0 | ||
} | ||
|
||
max := values[0] | ||
for _, value := range values { | ||
if value > max { | ||
max = value | ||
} | ||
} | ||
return max | ||
} | ||
|
||
func CalculateMin(values []float64) float64 { | ||
if len(values) == 0 { | ||
return 0.0 | ||
} | ||
|
||
min := values[0] | ||
for _, value := range values { | ||
if value < min { | ||
min = value | ||
} | ||
} | ||
return min | ||
} | ||
|
||
func CalculateMedian(values []float64) float64 { | ||
if len(values) == 0 { | ||
return 0.0 | ||
} | ||
|
||
// Sort the values | ||
sortedValues := make([]float64, len(values)) | ||
copy(sortedValues, values) | ||
sort.Float64s(sortedValues) | ||
|
||
// Calculate the median | ||
middle := len(sortedValues) / 2 | ||
if len(sortedValues)%2 == 0 { | ||
return (sortedValues[middle-1] + sortedValues[middle]) / 2 | ||
} | ||
return sortedValues[middle] | ||
} | ||
|
||
func CalculateAverage(values []float64) float64 { | ||
sum := 0.0 | ||
|
||
for _, value := range values { | ||
sum += value | ||
} | ||
if len(values) > 0 { | ||
return sum / float64(len(values)) | ||
} | ||
|
||
return 0.0 | ||
} | ||
|
||
func CalculatePercentile(values sort.Float64Slice, perc float64) float64 { | ||
if len(values) == 0 { | ||
return 0.0 | ||
} | ||
// Calculate the index for the requested percentile | ||
i := math.Ceil(float64(len(values)) * perc / 100) | ||
|
||
return values[int(i-1)] | ||
} |
57 changes: 55 additions & 2 deletions
57
metrics-operator/controllers/common/providers/fake/provider_mock.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.