aboutsummaryrefslogtreecommitdiffstats
path: root/app/models/result.rb
blob: 546e6b45fce5de262720bc94a28467d18f34b97f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
require "cgi"

class Result < ApplicationRecord
  belongs_to :snippet
  belongs_to :compiler

  enum result: [:success, :failed, :errored, :timedout, :running]

  def finished?
    !running?
  end

  def parse_output
    orig = output.b
    ret = []
    while orig.bytesize > 0
      fd, len = orig.slice!(0, 8).unpack("ii")
      if !truncated? && (!len || orig.bytesize < len)
        p orig
        p output.b
        raise "output is too short"
      end
      ret << [fd, orig.slice!(0, len)]
    end
    ret
  end

  def formatted_output
    s = "".b
    last_c = nil
    parse_output.each { |fd, c|
      if fd == 1
        s << CGI.escapeHTML(c)
      else
        s << "<span style='color: red'>" << CGI.escapeHTML(c) << "</span>"
      end
      last_c = c
    }

    if truncated?
      s << "<span style='color: white; background-color: black'>#truncated#</span>"
    elsif last_c && last_c[-1] != "\n"
      s << "<span style='color: white; background-color: black'>%</span>"
    end

    if error.present?
      s << "<span style='color: #6666ff; font-style: bold'>" << CGI.escapeHTML(error) << "</span>"
    end

    s.html_safe
  end

  def self.prepare_execution(compiler, snippet)
    # kuso
    r = Result.find_by(snippet: snippet, compiler: compiler)
    if r
      r.update(output: "", status: -1, result: :running)
    else
      r = Result.create!(snippet: snippet, compiler: compiler, output: "", status: -1, result: :running)
    end
    r
  end
end