|
1 | 1 | package models |
2 | 2 |
|
3 | | -import "time" |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + "time" |
| 7 | +) |
4 | 8 |
|
5 | 9 | type Request struct { |
6 | 10 | Language string `json:"language"` // Programming language (e.g., "c", "python", "javascript") |
7 | 11 | Code string `json:"code"` // Source code to execute |
8 | | - Timeout time.Duration `json:"timeout"` // time limit in seconds |
| 12 | + Timeout time.Duration `json:"timeout"` // time limit in nanoseconds |
9 | 13 | MemoryLimit uint `json:"max_memory"` // memory limit in KB |
10 | | - Stdin []string `json:"inputs,omitempty"` // Array of strings for multiple lines of input, optional |
| 14 | + Stdin string `json:"inputs,omitempty"` // input to take from STDIN, optional, can be multiple lines of input |
| 15 | +} |
| 16 | + |
| 17 | +type SimpleRequest struct { |
| 18 | + Language string `json:"language"` // Programming language (e.g., "c", "python", "javascript") |
| 19 | + Code string `json:"code"` // Source code to execute |
| 20 | + Timeout uint `json:"timeout"` // time limit in seconds |
| 21 | + MemoryLimit uint `json:"max_memory"` // memory limit in KB |
| 22 | + Stdin []string `json:"inputs,omitempty"` // Array of strings for multiple lines of input, optional |
| 23 | +} |
| 24 | + |
| 25 | +func (sr *SimpleRequest) ToRequest() Request { |
| 26 | + return Request{ |
| 27 | + Language: sr.Language, |
| 28 | + Code: sr.Code, |
| 29 | + Timeout: time.Duration(sr.Timeout) * time.Second, |
| 30 | + MemoryLimit: sr.MemoryLimit, |
| 31 | + Stdin: strings.Join(sr.Stdin, "\n"), |
| 32 | + } |
11 | 33 | } |
12 | 34 |
|
13 | 35 | type Response struct { |
14 | 36 | Stdout string `json:"output"` |
15 | 37 | Stderr string `json:"error"` |
16 | | - ExecutionTime time.Duration `json:"cpu_time"` |
17 | | - MemoryUsed uint `json:"memory_used"` |
| 38 | + ExecutionTime time.Duration `json:"cpu_time"` // CPU time used by the program in nanoseconds |
| 39 | + MemoryUsed uint `json:"memory_used"` // Peak memory used by the program in KB |
| 40 | +} |
| 41 | + |
| 42 | +type SimpleResponse struct { |
| 43 | + Stdout string `json:"output"` |
| 44 | + Stderr string `json:"error"` |
| 45 | + MemoryUsed string `json:"memory_used"` // Peak memory used by the program in KB |
| 46 | + ExecutionTime string `json:"cpu_time"` // CPU time used by the program in seconds (ends in s, e.g., "0.5s") |
| 47 | +} |
| 48 | + |
| 49 | +func (r *Response) ToSimpleResponse() SimpleResponse { |
| 50 | + return SimpleResponse{ |
| 51 | + Stdout: r.Stdout, |
| 52 | + Stderr: r.Stderr, |
| 53 | + ExecutionTime: r.ExecutionTime.String(), |
| 54 | + MemoryUsed: fmt.Sprintf("%d KB", r.MemoryUsed), |
| 55 | + } |
18 | 56 | } |
0 commit comments