Skip to content

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

chore: move converter/formatter to pkg/units/... (#10408)

Srikanth Chekuri43933f3a3357
diff --git a/ee/query-service/rules/anomaly.go b/ee/query-service/rules/anomaly.go
index 863b819b47..8fd4432c38 100644
--- a/ee/query-service/rules/anomaly.go
+++ b/ee/query-service/rules/anomaly.go
@@ -26,7 +26,7 @@ import (
"github.com/SigNoz/signoz/pkg/query-service/utils/times"
"github.com/SigNoz/signoz/pkg/query-service/utils/timestamp"
- "github.com/SigNoz/signoz/pkg/query-service/formatter"
+ "github.com/SigNoz/signoz/pkg/units"
baserules "github.com/SigNoz/signoz/pkg/query-service/rules"
@@ -335,7 +335,7 @@ func (r *AnomalyRule) Eval(ctx context.Context, ts time.Time) (int, error) {
prevState := r.State()
- valueFormatter := formatter.FromUnit(r.Unit())
+ valueFormatter := units.FormatterFromUnit(r.Unit())
var res ruletypes.Vector
var err error
diff --git a/pkg/query-service/formatter/data.go b/pkg/query-service/formatter/data.go
deleted file mode 100644
index c618e3b07f..0000000000
--- a/pkg/query-service/formatter/data.go
+++ /dev/null
@@ -1,86 +0,0 @@
-package formatter
-
-import (
- "fmt"
-
- "github.com/SigNoz/signoz/pkg/query-service/converter"
- "github.com/dustin/go-humanize"
-)
-
-type dataFormatter struct {
-}
-
-func NewDataFormatter() Formatter {
- return &dataFormatter{}
-}
-
-func (*dataFormatter) Name() string {
- return "data"
-}
-
-func (f *dataFormatter) Format(value float64, unit string) string {
- switch unit {
- case "bytes", "By":
- return humanize.IBytes(uint64(value))
- case "decbytes":
- return humanize.Bytes(uint64(value))
- case "bits", "bit":
- // humanize.IBytes/Bytes doesn't support bits
- // and returns 0 B for values less than a byte
- if value < 8 {
- return fmt.Sprintf("%v b", value)
- }
- return humanize.IBytes(uint64(value / 8))
- case "decbits":
- if value < 8 {
- return fmt.Sprintf("%v b", value)
- }
- return humanize.Bytes(uint64(value / 8))
- case "kbytes", "KiBy":
- return humanize.IBytes(uint64(value * converter.Kibibit))
- case "Kibit":
- return humanize.IBytes(uint64(value * converter.Kibibit / 8))
- case "decKbytes", "deckbytes", "kBy":
- return humanize.Bytes(uint64(value * converter.Kilobit))
- case "kbit":
- return humanize.Bytes(uint64(value * converter.Kilobit / 8))
- case "mbytes", "MiBy":
- return humanize.IBytes(uint64(value * converter.Mebibit))
- case "Mibit":
- return humanize.IBytes(uint64(value * converter.Mebibit / 8))
- case "decMbytes", "decmbytes", "MBy":
- return humanize.Bytes(uint64(value * converter.Megabit))
- case "Mbit":
- return humanize.Bytes(uint64(value * converter.Megabit / 8))
- case "gbytes", "GiBy":
- return humanize.IBytes(uint64(value * converter.Gibibit))
- case "Gibit":
- return humanize.IBytes(uint64(value * converter.Gibibit / 8))
- case "decGbytes", "decgbytes", "GBy":
- return humanize.Bytes(uint64(value * converter.Gigabit))
- case "Gbit":
- return humanize.Bytes(uint64(value * converter.Gigabit / 8))
- case "tbytes", "TiBy":
- return humanize.IBytes(uint64(value * converter.Tebibit))
- case "Tibit":
- return humanize.IBytes(uint64(value * converter.Tebibit / 8))
- case "decTbytes", "dectbytes", "TBy":
- return humanize.Bytes(uint64(value * converter.Terabit))
- case "Tbit":
- return humanize.Bytes(uint64(value * converter.Terabit / 8))
- case "pbytes", "PiBy":
- return humanize.IBytes(uint64(value * converter.Pebibit))
- case "Pbit":
- return humanize.Bytes(uint64(value * converter.Petabit / 8))
- case "decPbytes", "decpbytes", "PBy":
- return humanize.Bytes(uint64(value * converter.Petabit))
- case "EiBy":
- return humanize.IBytes(uint64(value * converter.Exbibit))
- case "Ebit":
- return humanize.Bytes(uint64(value * converter.Exabit / 8))
- case "EBy":
- return humanize.Bytes(uint64(value * converter.Exabit))
- }
- // When unit is not matched, return the value as it is.
- return fmt.Sprintf("%v", value)
-}
diff --git a/pkg/query-service/formatter/data_rate.go b/pkg/query-service/formatter/data_rate.go
deleted file mode 100644
index 2c1cfd4be0..0000000000
--- a/pkg/query-service/formatter/data_rate.go
+++ /dev/null
@@ -1,91 +0,0 @@
-package formatter
-
-import (
- "fmt"
-
- "github.com/SigNoz/signoz/pkg/query-service/converter"
- "github.com/dustin/go-humanize"
-)
-
-type dataRateFormatter struct {
-}
-
-func NewDataRateFormatter() Formatter {
- return &dataRateFormatter{}
-}
-
-func (*dataRateFormatter) Name() string {
- return "data_rate"
-}
-
-func (f *dataRateFormatter) Format(value float64, unit string) string {
- switch unit {
- case "binBps":
- return humanize.IBytes(uint64(value)) + "/s"
- case "Bps", "By/s":
- return humanize.Bytes(uint64(value)) + "/s"
- case "binbps":
- // humanize.IBytes/Bytes doesn't support bits
- // and returns 0 B for values less than a byte
- if value < 8 {
- return fmt.Sprintf("%v b/s", value)
- }
- return humanize.IBytes(uint64(value/8)) + "/s"
- case "bps", "bit/s":
- if value < 8 {
- return fmt.Sprintf("%v b/s", value)
- }
- return humanize.Bytes(uint64(value/8)) + "/s"
- case "KiBs", "KiBy/s":
- return humanize.IBytes(uint64(value*converter.KibibitPerSecond)) + "/s"
- case "Kibits", "Kibit/s":
- return humanize.IBytes(uint64(value*converter.KibibitPerSecond/8)) + "/s"
- case "KBs", "kBy/s":
- return humanize.IBytes(uint64(value*converter.KilobitPerSecond)) + "/s"
- case "Kbits", "kbit/s":
- return humanize.Bytes(uint64(value*converter.KilobitPerSecond/8)) + "/s"
- case "MiBs", "MiBy/s":
- return humanize.IBytes(uint64(value*converter.MebibitPerSecond)) + "/s"
- case "Mibits", "Mibit/s":
- return humanize.IBytes(uint64(value*converter.MebibitPerSecond/8)) + "/s"
- case "MBs", "MBy/s":
- return humanize.IBytes(uint64(value*converter.MegabitPerSecond)) + "/s"
- case "Mbits", "Mbit/s":
- return humanize.Bytes(uint64(value*converter.MegabitPerSecond/8)) + "/s"
- case "GiBs", "GiBy/s":
- return humanize.IBytes(uint64(value*converter.GibibitPerSecond)) + "/s"
- case "Gibits", "Gibit/s":
- return humanize.IBytes(uint64(value*converter.GibibitPerSecond/8)) + "/s"
- case "GBs", "GBy/s":
- return humanize.IBytes(uint64(value*converter.GigabitPerSecond)) + "/s"
- case "Gbits", "Gbit/s":
- return humanize.Bytes(uint64(value*converter.GigabitPerSecond/8)) + "/s"
- case "TiBs", "TiBy/s":
- return humanize.IBytes(uint64(value*converter.TebibitPerSecond)) + "/s"
- case "Tibits", "Tibit/s":
- return humanize.IBytes(uint64(value*converter.TebibitPerSecond/8)) + "/s"
- case "TBs", "TBy/s":
- return humanize.IBytes(uint64(value*converter.TerabitPerSecond)) + "/s"
- case "Tbits", "Tbit/s":
- return humanize.Bytes(uint64(value*converter.TerabitPerSecond/8)) + "/s"
- case "PiBs", "PiBy/s":
- return humanize.IBytes(uint64(value*converter.PebibitPerSecond)) + "/s"
- case "Pibits", "Pibit/s":
- return humanize.IBytes(uint64(value*converter.PebibitPerSecond/8)) + "/s"
- case "PBs", "PBy/s":
- return humanize.IBytes(uint64(value*converter.PetabitPerSecond)) + "/s"
- case "Pbits", "Pbit/s":
- return humanize.Bytes(uint64(value*converter.PetabitPerSecond/8)) + "/s"
- // Exa units
- case "EBy/s":
- return humanize.Bytes(uint64(value*converter.ExabitPerSecond)) + "/s"
- case "Ebit/s":
- return humanize.Bytes(uint64(value*converter.ExabitPerSecond/8)) + "/s"
- case "EiBy/s":
- return humanize.IBytes(uint64(value*converter.ExbibitPerSecond)) + "/s"
- case "Eibit/s":
- return humanize.IBytes(uint64(value*converter.ExbibitPerSecond/8)) + "/s"
- }
- // When unit is not matched, return the value as it is.
- return fmt.Sprintf("%v", value)
-}
diff --git a/pkg/query-service/rules/prom_rule.go b/pkg/query-service/rules/prom_rule.go
index 610d3b17c2..a7a97fd1ae 100644
--- a/pkg/query-service/rules/prom_rule.go
+++ b/pkg/query-service/rules/prom_rule.go
@@ -9,7 +9,7 @@ import (
"github.com/SigNoz/signoz/pkg/errors"
"github.com/SigNoz/signoz/pkg/prometheus"
- "github.com/SigNoz/signoz/pkg/query-service/formatter"
+ "github.com/SigNoz/signoz/pkg/units"
"github.com/SigNoz/signoz/pkg/query-service/interfaces"
"github.com/SigNoz/signoz/pkg/query-service/model"
v3 "github.com/SigNoz/signoz/pkg/query-service/model/v3"
@@ -185,7 +185,7 @@ func (r *PromRule) buildAndRunQuery(ctx context.Context, ts time.Time) (ruletype
func (r *PromRule) Eval(ctx context.Context, ts time.Time) (int, error) {
prevState := r.State()
- valueFormatter := formatter.FromUnit(r.Unit())
+ valueFormatter := units.FormatterFromUnit(r.Unit())
// prepare query, run query get data and filter the data based on the threshold
results, err := r.buildAndRunQuery(ctx, ts)
diff --git a/pkg/query-service/rules/threshold_rule.go b/pkg/query-service/rules/threshold_rule.go
index aa017f3339..0ebb37cabb 100644
--- a/pkg/query-service/rules/threshold_rule.go
+++ b/pkg/query-service/rules/threshold_rule.go
@@ -33,7 +33,7 @@ import (
logsv3 "github.com/SigNoz/signoz/pkg/query-service/app/logs/v3"
tracesV4 "github.com/SigNoz/signoz/pkg/query-service/app/traces/v4"
- "github.com/SigNoz/signoz/pkg/query-service/formatter"
+ "github.com/SigNoz/signoz/pkg/units"
querierV5 "github.com/SigNoz/signoz/pkg/querier"
@@ -571,7 +571,7 @@ func (r *ThresholdRule) buildAndRunQueryV5(ctx context.Context, orgID valuer.UUI
func (r *ThresholdRule) Eval(ctx context.Context, ts time.Time) (int, error) {
prevState := r.State()
- valueFormatter := formatter.FromUnit(r.Unit())
+ valueFormatter := units.FormatterFromUnit(r.Unit())
var res ruletypes.Vector
var err error
diff --git a/pkg/types/ruletypes/threshold.go b/pkg/types/ruletypes/threshold.go
index c503f8c9df..3dc4ddc529 100644
--- a/pkg/types/ruletypes/threshold.go
+++ b/pkg/types/ruletypes/threshold.go
@@ -7,7 +7,7 @@ import (
"strings"
"github.com/SigNoz/signoz/pkg/errors"
- "github.com/SigNoz/signoz/pkg/query-service/converter"
+ "github.com/SigNoz/signoz/pkg/units"
v3 "github.com/SigNoz/signoz/pkg/query-service/model/v3"
"github.com/SigNoz/signoz/pkg/query-service/utils/labels"
"github.com/SigNoz/signoz/pkg/valuer"
@@ -201,12 +201,12 @@ func sortThresholds(thresholds []BasicRuleThreshold) {
// convertToRuleUnit converts the given value from the target unit to the rule unit
func (b BasicRuleThreshold) convertToRuleUnit(val float64, ruleUnit string) float64 {
- unitConverter := converter.FromUnit(converter.Unit(b.TargetUnit))
+ unitConverter := units.ConverterFromUnit(units.Unit(b.TargetUnit))
// convert the target value to the y-axis unit
- value := unitConverter.Convert(converter.Value{
+ value := unitConverter.Convert(units.Value{
F: val,
- U: converter.Unit(b.TargetUnit),
- }, converter.Unit(ruleUnit))
+ U: units.Unit(b.TargetUnit),
+ }, units.Unit(ruleUnit))
return value.F
}
diff --git a/pkg/query-service/converter/converter.go b/pkg/units/converter.go
similarity index 97%
rename from pkg/query-service/converter/converter.go
rename to pkg/units/converter.go
index 1584116ca2..185edf5d52 100644
--- a/pkg/query-service/converter/converter.go
+++ b/pkg/units/converter.go
@@ -1,4 +1,4 @@
-package converter
+package units
// Unit represents a unit of measurement
type Unit string
@@ -40,8 +40,8 @@ var (
NoneConverter = &noneConverter{}
)
-// FromUnit returns a converter for the given unit
-func FromUnit(u Unit) Converter {
+// ConverterFromUnit returns a converter for the given unit
+func ConverterFromUnit(u Unit) Converter {
switch u {
case "ns", "us", "µs", "ms", "s", "m", "h", "d", "min", "w", "wk":
return DurationConverter
diff --git a/pkg/query-service/converter/bool.go b/pkg/units/converter_bool.go
similarity index 95%
rename from pkg/query-service/converter/bool.go
rename to pkg/units/converter_bool.go
index 74d6bb78a6..81d4fe1977 100644
--- a/pkg/query-service/converter/bool.go
+++ b/pkg/units/converter_bool.go
@@ -1,4 +1,4 @@
-package converter
+package units
// boolConverter is a Converter implementation for bool
type boolConverter struct{}
diff --git a/pkg/query-service/converter/data.go b/pkg/units/converter_data.go
similarity index 99%
rename from pkg/query-service/converter/data.go
rename to pkg/units/converter_data.go
index 0502bd1bdc..772f701613 100644
--- a/pkg/query-service/converter/data.go
+++ b/pkg/units/converter_data.go
@@ -1,4 +1,4 @@
-package converter
+package units
const (
// base 10 (SI prefixes)
diff --git a/pkg/query-service/converter/data_rate.go b/pkg/units/converter_data_rate.go
similarity index 99%
rename from pkg/query-service/converter/data_rate.go
rename to pkg/units/converter_data_rate.go
index e135bfcc5d..f99506cabe 100644
--- a/pkg/query-service/converter/data_rate.go
+++ b/pkg/units/converter_data_rate.go
@@ -1,4 +1,4 @@
-package converter
+package units
const (
// base 10 (SI prefixes)
diff --git a/pkg/query-service/converter/data_rate_test.go b/pkg/units/converter_data_rate_test.go
similarity index 99%
rename from pkg/query-service/converter/data_rate_test.go
rename to pkg/units/converter_data_rate_test.go
index b1cf300cfc..f879961606 100644
--- a/pkg/query-service/converter/data_rate_test.go
+++ b/pkg/units/converter_data_rate_test.go
@@ -1,4 +1,4 @@
-package converter
+package units
import (
"testing"
diff --git a/pkg/query-service/converter/data_test.go b/pkg/units/converter_data_test.go
similarity index 99%
rename from pkg/query-service/converter/data_test.go
rename to pkg/units/converter_data_test.go
index 4e6a4ed2c5..6dccbf75ad 100644
--- a/pkg/query-service/converter/data_test.go
+++ b/pkg/units/converter_data_test.go
@@ -1,4 +1,4 @@
-package converter
+package units
import (
"testing"
diff --git a/pkg/query-service/converter/percent_converter.go b/pkg/units/converter_percent.go
similarity index 96%
rename from pkg/query-service/converter/percent_converter.go
rename to pkg/units/converter_percent.go
index 275d2e2cb6..7748695785 100644
--- a/pkg/query-service/converter/percent_converter.go
+++ b/pkg/units/converter_percent.go
@@ -1,4 +1,4 @@
-package converter
+package units
// percentConverter is a converter for percent unit
type percentConverter struct{}
diff --git a/pkg/query-service/converter/percent_converter_test.go b/pkg/units/converter_percent_test.go
similarity index 97%
rename from pkg/query-service/converter/percent_converter_test.go
rename to pkg/units/converter_percent_test.go
index 7447aa8370..652aa3b65d 100644
--- a/pkg/query-service/converter/percent_converter_test.go
+++ b/pkg/units/converter_percent_test.go
@@ -1,4 +1,4 @@
-package converter
+package units
import (
"testing"
diff --git a/pkg/query-service/converter/throughput.go b/pkg/units/converter_throughput.go
similarity index 95%
rename from pkg/query-service/converter/throughput.go
rename to pkg/units/converter_throughput.go
index 9fdb4703db..eb6ddc6542 100644
--- a/pkg/query-service/converter/throughput.go
+++ b/pkg/units/converter_throughput.go
@@ -1,4 +1,4 @@
-package converter
+package units
// throughputConverter is an implementation of Converter that converts throughput
type throughputConverter struct {
diff --git a/pkg/query-service/converter/time.go b/pkg/units/converter_time.go
similarity index 98%
rename from pkg/query-service/converter/time.go
rename to pkg/units/converter_time.go
index 3971c39359..30c7b3add6 100644
--- a/pkg/query-service/converter/time.go
+++ b/pkg/units/converter_time.go
@@ -1,4 +1,4 @@
-package converter
+package units
type Duration float64
diff --git a/pkg/query-service/converter/time_test.go b/pkg/units/converter_time_test.go
similarity index 99%
rename from pkg/query-service/converter/time_test.go
rename to pkg/units/converter_time_test.go
index b2e63abdd4..a918d396b8 100644
--- a/pkg/query-service/converter/time_test.go
+++ b/pkg/units/converter_time_test.go
@@ -1,4 +1,4 @@
-package converter
+package units
import (
"testing"
diff --git a/pkg/query-service/formatter/formatter.go b/pkg/units/formatter.go
similarity index 96%
rename from pkg/query-service/formatter/formatter.go
rename to pkg/units/formatter.go
index 078a96d4cd..055c3a45ee 100644
--- a/pkg/query-service/formatter/formatter.go
+++ b/pkg/units/formatter.go
@@ -1,4 +1,4 @@
-package formatter
+package units
type Formatter interface {
Format(value float64, unit string) string
@@ -16,7 +16,7 @@ var (
ThroughputFormatter = NewThroughputFormatter()
)
-func FromUnit(u string) Formatter {
+func FormatterFromUnit(u string) Formatter {
switch u {
case "ns", "us", "µs", "ms", "s", "m", "h", "d", "min", "w", "wk":
return DurationFormatter
diff --git a/pkg/query-service/formatter/bool.go b/pkg/units/formatter_bool.go
similarity index 97%
rename from pkg/query-service/formatter/bool.go
rename to pkg/units/formatter_bool.go
index 74952cde0e..e74dd7285c 100644
--- a/pkg/query-service/formatter/bool.go
+++ b/pkg/units/formatter_bool.go
@@ -1,4 +1,4 @@
-package formatter
+package units
import "fmt"
diff --git a/pkg/units/formatter_data.go b/pkg/units/formatter_data.go
new file mode 100644
index 0000000000..a3499c70d3
--- /dev/null
+++ b/pkg/units/formatter_data.go
@@ -0,0 +1,85 @@
+package units
+
+import (
+ "fmt"
+
+ "github.com/dustin/go-humanize"
+)
+
+type dataFormatter struct {
+}
+
+func NewDataFormatter() Formatter {
+ return &dataFormatter{}
+}
+
+func (*dataFormatter) Name() string {
+ return "data"
+}
+
+func (f *dataFormatter) Format(value float64, unit string) string {
+ switch unit {
+ case "bytes", "By":
+ return humanize.IBytes(uint64(value))
+ case "decbytes":
+ return humanize.Bytes(uint64(value))
+ case "bits", "bit":
+ // humanize.IBytes/Bytes doesn't support bits
+ // and returns 0 B for values less than a byte
+ if value < 8 {
+ return fmt.Sprintf("%v b", value)
+ }
+ return humanize.IBytes(uint64(value / 8))
+ case "decbits":
+ if value < 8 {
+ return fmt.Sprintf("%v b", value)
+ }
+ return humanize.Bytes(uint64(value / 8))
+ case "kbytes", "KiBy":
+ return humanize.IBytes(uint64(value * Kibibit))
+ case "Kibit":
+ return humanize.IBytes(uint64(value * Kibibit / 8))
+ case "decKbytes", "deckbytes", "kBy":
+ return humanize.Bytes(uint64(value * Kilobit))
+ case "kbit":
+ return humanize.Bytes(uint64(value * Kilobit / 8))
+ case "mbytes", "MiBy":
+ return humanize.IBytes(uint64(value * Mebibit))
+ case "Mibit":
+ return humanize.IBytes(uint64(value * Mebibit / 8))
+ case "decMbytes", "decmbytes", "MBy":
+ return humanize.Bytes(uint64(value * Megabit))
+ case "Mbit":
+ return humanize.Bytes(uint64(value * Megabit / 8))
+ case "gbytes", "GiBy":
+ return humanize.IBytes(uint64(value * Gibibit))
+ case "Gibit":
+ return humanize.IBytes(uint64(value * Gibibit / 8))
+ case "decGbytes", "decgbytes", "GBy":
+ return humanize.Bytes(uint64(value * Gigabit))
+ case "Gbit":
+ return humanize.Bytes(uint64(value * Gigabit / 8))
+ case "tbytes", "TiBy":
+ return humanize.IBytes(uint64(value * Tebibit))
+ case "Tibit":
+ return humanize.IBytes(uint64(value * Tebibit / 8))
+ case "decTbytes", "dectbytes", "TBy":
+ return humanize.Bytes(uint64(value * Terabit))
+ case "Tbit":
+ return humanize.Bytes(uint64(value * Terabit / 8))
+ case "pbytes", "PiBy":
+ return humanize.IBytes(uint64(value * Pebibit))
+ case "Pbit":
+ return humanize.Bytes(uint64(value * Petabit / 8))
+ case "decPbytes", "decpbytes", "PBy":
+ return humanize.Bytes(uint64(value * Petabit))
+ case "EiBy":
+ return humanize.IBytes(uint64(value * Exbibit))
+ case "Ebit":
+ return humanize.Bytes(uint64(value * Exabit / 8))
+ case "EBy":
+ return humanize.Bytes(uint64(value * Exabit))
+ }
+ // When unit is not matched, return the value as it is.
+ return fmt.Sprintf("%v", value)
+}
diff --git a/pkg/units/formatter_data_rate.go b/pkg/units/formatter_data_rate.go
new file mode 100644
index 0000000000..3c5319f9fe
--- /dev/null
+++ b/pkg/units/formatter_data_rate.go
@@ -0,0 +1,90 @@
+package units
+
+import (
+ "fmt"
+
+ "github.com/dustin/go-humanize"
+)
+
+type dataRateFormatter struct {
+}
+
+func NewDataRateFormatter() Formatter {
+ return &dataRateFormatter{}
+}
+
+func (*dataRateFormatter) Name() string {
+ return "data_rate"
+}
+
+func (f *dataRateFormatter) Format(value float64, unit string) string {
+ switch unit {
+ case "binBps":
+ return humanize.IBytes(uint64(value)) + "/s"
+ case "Bps", "By/s":
+ return humanize.Bytes(uint64(value)) + "/s"
+ case "binbps":
+ // humanize.IBytes/Bytes doesn't support bits
+ // and returns 0 B for values less than a byte
+ if value < 8 {
+ return fmt.Sprintf("%v b/s", value)
+ }
+ return humanize.IBytes(uint64(value/8)) + "/s"
+ case "bps", "bit/s":
+ if value < 8 {
+ return fmt.Sprintf("%v b/s", value)
+ }
+ return humanize.Bytes(uint64(value/8)) + "/s"
+ case "KiBs", "KiBy/s":
+ return humanize.IBytes(uint64(value*KibibitPerSecond)) + "/s"
+ case "Kibits", "Kibit/s":
+ return humanize.IBytes(uint64(value*KibibitPerSecond/8)) + "/s"
+ case "KBs", "kBy/s":
+ return humanize.IBytes(uint64(value*KilobitPerSecond)) + "/s"
+ case "Kbits", "kbit/s":
+ return humanize.Bytes(uint64(value*KilobitPerSecond/8)) + "/s"
+ case "MiBs", "MiBy/s":
+ return humanize.IBytes(uint64(value*MebibitPerSecond)) + "/s"
+ case "Mibits", "Mibit/s":
+ return humanize.IBytes(uint64(value*MebibitPerSecond/8)) + "/s"
+ case "MBs", "MBy/s":
+ return humanize.IBytes(uint64(value*MegabitPerSecond)) + "/s"
+ case "Mbits", "Mbit/s":
+ return humanize.Bytes(uint64(value*MegabitPerSecond/8)) + "/s"
+ case "GiBs", "GiBy/s":
+ return humanize.IBytes(uint64(value*GibibitPerSecond)) + "/s"
+ case "Gibits", "Gibit/s":
+ return humanize.IBytes(uint64(value*GibibitPerSecond/8)) + "/s"
+ case "GBs", "GBy/s":
+ return humanize.IBytes(uint64(value*GigabitPerSecond)) + "/s"
+ case "Gbits", "Gbit/s":
+ return humanize.Bytes(uint64(value*GigabitPerSecond/8)) + "/s"
+ case "TiBs", "TiBy/s":
+ return humanize.IBytes(uint64(value*TebibitPerSecond)) + "/s"
+ case "Tibits", "Tibit/s":
+ return humanize.IBytes(uint64(value*TebibitPerSecond/8)) + "/s"
+ case "TBs", "TBy/s":
+ return humanize.IBytes(uint64(value*TerabitPerSecond)) + "/s"
+ case "Tbits", "Tbit/s":
+ return humanize.Bytes(uint64(value*TerabitPerSecond/8)) + "/s"
+ case "PiBs", "PiBy/s":
+ return humanize.IBytes(uint64(value*PebibitPerSecond)) + "/s"
+ case "Pibits", "Pibit/s":
+ return humanize.IBytes(uint64(value*PebibitPerSecond/8)) + "/s"
+ case "PBs", "PBy/s":
+ return humanize.IBytes(uint64(value*PetabitPerSecond)) + "/s"
+ case "Pbits", "Pbit/s":
+ return humanize.Bytes(uint64(value*PetabitPerSecond/8)) + "/s"
+ // Exa units
+ case "EBy/s":
+ return humanize.Bytes(uint64(value*ExabitPerSecond)) + "/s"
+ case "Ebit/s":
+ return humanize.Bytes(uint64(value*ExabitPerSecond/8)) + "/s"
+ case "EiBy/s":
+ return humanize.IBytes(uint64(value*ExbibitPerSecond)) + "/s"
+ case "Eibit/s":
+ return humanize.IBytes(uint64(value*ExbibitPerSecond/8)) + "/s"
+ }
+ // When unit is not matched, return the value as it is.
+ return fmt.Sprintf("%v", value)
+}
diff --git a/pkg/query-service/formatter/data_rate_test.go b/pkg/units/formatter_data_rate_test.go
similarity index 99%
rename from pkg/query-service/formatter/data_rate_test.go
rename to pkg/units/formatter_data_rate_test.go
index 00b3d8f0d0..64331d9af8 100644
--- a/pkg/query-service/formatter/data_rate_test.go
+++ b/pkg/units/formatter_data_rate_test.go
@@ -1,4 +1,4 @@
-package formatter
+package units
import (
"testing"
diff --git a/pkg/query-service/formatter/data_test.go b/pkg/units/formatter_data_test.go
similarity index 99%
rename from pkg/query-service/formatter/data_test.go
rename to pkg/units/formatter_data_test.go
index 465510b6ef..990428c584 100644
--- a/pkg/query-service/formatter/data_test.go
+++ b/pkg/units/formatter_data_test.go
@@ -1,4 +1,4 @@
-package formatter
+package units
import (
"testing"
@@ -6,7 +6,7 @@ import (
"github.com/stretchr/testify/assert"
)
-func TestData(t *testing.T) {
+func TestFormatterData(t *testing.T) {
dataFormatter := NewDataFormatter()
assert.Equal(t, "1 B", dataFormatter.Format(1, "bytes"))
diff --git a/pkg/query-service/formatter/none.go b/pkg/units/formatter_none.go
similarity index 93%
rename from pkg/query-service/formatter/none.go
rename to pkg/units/formatter_none.go
index 208df98821..5a3bbb29a1 100644
--- a/pkg/query-service/formatter/none.go
+++ b/pkg/units/formatter_none.go
@@ -1,4 +1,4 @@
-package formatter
+package units
import "fmt"
diff --git a/pkg/query-service/formatter/percent.go b/pkg/units/formatter_percent.go
similarity index 97%
rename from pkg/query-service/formatter/percent.go
rename to pkg/units/formatter_percent.go
index 2d582855d7..05ca10f918 100644
--- a/pkg/query-service/formatter/percent.go
+++ b/pkg/units/formatter_percent.go
@@ -1,4 +1,4 @@
-package formatter
+package units
import "fmt"
diff --git a/pkg/query-service/formatter/scale.go b/pkg/units/formatter_scale.go
similarity index 70%
rename from pkg/query-service/formatter/scale.go
rename to pkg/units/formatter_scale.go
index dd1aea06d9..dfb88c6a46 100644
--- a/pkg/query-service/formatter/scale.go
+++ b/pkg/units/formatter_scale.go
@@ -1,4 +1,4 @@
-package formatter
+package units
import (
"fmt"
@@ -14,36 +14,36 @@ type IntervalsInSecondsType map[Interval]int
type Interval string
const (
- Year Interval = "year"
- Month Interval = "month"
- Week Interval = "week"
- Day Interval = "day"
- Hour Interval = "hour"
- Minute Interval = "minute"
- Second Interval = "second"
- Millisecond Interval = "millisecond"
+ IntervalYear Interval = "year"
+ IntervalMonth Interval = "month"
+ IntervalWeek Interval = "week"
+ IntervalDay Interval = "day"
+ IntervalHour Interval = "hour"
+ IntervalMinute Interval = "minute"
+ IntervalSecond Interval = "second"
+ IntervalMillisecond Interval = "millisecond"
)
var Units = []Interval{
- Year,
- Month,
- Week,
- Day,
- Hour,
- Minute,
- Second,
- Millisecond,
+ IntervalYear,
+ IntervalMonth,
+ IntervalWeek,
+ IntervalDay,
+ IntervalHour,
+ IntervalMinute,
+ IntervalSecond,
+ IntervalMillisecond,
}
var IntervalsInSeconds = IntervalsInSecondsType{
- Year: 31536000,
- Month: 2592000,
- Week: 604800,
- Day: 86400,
- Hour: 3600,
- Minute: 60,
- Second: 1,
- Millisecond: 1,
+ IntervalYear: 31536000,
+ IntervalMonth: 2592000,
+ IntervalWeek: 604800,
+ IntervalDay: 86400,
+ IntervalHour: 3600,
+ IntervalMinute: 60,
+ IntervalSecond: 1,
+ IntervalMillisecond: 1,
}
type DecimalCount *int
@@ -78,9 +78,8 @@ func toFixed(value float64, decimals DecimalCount) string {
}
decimalPos := strings.Index(formatted, ".")
- precision := 0
if decimalPos != -1 {
- precision = len(formatted) - decimalPos - 1
+ precision := len(formatted) - decimalPos - 1
if precision < *decimals {
return formatted + strings.Repeat("0", *decimals-precision)
}
@@ -89,8 +88,8 @@ func toFixed(value float64, decimals DecimalCount) string {
return formatted
}
-func toFixedScaled(value float64, decimals DecimalCount, scaleFormat string) string {
- return toFixed(value, decimals) + scaleFormat
+func toFixedScaled(value float64, scaleFormat string) string {
+ return toFixed(value, nil) + scaleFormat
}
func getDecimalsForValue(value float64) int {
diff --git a/pkg/query-service/formatter/scale_test.go b/pkg/units/formatter_scale_test.go
similarity index 94%
rename from pkg/query-service/formatter/scale_test.go
rename to pkg/units/formatter_scale_test.go
index 3bcec2ccdd..b77776fc3a 100644
--- a/pkg/query-service/formatter/scale_test.go
+++ b/pkg/units/formatter_scale_test.go
@@ -1,4 +1,4 @@
-package formatter
+package units
import (
"testing"
diff --git a/pkg/query-service/formatter/throughput.go b/pkg/units/formatter_throughput.go
similarity index 55%
rename from pkg/query-service/formatter/throughput.go
rename to pkg/units/formatter_throughput.go
index 1c3ada9bd1..7d17b6c1f0 100644
--- a/pkg/query-service/formatter/throughput.go
+++ b/pkg/units/formatter_throughput.go
@@ -1,4 +1,4 @@
-package formatter
+package units
import "fmt"
@@ -13,35 +13,35 @@ func (*throughputFormatter) Name() string {
return "throughput"
}
-func simpleCountUnit(value float64, decimals *int, symbol string) string {
+func simpleCountUnit(value float64, symbol string) string {
units := []string{"", "K", "M", "B", "T"}
scaler := scaledUnits(1000, units, 0)
- return scaler(value, decimals) + " " + symbol
+ return scaler(value, nil) + " " + symbol
}
func (f *throughputFormatter) Format(value float64, unit string) string {
switch unit {
case "cps", "{count}/s":
- return simpleCountUnit(value, nil, "c/s")
+ return simpleCountUnit(value, "c/s")
case "ops", "{ops}/s":
- return simpleCountUnit(value, nil, "op/s")
+ return simpleCountUnit(value, "op/s")
case "reqps", "{req}/s":
- return simpleCountUnit(value, nil, "req/s")
+ return simpleCountUnit(value, "req/s")
case "rps", "{read}/s":
- return simpleCountUnit(value, nil, "r/s")
+ return simpleCountUnit(value, "r/s")
case "wps", "{write}/s":
- return simpleCountUnit(value, nil, "w/s")
+ return simpleCountUnit(value, "w/s")
case "iops", "{iops}/s":
- return simpleCountUnit(value, nil, "iops")
+ return simpleCountUnit(value, "iops")
case "cpm", "{count}/min":
- return simpleCountUnit(value, nil, "c/m")
+ return simpleCountUnit(value, "c/m")
case "opm", "{ops}/min":
- return simpleCountUnit(value, nil, "op/m")
+ return simpleCountUnit(value, "op/m")
case "rpm", "{read}/min":
- return simpleCountUnit(value, nil, "r/m")
+ return simpleCountUnit(value, "r/m")
case "wpm", "{write}/min":
- return simpleCountUnit(value, nil, "w/m")
+ return simpleCountUnit(value, "w/m")
}
// When unit is not matched, return the value as it is.
return fmt.Sprintf("%v", value)
diff --git a/pkg/query-service/formatter/throughput_test.go b/pkg/units/formatter_throughput_test.go
similarity index 97%
rename from pkg/query-service/formatter/throughput_test.go
rename to pkg/units/formatter_throughput_test.go
index 004f45e101..1592f2e76a 100644
--- a/pkg/query-service/formatter/throughput_test.go
+++ b/pkg/units/formatter_throughput_test.go
@@ -1,4 +1,4 @@
-package formatter
+package units
import (
"testing"
diff --git a/pkg/query-service/formatter/time.go b/pkg/units/formatter_time.go
similarity index 69%
rename from pkg/query-service/formatter/time.go
rename to pkg/units/formatter_time.go
index d16db65461..025738dfac 100644
--- a/pkg/query-service/formatter/time.go
+++ b/pkg/units/formatter_time.go
@@ -1,4 +1,4 @@
-package formatter
+package units
import (
"fmt"
@@ -46,17 +46,17 @@ func toNanoSeconds(value float64) string {
if absValue < 1000 {
return toFixed(value, nil) + " ns"
} else if absValue < 1000000 { // 2000 ns is better represented as 2 µs
- return toFixedScaled(value/1000, nil, " µs")
+ return toFixedScaled(value/1000, " µs")
} else if absValue < 1000000000 { // 2000000 ns is better represented as 2 ms
- return toFixedScaled(value/1000000, nil, " ms")
+ return toFixedScaled(value/1000000, " ms")
} else if absValue < 60000000000 {
- return toFixedScaled(value/1000000000, nil, " s")
+ return toFixedScaled(value/1000000000, " s")
} else if absValue < 3600000000000 {
- return toFixedScaled(value/60000000000, nil, " min")
+ return toFixedScaled(value/60000000000, " min")
} else if absValue < 86400000000000 {
- return toFixedScaled(value/3600000000000, nil, " hour")
+ return toFixedScaled(value/3600000000000, " hour")
} else {
- return toFixedScaled(value/86400000000000, nil, " day")
+ return toFixedScaled(value/86400000000000, " day")
}
}
@@ -66,9 +66,9 @@ func toMicroSeconds(value float64) string {
if absValue < 1000 {
return toFixed(value, nil) + " µs"
} else if absValue < 1000000 { // 2000 µs is better represented as 2 ms
- return toFixedScaled(value/1000, nil, " ms")
+ return toFixedScaled(value/1000, " ms")
} else {
- return toFixedScaled(value/1000000, nil, " s")
+ return toFixedScaled(value/1000000, " s")
}
}
@@ -80,16 +80,16 @@ func toMilliSeconds(value float64) string {
if absValue < 1000 {
return toFixed(value, nil) + " ms"
} else if absValue < 60000 {
- return toFixedScaled(value/1000, nil, " s")
+ return toFixedScaled(value/1000, " s")
} else if absValue < 3600000 {
- return toFixedScaled(value/60000, nil, " min")
+ return toFixedScaled(value/60000, " min")
} else if absValue < 86400000 { // 172800000 ms is better represented as 2 day
- return toFixedScaled(value/3600000, nil, " hour")
+ return toFixedScaled(value/3600000, " hour")
} else if absValue < 31536000000 {
- return toFixedScaled(value/86400000, nil, " day")
+ return toFixedScaled(value/86400000, " day")
}
- return toFixedScaled(value/31536000000, nil, " year")
+ return toFixedScaled(value/31536000000, " year")
}
// toSeconds returns a easy to read string representation of the given value in seconds
@@ -97,24 +97,24 @@ func toSeconds(value float64) string {
absValue := math.Abs(value)
if absValue < 0.000001 {
- return toFixedScaled(value*1e9, nil, " ns")
+ return toFixedScaled(value*1e9, " ns")
} else if absValue < 0.001 {
- return toFixedScaled(value*1e6, nil, " µs")
+ return toFixedScaled(value*1e6, " µs")
} else if absValue < 1 {
- return toFixedScaled(value*1e3, nil, " ms")
+ return toFixedScaled(value*1e3, " ms")
} else if absValue < 60 {
return toFixed(value, nil) + " s"
} else if absValue < 3600 {
- return toFixedScaled(value/60, nil, " min")
+ return toFixedScaled(value/60, " min")
} else if absValue < 86400 { // 56000 s is better represented as 15.56 hour
- return toFixedScaled(value/3600, nil, " hour")
+ return toFixedScaled(value/3600, " hour")
} else if absValue < 604800 {
- return toFixedScaled(value/86400, nil, " day")
+ return toFixedScaled(value/86400, " day")
} else if absValue < 31536000 {
- return toFixedScaled(value/604800, nil, " week")
+ return toFixedScaled(value/604800, " week")
}
- return toFixedScaled(value/3.15569e7, nil, " year")
+ return toFixedScaled(value/3.15569e7, " year")
}
// toMinutes returns a easy to read string representation of the given value in minutes
@@ -124,13 +124,13 @@ func toMinutes(value float64) string {
if absValue < 60 {
return toFixed(value, nil) + " min"
} else if absValue < 1440 {
- return toFixedScaled(value/60, nil, " hour")
+ return toFixedScaled(value/60, " hour")
} else if absValue < 10080 {
- return toFixedScaled(value/1440, nil, " day")
+ return toFixedScaled(value/1440, " day")
} else if absValue < 604800 {
- return toFixedScaled(value/10080, nil, " week")
+ return toFixedScaled(value/10080, " week")
} else {
- return toFixedScaled(value/5.25948e5, nil, " year")
+ return toFixedScaled(value/5.25948e5, " year")
}
}
@@ -142,11 +142,11 @@ func toHours(value float64) string {
if absValue < 24 {
return toFixed(value, nil) + " hour"
} else if absValue < 168 {
- return toFixedScaled(value/24, nil, " day")
+ return toFixedScaled(value/24, " day")
} else if absValue < 8760 {
- return toFixedScaled(value/168, nil, " week")
+ return toFixedScaled(value/168, " week")
} else {
- return toFixedScaled(value/8760, nil, " year")
+ return toFixedScaled(value/8760, " year")
}
}
@@ -157,9 +157,9 @@ func toDays(value float64) string {
if absValue < 7 {
return toFixed(value, nil) + " day"
} else if absValue < 365 {
- return toFixedScaled(value/7, nil, " week")
+ return toFixedScaled(value/7, " week")
} else {
- return toFixedScaled(value/365, nil, " year")
+ return toFixedScaled(value/365, " year")
}
}
@@ -170,6 +170,6 @@ func toWeeks(value float64) string {
if absValue < 52 {
return toFixed(value, nil) + " week"
} else {
- return toFixedScaled(value/52, nil, " year")
+ return toFixedScaled(value/52, " year")
}
}
diff --git a/pkg/query-service/formatter/time_test.go b/pkg/units/formatter_time_test.go
similarity index 98%
rename from pkg/query-service/formatter/time_test.go
rename to pkg/units/formatter_time_test.go
index 8224eaea9b..f55d91dd4b 100644
--- a/pkg/query-service/formatter/time_test.go
+++ b/pkg/units/formatter_time_test.go
@@ -1,4 +1,4 @@
-package formatter
+package units
import (
"testing"