Skip to content

Commit

Permalink
Merge pull request #5 from Project-IPCA/feature/APP-0002
Browse files Browse the repository at this point in the history
[APP-0002] Add json map for activity log and add constants value
  • Loading branch information
CheIby authored Dec 11, 2024
2 parents 9fca4b3 + 893b697 commit f9d16aa
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 92 deletions.
Binary file modified main
Binary file not shown.
20 changes: 10 additions & 10 deletions models/activity_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ import (
)

type ActivityLog struct {
LogID string `gorm:"type:varchar(26);primary_key;column:log_id"`
Timestamp time.Time `gorm:"type:timestamp;not null;default:CURRENT_TIMESTAMP;column:timestamp"`
GroupID *uuid.UUID `gorm:"type:varchar(36);column:group_id"`
Username string `gorm:"type:varchar(30);not null;column:username"`
RemoteIP string `gorm:"type:varchar(15);not null;column:remote_ip"`
RemotePort *int `gorm:"column:remote_port"`
Agent *string `gorm:"type:varchar(255);column:agent"`
PageName string `gorm:"type:varchar(25);not null;column:page_name"`
Action string `gorm:"type:text;not null;column:action"`
CI *uint `gorm:"column:ci"`
LogID string `json:"log_id" gorm:"type:varchar(26);primary_key;column:log_id"`
Timestamp time.Time `json:"timestamp" gorm:"type:timestamp;not null;default:CURRENT_TIMESTAMP;column:timestamp"`
GroupID *uuid.UUID `json:"group_id" gorm:"type:varchar(36);column:group_id"`
Username string `json:"username" gorm:"type:varchar(30);not null;column:username"`
RemoteIP string `json:"remote_ip" gorm:"type:varchar(15);not null;column:remote_ip"`
RemotePort *int `json:"remote_port" gorm:"column:remote_port"`
Agent *string `json:"agent" gorm:"type:varchar(255);column:agent"`
PageName string `json:"page_name" gorm:"type:varchar(25);not null;column:page_name"`
Action string `json:"action" gorm:"type:text;not null;column:action"`
CI *uint `json:"ci" gorm:"column:ci"`
}

