Skip to content

Commit

Permalink
more budget fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Killerrekt committed Sep 27, 2024
1 parent 10094d2 commit 6ebe8aa
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 31 deletions.
85 changes: 73 additions & 12 deletions internal/controllers/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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")
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion internal/helpers/submission/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
2 changes: 0 additions & 2 deletions internal/helpers/submission/submission.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion internal/server/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
30 changes: 15 additions & 15 deletions internal/worker/submissionWorker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 6ebe8aa

Please sign in to comment.