Skip to content
This repository has been archived by the owner on Jan 23, 2025. It is now read-only.

Commit

Permalink
simplify the conversion
Browse files Browse the repository at this point in the history
Signed-off-by: ianmuchyri <ianmuchiri8@gmail.com>
  • Loading branch information
ianmuchyri authored and dborovcanin committed Mar 14, 2024
1 parent 323616f commit e0d61c5
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 41 deletions.
5 changes: 3 additions & 2 deletions ui/api/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"encoding/csv"
"encoding/json"
"fmt"
"math"
"net/http"
"net/url"
"strconv"
Expand Down Expand Up @@ -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,
},
Expand Down
42 changes: 3 additions & 39 deletions ui/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)))
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

0 comments on commit e0d61c5

Please sign in to comment.