From e0d61c5ce73201e4ea09bb022204443bf292d507 Mon Sep 17 00:00:00 2001 From: ianmuchyri Date: Thu, 14 Mar 2024 14:59:24 +0300 Subject: [PATCH] simplify the conversion Signed-off-by: ianmuchyri --- ui/api/transport.go | 5 +++-- ui/service.go | 42 +++--------------------------------------- 2 files changed, 6 insertions(+), 41 deletions(-) diff --git a/ui/api/transport.go b/ui/api/transport.go index ccd3a1a2..5a2a9def 100644 --- a/ui/api/transport.go +++ b/ui/api/transport.go @@ -8,6 +8,7 @@ import ( "encoding/csv" "encoding/json" "fmt" + "math" "net/http" "net/url" "strconv" @@ -1951,8 +1952,8 @@ func decodeReadMessagesRequest(_ context.Context, r *http.Request) (interface{}, StringValue: vs, DataValue: vd, BoolValue: &vb, - From: ui.FixUnixPrecision(from, ui.NanosecondsLevel), - To: ui.FixUnixPrecision(to, ui.NanosecondsLevel), + From: from * math.Pow10(ui.PrecisionDiff), + To: to * math.Pow10(ui.PrecisionDiff), Aggregation: aggregation, Interval: interval, }, diff --git a/ui/service.go b/ui/service.go index 02955aa7..3b7d7122 100644 --- a/ui/service.go +++ b/ui/service.go @@ -55,15 +55,7 @@ const ( membersActive = "members" invitationsActive = "invitations" domainInvitationsActive = "domaininvitations" -) - -type precisionLevel uint8 - -const ( - SecondsLevel precisionLevel = iota - MillisecondsLevel - MicrosecondsLevel - NanosecondsLevel + PrecisionDiff = 6 ) type LoginStatus string @@ -1728,7 +1720,7 @@ func (us *uiService) ReadMessages(s Session, channelID, thingKey string, mpgm sd } for _, message := range msg.Messages { - message.Time = FixUnixPrecision(message.Time, MillisecondsLevel) + message.Time = message.Time / math.Pow10(-PrecisionDiff) } noOfPages := int(math.Ceil(float64(msg.Total) / float64(mpgm.Limit))) @@ -1778,7 +1770,7 @@ func (us *uiService) FetchChartData(token string, channelID string, mpgm sdk.Mes } for _, message := range msg.Messages { - message.Time = FixUnixPrecision(message.Time, MillisecondsLevel) + message.Time = message.Time / math.Pow10(-PrecisionDiff) } data, err := json.Marshal(msg) @@ -2721,31 +2713,3 @@ func parseTemplates(mfsdk sdk.SDK, prefix string) (tpl *template.Template, err e return tpl.ParseFS(templatesFS, templates...) } - -func FixUnixPrecision(currTime float64, precisionLevel precisionLevel) float64 { - currentPrecision := len(fmt.Sprint(int64(currTime))) - var desiredPrecision int - switch precisionLevel { - case SecondsLevel: - desiredPrecision = len(fmt.Sprint(time.Now().Unix())) - case MillisecondsLevel: - desiredPrecision = len(fmt.Sprint(time.Now().UnixMilli())) - case MicrosecondsLevel: - desiredPrecision = len(fmt.Sprint(time.Now().UnixMicro())) - case NanosecondsLevel: - desiredPrecision = len(fmt.Sprint(time.Now().UnixNano())) - } - - precisionDiff := desiredPrecision - currentPrecision - var adjustedTime float64 - switch { - case precisionDiff < 0: - adjustedTime = currTime / math.Pow10(-precisionDiff) - case precisionDiff > 0: - adjustedTime = currTime * math.Pow10(precisionDiff) - default: - adjustedTime = currTime - } - - return adjustedTime -}