<commit_before>window.observableCount = 0

class Base
  constructor: (json, defaults={}, nonObservables=[], observe=true) ->
    for k,v of defaults
      if observe and nonObservables.indexOf(k) == -1
        @[k] = @observable(v)
      else
        @[k] = v

    for k,v of json
      if observe and nonObservables.indexOf(k) == -1
        @[k] = @observable(v)
      else
        @[k] = v

  observable: (obj) ->
    observableCount += 1
    if $.isArray obj
      ko.observableArray obj
    else
      ko.observable obj

  komp: (args...) =>
    observableCount += 1
    ko.computed args...


class HasUrl extends Base
  constructor: (json, defaults, nonObservables) ->
    super json, defaults, nonObservables

    @project_name = @komp =>
      @vcs_url().substring(19)

    @project_path = @komp =>
      "/gh/#{@project_name()}"





class LogOutput extends Base
  constructor: (json) ->
    super json

    @output_style = @komp =>
      out: @type() == "out"
      err: @type() == "err"


class ActionLog extends Base
  constructor: (json) ->
    json.out = (new LogOutput(j) for j in json.out) if json.out
    super json, {timedout: null, exit_code: 0, out: null, minimize: true}

    @status = @komp =>
      if @end_time() == null
        "running"
      else if @timedout()
        "timedout"

      else if (@exit_code() == null || @exit_code() == 0)
        "success"
      else
        "failed"

    @success = @komp => (@status() == "success")

    # Expand failing actions
    @minimize(@success())


    @action_header_style = @komp =>
      css = @status()

      result =
        minimize: @minimize()
        contents: @out()

      result[css] = true
      result

    @action_log_style = @komp =>
      minimize: @minimize()

    @duration = @komp () =>
      Circle.time.as_duration(@run_time_millis())

  toggle_minimize: =>
    @minimize(!@minimize())






#TODO: next step is to add the vcs_url, which is why I was looking at the knockout.model and knockout.mapping plugin
class Build extends HasUrl
  constructor: (json) ->
    # make the actionlogs observable
    json.action_logs = (new ActionLog(j) for j in json.action_logs) if json.action_logs
    super json, {}, ["build_num", "status", "committer_name", "committer_email", "why", "user", "job_name", "branch", "vcs_revision", "start_time", "build_time_millis"]


    @url = @komp =>
      "#{@project_path()}/#{@build_num}"

    @style = @komp =>
      klass = switch @status
        when "failed"
          "important"
        when "infrastructure_fail"
          "warning"<commit_msg>Greatly reduce the number of observables in the ActionLog output. Doesn't do much for the render time, sadly, but observables go from 12000, to 200 or so.<commit_after>window.observableCount = 0

class Base
  constructor: (json, defaults={}, nonObservables=[], observe=true) ->
    for k,v of defaults
      if observe and nonObservables.indexOf(k) == -1
        @[k] = @observable(v)
      else
        @[k] = v

    for k,v of json
      if observe and nonObservables.indexOf(k) == -1
        @[k] = @observable(v)
      else
        @[k] = v

  observable: (obj) ->
    observableCount += 1
    if $.isArray obj
      ko.observableArray obj
    else
      ko.observable obj

  komp: (args...) =>
    observableCount += 1
    ko.computed args...


class HasUrl extends Base
  constructor: (json, defaults, nonObservables) ->
    super json, defaults, nonObservables

    @project_name = @komp =>
      @vcs_url().substring(19)

    @project_path = @komp =>
      "/gh/#{@project_name()}"





class LogOutput extends Base
  constructor: (json) ->
    super json, {}, ["type"], false

  output_style: =>
    out: @type == "out"
    err: @type == "err"


class ActionLog extends Base
  constructor: (json) ->
    json.out = (new LogOutput(j) for j in json.out) if json.out
    super json, {timedout: null, exit_code: 0, out: null, minimize: true}, ["end_time", "timedout", "exit_code", "run_time_millis"]

    @status = @komp =>
      if @end_time == null
        "running"
      else if @timedout
        "timedout"

      else if (@exit_code == null || @exit_code == 0)
        "success"
      else
        "failed"

    @success = @komp => (@status() == "success")

    # Expand failing actions
    @minimize(@success())


    @action_header_style = @komp =>
      css = @status()

      result =
        minimize: @minimize()
        contents: @out()

      result[css] = true
      result

    @action_log_style = @komp =>
      minimize: @minimize()

    @duration = @komp () =>
      Circle.time.as_duration(@run_time_millis)

  toggle_minimize: =>
    @minimize(!@minimize())






#TODO: next step is to add the vcs_url, which is why I was looking at the knockout.model and knockout.mapping plugin
class Build extends HasUrl
  constructor: (json) ->
    # make the actionlogs observable
    json.action_logs = (new ActionLog(j) for j in json.action_logs) if json.action_logs
    super json, {}, ["build_num", "status", "committer_name", "committer_email", "why", "user", "job_name", "branch", "vcs_revision", "start_time", "build_time_millis"]


    @url = @komp =>
      "#{@project_path()}/#{@build_num}"

    @style = @komp =>
      klass = switch @status
        when "failed"
          "important"
        when "infrastructure_fail"
          "warning"
{'fraction_non_alphanumeric': 0.13913857677902622, 'fraction_numerical': 0.0048689138576779025, 'mean_word_length': 2.618563685636856, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3167028', 'n_tokens_mistral': 1805, 'n_tokens_neox': 1717, 'n_words': 486}