Skip to content

Commit b23e947

Browse files
Trim the view count to 1024 to reduce traffic volume (#43)
1 parent 34441fa commit b23e947

12 files changed

Lines changed: 38 additions & 18 deletions

internal/controllers/stats_controller.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ import (
1616
"github.com/gin-gonic/gin"
1717
)
1818

19+
const (
20+
// language=SVG
21+
pixel = `<svg xmlns="http://www.w3.org/2000/svg" width="1" height="1">
22+
<rect width="1" height="1" fill="white" />
23+
</svg>`
24+
)
25+
1926
type StatsController struct {
2027
userService *services.UserService
2128
statsService *services.StatsService
@@ -113,17 +120,17 @@ func (c *StatsController) GitHubDayWeekMonthTotalCountBadge(ctx *gin.Context) {
113120
return
114121
}
115122

123+
// Temporary solution to reduce traffic volume
124+
if statsCount.HourCount > 1024 {
125+
c.renderImage(ctx, []byte(pixel))
126+
127+
return
128+
}
129+
116130
c.renderImage(ctx, []byte(tmv2.Badge(statsCount)))
117131
}
118132

119133
func (c *StatsController) Pixel(ctx *gin.Context) {
120-
const (
121-
// language=SVG
122-
pixel = `<svg xmlns="http://www.w3.org/2000/svg" width="1" height="1">
123-
<rect width="1" height="1" fill="white" />
124-
</svg>`
125-
)
126-
127134
done := c.increment(ctx, dbs.SocialProviderGithub)
128135
if done {
129136
return

internal/controllers/web_controller.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ func (c *WebController) Index(ctx *gin.Context) {
7575
Name: user.Name,
7676
},
7777
ProfileViewsStats: tmv2.ProfileViewsStats{
78+
HourCount: stats.HourCount,
7879
DayCount: stats.DayCount,
7980
WeekCount: stats.WeekCount,
8081
MonthCount: stats.MonthCount,

internal/models/stats.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
package models
22

33
type ProfileViewsStats struct {
4+
HourCount int64
45
DayCount int64
56
WeekCount int64
67
MonthCount int64
78
TotalCount int64
89
}
910

1011
type DayWeekMonthViewsStats struct {
12+
HourCount int64
1113
DayCount int64
1214
WeekCount int64
1315
MonthCount int64
1416
}
1517

1618
func (s *DayWeekMonthViewsStats) Inc() {
19+
s.HourCount += 1
1720
s.DayCount += 1
1821
s.WeekCount += 1
1922
s.MonthCount += 1

internal/services/stats_service.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ func (s *StatsService) StatsCount(ctx context.Context, userID int64, increment b
7676
}
7777

7878
return ProfileViewsStats{
79+
HourCount: stats.HourCount,
7980
DayCount: stats.DayCount,
8081
WeekCount: stats.WeekCount,
8182
MonthCount: stats.MonthCount,
@@ -98,6 +99,7 @@ func (s *StatsService) Increment(ctx context.Context, userID int64) (err error)
9899

99100
func (s *StatsService) UserDayWeekMonthViewsStatsMap(ctx context.Context, userIDs []int64, now time.Time) (map[int64]models.DayWeekMonthViewsStats, error) {
100101
rows, err := s.repository.Queries().ProfileHourlyViewsStats(ctx, dbs.ProfileHourlyViewsStatsParams{
102+
Hour: now,
101103
Day: now.AddDate(0, 0, -1),
102104
Week: now.AddDate(0, 0, -7),
103105
Month: now.AddDate(0, -1, 0),
@@ -110,6 +112,7 @@ func (s *StatsService) UserDayWeekMonthViewsStatsMap(ctx context.Context, userID
110112
result := make(map[int64]models.DayWeekMonthViewsStats, len(rows))
111113
for _, row := range rows {
112114
result[row.UserID] = models.DayWeekMonthViewsStats{
115+
HourCount: row.HourCount,
113116
DayCount: row.DayCount,
114117
WeekCount: row.WeekCount,
115118
MonthCount: row.MonthCount,

internal/storage/dbs/db.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/storage/dbs/models.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/storage/dbs/profile_hourly_views_stats.sql.go

Lines changed: 10 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/storage/dbs/profile_total_views.sql.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/storage/dbs/referrals.sql.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/storage/dbs/username_history.sql.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)