Skip to content

Commit 0dcc556

Browse files
authored
Merge pull request #20 from aayushk231/worker
feat: add the worker and helper functions
2 parents 8421f2f + 27cf902 commit 0dcc556

File tree

7 files changed

+484
-49
lines changed

7 files changed

+484
-49
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ tmp/
2121
# IDE specific files
2222
.vscode
2323
.idea
24+
*.rest
25+
dump.txt
2426

2527
# .env file
2628
.env

database/queries/submission.sql

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,32 @@ SELECT
3434
user_id
3535
FROM submissions
3636
WHERE id = $1;
37+
38+
-- name: UpdateSubmission :exec
39+
UPDATE submissions
40+
SET
41+
runtime = $1,
42+
memory = $2,
43+
status = $3,
44+
testcases_passed = $4,
45+
testcases_failed = $5
46+
WHERE id = $6;
47+
48+
-- name: UpdateScore :exec
49+
WITH best_submissions AS (
50+
SELECT
51+
s.user_id AS user_id,
52+
s.question_id,
53+
MAX((s.testcases_passed) * q.points / (s.testcases_passed + s.testcases_failed)::numeric) AS best_score
54+
FROM submissions s
55+
INNER JOIN questions q ON s.question_id = q.id
56+
INNER JOIN users u on s.user_id = u.id
57+
WHERE s.user_id = (select user_id from submissions where id = $1) AND q.round = u.round_qualified
58+
GROUP BY s.user_id, s.question_id
59+
)
60+
UPDATE users
61+
SET score = (
62+
SELECT SUM(best_score)
63+
FROM best_submissions
64+
)
65+
WHERE users.id = (select user_id from submissions s where s.id = $1);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- name: CreateSubmissionResult :exec
2+
INSERT INTO submission_results (id, testcase_id, submission_id, runtime, memory, points_awarded, status, description)
3+
VALUES ($1, $2, $3, $4, $5, $6, $7, $8);
4+
5+
-- name: GetStatsForFinalSubEntry :many
6+
SELECT
7+
runtime,
8+
memory,
9+
status
10+
FROM submission_results
11+
WHERE submission_id = $1;

pkg/db/submission.sql.go

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

pkg/db/submission_results.sql.go

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

pkg/utils/tokenCache.go

Lines changed: 114 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,131 @@
11
package utils
22

33
import (
4-
"context"
5-
"fmt"
6-
"os"
4+
"context"
5+
"fmt"
6+
"log"
7+
"math/big"
8+
"os"
79

8-
"github.com/redis/go-redis/v9"
10+
"github.com/CodeChefVIT/cookoff-10.0-be/pkg/db"
11+
"github.com/google/uuid"
12+
"github.com/jackc/pgx/v5/pgtype"
13+
"github.com/redis/go-redis/v9"
914
)
1015

1116
var (
12-
TokenCache *redis.Client
13-
Ctx = context.Background()
17+
TokenCache *redis.Client
18+
Ctx = context.Background()
1419
)
1520

1621
func InitTokenCache() {
17-
redisHost := os.Getenv("REDIS_HOST")
18-
if redisHost == "" {
19-
redisHost = "localhost"
20-
}
21-
22-
redisPort := os.Getenv("REDIS_PORT")
23-
if redisPort == "" {
24-
redisPort = "6379"
25-
}
26-
27-
TokenCache = redis.NewClient(&redis.Options{
28-
Addr: fmt.Sprintf("%s:%s", redisHost, redisPort),
29-
DB: 1,
30-
})
31-
32-
if err := TokenCache.Ping(Ctx).Err(); err != nil {
33-
panic(fmt.Sprintf("failed to connect to Redis token cache: %v", err))
34-
}
22+
redisHost := os.Getenv("REDIS_HOST")
23+
if redisHost == "" {
24+
redisHost = "localhost"
25+
}
26+
27+
redisPort := os.Getenv("REDIS_PORT")
28+
if redisPort == "" {
29+
redisPort = "6379"
30+
}
31+
32+
TokenCache = redis.NewClient(&redis.Options{
33+
Addr: fmt.Sprintf("%s:%s", redisHost, redisPort),
34+
DB: 1,
35+
})
36+
37+
if err := TokenCache.Ping(Ctx).Err(); err != nil {
38+
panic(fmt.Sprintf("failed to connect to Redis token cache: %v", err))
39+
}
3540
}
3641

