Skip to content

Commit

Permalink
Retreive scanned codes
Browse files Browse the repository at this point in the history
  • Loading branch information
jggoebel committed Oct 24, 2024
1 parent 0517a87 commit 82085e0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
37 changes: 32 additions & 5 deletions v3/services/scoresvc/internal/scoreservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"os"
"sort"
"strings"
"time"

"github.com/golang/glog"
Expand All @@ -18,6 +19,8 @@ import (

const (
DEFAULT_COOLDOWN_DURATION = time.Hour * 1
SCAN_CACHE_PREFIX = "scan_"
COOLDOWN_PREFIX = "cooldown_"
)

type Cooldown struct {
Expand Down Expand Up @@ -87,7 +90,7 @@ func (s *ScoreServer) AddScoreFunc(w http.ResponseWriter, r *http.Request) {

if newScore.Code != "" {
// Check if this score.code is on cooldown
codeCooldownCacheId := "scan_" + newScore.Code + "_cooldown"
codeCooldownCacheId := COOLDOWN_PREFIX + newScore.Code
_, exp, found := s.Cache.GetWithExpiration(codeCooldownCacheId)

if found {
Expand Down Expand Up @@ -153,8 +156,8 @@ func (s *ScoreServer) ScanFunc(w http.ResponseWriter, r *http.Request) {
return
}

codeCacheId := "scan_" + code
scanner_cooldown_id := codeCacheId + "_cooldown"
codeCacheId := SCAN_CACHE_PREFIX + code
scanner_cooldown_id := COOLDOWN_PREFIX + code

// Retrieve existing scan from cache
_, expiration, found := s.Cache.GetWithExpiration(scanner_cooldown_id)
Expand Down Expand Up @@ -259,13 +262,13 @@ func (s *ScoreServer) GetTimeout() time.Duration {
func (s *ScoreServer) SendNotification(code string) {
glog.Infof("Trying to send notification for ", code)
teamsWebhookUrl, found := os.LookupEnv("WEBHOOK_URL")
if !found {
if !found || teamsWebhookUrl == "" {
glog.Infof("WEBHOOK_URL not found")
return
}

baseUrl, found := os.LookupEnv("BASE_URL")
if !found {
if !found || baseUrl == "" {
glog.Infof("BASE_URL not found")
return
}
Expand Down Expand Up @@ -340,6 +343,30 @@ func (s *ScoreServer) SendNotification(code string) {
glog.Infof("Message sent successfully")
}

func (s *ScoreServer) GetCodes(w http.ResponseWriter, r *http.Request) {
m := make(map[string]bool)
a := []string{}

for i, _ := range s.Cache.Items() {
if strings.HasPrefix(i, SCAN_CACHE_PREFIX) {
if m[i] {
continue
}
a = append(a, strings.TrimPrefix(i, SCAN_CACHE_PREFIX))
m[i] = true
}
}

// Marshal the map into a JSON string.
jsonData, err := json.Marshal(a)
if err != nil {
glog.Errorf("Error creating JSON payload:", err)
return
}

util.ReturnHTTPContent(w, r, 200, "ok", jsonData)
}

// top10Scores returns a LanguageLeaderboard with only the top 10 scores, ranked by score in descending order.
func (s *ScoreServer) rangeScores(leaderboard LanguageLeaderboard, offset int, limit int) LanguageLeaderboard {
// Sort the Scores slice based on the Score field, in descending order.
Expand Down
1 change: 1 addition & 0 deletions v3/services/scoresvc/internal/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func NewScoreServer() (*ScoreServer, error) {
func (s *ScoreServer) SetupRoutes(r *mux.Router) {
r.HandleFunc("/score/leaderboard/{language}", s.GetFunc).Methods("GET")
r.HandleFunc("/score/add/{language}", s.AddScoreFunc).Methods("POST")
r.HandleFunc("/score/scan", s.GetCodes).Methods("GET")
r.HandleFunc("/score/scan/{code}", s.ScanFunc).Methods("POST")
r.HandleFunc("/score/qrcode/{code}", s.HandleGenerateQR).Methods("GET")
r.HandleFunc("/score/healthz", s.Healthz).Methods("GET")
Expand Down

0 comments on commit 82085e0

Please sign in to comment.