func (ActivityLog) TableName() string {
Expand Down
2 changes: 2 additions & 0 deletions repositories/activity_log_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package repositories

import (
"encoding/json"
"time"

"github.com/Project-IPCA/ipca-worker-go-v2/models"
"github.com/Project-IPCA/ipca-worker-go-v2/utils"
Expand Down Expand Up @@ -42,6 +43,7 @@ func (activityLogRepository ActivityLogRepository)AddSubmissionLog(log_data *mod
PageName: log_data.PageName,
Action: string(action_str),
RemotePort: &log_data.RemotePort,
Timestamp: time.Now(),
}

if err := activityLogRepository.DB.Create(&add_Log).Error; err != nil {
Expand Down
163 changes: 81 additions & 82 deletions service/run_submission.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,35 @@ import (
"gorm.io/gorm"
)

type SubmissionResult struct{
type SubmissionResult struct {
SubmissionID string
Status string
Marking string
Result string
ErrorMessage string
Status string
Marking string
Result string
ErrorMessage string
}

type TestCaseResult struct{
TestCaseNo int `json:"testcase_no"`
IsPassed bool `json:"is_passed"`
ShowToStudent bool `json:"show_to_student"`
Expected string `json:"expected"`
Actual string `json:"actual"`
type TestCaseResult struct {
TestCaseNo int `json:"testcase_no"`
IsPassed bool `json:"is_passed"`
ShowToStudent bool `json:"show_to_student"`
Expected string `json:"expected"`
Actual string `json:"actual"`
}

func RunSubmission(channel *amqp.Channel, db_pool *gorm.DB, msg amqp.Delivery, msgBody models.ReciveMessage, redisClient *redis.Client){
func RunSubmission(channel *amqp.Channel, db_pool *gorm.DB, msg amqp.Delivery, msgBody models.ReciveMessage, redisClient *redis.Client) {
publisher := redis_client.NewRedisAction(redisClient)
activityLogRepo := repositories.NewActivityLogRePository(db_pool)
excerciseSubmissionRepo := repositories.NewExerciseSubmissionRePository(db_pool)
var publishLog *models.ActivityLog

tempLog,err := compileCode(db_pool,msgBody);
tempLog, err := compileCode(db_pool, msgBody)
publishLog = tempLog
if(err!=nil){
if err != nil {
appErr, ok := err.(*utils.AppError)
if(ok && (appErr.Name == utils.ERROR_NAME.DATABASE_ERROR || appErr.Name == utils.ERROR_NAME.FUNCTION_ERROR)){
channel.Nack(msg.DeliveryTag,false,false)
}else{
if ok && (appErr.Name == utils.ERROR_NAME.DATABASE_ERROR || appErr.Name == utils.ERROR_NAME.FUNCTION_ERROR) {
channel.Nack(msg.DeliveryTag, false, false)
} else {
newAction := msgBody.LogData.Actoin

var output interface{}
Expand All @@ -58,56 +58,55 @@ func RunSubmission(channel *amqp.Channel, db_pool *gorm.DB, msg amqp.Delivery, m
resultJson, err := json.Marshal(output)
if err != nil {
fmt.Println("Error marshalling testcaseResult:", err)
return
return
}
outputStr := string(resultJson)
errorMessage := string(appErr.Err.Error())

marking := 0

submissionUuid,err := uuid.Parse(*msgBody.SubmissionID)
if(err!=nil){
submissionUuid, err := uuid.Parse(*msgBody.SubmissionID)
if err != nil {
fmt.Println("fail to convert uuid")
return
return
}

submission := models.UpdateSubmissionInfo{
SubmissionID: submissionUuid,
Status: "error",
Marking: marking,
Result: &outputStr,
Status: utils.ExerciseStatus.Error,
Marking: marking,
Result: &outputStr,
ErrorMessage: &errorMessage,
}

saveLog := &msgBody.LogData
newAction.Status = "error"
newAction.Status = utils.ExerciseStatus.Error
newAction.Marking = &marking

err = excerciseSubmissionRepo.UpdateSubmission(&submission)
if err != nil {
channel.Nack(msg.DeliveryTag,false,false)
fmt.Println("Error updating submission:", err)
return
}
if err != nil {
channel.Nack(msg.DeliveryTag, false, false)
fmt.Println("Error updating submission:", err)
return
}

tempLog, err := activityLogRepo.AddSubmissionLog(saveLog)
publishLog = tempLog
if err != nil {
channel.Nack(msg.DeliveryTag,false,false)
fmt.Println("Error adding submission log:", err)
return
}
tempLog, err := activityLogRepo.AddSubmissionLog(saveLog)
publishLog = tempLog
if err != nil {
channel.Nack(msg.DeliveryTag, false, false)
fmt.Println("Error adding submission log:", err)
return
}
}
}

err = publisher.PublishMessage(fmt.Sprintf("submission-result:%s", msgBody.JobID),"done")
err = publisher.PublishMessage(fmt.Sprintf("submission-result:%s", msgBody.JobID), "done")
if err != nil {
fmt.Println("Error publishing to Redis:", err)
return
}
if publishLog != nil {
logJSON, _ := json.Marshal(publishLog)
err = publisher.PublishMessage(fmt.Sprintf("logs:%s", msgBody.LogData.GroupID), string(logJSON))
err = publisher.PublishMessage(fmt.Sprintf("logs:%s", msgBody.LogData.GroupID), publishLog)
if err != nil {
fmt.Println("Error publishing log to Redis:", err)
}
Expand All @@ -117,52 +116,52 @@ func RunSubmission(channel *amqp.Channel, db_pool *gorm.DB, msg amqp.Delivery, m
channel.Ack(msg.DeliveryTag, false)
}

func compileCode (db_pool *gorm.DB, msgBody models.ReciveMessage) (*models.ActivityLog,error){
submissionUuid,err := uuid.Parse(*msgBody.SubmissionID)
if(err!=nil){
func compileCode(db_pool *gorm.DB, msgBody models.ReciveMessage) (*models.ActivityLog, error) {
submissionUuid, err := uuid.Parse(*msgBody.SubmissionID)
if err != nil {
fmt.Println("fail to convert uuid")
return nil,utils.NewAppError(utils.ERROR_NAME.FUNCTION_ERROR,"failed to convert uuid", err.Error())
return nil, utils.NewAppError(utils.ERROR_NAME.FUNCTION_ERROR, "failed to convert uuid", err.Error())
}
activityLogRepo := repositories.NewActivityLogRePository(db_pool)
excerciseSubmissionRepo := repositories.NewExerciseSubmissionRePository(db_pool)

if(err!=nil){
if err != nil {
fmt.Println("fail to convert to int")
return nil,utils.NewAppError(utils.ERROR_NAME.FUNCTION_ERROR,"failed to convert", err.Error())
return nil, utils.NewAppError(utils.ERROR_NAME.FUNCTION_ERROR, "failed to convert", err.Error())
}

testcaseResult := []TestCaseResult{}
newAction := msgBody.LogData.Actoin
insertedLog := models.ActivityLog{}

if(len(msgBody.TestCaseList)>0){
if len(msgBody.TestCaseList) > 0 {
for i, testcase := range msgBody.TestCaseList {
result, err := utils.RunPythonScript(testcase, msgBody.SourceCode)
if err != nil {
appErr, ok := err.(*utils.AppError)
if(ok){
if ok {
fmt.Println("Error running Python script:", appErr)
return nil,utils.NewAppError(appErr.Name,appErr.Error(), appErr.Stdout)
return nil, utils.NewAppError(appErr.Name, appErr.Error(), appErr.Stdout)
}
}
passed := strings.TrimSpace(result) == strings.TrimSpace(testcase.TestCaseOutput)
fmt.Printf("Testcase %d: %v\n", i+1, passed)
testcaseResult = append(testcaseResult,TestCaseResult{
TestCaseNo: i+1,
IsPassed: passed,

testcaseResult = append(testcaseResult, TestCaseResult{
TestCaseNo: i + 1,
IsPassed: passed,
ShowToStudent: testcase.ShowToStudent,
Expected: strings.TrimSpace(testcase.TestCaseOutput),
Actual: strings.TrimSpace(result),
Expected: strings.TrimSpace(testcase.TestCaseOutput),
Actual: strings.TrimSpace(result),
})
}

isPassedAllTestcase := true
for _, testcase := range testcaseResult {
if !testcase.IsPassed {
isPassedAllTestcase = false
break
}
if !testcase.IsPassed {
isPassedAllTestcase = false
break
}
}

studentMarking := 0
Expand All @@ -173,22 +172,22 @@ func compileCode (db_pool *gorm.DB, msgBody models.ReciveMessage) (*models.Activ
jsonData, err := json.Marshal(testcaseResult)
if err != nil {
fmt.Println("Error marshalling testcaseResult:", err)
return nil,utils.NewAppError(utils.ERROR_NAME.DATABASE_ERROR,"Error marshalling testcaseResult", err.Error())
return nil, utils.NewAppError(utils.ERROR_NAME.DATABASE_ERROR, "Error marshalling testcaseResult", err.Error())
}

outputStr := string(jsonData)
status := "wrong_answer";
if(studentMarking == 2){
status := utils.ExerciseStatus.WrongAnswer
if studentMarking == 2 {
studentAssignItemRepo := repositories.NewStudentAssignChapterItemRepository(db_pool)
studentAssignItemRepo.UpdateStudentAssignItemMarking(msgBody.StudentId, msgBody.ChapterId,msgBody.ItemId,studentMarking)
status = "accepted";
studentAssignItemRepo.UpdateStudentAssignItemMarking(msgBody.StudentId, msgBody.ChapterId, msgBody.ItemId, studentMarking)
status = utils.ExerciseStatus.Accepted
}

submission := models.UpdateSubmissionInfo{
SubmissionID: submissionUuid,
Status: status,
Marking: studentMarking,
Result: &outputStr,
Status: status,
Marking: studentMarking,
Result: &outputStr,
ErrorMessage: nil,
}

Expand All @@ -200,58 +199,58 @@ func compileCode (db_pool *gorm.DB, msgBody models.ReciveMessage) (*models.Activ
err = excerciseSubmissionRepo.UpdateSubmission(&submission)
if err != nil {
fmt.Println("Error updating submission:", err)
return nil,utils.NewAppError(utils.ERROR_NAME.DATABASE_ERROR,"Error marshalling testcaseResult", err.Error())
return nil, utils.NewAppError(utils.ERROR_NAME.DATABASE_ERROR, "Error marshalling testcaseResult", err.Error())
}

tempLog, err := activityLogRepo.AddSubmissionLog(saveLog)
if err != nil {
fmt.Println("Error adding submission log:", err)
return nil,utils.NewAppError(utils.ERROR_NAME.DATABASE_ERROR,"Error marshalling testcaseResult", err.Error())
return nil, utils.NewAppError(utils.ERROR_NAME.DATABASE_ERROR, "Error marshalling testcaseResult", err.Error())
}
insertedLog = *tempLog
}else{
} else {
result, err := utils.RunPythonScriptWithoutTestcase(msgBody.SourceCode)
if err != nil {
fmt.Println("Error running Python script:", err)
return nil,utils.NewAppError(utils.ERROR_NAME.DATABASE_ERROR,"Error running Python script", err.Error())
return nil, utils.NewAppError(utils.ERROR_NAME.DATABASE_ERROR, "Error running Python script", err.Error())
}
fmt.Println("Output : ",strings.TrimSpace(result))
fmt.Println("Output : ", strings.TrimSpace(result))

jsonData, err := json.Marshal(strings.TrimSpace(result))
if err != nil {
fmt.Println("Error marshalling testcaseResult:", err)
return nil,utils.NewAppError(utils.ERROR_NAME.DATABASE_ERROR,"Error marshalling testcaseResult", err.Error())
return nil, utils.NewAppError(utils.ERROR_NAME.DATABASE_ERROR, "Error marshalling testcaseResult", err.Error())
}

outputStr := string(jsonData)

studentMarking :=2
studentMarking := 2

submission := models.UpdateSubmissionInfo{
SubmissionID: submissionUuid,
Status: "accepted",
Marking: studentMarking,
Result: &outputStr,
Status: utils.ExerciseStatus.Accepted,
Marking: studentMarking,
Result: &outputStr,
ErrorMessage: nil,
}

newAction.Status = "accepted"
newAction.Status = utils.ExerciseStatus.Accepted
newAction.Marking = &studentMarking
saveLog := &msgBody.LogData
saveLog.Actoin = newAction

err = excerciseSubmissionRepo.UpdateSubmission(&submission)
if err != nil {
fmt.Println("Error updating submission:", err)
return nil,utils.NewAppError(utils.ERROR_NAME.DATABASE_ERROR,"Error marshalling testcaseResult", err.Error())
return nil, utils.NewAppError(utils.ERROR_NAME.DATABASE_ERROR, "Error marshalling testcaseResult", err.Error())
}

tempLog, err := activityLogRepo.AddSubmissionLog(saveLog)
if err != nil {
fmt.Println("Error adding submission log:", err)
return nil,utils.NewAppError(utils.ERROR_NAME.DATABASE_ERROR,"Error marshalling testcaseResult", err.Error())
return nil, utils.NewAppError(utils.ERROR_NAME.DATABASE_ERROR, "Error marshalling testcaseResult", err.Error())
}
insertedLog = *tempLog
}
return &insertedLog,nil
}
return &insertedLog, nil
}
17 changes: 17 additions & 0 deletions utils/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package utils

type ExerciseStatusStruct struct {
Accepted string
WrongAnswer string
Pending string
Rejected string
Error string
}

var ExerciseStatus = ExerciseStatusStruct{
Accepted: "ACCEPTED",
WrongAnswer: "WRONG_ANSWER",
Pending: "PENDING",
Rejected: "REJECTED",
Error: "ERROR",
}

0 comments on commit f9d16aa

Please sign in to comment.