Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solomon/code execute #62

Merged
merged 18 commits into from
Nov 5, 2024
Merged

Solomon/code execute #62

merged 18 commits into from
Nov 5, 2024

Conversation

solomonng2001
Copy link

@solomonng2001 solomonng2001 commented Nov 1, 2024

Execution Service

Installation

  1. Install dependencies:
go mod tidy
  1. Create the .env file from copying the .env.example, and copy the firebase JSON file into execution-service/ fill in the FIREBASE_CREDENTIAL_PATH with the path of the firebase credential JSON file.

Running the Application

To start the server, run the following command:

go run main.go

The server will be available at http://localhost:8083.

Running the Application via Docker

To run the application via Docker, run the following command:

docker build -t execution-service .
docker run -p 8083:8083 --env-file .env -d execution-service

The server will be available at http://localhost:8083.

API Endpoints

  • POST /tests/populate
  • GET /tests/{questionDocRefId}/
  • POST /tests/{questionDocRefId}/execute
  • POST /tests/{questionDocRefId}/submit

Managing Firebase

To reset and repopulate the database, run the following command:

go run main.go

Repopulate test cases

To repopulate test cases, you need to repopulate the questions in the question-service, which will automatically call the execution-service to populate the test cases.

In question-service, run the following command:

go run main.go -populate

API Documentation

GET /tests/{questionDocRefId}/

To read visible test cases via a question ID, run the following command:

curl -X GET http://localhost:8083/tests/{questioinDocRefId}/ \
-H "Content-Type: application/json"

The following json format will be returned:

[
   {
      "input":"hello",
      "expected":"olleh"
   }
]

POST /tests/{questionDocRefId}/execute

To execute test cases via a question ID without custom test cases, run the following command, with custom code and language:

curl -X POST http://localhost:8083/tests/{questioinDocRefId}/execute \
-H "Content-Type: application/json" \
-d '{
"code": "name = input()\nprint(name[::-1])",
"language": "Python"
}'

The following json format will be returned:

{
  "visibleTestResults":[
    {
      "input":"hello",
      "expected":"olleh",
      "actual":"olleh",
      "passed":true,
      "error":""
    }
  ],
  "customTestResults":null
}

To execute visible and custom test cases via a question ID with custom test cases, run the following command, with custom code, language and custom test cases:

curl -X POST http://localhost:8083/tests/{questioinDocRefId}/execute \
-H "Content-Type: application/json" \
-d '{
"code": "name = input()\nprint(name[::-1])",
"language": "Python",
"customTestCases": "2\nHannah\nhannaH\nabcdefg\ngfedcba\n"
}'

The following json format will be returned:

{
  "visibleTestResults":[
    {
      "input":"hello",
      "expected":"olleh",
      "actual":"olleh",
      "passed":true,
      "error":""
    }
  ],
  "customTestResults":[
    {
      "input":"Hannah",
      "expected":"hannaH",
      "actual":"hannaH",
      "passed":true,
      "error":""
    },
    {
      "input":"abcdefg",
      "expected":"gfedcba",
      "actual":"gfedcba",
      "passed":true,
      "error":""
    }
  ]
}

POST /tests/{questionDocRefId}/submit

To submit a solution and execute visible and hidden test cases via a question ID, run the following command, with custom code and language:

curl -X POST http://localhost:8083/tests/{questioinDocRefId}/submit \
-H "Content-Type: application/json" \
-d '{
"title": "Example Title",
"code": "name = input()\nprint(name[::-1])",
"language": "Python",
"user": "user123",
"matchedUser": "user456",
"matchedTopics": ["topic1", "topic2"],
"questionDifficulty": "Medium",
"questionTopics": ["Loops", "Strings"]
}'

The following json format will be returned:

{
  "visibleTestResults":[
    {
      "input":"hello",
      "expected":"olleh",
      "actual":"olleh",
      "passed":true,
      "error":""
    }
  ],
  "hiddenTestResults":{
    "passed":2,
    "total":2
  },
  "status":"Accepted"
}

If compilation error exists or any of the tests (visible and hidden) fails, status "Attempted" will be returned:

{
  "visibleTestResults":[
    {
      "input":"hello",
      "expected":"olleh",
      "actual":"",
      "passed":false,
      "error":"Command execution failed: Traceback (most recent call last):\n  File \"/tmp/4149249165.py\", line 2, in \u003cmodule\u003e\n    prit(name[::-1])\n    ^^^^\nNameError: name 'prit' is not defined. Did you mean: 'print'?\n: %!w(*exec.ExitError=\u0026{0x4000364678 []})"
    }
  ],
  "hiddenTestResults":{
    "passed":0,
    "total":2
  },
  "status":"Attempted"
}

TODOs

  • Add validation to API endpoints
  • Add validation to test cases in database which will be used to validate the custom test cases

@solomonng2001 solomonng2001 self-assigned this Nov 1, 2024
Copy link

@tituschewxj tituschewxj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks for implementing the code execution service. Once the comments are addressed you can merge this PR, as we still need to integrate the other changes together.

We probably can work on the message queuing after this week's demo as we don't want to cause potential issues with deployment.

However, there may be an issue when repopulating the questions as it overwrites the questionDocRefId with random values each time, which affects other services as they depend on this value being the same.

apps/execution-service/handlers/execute.go Show resolved Hide resolved
apps/execution-service/handlers/submit.go Outdated Show resolved Hide resolved
.github/workflows/test.yml Outdated Show resolved Hide resolved
setIsLoadingSubmission(true);
try {
const data = await ExecuteVisibleAndHiddenTestsAndSubmit(
questionDocRefId,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There may be a potential problem with questionDocRefId, which is whenever the function in apps/question-service/utils/populate.go is run, all the questionDocRefId are reinitialized to random values, which affects the retrieval of records that utilize this field as an index.

We can either ensure that questionDocRefId remains the same when updating the DB, or use something else to identify a question.

@solomonng2001 solomonng2001 merged commit 3396c29 into staging Nov 5, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants