aboutsummaryrefslogtreecommitdiffstats
path: root/app/models/result.rb
diff options
context:
space:
mode:
Diffstat (limited to 'app/models/result.rb')
-rw-r--r--app/models/result.rb40
1 files changed, 40 insertions, 0 deletions
diff --git a/app/models/result.rb b/app/models/result.rb
index 8ca10e2..1bbdb29 100644
--- a/app/models/result.rb
+++ b/app/models/result.rb
@@ -1,4 +1,44 @@
+require "cgi"
+
class Result < ApplicationRecord
belongs_to :snippet
belongs_to :compiler
+
+ enum result: [:success, :failed, :errored, :running]
+
+ def finished?
+ !running?
+ end
+
+ def parse_output
+ orig = output.b
+ ret = []
+ while orig.bytesize > 0
+ fd, len = orig.slice!(0, 5).unpack("CV")
+ raise "output is too short" if !len || orig.bytesize < len
+ ret << [fd, orig.slice!(0, len)]
+ end
+ ret
+ end
+
+ def formatted_output
+ parse_output.inject("".b) { |s, (fd, c)|
+ if fd == 1
+ s << CGI.escapeHTML(c)
+ else
+ s << "<span style='color: red'>" << CGI.escapeHTML(c) << "</span>"
+ end
+ }.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