Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 53 additions & 16 deletions JustBigO(Fun)/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using JustBigO_Fun_.Data;
using JustBigO_Fun_.Models;
using JustBigO_Fun_.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Diagnostics;
using System.Security.Claims;

namespace JustBigO_Fun_.Controllers
Expand Down Expand Up @@ -145,20 +146,33 @@ public IActionResult Error()

[HttpPost]
public async Task<IActionResult> AnalyzeComplexity(
[FromBody] SubmissionViewModel model,
[FromServices] IComplexityAnalyzer complexityAnalyzer)
[FromBody] SubmissionViewModel model,
[FromServices] IComplexityAnalyzer complexityAnalyzer)
{
if (string.IsNullOrWhiteSpace(model.SourceCode)) return BadRequest("Codul este gol.");

// DECOMENTĂM ASTA CA SĂ MEARGĂ PE BUNE:
var complexity = await complexityAnalyzer.AnalyzeCodeAsync(model.SourceCode);
try
{
// Gestionăm timeout-ul de 10 secunde să nu blocăm interfața.
var analyzeTask = complexityAnalyzer.AnalyzeCodeAsync(model.SourceCode);
var timeoutTask = Task.Delay(TimeSpan.FromSeconds(10));

// RETURNĂM REZULTATUL REAL:
return Json(new
if (await Task.WhenAny(analyzeTask, timeoutTask) == analyzeTask)
{
var complexity = await analyzeTask;
return Json(new
{
timeComplexity = complexity.TimeComplexity,
spaceComplexity = complexity.SpaceComplexity
});
}

return StatusCode(504, "Timeout: Agentul AI a durat prea mult.");
}
catch (Exception)
{
timeComplexity = complexity.TimeComplexity,
spaceComplexity = complexity.SpaceComplexity
});
return StatusCode(500, "Eroare internă a agentului AI.");
}
}

[HttpPost]
Expand All @@ -170,6 +184,9 @@ public async Task<IActionResult> GetHint(
if (model.ProblemId <= 0)
return BadRequest("ProblemId invalid.");

//this is a command to stop the AI enough in order to show the 10s error
//await Task.Delay(15000);

var problem = await _db.Problems.FirstOrDefaultAsync(p => p.Id == model.ProblemId);
if (problem == null)
return NotFound("Problema nu a fost gasita.");
Expand All @@ -196,6 +213,7 @@ public async Task<IActionResult> CompleteCurrentCode(
return BadRequest("Source code is empty.");

var lang = string.IsNullOrWhiteSpace(model.Language) ? "python" : model.Language;

var result = await completionService.CompleteAsync(
model.ProblemId,
model.SourceCode,
Expand Down Expand Up @@ -289,23 +307,42 @@ public async Task<IActionResult> SubmitSolution(
{
if (!ModelState.IsValid) return BadRequest("Date invalide.");

// 1. Executăm codul prin Docker
// 1. Executăm codul prin Docker (Garantăm rularea neobstrucționată)
await codeExecutor.ExecuteAsync(model.ProblemId);

// TODO: Aici îți pui logica ta prin care citești dacă testele au trecut
// Momentan simulăm succesul pentru a vedea AI-ul în acțiune pe interfață
bool isSuccess = true;
string testCasesJson = "[]";

string timeO = "O(?)";
string spaceO = "O(?)";

// 2. Dacă codul trece testele, apelăm Agentul AI (Acceptance Criteria)
// 2. Dacă codul trece testele, apelăm Agentul AI cu TIMEOUT de 10 secunde
if (isSuccess)
{
var complexity = await complexityAnalyzer.AnalyzeCodeAsync(model.SourceCode);
timeO = complexity.TimeComplexity;
spaceO = complexity.SpaceComplexity;
try
{
var analyzeTask = complexityAnalyzer.AnalyzeCodeAsync(model.SourceCode);
var timeoutTask = Task.Delay(TimeSpan.FromSeconds(10));

if (await Task.WhenAny(analyzeTask, timeoutTask) == analyzeTask)
{
var complexity = await analyzeTask;
timeO = complexity.TimeComplexity;
spaceO = complexity.SpaceComplexity;
}
else
{
// Timeout depășit, AI-ul pică, nu blocăm Docker-ul.
timeO = "Timeout AI";
spaceO = "Timeout AI";
}
}
catch
{
timeO = "Eroare AI";
spaceO = "Eroare AI";
}
}

// 3. Returnăm formatul exact pe care îl așteaptă JavaScript-ul
Expand Down
Loading
Loading