Skip to content
This repository was archived by the owner on May 24, 2024. It is now read-only.

Commit a47298a

Browse files
Ajinkya Naharajinkyan83
authored andcommitted
Add common utility methods for calculations
1 parent 6c24c10 commit a47298a

6 files changed

Lines changed: 291 additions & 1 deletion

File tree

compute/calc.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package compute
2+
3+
const (
4+
// Up label
5+
Up string = "UP"
6+
// Down label
7+
Down string = "DOWN"
8+
// Flat label
9+
Flat string = "SAME"
10+
)
11+
12+
// GetStatsComparison returns the comparison of 2 metrics
13+
func GetStatsComparison(currentStats float64, previousStats float64) string {
14+
if currentStats == previousStats {
15+
return Flat
16+
} else if currentStats < previousStats {
17+
return Down
18+
} else {
19+
return Up
20+
}
21+
}
22+
23+
// CalculatePercent based on numerator & denominator values
24+
func CalculatePercent(numerator float64, denominator float64) float64 {
25+
if denominator == 0 {
26+
return 0.0
27+
}
28+
return (numerator / denominator) * 100
29+
}
30+
31+
// Subtract num2 from num1
32+
func Subtract(num1 float64, num2 float64) float64 {
33+
if num2 > num1 {
34+
return 0.0
35+
}
36+
return (num1 - num2)
37+
}
38+
39+
// Ratio of 2 numbers
40+
func Ratio(numerator float64, denominator float64) float64 {
41+
if denominator == 0 {
42+
return 0.0
43+
}
44+
return (float64(numerator) / float64(denominator))
45+
}
46+
47+
// SumStats sum each element of a given slice and return the result
48+
func SumStats(stats []int64) float64 {
49+
result := 0.0
50+
for _, v := range stats {
51+
result += float64(v)
52+
}
53+
return result
54+
}
55+
56+
// AggregateStats sum each element of the 2 different stats and returns the results.
57+
func AggregateStats(stats1 []int64, stats2 []int64) []int64 {
58+
if len(stats1) > 0 && len(stats2) > 0 && len(stats2) == len(stats1) {
59+
for i := 0; i < len(stats1); i++ {
60+
stats1[i] = stats1[i] + stats2[i]
61+
}
62+
}
63+
64+
return stats1
65+
}

compute/calc_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package compute
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
// Check the subtract util method returns expected computed value
10+
func TestSubtract(t *testing.T) {
11+
result := Subtract(540.00, 230.00)
12+
assert.Equal(t, result, 310.00)
13+
14+
result = Subtract(140.00, 230.00)
15+
assert.Equal(t, result, 0.00)
16+
17+
result = Subtract(340.00, 230.00)
18+
assert.NotEqual(t, result, 111.00)
19+
20+
}
21+
22+
// Check the calculate percent util method returns expected computed value
23+
func TestCalculatePercent(t *testing.T) {
24+
result := CalculatePercent(500.00, 1000.00)
25+
assert.Equal(t, result, 50.00)
26+
27+
result = CalculatePercent(500.00, 0.00)
28+
assert.Equal(t, result, 0.00)
29+
30+
}
31+
32+
// Check the ratio util method returns expected computed value
33+
func TestRatio(t *testing.T) {
34+
result := Ratio(500.00, 1000.00)
35+
assert.Equal(t, result, 0.50)
36+
37+
result = Ratio(750.00, 1000.00)
38+
assert.Equal(t, result, 0.75)
39+
40+
result = Ratio(1500.00, 1000.00)
41+
assert.Equal(t, result, 1.5)
42+
43+
result = Ratio(500.00, 0.00)
44+
assert.Equal(t, result, 0.00)
45+
46+
}
47+
48+
// Check the stats comparison util method returns expected trend for different values
49+
func TestStatsComparison(t *testing.T) {
50+
assert.Equal(t, Up, "UP")
51+
assert.Equal(t, Down, "DOWN")
52+
assert.Equal(t, Flat, "SAME")
53+
54+
result := GetStatsComparison(1200.00, 1000.00)
55+
assert.Equal(t, result, Up)
56+
57+
result = GetStatsComparison(750.00, 1000.00)
58+
assert.Equal(t, result, Down)
59+
60+
result = GetStatsComparison(1500.00, 1500.00)
61+
assert.Equal(t, Flat, result)
62+
63+
}

