From 80e4d771fa5d652a44b25b45c3c454923916d8dc Mon Sep 17 00:00:00 2001 From: ianmuchyri Date: Wed, 13 Mar 2024 19:30:08 +0300 Subject: [PATCH] fix send messages page time format Signed-off-by: ianmuchyri --- docker/docker-compose.yml | 32 +++++++++++++++--------------- ui/api/transport.go | 2 +- ui/service.go | 22 ++++++++++++++++++-- ui/web/templates/readmessages.html | 2 +- 4 files changed, 38 insertions(+), 20 deletions(-) diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index f2ec6c62a..f186c3432 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -50,19 +50,19 @@ services: MG_UI_BLOCK_KEY: ${MG_UI_BLOCK_KEY} MG_UI_PATH_PREFIX: ${MG_UI_PATH_PREFIX} - ui-db: - image: postgres:16.1-alpine - container_name: magistrala-ui-db - restart: on-failure - command: postgres -c "max_connections=${MG_POSTGRES_MAX_CONNECTIONS}" - ports: - - 6007:5432 - networks: - - magistrala-base-net - volumes: - - magistrala-ui-db-volume:/var/lib/postgresql/data - environment: - POSTGRES_USER: ${MG_UI_DB_USER} - POSTGRES_PASSWORD: ${MG_UI_DB_PASS} - POSTGRES_DB: ${MG_UI_DB_NAME} - MG_POSTGRES_MAX_CONNECTIONS: ${MG_POSTGRES_MAX_CONNECTIONS} + # ui-db: + # image: postgres:16.1-alpine + # container_name: magistrala-ui-db + # restart: on-failure + # command: postgres -c "max_connections=${MG_POSTGRES_MAX_CONNECTIONS}" + # ports: + # - 6007:5432 + # networks: + # - magistrala-base-net + # volumes: + # - magistrala-ui-db-volume:/var/lib/postgresql/data + # environment: + # POSTGRES_USER: ${MG_UI_DB_USER} + # POSTGRES_PASSWORD: ${MG_UI_DB_PASS} + # POSTGRES_DB: ${MG_UI_DB_NAME} + # MG_POSTGRES_MAX_CONNECTIONS: ${MG_POSTGRES_MAX_CONNECTIONS} diff --git a/ui/api/transport.go b/ui/api/transport.go index 455df5679..d71c6b676 100644 --- a/ui/api/transport.go +++ b/ui/api/transport.go @@ -1833,7 +1833,7 @@ func decodePublishRequest(_ context.Context, r *http.Request) (interface{}, erro thingKey: r.PostFormValue("thingKey"), channelID: r.PostFormValue("channelID"), Message: ui.Message{ - BaseTime: float64(time.Now().Unix()), + BaseTime: float64(time.Now().UnixMilli()), BaseUnit: r.PostFormValue("unit"), Name: r.PostFormValue("name"), Unit: r.PostFormValue("unit"), diff --git a/ui/service.go b/ui/service.go index bea8833a0..9768912b4 100644 --- a/ui/service.go +++ b/ui/service.go @@ -2656,11 +2656,11 @@ func parseTemplates(mfsdk sdk.SDK, prefix string) (tpl *template.Template, err e } return result }, - "unixTimeToHumanTime": func(t float64) string { + "unixTimeToHumanTime": func(t float64, precision int) string { if t == 0 { return "" } - return time.Unix(int64(t), 0).String() + return time.Unix(adjustPrecision(int64(t), precision), 0).String() }, "hasPermission": func(permissions []string, permission string) bool { return slices.Contains(permissions, permission) @@ -2704,3 +2704,21 @@ func parseTemplates(mfsdk sdk.SDK, prefix string) (tpl *template.Template, err e return tpl.ParseFS(templatesFS, templates...) } + +// Function to adjust the precision of a timestamp +func adjustPrecision(timestamp int64, desiredPrecision int) int64 { + currentPrecision := len(fmt.Sprintf("%d", timestamp)) + precisionDifference := desiredPrecision - currentPrecision + var adjustedTimestamp int64 + switch { + case precisionDifference < 0: + adjustedTimestamp = timestamp / int64(math.Pow10(-precisionDifference)) + case precisionDifference > 0: + adjustedTimestamp = timestamp * int64(math.Pow10(precisionDifference)) + default: + adjustedTimestamp = timestamp + + } + + return adjustedTimestamp +} diff --git a/ui/web/templates/readmessages.html b/ui/web/templates/readmessages.html index a72540a9d..6d3d3605a 100644 --- a/ui/web/templates/readmessages.html +++ b/ui/web/templates/readmessages.html @@ -75,7 +75,7 @@ {{ end }} {{ if $c.Sum }}{{ $c.Sum }}{{ end }} - {{ unixTimeToHumanTime $c.Time }} + {{ unixTimeToHumanTime $c.Time 10 }} {{ end }}