Skip to content

Commit 6ebe8aa

Browse files
committed
more budget fixes
1 parent 10094d2 commit 6ebe8aa

5 files changed

Lines changed: 90 additions & 31 deletions

File tree

internal/controllers/result.go

Lines changed: 73 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,20 @@ import (
55
"encoding/json"
66
"fmt"
77
"io"
8+
"log"
9+
"math/big"
810
"net/http"
11+
"strconv"
912
"strings"
1013
"time"
1114

15+
"github.com/CodeChefVIT/cookoff-backend/internal/db"
16+
"github.com/CodeChefVIT/cookoff-backend/internal/helpers/database"
1217
httphelpers "github.com/CodeChefVIT/cookoff-backend/internal/helpers/http"
1318
logger "github.com/CodeChefVIT/cookoff-backend/internal/helpers/logging"
1419
"github.com/CodeChefVIT/cookoff-backend/internal/helpers/submission"
15-
"github.com/CodeChefVIT/cookoff-backend/internal/helpers/validator"
20+
"github.com/go-chi/chi/v5"
21+
"github.com/jackc/pgx/v5/pgtype"
1622

1723
"github.com/google/uuid"
1824
)
@@ -26,17 +32,8 @@ func GetResult(w http.ResponseWriter, r *http.Request) {
2632
defer cancel()
2733

2834
var req resultreq
29-
err := httphelpers.ParseJSON(r, &req)
30-
if err != nil {
31-
httphelpers.WriteError(w, http.StatusBadRequest, "Invalid request payload")
32-
return
33-
}
34-
35-
if err := validator.ValidatePayload(w, req); err != nil {
36-
httphelpers.WriteError(w, http.StatusNotAcceptable, "Please provide values for all required fields.")
37-
return
38-
}
3935

36+
req.SubID = chi.URLParam(r, "submission_id")
4037
subid, err := uuid.Parse(req.SubID)
4138
if err != nil {
4239
httphelpers.WriteError(w, http.StatusBadRequest, "Invalid UUID format")
@@ -59,7 +56,7 @@ func GetResult(w http.ResponseWriter, r *http.Request) {
5956
return
6057
}
6158

62-
ticker := time.NewTicker(20 * time.Second)
59+
ticker := time.NewTicker(10 * time.Second)
6360
defer ticker.Stop()
6461

6562
for {
@@ -184,8 +181,72 @@ func BadCodeAlert(ctx context.Context, id uuid.UUID, w http.ResponseWriter) erro
184181
for _, v := range temp.Submissions {
185182
if v.Status.ID != 1 || v.Status.ID != 2 {
186183
submission.Tokens.DeleteToken(ctx, v.Token)
184+
_, testcase, err := submission.GetSubID(ctx, v.Token)
185+
if err != nil {
186+
fmt.Println("Failed to get details from redis:", err)
187+
httphelpers.WriteError(w, http.StatusInternalServerError, "Internal server error!(Failed to get from redis)")
188+
return err
189+
}
190+
timeValue, err := parseTime(*v.Time)
191+
if err != nil {
192+
fmt.Println("Error converting to float:", err)
193+
httphelpers.WriteError(w, http.StatusInternalServerError, "Internal server error!(Failed to convert to float)")
194+
return err
195+
}
196+
tid := uuid.MustParse(testcase)
197+
switch v.Status.ID {
198+
case 3:
199+
err = HandleCompilationError(ctx, id, v, int(timeValue*1000), tid, "success")
200+
case 4:
201+
err = HandleCompilationError(ctx, id, v, int(timeValue*1000), tid, "wrong answer")
202+
case 6:
203+
err = HandleCompilationError(ctx, id, v, int(timeValue*1000), tid, "Compilation error")
204+
case 11:
205+
err = HandleCompilationError(ctx, id, v, int(timeValue*1000), tid, "Runtime error")
206+
}
207+
if err != nil {
208+
fmt.Println("Failed to add submission_results")
209+
}
187210
}
188211
}
189212

190213
return nil
191214
}
215+
216+
func parseTime(timeStr string) (float64, error) {
217+
if timeStr == "" {
218+
log.Println("Time value is empty, setting time to 0 for this submission.")
219+
return 0, nil
220+
}
221+
222+
timeValue, err := strconv.ParseFloat(timeStr, 64)
223+
if err != nil {
224+
return 0, err
225+
}
226+
return timeValue, nil
227+
}
228+
229+
func HandleCompilationError(ctx context.Context, idUUID uuid.UUID, data GetSub, time int, testcase uuid.UUID, status string) error {
230+
subID, err := uuid.NewV7()
231+
232+
if err != nil {
233+
log.Println("Error updating submission for compilation error: ", err)
234+
return err
235+
}
236+
237+
err = database.Queries.CreateSubmissionStatus(ctx, db.CreateSubmissionStatusParams{
238+
ID: subID,
239+
SubmissionID: idUUID,
240+
TestcaseID: uuid.NullUUID{UUID: testcase, Valid: true},
241+
Runtime: pgtype.Numeric{Int: big.NewInt(int64(time)), Valid: true},
242+
Memory: pgtype.Numeric{Int: big.NewInt(int64(*data.Memory)), Valid: true},
243+
Description: &data.Status.Description,
244+
Status: status,
245+
})
246+
247+
if err != nil {
248+
log.Println("Error creating submission status error: ", err)
249+
return err
250+
}
251+
return nil
252+
}

internal/helpers/submission/result.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func UpdateSubmission(ctx context.Context, idUUID uuid.UUID) error {
111111
}
112112

113113
err = database.Queries.UpdateSubmission(ctx, db.UpdateSubmissionParams{
114-
Runtime: pgtype.Numeric{Int: big.NewInt(int64(runtime * 1000)), Valid: true},
114+
Runtime: pgtype.Numeric{Int: big.NewInt(int64(runtime)), Valid: true},
115115
Memory: pgtype.Numeric{Int: big.NewInt(int64(memory)), Valid: true},
116116
Status: &status,
117117
TestcasesPassed: pgtype.Int4{Int32: int32(passed), Valid: true},

internal/helpers/submission/submission.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ func CreateSubmission(
6666
return nil, nil, fmt.Errorf("error marshaling payload: %v", err)
6767
}
6868

69-
fmt.Println(payload.Submissions)
70-
7169
return payloadJSON, testcases_id, nil
7270
}
7371

internal/server/routes.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func (s *Server) RegisterRoutes(taskClient *asynq.Client) http.Handler {
3535
protected.Get("/protected", controllers.ProtectedHandler)
3636
protected.Post("/submit", controllers.SubmitCode)
3737
protected.Post("/runcode", controllers.RunCode)
38-
protected.Get("/result", controllers.GetResult)
38+
protected.Get("/result/{submission_id}", controllers.GetResult)
3939
protected.Get("/question/round", controllers.GetQuestionsByRound)
4040

4141
adminRoutes := protected.With(middlewares.RoleAuthorizationMiddleware("admin"))

internal/worker/submissionWorker.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,28 +58,28 @@ func ProcessSubmissionTask(ctx context.Context, t *asynq.Task) error {
5858
log.Fatalf("Error parsing UUID: %v", err)
5959
}
6060

61-
sub, err := database.Queries.GetSubmission(ctx, idUUID)
62-
if err != nil {
63-
log.Println("Error retrieving submission: ", err)
64-
return err
65-
}
61+
//sub, err := database.Queries.GetSubmission(ctx, idUUID)
62+
//if err != nil {
63+
// log.Println("Error retrieving submission: ", err)
64+
// return err
65+
//}
6666

67-
testcasesPassed := int(sub.TestcasesPassed.Int32)
68-
testcasesFailed := int(sub.TestcasesFailed.Int32)
67+
//testcasesPassed := int(sub.TestcasesPassed.Int32)
68+
//testcasesFailed := int(sub.TestcasesFailed.Int32)
6969

7070
switch data.Status.ID {
7171
case "3":
72-
testcasesPassed++
73-
err = handleCompilationError(ctx, idUUID, data, int(timeValue), testidUUID, "success")
72+
//testcasesPassed++
73+
err = handleCompilationError(ctx, idUUID, data, int(timeValue*1000), testidUUID, "success")
7474
case "4":
75-
testcasesFailed++
76-
err = handleCompilationError(ctx, idUUID, data, int(timeValue), testidUUID, "wrong answer")
75+
//testcasesFailed++
76+
err = handleCompilationError(ctx, idUUID, data, int(timeValue*1000), testidUUID, "wrong answer")
7777
case "6":
78-
testcasesFailed++
79-
err = handleCompilationError(ctx, idUUID, data, int(timeValue), testidUUID, "Compilation error")
78+
//testcasesFailed++
79+
err = handleCompilationError(ctx, idUUID, data, int(timeValue*1000), testidUUID, "Compilation error")
8080
case "11":
81-
testcasesFailed++
82-
err = handleCompilationError(ctx, idUUID, data, int(timeValue), testidUUID, "Runtime error")
81+
//testcasesFailed++
82+
err = handleCompilationError(ctx, idUUID, data, int(timeValue*1000), testidUUID, "Runtime error")
8383
}
8484

8585
if err != nil {

0 commit comments

Comments
 (0)