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

Commit

Permalink
fix send messages page time format
Browse files Browse the repository at this point in the history
Signed-off-by: ianmuchyri <ianmuchiri8@gmail.com>
  • Loading branch information
ianmuchyri committed Mar 13, 2024
1 parent 65b5b55 commit 80e4d77
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 20 deletions.
32 changes: 16 additions & 16 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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}
2 changes: 1 addition & 1 deletion ui/api/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
22 changes: 20 additions & 2 deletions ui/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion ui/web/templates/readmessages.html
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
{{ end }}
</td>
<td>{{ if $c.Sum }}{{ $c.Sum }}{{ end }}</td>
<td>{{ unixTimeToHumanTime $c.Time }}</td>
<td>{{ unixTimeToHumanTime $c.Time 10 }}</td>
</tr>
{{ end }}
</tbody>
Expand Down

0 comments on commit 80e4d77

Please sign in to comment.