Skip to content

Commit 27cf902

Browse files
committed
fix: add remaining code for worker
1 parent a4d9800 commit 27cf902

File tree

6 files changed

+198
-13
lines changed

6 files changed

+198
-13
lines changed

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: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
11
-- name: CreateSubmissionResult :exec
22
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);
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: 35 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: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@ package utils
33
import (
44
"context"
55
"fmt"
6+
"log"
7+
"math/big"
68
"os"
79

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

@@ -74,3 +79,53 @@ func GetTokenCount(ctx context.Context, subID string) (int64, error) {
7479
}
7580
return tokenCount, nil
7681
}
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
131+
}

pkg/workers/submission.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package workers
33
import (
44
"context"
55
"encoding/json"
6+
"fmt"
67
"log"
78
"math/big"
89
"strings"
@@ -151,20 +152,20 @@ func ProcessJudge0CallbackTask(ctx context.Context, t *asynq.Task) error {
151152
return err
152153
}
153154

154-
// tokenCount, err := submission.Tokens.GetTokenCount(ctx, value)
155-
// if err != nil {
156-
// log.Println("Error getting token count: ", err)
157-
// return err
158-
// }
155+
tokenCount, err := utils.GetTokenCount(ctx, value)
156+
if err != nil {
157+
log.Println("Error getting token count: ", err)
158+
return err
159+
}
159160

160-
// fmt.Println("Token :- ", tokenCount)
161+
fmt.Println("Token :- ", tokenCount)
161162

162-
// if tokenCount == 0 {
163-
// err = submission.UpdateSubmission(ctx, idUUID)
164-
// if err != nil {
165-
// return err
166-
// }
167-
// }
163+
if tokenCount == 0 {
164+
err = utils.UpdateSubmission(ctx, idUUID)
165+
if err != nil {
166+
return err
167+
}
168+
}
168169

169170
return nil
170171
}

0 commit comments

Comments
 (0)