Skip to content

Commit

Permalink
Merge pull request #78 from arc42/#55-format-large-numbers
Browse files Browse the repository at this point in the history
#55 format large numbers
  • Loading branch information
gernotstarke authored Dec 12, 2023
2 parents 9b1bf25 + 2fc0cdb commit 9f7dc5a
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 21 deletions.
28 changes: 28 additions & 0 deletions go-app/cmd/easy2read/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"fmt"
"golang.org/x/text/language"
"golang.org/x/text/message"
)

// format large numbers with separators
func formatWithSeparator(numAsString int) string {

p := message.NewPrinter(language.German)
withCommaThousandSep := p.Sprintf("%d", numAsString)
fmt.Printf("formated string %s", withCommaThousandSep)

return withCommaThousandSep
}

func main() {
fmt.Printf("1: %s\n", formatWithSeparator(1))
fmt.Printf("10: %s\n", formatWithSeparator(10))
fmt.Printf("100: %s\n", formatWithSeparator(100))
fmt.Printf("1000: %s\n", formatWithSeparator(1000))
fmt.Printf("10000: %s\n", formatWithSeparator(10000))
fmt.Printf("100000: %s\n", formatWithSeparator(100000))
fmt.Printf("1000000: %s\n", formatWithSeparator(1000000))

}
1 change: 1 addition & 0 deletions go-app/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ require (
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.50.0 // indirect
golang.org/x/sys v0.14.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.31.0 // indirect
)
2 changes: 2 additions & 0 deletions go-app/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c=
Expand Down
29 changes: 22 additions & 7 deletions go-app/internal/domain/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"arc42-status/internal/plausible"
"arc42-status/internal/types"
"github.com/rs/zerolog/log"
"golang.org/x/text/language"
"golang.org/x/text/message"
"sync"
"time"
)
Expand Down Expand Up @@ -87,14 +89,27 @@ func calculateTotals(stats []types.SiteStats) types.TotalsForAllSites {
var totals types.TotalsForAllSites

for index := range types.Arc42sites {
totals.SumOfVisitors7d += stats[index].Visitors7dNr
totals.SumOfPageviews7d += stats[index].Pageviews7dNr
totals.SumOfVisitors30d += stats[index].Visitors30dNr
totals.SumOfPageviews30d += stats[index].Pageviews30dNr
totals.SumOfVisitors12m += stats[index].Visitors12mNr
totals.SumOfPageviews12m += stats[index].Pageviews12mNr
totals.SumOfVisitors7dNr += stats[index].Visitors7dNr
totals.SumOfPageviews7dNr += stats[index].Pageviews7dNr
totals.SumOfVisitors30dNr += stats[index].Visitors30dNr
totals.SumOfPageviews30dNr += stats[index].Pageviews30dNr
totals.SumOfVisitors12mNr += stats[index].Visitors12mNr
totals.SumOfPageviews12mNr += stats[index].Pageviews12mNr
}
log.Debug().Msgf("Total visits and pageviews (V/PV, 7d, 30d, 12m)= %d/%d, %d/%d, %d/%d", totals.SumOfVisitors7d, totals.SumOfPageviews7d, totals.SumOfVisitors30d, totals.SumOfPageviews30d, totals.SumOfVisitors12m, totals.SumOfPageviews12m)

// now convert numbers to strings-with-separators
p := message.NewPrinter(language.German)

totals.SumOfVisitors7d = p.Sprintf("%d", totals.SumOfVisitors7dNr)
totals.SumOfPageviews7d = p.Sprintf("%d", totals.SumOfPageviews7dNr)

totals.SumOfVisitors30d = p.Sprintf("%d", totals.SumOfVisitors30dNr)
totals.SumOfPageviews30d = p.Sprintf("%d", totals.SumOfPageviews30dNr)

totals.SumOfVisitors12m = p.Sprintf("%d", totals.SumOfVisitors12mNr)
totals.SumOfPageviews12m = p.Sprintf("%d", totals.SumOfPageviews12mNr)

log.Debug().Msgf("Total visits and pageviews (V/PV, 7d, 30d, 12m)= %d/%d, %d/%d, %d/%d", totals.SumOfVisitors7dNr, totals.SumOfPageviews7dNr, totals.SumOfVisitors30dNr, totals.SumOfPageviews30dNr, totals.SumOfVisitors12mNr, totals.SumOfPageviews12mNr)

return totals
}
Expand Down
17 changes: 11 additions & 6 deletions go-app/internal/plausible/vpvStatistics.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"arc42-status/internal/types"
"github.com/andrerfcsantos/go-plausible/plausible"
"github.com/rs/zerolog/log"
"golang.org/x/text/language"
"golang.org/x/text/message"
"os"
"strconv"
"sync"
Expand Down Expand Up @@ -86,19 +88,22 @@ func StatsForSite(thisSite string, stats *types.SiteStats) {

wg.Wait()

p := message.NewPrinter(language.German)

// now process results
stats.Visitors7d = stats7D.Visitors
// before #55 these assignments read: stats.Visitors7d = stats7D.Visitors
stats.Visitors7d = p.Sprintf("%d", stats7D.VisitorNr)
stats.Pageviews7d = p.Sprintf("%d", stats7D.PageviewNr)
stats.Visitors7dNr = stats7D.VisitorNr
stats.Pageviews7d = stats7D.Pageviews
stats.Pageviews7dNr = stats7D.PageviewNr

stats.Visitors30d = stats30D.Visitors
stats.Visitors30d = p.Sprintf("%d", stats30D.VisitorNr)
stats.Pageviews30d = p.Sprintf("%d", stats30D.PageviewNr)
stats.Visitors30dNr = stats30D.VisitorNr
stats.Pageviews30d = stats30D.Pageviews
stats.Pageviews30dNr = stats30D.PageviewNr

stats.Visitors12m = stats12M.Visitors
stats.Pageviews12m = stats12M.Pageviews
stats.Visitors12m = p.Sprintf("%d", stats12M.VisitorNr)
stats.Pageviews12m = p.Sprintf("%d", stats12M.PageviewNr)
stats.Visitors12mNr = stats12M.VisitorNr
stats.Pageviews12mNr = stats12M.PageviewNr
}
Expand Down
18 changes: 12 additions & 6 deletions go-app/internal/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,18 @@ type RepoStats struct {
// If certain values are "n/a" (when the external API sends errors),
// we let these values count 0.
type TotalsForAllSites struct {
SumOfVisitors7d int
SumOfPageviews7d int
SumOfVisitors30d int
SumOfPageviews30d int
SumOfVisitors12m int
SumOfPageviews12m int
SumOfVisitors7dNr int
SumOfVisitors7d string
SumOfPageviews7dNr int
SumOfPageviews7d string
SumOfVisitors30dNr int
SumOfVisitors30d string
SumOfPageviews30dNr int
SumOfPageviews30d string
SumOfVisitors12mNr int
SumOfVisitors12m string
SumOfPageviews12mNr int
SumOfPageviews12m string
}

// Arc42Statistics collects information about the sites and subdomains
Expand Down
5 changes: 3 additions & 2 deletions go-app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import (
"strings"
)

const AppVersion = "0.4.4"
const AppVersion = "0.4.5"

// version history
// 0.4.4 fix bad hyperlink to github issues
// 0.4.5 fix missing separators in large numbers
// 0.4.4 fix bad hyperlink to GitHub issues
// 0.4.3 fix #57 (local svg images for issues and badges)
// 0.4.2 merge repositories (site-statistics and status.arc42.org-site) into one!
// 0.4.1 added links to issue & bug badges
Expand Down

0 comments on commit 9f7dc5a

Please sign in to comment.