Skip to content

Commit

Permalink
pixel (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
YaroslavPodorvanov authored Dec 28, 2023
1 parent 54a4fdd commit 38440f4
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/v3/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func main() {
r.GET("/api/v1/github/profiles/:social_provider_user_id/views/count.json", statsController.GitHubDayWeekMonthTotalCount)
r.GET("/api/v1/github/profiles/:social_provider_user_id/views/stats.json", statsController.GitHubStats)
r.GET("/api/v1/github/profiles/:social_provider_user_id/views/day-week-month-total-count.svg", statsController.GitHubDayWeekMonthTotalCountBadge)
r.GET("/api/v1/github/profiles/:social_provider_user_id/views/pixel.svg", statsController.Pixel)
r.GET("/api/v1/github/profiles/:social_provider_user_id/views/total-count.svg", statsController.TotalCountBadge)
r.GET("/api/v1/github/profiles/:social_provider_user_id/referrals/stats.json", statsController.ReferralsStats)
r.GET("/api/v1/users/stats.json", statsController.UsersCreatedAtStatsByDay)
Expand Down
49 changes: 49 additions & 0 deletions internal/controllers/stats_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,25 @@ func (c *StatsController) GitHubDayWeekMonthTotalCountBadge(ctx *gin.Context) {
ctx.Data(http.StatusOK, "image/svg+xml", []byte(tmv2.Badge(statsCount)))
}

func (c *StatsController) Pixel(ctx *gin.Context) {
const (
// language=SVG
pixel = `<svg xmlns="http://www.w3.org/2000/svg" width="1" height="1">
<rect width="1" height="1" fill="white" />
</svg>`
)

done := c.increment(ctx, dbs.SocialProviderGithub)
if done {
return
}

ctx.Header("Cache-Control", "no-cache, no-store, must-revalidate")
ctx.Header("Pragma", "no-cache")
ctx.Header("Expires", "0")
ctx.Data(http.StatusOK, "image/svg+xml", []byte(pixel))
}

func (c *StatsController) GitHubStats(ctx *gin.Context) {
socialProviderUserID, done := c.parseSocialProviderUserID(ctx)
if done {
Expand Down Expand Up @@ -188,6 +207,36 @@ func (c *StatsController) statsCount(ctx *gin.Context, provider dbs.SocialProvid
return statsCount, false
}

func (c *StatsController) increment(ctx *gin.Context, provider dbs.SocialProvider) (done bool) {
socialProviderUserID, done := c.parseSocialProviderUserID(ctx)
if done {
return
}

userID, done := c.toUserID(ctx, provider, socialProviderUserID)
if done {
return
}

increment := strings.HasPrefix(ctx.GetHeader("User-Agent"), "github-camo")
if !increment {
return
}

err := c.statsService.Increment(ctx, userID)
if err != nil {
log.Printf("Database error (stats) %s\n", err)

ctx.JSON(http.StatusInternalServerError, &ErrorResponse{
ErrorMessage: "Database error",
})

return true
}

return false
}

func (c *StatsController) parseSocialProviderUserID(ctx *gin.Context) (string, bool) {
var uri ProfileCountURI

Expand Down
13 changes: 13 additions & 0 deletions internal/services/stats_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ func (s *StatsService) StatsCount(ctx context.Context, userID int64, increment b
}, nil
}

func (s *StatsService) Increment(ctx context.Context, userID int64) (err error) {
now := time.Now().UTC().Truncate(time.Hour)

go func() {
err := s.increment(context.Background(), userID, now)
if err != nil {
log.Printf("Database err %s\n", err)
}
}()

return nil
}

func (s *StatsService) UserDayWeekMonthViewsStatsMap(ctx context.Context, userIDs []int64, now time.Time) (map[int64]models.DayWeekMonthViewsStats, error) {
rows, err := s.repository.Queries().ProfileHourlyViewsStats(ctx, dbs.ProfileHourlyViewsStatsParams{
Day: now.AddDate(0, 0, -1),
Expand Down

0 comments on commit 38440f4

Please sign in to comment.