diff --git a/compose.yaml b/compose.yaml index e4401a3..eabd04b 100644 --- a/compose.yaml +++ b/compose.yaml @@ -375,13 +375,18 @@ services: build: context: ./services/checker dockerfile: Dockerfile - restart: unless-stopped + env_file: + - path: ./infrastructure/checker/.env.template + required: true + - path: ./infrastructure/checker/.env + required: false ports: - name: web target: 8000 published: 8009 host_ip: 0.0.0.0 protocol: tcp + restart: unless-stopped volumes: - type: bind source: /var/run/docker.sock diff --git a/infrastructure/checker/.env.template b/infrastructure/checker/.env.template new file mode 100644 index 0000000..7c6f9d3 --- /dev/null +++ b/infrastructure/checker/.env.template @@ -0,0 +1,2 @@ +REGISTRY_LOGIN=devitq +REGISTRY_PASSWORD=14zQrbzDTM0WXK@CogMQikAvP74Rj4 diff --git a/services/backend/api/v1/task/views.py b/services/backend/api/v1/task/views.py index b5e0de7..1b86af1 100644 --- a/services/backend/api/v1/task/views.py +++ b/services/backend/api/v1/task/views.py @@ -19,6 +19,7 @@ from apps.task.models import ( CompetitionTaskAttachment, CompetitionTaskSubmission, ) +from apps.task.tasks import analyze_data_task router = Router(tags=["competition"]) @@ -124,6 +125,7 @@ def submit_task( status=CompetitionTaskSubmission.StatusChoices.CHECKING, content=content, ) + analyze_data_task.delay(submission_id=submission.id) return TaskSubmissionOut(submission_id=submission.id) @@ -155,6 +157,4 @@ def get_submissions_history(request, competition_id: UUID, task_id: UUID): ) def get_task_attachments(request, competition_id: UUID, task_id: UUID): task = get_object_or_404(CompetitionTask, id=task_id) - return status.OK, CompetitionTaskAttachment.objects.filter( - task=task - ).all() + return status.OK, CompetitionTaskAttachment.objects.filter(task=task).all() diff --git a/services/backend/api/v1/user/schemas.py b/services/backend/api/v1/user/schemas.py index 832d91f..b97f6ac 100644 --- a/services/backend/api/v1/user/schemas.py +++ b/services/backend/api/v1/user/schemas.py @@ -23,3 +23,8 @@ class UserSchema(ModelSchema): class Meta: model = User fields = ["id", "email", "username", "created_at", "achievements"] + + +class StatSchema(Schema): + total_attempts: int + solved_tasks: int diff --git a/services/backend/api/v1/user/views.py b/services/backend/api/v1/user/views.py index 2b9cdbe..d7a3dfb 100644 --- a/services/backend/api/v1/user/views.py +++ b/services/backend/api/v1/user/views.py @@ -11,15 +11,17 @@ from api.v1.schemas import ( BadRequestError, ConflictError, ForbiddenError, - NotFoundError, + NotFoundError, UnauthorizedError, ) from api.v1.user.schemas import ( LoginSchema, RegisterSchema, TokenSchema, UserSchema, + StatSchema ) from apps.user.models import User +from apps.task.models import CompetitionTaskSubmission, ReviewStatusChoices router = Router(tags=["user"]) @@ -85,3 +87,31 @@ def get_me(request): def get_user(request, user_id: str): user = get_object_or_404(User, id=user_id) return status.OK, user + + +@router.get( + "/me/stat", + response={ + status.OK: StatSchema, + status.UNAUTHORIZED: UnauthorizedError + }, +) +def get_my_stat(request): + user_submissions = CompetitionTaskSubmission.objects.filter( + user=request.auth + ) + checked_attempts = user_submissions.filter(status=CompetitionTaskSubmission.StatusChoices.CHECKED).all() + success_attempts_cnt = 0 + + for attempt in checked_attempts: + is_correct = attempt.result.get("correct", None) + if is_correct is None: + is_correct = attempt.result.get("total_points", 0) > 0 + + if is_correct: + success_attempts_cnt += 1 + + return StatSchema( + total_attempts=len(user_submissions), + solved_tasks=success_attempts_cnt + ) diff --git a/services/backend/apps/competition/tests.py b/services/backend/apps/competition/tests.py index 03f94c6..b4a1609 100644 --- a/services/backend/apps/competition/tests.py +++ b/services/backend/apps/competition/tests.py @@ -35,7 +35,7 @@ class CompetitionEndpointTests(TestCase): self.valid_headers = {"HTTP_AUTHORIZATION": f"Bearer {token}"} def get_url(self, competition_id): - return f"/api/v1/competition/{competition_id}" + return f"/api/v1/competitions/{competition_id}" def test_get_competition_success(self): response = self.client.get( diff --git a/services/backend/apps/task/admin.py b/services/backend/apps/task/admin.py index 1cf4361..4a8ff15 100644 --- a/services/backend/apps/task/admin.py +++ b/services/backend/apps/task/admin.py @@ -31,7 +31,7 @@ class CompetitionTaskSubmissionAdmin(admin.ModelAdmin): "user__username", "user__email", ) - filter = ("plagiarism_checked",) + list_filter = ("plagiarism_checked", "status",) ordering = ["-timestamp"] def has_add_permission(self, request, obj=None): diff --git a/services/backend/apps/task/models.py b/services/backend/apps/task/models.py index ad7255e..c0a4ea5 100644 --- a/services/backend/apps/task/models.py +++ b/services/backend/apps/task/models.py @@ -92,9 +92,9 @@ class CompetitionTaskAttachment(BaseModel): class CompetitionTaskSubmission(BaseModel): class StatusChoices(models.TextChoices): - SENT = "sent" - CHECKING = "checking" - CHECKED = "checked" + SENT = "sent", "Отправлено на проверку" + CHECKING = "checking", "Проверка" + CHECKED = "checked", "Проверено" def submission_content_upload_to(instance, filename) -> str: return f"submissions/{instance.id}/content/{filename}" diff --git a/services/backend/apps/task/tasks.py b/services/backend/apps/task/tasks.py index 0c0a6a9..2f3d50c 100644 --- a/services/backend/apps/task/tasks.py +++ b/services/backend/apps/task/tasks.py @@ -1,4 +1,4 @@ -import requests +import httpx from celery import shared_task from django.core.files.base import ContentFile @@ -17,7 +17,7 @@ def analyze_data_task(self, submission_id): for f in submission.task.attachments.filter(public=True) ] - response = requests.post( + response = httpx.post( f"{settings.CHECKER_API_ENDPOINT}/execute", files=[("files", (f.name, f)) for f in files] + [ @@ -40,10 +40,10 @@ def analyze_data_task(self, submission_id): ) submission.status = CompetitionTaskSubmission.StatusChoices.CHECKED - except requests.exceptions.RequestException as e: + except httpx.RequestError as e: self.retry(countdown=2**self.request.retries) except Exception as e: - submission.result = {"error": str(e)} + submission.result = {"error": str(e), "success": False} submission.status = CompetitionTaskSubmission.StatusChoices.CHECKED submission.earned_points = 0 finally: diff --git a/services/checker/config.py b/services/checker/config.py new file mode 100644 index 0000000..b297cc8 --- /dev/null +++ b/services/checker/config.py @@ -0,0 +1,18 @@ +import os +from pathlib import Path + +from dotenv import load_dotenv + +BASE_DIR = Path(__file__).resolve().parent + +load_dotenv(BASE_DIR / ".env") + +REGISTRY_LOGIN = os.getenv("REGISTRY_USERNAME", None) + +REGISTRY_PASSWORD = os.getenv("REGISTRY_USERNAME", None) + +REGISTRY_URL = os.getenv("REGISTRY_URL", "gitlab.prodcontest.ru:5050") + +DOCKER_IMAGE = os.getenv( + "IMAGE", default="gitlab.prodcontest.ru:5050/team-15/project/custom-python" +) diff --git a/services/checker/main.py b/services/checker/main.py index 4b01fac..f30a3de 100644 --- a/services/checker/main.py +++ b/services/checker/main.py @@ -10,19 +10,25 @@ import tempfile import logging from urllib.parse import urlparse import re - -app = FastAPI() -docker_client = docker.from_env() -logger = logging.getLogger(__name__) -logging.basicConfig(level=logging.INFO) +import config -DOCKER_IMAGE = "gitlab.python:3-slim" CONTAINER_TIMEOUT = 60 MAX_FILE_SIZE = 4 * 1024 * 1024 ALLOWED_FILENAME_CHARS = r"[^a-zA-Z0-9_\-.]" +app = FastAPI() +docker_client = docker.from_env() +logger = logging.getLoggerQ(__name__) +logging.basicConfig(level=logging.INFO) +docker_client.login( + username=config.REGISTRY_LOGIN, + password=config.REGISTRY_PASSWORD, + registry=config.REGISTRY_URL, +) + + class FileDetails(BaseModel): url: HttpUrl = Field( ..., description="URL to download the file from (supports HTTP/HTTPS)" @@ -130,7 +136,7 @@ def run_container_safely( volumes[host_path] = {"bind": container_path, "mode": "ro"} container = docker_client.containers.run( - image=DOCKER_IMAGE, + image=config.DOCKER_IMAGE, command=command, volumes=volumes, working_dir="/execution", @@ -166,6 +172,14 @@ def run_container_safely( pass +def validate_file_path(path: str) -> bool: + return ( + not os.path.isabs(path) + and os.path.basename(path) == path + and all(c.isalnum() or c in {"_", "-", "."} for c in path) + ) + + @app.post("/execute", response_model=ExecutionResponse) async def execute_code(request: ExecutionRequest) -> ExecutionResponse: try: @@ -279,11 +293,3 @@ async def health_check() -> HealthCheckResponse: return HealthCheckResponse(status="healthy", docker="connected") except docker.errors.DockerException: return HealthCheckResponse(status="degraded", docker="unavailable") - - -def validate_file_path(path: str) -> bool: - return ( - not os.path.isabs(path) - and os.path.basename(path) == path - and all(c.isalnum() or c in {"_", "-", "."} for c in path) - ) diff --git a/services/checker/pyproject.toml b/services/checker/pyproject.toml index 2e47424..aaa49d4 100644 --- a/services/checker/pyproject.toml +++ b/services/checker/pyproject.toml @@ -7,6 +7,7 @@ dependencies = [ "aiohttp>=3.11.13", "docker>=7.1.0", "fastapi>=0.115.11", + "python-dotenv>=1.0.1", "python-multipart>=0.0.20", "regex>=2024.11.6", "uvicorn>=0.34.0", diff --git a/services/frontend/src/pages/CompetitionSession/modules/TaskSolution/components/FileSolution/index.tsx b/services/frontend/src/pages/CompetitionSession/modules/TaskSolution/components/FileSolution/index.tsx index 5e103b6..cd2b1d4 100644 --- a/services/frontend/src/pages/CompetitionSession/modules/TaskSolution/components/FileSolution/index.tsx +++ b/services/frontend/src/pages/CompetitionSession/modules/TaskSolution/components/FileSolution/index.tsx @@ -1,17 +1,19 @@ import React from 'react'; -import { FileIcon } from 'lucide-react'; +import { FileIcon, Download } from 'lucide-react'; import { Button } from "@/components/ui/button"; interface FileSolutionProps { selectedFile: File | null; setSelectedFile: (file: File | null) => void; fileInputRef: React.RefObject; + existingFileUrl?: string | null; } const FileSolution: React.FC = ({ selectedFile, setSelectedFile, - fileInputRef + fileInputRef, + existingFileUrl = null }) => { const handleFileChange = (event: React.ChangeEvent) => { if (event.target.files && event.target.files[0]) { @@ -42,6 +44,14 @@ const FileSolution: React.FC = ({ } }; + const fileName = selectedFile + ? selectedFile.name + : existingFileUrl + ? existingFileUrl.split('/').pop() || 'file' + : ''; + + const hasFile = !!selectedFile || !!existingFileUrl; + return ( <> = ({ accept=".jpg,.jpeg,.png,.pptx,.docx,.pdf,.xlsx,.txt" /> - {selectedFile ? ( + {hasFile ? (
- {selectedFile.name} - {(selectedFile.size / 1024).toFixed(1)} KB - + {fileName} + +
+ {existingFileUrl && !selectedFile && ( + + + Скачать + + )} + +
) : ( @@ -82,7 +104,7 @@ const FileSolution: React.FC = ({ Загрузить файл

- Доступные форматы: jpg, jpeg, png + Доступные форматы: jpg, jpeg, png, pptx, docx, pdf, xlsx, txt

)} diff --git a/services/frontend/src/pages/CompetitionSession/modules/TaskSolution/components/SolutionHistorySheet/index.tsx b/services/frontend/src/pages/CompetitionSession/modules/TaskSolution/components/SolutionHistorySheet/index.tsx index dc8fe95..870d511 100644 --- a/services/frontend/src/pages/CompetitionSession/modules/TaskSolution/components/SolutionHistorySheet/index.tsx +++ b/services/frontend/src/pages/CompetitionSession/modules/TaskSolution/components/SolutionHistorySheet/index.tsx @@ -3,20 +3,22 @@ import { Sheet, SheetClose, SheetContent, SheetHeader, SheetTitle } from "@/comp import { Button } from "@/components/ui/button"; import { X } from "lucide-react"; import SolutionStatus from '../SolutionStatus'; -import { Solution } from '@/shared/types/task'; +import { Solution, TaskType } from '@/shared/types/task'; interface SolutionHistorySheetProps { isOpen: boolean; onOpenChange: (open: boolean) => void; solutions: Solution[]; - maxPoints: number + maxPoints: number; + onSolutionSelect: (solution: Solution) => void; } const SolutionHistorySheet: React.FC = ({ isOpen, onOpenChange, solutions, - maxPoints + maxPoints, + onSolutionSelect }) => { return ( @@ -32,10 +34,17 @@ const SolutionHistorySheet: React.FC = ({ -
+
{solutions.length > 0 ? ( solutions.map((solution, index) => ( -
+
{ + onSolutionSelect(solution); + onOpenChange(false); + }} + >
)) diff --git a/services/frontend/src/pages/CompetitionSession/modules/TaskSolution/index.tsx b/services/frontend/src/pages/CompetitionSession/modules/TaskSolution/index.tsx index ea0c35c..d78f313 100644 --- a/services/frontend/src/pages/CompetitionSession/modules/TaskSolution/index.tsx +++ b/services/frontend/src/pages/CompetitionSession/modules/TaskSolution/index.tsx @@ -29,6 +29,7 @@ const TaskSolution: React.FC = ({ }) => { const fileInputRef = useRef(null); const [isHistoryOpen, setIsHistoryOpen] = useState(false); + const [selectedSolutionUrl, setSelectedSolutionUrl] = useState(null); const { id: competitionId } = useParams<{ id: string }>(); const solutionsQuery = useQuery({ @@ -45,6 +46,25 @@ const TaskSolution: React.FC = ({ const latestSolution = solutionHistory && solutionHistory.length > 0 ? solutionHistory[solutionHistory.length - 1] : null; + const handleSolutionSelect = async (solution: Solution) => { + if (!solution.content) return; + + setSelectedSolutionUrl(solution.content); + + try { + if (task.type !== TaskType.FILE) { + const response = await fetch(solution.content); + if (!response.ok) { + throw new Error(`Failed to fetch solution content: ${response.status}`); + } + const text = await response.text(); + setAnswer(text); + } + } catch (error) { + console.error('Error loading solution content:', error); + } + }; + return (
{latestSolution ? ( @@ -64,6 +84,7 @@ const TaskSolution: React.FC = ({ selectedFile={selectedFile} setSelectedFile={setSelectedFile} fileInputRef={fileInputRef} + existingFileUrl={selectedSolutionUrl} /> )} @@ -81,6 +102,7 @@ const TaskSolution: React.FC = ({ onOpenChange={setIsHistoryOpen} solutions={solutionHistory} maxPoints={task.points} + onSolutionSelect={handleSolutionSelect} />
); diff --git a/services/frontend/src/shared/types/task.ts b/services/frontend/src/shared/types/task.ts index 07c6fe1..64e829e 100644 --- a/services/frontend/src/shared/types/task.ts +++ b/services/frontend/src/shared/types/task.ts @@ -15,8 +15,8 @@ export interface TaskAttachment { enum TaskType { INPUT = "input", - FILE = "checker", - CODE = "review", + FILE = "review", + CODE = "checker", } enum SolutionStatus { @@ -29,7 +29,8 @@ interface Solution { id: string, status: SolutionStatus, timestamp: string, - earned_points: number + earned_points: number, + content: string } export type {Task, Solution}