From 6ebe8aa25f4539b58bab9390f842a6f4adc6ef08 Mon Sep 17 00:00:00 2001 From: killerrekt Date: Fri, 27 Sep 2024 02:44:35 +0000 Subject: [PATCH] more budget fixes --- internal/controllers/result.go | 85 +++++++++++++++++++---- internal/helpers/submission/result.go | 2 +- internal/helpers/submission/submission.go | 2 - internal/server/routes.go | 2 +- internal/worker/submissionWorker.go | 30 ++++---- 5 files changed, 90 insertions(+), 31 deletions(-) diff --git a/internal/controllers/result.go b/internal/controllers/result.go index 1483a35..81a4aa1 100644 --- a/internal/controllers/result.go +++ b/internal/controllers/result.go @@ -5,14 +5,20 @@ import ( "encoding/json" "fmt" "io" + "log" + "math/big" "net/http" + "strconv" "strings" "time" + "github.com/CodeChefVIT/cookoff-backend/internal/db" + "github.com/CodeChefVIT/cookoff-backend/internal/helpers/database" httphelpers "github.com/CodeChefVIT/cookoff-backend/internal/helpers/http" logger "github.com/CodeChefVIT/cookoff-backend/internal/helpers/logging" "github.com/CodeChefVIT/cookoff-backend/internal/helpers/submission" - "github.com/CodeChefVIT/cookoff-backend/internal/helpers/validator" + "github.com/go-chi/chi/v5" + "github.com/jackc/pgx/v5/pgtype" "github.com/google/uuid" ) @@ -26,17 +32,8 @@ func GetResult(w http.ResponseWriter, r *http.Request) { defer cancel() var req resultreq - err := httphelpers.ParseJSON(r, &req) - if err != nil { - httphelpers.WriteError(w, http.StatusBadRequest, "Invalid request payload") - return - } - - if err := validator.ValidatePayload(w, req); err != nil { - httphelpers.WriteError(w, http.StatusNotAcceptable, "Please provide values for all required fields.") - return - } + req.SubID = chi.URLParam(r, "submission_id") subid, err := uuid.Parse(req.SubID) if err != nil { httphelpers.WriteError(w, http.StatusBadRequest, "Invalid UUID format") @@ -59,7 +56,7 @@ func GetResult(w http.ResponseWriter, r *http.Request) { return } - ticker := time.NewTicker(20 * time.Second) + ticker := time.NewTicker(10 * time.Second) defer ticker.Stop() for { @@ -184,8 +181,72 @@ func BadCodeAlert(ctx context.Context, id uuid.UUID, w http.ResponseWriter) erro for _, v := range temp.Submissions { if v.Status.ID != 1 || v.Status.ID != 2 { submission.Tokens.DeleteToken(ctx, v.Token) + _, testcase, err := submission.GetSubID(ctx, v.Token) + if err != nil { + fmt.Println("Failed to get details from redis:", err) + httphelpers.WriteError(w, http.StatusInternalServerError, "Internal server error!(Failed to get from redis)") + return err + } + timeValue, err := parseTime(*v.Time) + if err != nil { + fmt.Println("Error converting to float:", err) + httphelpers.WriteError(w, http.StatusInternalServerError, "Internal server error!(Failed to convert to float)") + return err + } + tid := uuid.MustParse(testcase) + switch v.Status.ID { + case 3: + err = HandleCompilationError(ctx, id, v, int(timeValue*1000), tid, "success") + case 4: + err = HandleCompilationError(ctx, id, v, int(timeValue*1000), tid, "wrong answer") + case 6: + err = HandleCompilationError(ctx, id, v, int(timeValue*1000), tid, "Compilation error") + case 11: + err = HandleCompilationError(ctx, id, v, int(timeValue*1000), tid, "Runtime error") + } + if err != nil { + fmt.Println("Failed to add submission_results") + } } } return nil } + +func parseTime(timeStr string) (float64, error) { + if timeStr == "" { + log.Println("Time value is empty, setting time to 0 for this submission.") + return 0, nil + } + + timeValue, err := strconv.ParseFloat(timeStr, 64) + if err != nil { + return 0, err + } + return timeValue, nil +} + +func HandleCompilationError(ctx context.Context, idUUID uuid.UUID, data GetSub, time int, testcase uuid.UUID, status string) error { + subID, err := uuid.NewV7() + + if err != nil { + log.Println("Error updating submission for compilation error: ", err) + return err + } + + err = database.Queries.CreateSubmissionStatus(ctx, db.CreateSubmissionStatusParams{ + ID: subID, + SubmissionID: idUUID, + TestcaseID: uuid.NullUUID{UUID: testcase, Valid: true}, + Runtime: pgtype.Numeric{Int: big.NewInt(int64(time)), Valid: true}, + Memory: pgtype.Numeric{Int: big.NewInt(int64(*data.Memory)), Valid: true}, + Description: &data.Status.Description, + Status: status, + }) + + if err != nil { + log.Println("Error creating submission status error: ", err) + return err + } + return nil +} diff --git a/internal/helpers/submission/result.go b/internal/helpers/submission/result.go index cfb2d37..15bfe37 100644 --- a/internal/helpers/submission/result.go +++ b/internal/helpers/submission/result.go @@ -111,7 +111,7 @@ func UpdateSubmission(ctx context.Context, idUUID uuid.UUID) error { } err = database.Queries.UpdateSubmission(ctx, db.UpdateSubmissionParams{ - Runtime: pgtype.Numeric{Int: big.NewInt(int64(runtime * 1000)), Valid: true}, + Runtime: pgtype.Numeric{Int: big.NewInt(int64(runtime)), Valid: true}, Memory: pgtype.Numeric{Int: big.NewInt(int64(memory)), Valid: true}, Status: &status, TestcasesPassed: pgtype.Int4{Int32: int32(passed), Valid: true}, diff --git a/internal/helpers/submission/submission.go b/internal/helpers/submission/submission.go index d0d5bb3..60de104 100644 --- a/internal/helpers/submission/submission.go +++ b/internal/helpers/submission/submission.go @@ -66,8 +66,6 @@ func CreateSubmission( return nil, nil, fmt.Errorf("error marshaling payload: %v", err) } - fmt.Println(payload.Submissions) - return payloadJSON, testcases_id, nil } diff --git a/internal/server/routes.go b/internal/server/routes.go index 5403566..3144728 100644 --- a/internal/server/routes.go +++ b/internal/server/routes.go @@ -35,7 +35,7 @@ func (s *Server) RegisterRoutes(taskClient *asynq.Client) http.Handler { protected.Get("/protected", controllers.ProtectedHandler) protected.Post("/submit", controllers.SubmitCode) protected.Post("/runcode", controllers.RunCode) - protected.Get("/result", controllers.GetResult) + protected.Get("/result/{submission_id}", controllers.GetResult) protected.Get("/question/round", controllers.GetQuestionsByRound) adminRoutes := protected.With(middlewares.RoleAuthorizationMiddleware("admin")) diff --git a/internal/worker/submissionWorker.go b/internal/worker/submissionWorker.go index 84f4746..7c9c7a9 100644 --- a/internal/worker/submissionWorker.go +++ b/internal/worker/submissionWorker.go @@ -58,28 +58,28 @@ func ProcessSubmissionTask(ctx context.Context, t *asynq.Task) error { log.Fatalf("Error parsing UUID: %v", err) } - sub, err := database.Queries.GetSubmission(ctx, idUUID) - if err != nil { - log.Println("Error retrieving submission: ", err) - return err - } + //sub, err := database.Queries.GetSubmission(ctx, idUUID) + //if err != nil { + // log.Println("Error retrieving submission: ", err) + // return err + //} - testcasesPassed := int(sub.TestcasesPassed.Int32) - testcasesFailed := int(sub.TestcasesFailed.Int32) + //testcasesPassed := int(sub.TestcasesPassed.Int32) + //testcasesFailed := int(sub.TestcasesFailed.Int32) switch data.Status.ID { case "3": - testcasesPassed++ - err = handleCompilationError(ctx, idUUID, data, int(timeValue), testidUUID, "success") + //testcasesPassed++ + err = handleCompilationError(ctx, idUUID, data, int(timeValue*1000), testidUUID, "success") case "4": - testcasesFailed++ - err = handleCompilationError(ctx, idUUID, data, int(timeValue), testidUUID, "wrong answer") + //testcasesFailed++ + err = handleCompilationError(ctx, idUUID, data, int(timeValue*1000), testidUUID, "wrong answer") case "6": - testcasesFailed++ - err = handleCompilationError(ctx, idUUID, data, int(timeValue), testidUUID, "Compilation error") + //testcasesFailed++ + err = handleCompilationError(ctx, idUUID, data, int(timeValue*1000), testidUUID, "Compilation error") case "11": - testcasesFailed++ - err = handleCompilationError(ctx, idUUID, data, int(timeValue), testidUUID, "Runtime error") + //testcasesFailed++ + err = handleCompilationError(ctx, idUUID, data, int(timeValue*1000), testidUUID, "Runtime error") } if err != nil {