37-
func StoreToken(token string, submissionID string) error {
38-
if err := TokenCache.Set(Ctx, token, submissionID, 0).Err(); err != nil {
39-
return err
40-
}
41-
return TokenCache.SAdd(Ctx, fmt.Sprintf("submission_tokens:%s", submissionID), token).Err()
42+
func StoreToken(token string, submissionID string, testcaseID string) error {
43+
if err := TokenCache.Set(Ctx, token, fmt.Sprintf("%s:%s", submissionID, testcaseID), 0).Err(); err != nil {
44+
return err
45+
}
46+
return TokenCache.SAdd(Ctx, fmt.Sprintf("submission_tokens:%s", submissionID), token).Err()
4247
}
43-
// get sub
48+
49+
// get sub
4450
func GetSubmissionIDByToken(token string) (string, error) {
45-
submissionID, err := TokenCache.Get(Ctx, token).Result()
46-
if err == redis.Nil {
47-
return "", fmt.Errorf("token not found")
48-
}
49-
return submissionID, err
51+
submissionID, err := TokenCache.Get(Ctx, token).Result()
52+
if err == redis.Nil {
53+
return "", fmt.Errorf("token not found")
54+
}
55+
return submissionID, err
5056
}
51-
//can be used to delete all tokens
57+
58+
// can be used to delete all tokens
5259
func DeleteTokensBySubmissionID(submissionID string) error {
53-
setKey := fmt.Sprintf("submission_tokens:%s", submissionID)
54-
tokens, err := TokenCache.SMembers(Ctx, setKey).Result()
55-
if err != nil {
56-
return err
57-
}
58-
59-
if len(tokens) > 0 {
60-
if err := TokenCache.Del(Ctx, tokens...).Err(); err != nil {
61-
return err
62-
}
63-
}
64-
65-
return TokenCache.Del(Ctx, setKey).Err()
60+
setKey := fmt.Sprintf("submission_tokens:%s", submissionID)
61+
tokens, err := TokenCache.SMembers(Ctx, setKey).Result()
62+
if err != nil {
63+
return err
64+
}
65+
66+
if len(tokens) > 0 {
67+
if err := TokenCache.Del(Ctx, tokens...).Err(); err != nil {
68+
return err
69+
}
70+
}
71+
72+
return TokenCache.Del(Ctx, setKey).Err()
73+
}
74+
75+
func GetTokenCount(ctx context.Context, subID string) (int64, error) {
76+
tokenCount, err := TokenCache.SCard(ctx, fmt.Sprintf("submission_tokens:%s", subID)).Result()
77+
if err != nil {
78+
return 0, err
79+
}
80+
return tokenCount, nil
81+
}
82+
83+
func UpdateSubmission(ctx context.Context, idUUID uuid.UUID) error {
84+
status := "DONE"
85+
86+
data, err := Queries.GetStatsForFinalSubEntry(ctx, idUUID)
87+
if err != nil {
88+
log.Println("Error Fetching submission results: ", err)
89+
return err
90+
}
91+
var runtime float64
92+
var memory int64
93+
var passed, failed int
94+
for _, v := range data {
95+
temp, err := v.Runtime.Float64Value()
96+
if err != nil {
97+
log.Println("Failed to convert runtime to float kms")
98+
return err
99+
}
100+
runtime += temp.Float64
101+
memory += v.Memory.Int.Int64()
102+
if v.Status == "success" {
103+
passed += 1
104+
} else {
105+
failed += 1
106+
}
107+
}
108+
109+
err = Queries.UpdateSubmission(ctx, db.UpdateSubmissionParams{
110+
Runtime: pgtype.Numeric{Int: big.NewInt(int64(runtime)), Valid: true},
111+
Memory: pgtype.Numeric{Int: big.NewInt(int64(memory)), Valid: true},
112+
Status: &status,
113+
TestcasesPassed: pgtype.Int4{Int32: int32(passed), Valid: true},
114+
TestcasesFailed: pgtype.Int4{Int32: int32(failed), Valid: true},
115+
ID: idUUID,
116+
})
117+
118+
if err != nil {
119+
log.Println("Error updating submission: ", err)
120+
return err
121+
}
122+
123+
err = Queries.UpdateScore(ctx, idUUID)
124+
if err != nil {
125+
log.Println("Error updating the score: ", err)
126+
return err
127+
}
128+
129+
log.Printf("Submission ID: %v\n", idUUID)
130+
return nil
66131
}

0 commit comments

Comments
 (0)