<commit_before>define [
  'art-foundation'
  'art-atomic'
  'art-canvas'
  '../core'
], (Foundation, Atomic, Canvas, EngineCore) ->
  {log, Map, miniInspect, currentSecond, max, min, timeout, peek} = Foundation
  {point, rect, Matrix, color} = Atomic
  {GlobalEpochCycle} = EngineCore
  {globalEpochCycle} = GlobalEpochCycle
  {floor} = Math

  DomConsole = Neptune.Art.DevTools?.DomConsole

  reactColor = color("gold")
  aimColor = color "#9c3"

  class GlobalEpochStat extends Foundation.BaseObject
    constructor: (@sampleTime, @total, @sampleSet) ->

    @statFields:  statFields  = ["total", "draw", "aim", "aimLayout", "aimTL", "aimRR", "react", "reactAim", "reactLC", "reactRender", "event", "flux"]
    @statColors: statColors =
      total:  "gray"
      draw:   "#39c"
      aim:          aimColor
      aimLayout:    aimColor.withLightness aimColor.lightness * .9
      aimTL:        aimColor.withLightness aimColor.lightness * .8
      aimRR:        aimColor.withLightness aimColor.lightness * .7
      react:        reactColor
      reactAim:     reactColor.withLightness reactColor.lightness *.9
      reactLC:      reactColor.withLightness reactColor.lightness *.8
      reactRender:  reactColor.withLightness reactColor.lightness *.7
      event:  "#ff6347"
      flux:   "#d936a3"

    getStacked: (sampleName) ->
      return @total if sampleName == "total"
      sum = 0
      for sn in statFields by -1
        sum += @sampleSet[sn] || 0
        break if sn == sampleName
      sum

    drawSample: (bitmap, drawMatrix, sampleWidth, sampleField, h) ->
      {sampleTime} = @
      sample = @getStacked sampleField
      x = floor drawMatrix.transformX sampleTime, sample
      y = floor drawMatrix.transformY sampleTime, sample
      bitmap.drawRectangle null, rect(x, y, sampleWidth, h - y), color: statColors[sampleField]

  class GlobalEpochStats extends Foundation.BaseObject
    @classGetter
      enabled: -> !!globalEpochCycle.globalEpochStats

    @enable: ->
      log "Enabled globalEpochStats"
      globalEpochCycle.globalEpochStats = new @
      true

    @toggle: ->
      if @enabled
        @disable()
      else
        @enable()

    @disable: ->
      log "Disabled globalEpochStats"
      globalEpochCycle.globalEpochStats = null
      false

    constructor: ->
      @reset()

    reset: ->
      @maxMs = 2/60
      @stats = []
      @nextEventIdIndex = 0
      @eventsById = {}
      @eventLegend = {}
      @_minSampleTime = null
      @_maxSampleTime = null

    add: (sampleTime, total, sampleSet) ->
      @stats.push ges = new GlobalEpochStat sampleTime, total, sampleSet

      @maxMs = max @maxMs, total * 1.5
      @logAndResetWhenIdle()
      @addSampleTime sampleTime

    addSampleTime: (time) ->
      @_minSampleTime = min time, @_minSampleTime || time
      @_maxSampleTime = max time, @_maxSampleTime || time

    logEvent: (name, id) ->
      now = currentSecond()
      @addSampleTime now
      colors =
        generateDrawCache: "green"
        animation: "#77f"
        animationAborted: "#f77"
        animationDone: "#77f"
        default: "gray"

      clr = colors[name] || colors.default

      ebi = @eventsById[id] ||=
        startTime: now
        endTime: now
        index: @nextEventIdIndex++
        events: []
        name:name
        clr: clr

      ebi.startTime = min now, ebi.startTime
      ebi.endTime = max now, ebi.endTime

      @eventLegend[name] = clr

      ebi.events.push
        time: now
        name: name
        clr: clr

    @getter
      minSampleTime: -> @_minSampleTime
      maxSampleTime: -> @_maxSampleTime
      sampleTimeRange: -> @maxSampleTime - @minSampleTime

    drawAllSamplesForOneField: (bitmap, drawMatrix, sampleField) ->
      {size} = bitmap
      {w, h} = size
      {sampleTimeRange, stats} = @
      sampleWidth = floor (w / sampleTimeRange) / 60

      for stat in stats
        stat.drawSample bitmap, drawMatrix, sampleWidth, sampleField, h
      null

    getDrawMatrix: (size)->
        {w, h} = size
        legendWidth = 80
        w -= legendWidth
        {sampleTimeRange, minSampleTime, maxMs} = @
        sampleWidth = floor (w / sampleTimeRange) / 60
        xScale = (w - sampleWidth) / sampleTimeRange
        yScale = h / maxMs
        Matrix.scale(1, -1).translate(-minSampleTime, 0).scale(xScale, yScale).translate(legendWidth, h)

    drawLabeledHLine: (bitmap, x1, x2, y, clr, label) ->
      bitmap.drawRectangle null, rect(x1, y, x2-x1, 1), color:color clr
      bitmap.drawText point(x1, y-5), label, size:14, color:color clr

    drawEvents: (bitmap, drawMatrix) ->
      {w, h} = bitmap.size

      eventTimeLineHeight = floor h / 20
      for id, {index, startTime, endTime, clr, events, name} of @eventsById
        x1 = floor drawMatrix.transformX startTime, 0
        x2 = floor drawMatrix.transformX endTime, 0
        y = (index + 1) * eventTimeLineHeight
        @drawLabeledHLine bitmap, x1, x2, y, clr, name
        for {time, name, clr} in events
          x = floor drawMatrix.transformX time, 0
          bitmap.drawRectangle null, rect(x, y, 1, eventTimeLineHeight * (1/3)), color:clr

    log: ->
      return unless @stats.length > 0

      !DomConsole?.enabled && ce = GlobalEpochCycle.activeCanvasElements[0]

      bitmap = new Canvas.Bitmap size = if ce then ce.canvasBitmap.size else point 1000, 600
      {w, h} = size
      bitmap.clear "#fff"
      drawMatrix = @getDrawMatrix size

      y = floor drawMatrix.transformY 0, 1/60
      tenMsY = floor drawMatrix.transformY 0, 1/100
      fiveMsY = floor drawMatrix.transformY 0, 1/200
      bitmap.drawRectangle null, rect(0, y, w, 1), color:"#0007"

      for sampleField in GlobalEpochStat.statFields
        @drawAllSamplesForOneField bitmap, drawMatrix, sampleField

      legend = {}

      @drawLabeledHLine bitmap, 40, w, tenMsY, "#0007", "10ms"
      @drawLabeledHLine bitmap, 40, w, fiveMsY, "#0007", "5ms"
      @drawEvents bitmap, drawMatrix

      totalFrames = @stats.length
      averageFrameTimeMs = @sampleTimeRange / totalFrames
      perfectFrameCount = @sampleTimeRange * 60 + .5 | 0
      missedFrames = perfectFrameCount - totalFrames
      averageFrameTimeMsY = floor drawMatrix.transformY 0, averageFrameTimeMs
      if (averageFps = 1 / averageFrameTimeMs + .5 | 0) < 55<commit_msg>fixed GlobalEpochStates / perfGraphs - nowpropperly logs to domConsole when present<commit_after>define [
  'art-foundation'
  'art-atomic'
  'art-canvas'
  '../core'
], (Foundation, Atomic, Canvas, EngineCore) ->
  {log, Map, miniInspect, currentSecond, max, min, timeout, peek} = Foundation
  {point, rect, Matrix, color} = Atomic
  {GlobalEpochCycle} = EngineCore
  {globalEpochCycle} = GlobalEpochCycle
  {floor} = Math

  reactColor = color("gold")
  aimColor = color "#9c3"

  class GlobalEpochStat extends Foundation.BaseObject
    constructor: (@sampleTime, @total, @sampleSet) ->

    @statFields:  statFields  = ["total", "draw", "aim", "aimLayout", "aimTL", "aimRR", "react", "reactAim", "reactLC", "reactRender", "event", "flux"]
    @statColors: statColors =
      total:  "gray"
      draw:   "#39c"
      aim:          aimColor
      aimLayout:    aimColor.withLightness aimColor.lightness * .9
      aimTL:        aimColor.withLightness aimColor.lightness * .8
      aimRR:        aimColor.withLightness aimColor.lightness * .7
      react:        reactColor
      reactAim:     reactColor.withLightness reactColor.lightness *.9
      reactLC:      reactColor.withLightness reactColor.lightness *.8
      reactRender:  reactColor.withLightness reactColor.lightness *.7
      event:  "#ff6347"
      flux:   "#d936a3"

    getStacked: (sampleName) ->
      return @total if sampleName == "total"
      sum = 0
      for sn in statFields by -1
        sum += @sampleSet[sn] || 0
        break if sn == sampleName
      sum

    drawSample: (bitmap, drawMatrix, sampleWidth, sampleField, h) ->
      {sampleTime} = @
      sample = @getStacked sampleField
      x = floor drawMatrix.transformX sampleTime, sample
      y = floor drawMatrix.transformY sampleTime, sample
      bitmap.drawRectangle null, rect(x, y, sampleWidth, h - y), color: statColors[sampleField]

  class GlobalEpochStats extends Foundation.BaseObject
    @classGetter
      enabled: -> !!globalEpochCycle.globalEpochStats

    @enable: ->
      log "Enabled globalEpochStats"
      globalEpochCycle.globalEpochStats = new @
      true

    @toggle: ->
      if @enabled
        @disable()
      else
        @enable()

    @disable: ->
      log "Disabled globalEpochStats"
      globalEpochCycle.globalEpochStats = null
      false

    constructor: ->
      @reset()

    reset: ->
      @maxMs = 2/60
      @stats = []
      @nextEventIdIndex = 0
      @eventsById = {}
      @eventLegend = {}
      @_minSampleTime = null
      @_maxSampleTime = null

    add: (sampleTime, total, sampleSet) ->
      @stats.push ges = new GlobalEpochStat sampleTime, total, sampleSet

      @maxMs = max @maxMs, total * 1.5
      @logAndResetWhenIdle()
      @addSampleTime sampleTime

    addSampleTime: (time) ->
      @_minSampleTime = min time, @_minSampleTime || time
      @_maxSampleTime = max time, @_maxSampleTime || time

    logEvent: (name, id) ->
      now = currentSecond()
      @addSampleTime now
      colors =
        generateDrawCache: "green"
        animation: "#77f"
        animationAborted: "#f77"
        animationDone: "#77f"
        default: "gray"

      clr = colors[name] || colors.default

      ebi = @eventsById[id] ||=
        startTime: now
        endTime: now
        index: @nextEventIdIndex++
        events: []
        name:name
        clr: clr

      ebi.startTime = min now, ebi.startTime
      ebi.endTime = max now, ebi.endTime

      @eventLegend[name] = clr

      ebi.events.push
        time: now
        name: name
        clr: clr

    @getter
      minSampleTime: -> @_minSampleTime
      maxSampleTime: -> @_maxSampleTime
      sampleTimeRange: -> @maxSampleTime - @minSampleTime

    drawAllSamplesForOneField: (bitmap, drawMatrix, sampleField) ->
      {size} = bitmap
      {w, h} = size
      {sampleTimeRange, stats} = @
      sampleWidth = floor (w / sampleTimeRange) / 60

      for stat in stats
        stat.drawSample bitmap, drawMatrix, sampleWidth, sampleField, h
      null

    getDrawMatrix: (size)->
        {w, h} = size
        legendWidth = 80
        w -= legendWidth
        {sampleTimeRange, minSampleTime, maxMs} = @
        sampleWidth = floor (w / sampleTimeRange) / 60
        xScale = (w - sampleWidth) / sampleTimeRange
        yScale = h / maxMs
        Matrix.scale(1, -1).translate(-minSampleTime, 0).scale(xScale, yScale).translate(legendWidth, h)

    drawLabeledHLine: (bitmap, x1, x2, y, clr, label) ->
      bitmap.drawRectangle null, rect(x1, y, x2-x1, 1), color:color clr
      bitmap.drawText point(x1, y-5), label, size:14, color:color clr

    drawEvents: (bitmap, drawMatrix) ->
      {w, h} = bitmap.size

      eventTimeLineHeight = floor h / 20
      for id, {index, startTime, endTime, clr, events, name} of @eventsById
        x1 = floor drawMatrix.transformX startTime, 0
        x2 = floor drawMatrix.transformX endTime, 0
        y = (index + 1) * eventTimeLineHeight
        @drawLabeledHLine bitmap, x1, x2, y, clr, name
        for {time, name, clr} in events
          x = floor drawMatrix.transformX time, 0
          bitmap.drawRectangle null, rect(x, y, 1, eventTimeLineHeight * (1/3)), color:clr

    log: ->
      return unless @stats.length > 0

      !Neptune.Art.DevTools.DomConsole?.enabled && ce = GlobalEpochCycle.activeCanvasElements[0]

      bitmap = new Canvas.Bitmap size = if ce then ce.canvasBitmap.size else point 1000, 600
      {w, h} = size
      bitmap.clear "#fff"
      drawMatrix = @getDrawMatrix size

      y = floor drawMatrix.transformY 0, 1/60
      tenMsY = floor drawMatrix.transformY 0, 1/100
      fiveMsY = floor drawMatrix.transformY 0, 1/200
      bitmap.drawRectangle null, rect(0, y, w, 1), color:"#0007"

      for sampleField in GlobalEpochStat.statFields
        @drawAllSamplesForOneField bitmap, drawMatrix, sampleField

      legend = {}

      @drawLabeledHLine bitmap, 40, w, tenMsY, "#0007", "10ms"
      @drawLabeledHLine bitmap, 40, w, fiveMsY, "#0007", "5ms"
      @drawEvents bitmap, drawMatrix

      totalFrames = @stats.length
      averageFrameTimeMs = @sampleTimeRange / totalFrames
      perfectFrameCount = @sampleTimeRange * 60 + .5 | 0
      missedFrames = perfectFrameCount - totalFrames
      averageFrameTimeMsY = floor drawMatrix.transformY 0, averageFrameTimeMs
      if (averageFps = 1 / averageFrameTimeMs + .5 | 0) < 55
{'fraction_non_alphanumeric': 0.10839766818969591, 'fraction_numerical': 0.018749015282810776, 'mean_word_length': 2.571026722925457, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 47, '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': '7144482', 'n_tokens_mistral': 4166, 'n_tokens_neox': 3962, 'n_words': 1039}