Skip to content

Commit

Permalink
webapi: Footer update time relative not absolute.
Browse files Browse the repository at this point in the history
Using a relative time rather than absolute requires far less effort for
a reader to understand, e.g. compare "25 Oct 2024 11:12:49 UTC" to "3
minutes ago".

As a nice side-effect this also fixes a minor privacy issue where using
the full absolute timestamp would expose the server timezone to the
public.
  • Loading branch information
jholdstock committed Oct 26, 2024
1 parent ddd7fd5 commit b0fb546
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
6 changes: 3 additions & 3 deletions internal/webapi/cache.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020-2023 The Decred developers
// Copyright (c) 2020-2024 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

Expand Down Expand Up @@ -34,7 +34,7 @@ type cacheData struct {
// the first time.
Initialized bool

UpdateTime string
UpdateTime time.Time
PubKey string
DatabaseSize string
Voting int64
Expand Down Expand Up @@ -118,7 +118,7 @@ func (c *cache) update() error {
defer c.mtx.Unlock()

c.data.Initialized = true
c.data.UpdateTime = dateTime(time.Now().Unix())
c.data.UpdateTime = time.Now()
c.data.DatabaseSize = humanize.Bytes(dbSize)
c.data.Voting = voting
c.data.Voted = voted
Expand Down
14 changes: 14 additions & 0 deletions internal/webapi/formatting.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Copyright (c) 2020-2024 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

package webapi

import (
Expand All @@ -9,6 +13,7 @@ import (

"github.com/decred/dcrd/dcrutil/v4"
"github.com/decred/slog"
"github.com/dustin/go-humanize"
)

func addressURL(blockExplorerURL string) func(string) string {
Expand All @@ -29,10 +34,19 @@ func blockURL(blockExplorerURL string) func(int64) string {
}
}

// dateTime returns a human readable representation of the provided unix
// timestamp. It includes the local timezone of the server so use on public
// webpages is not recommended.
func dateTime(t int64) string {
return time.Unix(t, 0).Format("2 Jan 2006 15:04:05 MST")
}

// timeAgo compares the provided unix timestamp to the current time to return a
// string like "3 minutes ago".
func timeAgo(t time.Time) string {
return humanize.Time(t)
}

func stripWss(input string) string {
input = strings.ReplaceAll(input, "wss://", "")
input = strings.ReplaceAll(input, "/ws", "")
Expand Down
2 changes: 1 addition & 1 deletion internal/webapi/templates/footer.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<footer class="row m-0">
<div class="col-md-8 col-12 d-flex justify-content-center align-items-center">
<p class="py-4 m-0">
<strong>Stats&nbsp;updated:</strong>&nbsp;{{ .WebApiCache.UpdateTime }}
<strong>Stats&nbsp;updated:</strong>&nbsp;{{ timeAgo .WebApiCache.UpdateTime }}
<br />
<strong>Support:</strong>&nbsp;<a href="mailto:{{ .WebApiCfg.SupportEmail }}" rel="noopener noreferrer">{{ .WebApiCfg.SupportEmail }}</a>
<br />
Expand Down
1 change: 1 addition & 0 deletions internal/webapi/webapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ func (w *WebAPI) router(cookieSecret []byte, dcrd rpc.DcrdConnect, wallets rpc.W
"addressURL": addressURL(explorerURL),
"blockURL": blockURL(explorerURL),
"dateTime": dateTime,
"timeAgo": timeAgo,
"stripWss": stripWss,
"indentJSON": indentJSON(w.log),
"atomsToDCR": atomsToDCR,
Expand Down

0 comments on commit b0fb546

Please sign in to comment.