compute/format.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package compute
2+
3+
import (
4+
"fmt"
5+
"math"
6+
"strconv"
7+
8+
"github.com/labstack/gommon/log"
9+
)
10+
11+
// NearestThousandFormat returns a string with %.2f<K/M/B>
12+
func NearestThousandFormat(count float64) string {
13+
var suffix = []string{"K", "M", "B", "T", "Qd", "Qt"}
14+
var formattedStr string
15+
if count < 1000 {
16+
return fmt.Sprintf("%d", int64(count))
17+
}
18+
exp := (int64)(math.Log(count) / math.Log(1000))
19+
if exp <= int64(len(suffix)) {
20+
formattedStr = fmt.Sprintf("%.2f%s", count/math.Pow(1000, float64(exp)), suffix[exp-1])
21+
}
22+
return formattedStr
23+
}
24+
25+
// RawNumberFormat returns a string with %.2f<K/M/B>
26+
func RawNumberFormat(metricValue string) int64 {
27+
if len(metricValue) <= 0 {
28+
return 0
29+
}
30+
var numberVal int64
31+
if len(metricValue) == 1 {
32+
numberVal, err := strconv.ParseInt(metricValue, 0, 64)
33+
if err != nil {
34+
log.Warnf("Failed converting to raw number format: %+v", err)
35+
}
36+
return numberVal
37+
}
38+
var metricValueWithoutSuffix, err = strconv.ParseFloat(metricValue[:len(metricValue)-1], 64)
39+
if err != nil {
40+
log.Fatalf("Failed converting to raw number format %+v", err)
41+
}
42+
switch metricValue[len(metricValue)-1:] {
43+
case "K":
44+
numberVal = int64(float64(metricValueWithoutSuffix) * math.Pow(1000, 1.00))
45+
case "M":
46+
numberVal = int64(float64(metricValueWithoutSuffix) * math.Pow(1000, 2.00))
47+
case "B":
48+
numberVal = int64(float64(metricValueWithoutSuffix) * math.Pow(1000, 3.00))
49+
case "T":
50+
numberVal = int64(float64(metricValueWithoutSuffix) * math.Pow(1000, 4.00))
51+
case "Qd":
52+
numberVal = int64(float64(metricValueWithoutSuffix) * math.Pow(1000, 5.00))
53+
case "Qt":
54+
numberVal = int64(float64(metricValueWithoutSuffix) * math.Pow(1000, 6.00))
55+
default:
56+
numberVal, err := strconv.ParseInt(metricValue, 0, 64)
57+
if err != nil {
58+
log.Fatalf("Failed converting to raw number format: %+v", err)
59+
}
60+
return numberVal
61+
}
62+
63+
return numberVal
64+
}

compute/format_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package compute
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestRawNumberFormat(t *testing.T) {
8+
type args struct {
9+
metricValue string
10+
}
11+
tests := []struct {
12+
name string
13+
args args
14+
want int64
15+
}{
16+
{
17+
name: "109",
18+
args: args{metricValue: "109"},
19+
want: 109,
20+
}, {
21+
name: "10.39K",
22+
args: args{metricValue: "10.39K"},
23+
want: 10390,
24+
}, {
25+
name: "145.78K",
26+
args: args{metricValue: "145.78K"},
27+
want: 145780,
28+
}, {
29+
name: "1M",
30+
args: args{metricValue: "1M"},
31+
want: 1000000,
32+
}, {
33+
name: "98.87M",
34+
args: args{metricValue: "98.87M"},
35+
want: 98870000,
36+
}, {
37+
name: "786.96M",
38+
args: args{metricValue: "786.96M"},
39+
want: 786960000,
40+
}, {
41+
name: "1B",
42+
args: args{metricValue: "1.00B"},
43+
want: 1000000000,
44+
}, {
45+
name: "12.65B",
46+
args: args{metricValue: "12.65B"},
47+
want: 12650000000,
48+
}, {
49+
name: "249.06B",
50+
args: args{metricValue: "249.06B"},
51+
want: 249060000000,
52+
}, {
53+
name: "1 trillion",
54+
args: args{metricValue: "1T"},
55+
want: 1000000000000,
56+
}, {
57+
name: "653.57 Trillion",
58+
args: args{metricValue: "653.57T"},
59+
want: 653570000000000,
60+
},
61+
}
62+
for _, tt := range tests {
63+
t.Run(tt.name, func(t *testing.T) {
64+
if got := RawNumberFormat(tt.args.metricValue); got != tt.want {
65+
t.Errorf("RawNumberFormat() = %v, want %v", got, tt.want)
66+
}
67+
})
68+
}
69+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ go 1.15
55
require (
66
github.com/avast/retry-go v3.0.0+incompatible
77
github.com/aws/aws-sdk-go v1.36.15
8-
github.com/aws/aws-sdk-go-v2 v1.3.1 // indirect
98
github.com/aws/aws-sdk-go-v2/config v1.1.4
109
github.com/aws/aws-sdk-go-v2/service/ssm v1.3.0
1110
github.com/dgrijalva/jwt-go v3.2.0+incompatible
@@ -14,6 +13,7 @@ require (
1413
github.com/google/go-github/v33 v33.0.0
1514
github.com/google/uuid v1.1.4
1615
github.com/json-iterator/go v1.1.10
16+
github.com/labstack/gommon v0.3.0
1717
github.com/pkg/errors v0.9.1
1818
github.com/sirupsen/logrus v1.7.0
1919
github.com/stretchr/testify v1.6.1

0 commit comments

Comments
 (0)