<commit_before>TurkServer.Util ?= {}

TurkServer.Util.duration = (millis) ->
  diff = moment.utc(millis)
  time = diff.format("H:mm:ss")
  days = +diff.format("DDD") - 1
  return (if days isnt 0 then days + "d " else "") + time

TurkServer.Util.timeSince = (timestamp) ->
  TurkServer.Util.duration(TimeSync.serverTime() - timestamp)
TurkServer.Util.timeUntil = (timestamp) ->
  TurkServer.Util.duration(timestamp - TimeSync.serverTime())

UI.registerHelper "_tsViewingBatch", -> Batches.findOne(Session.get("_tsViewingBatchId"))

UI.registerHelper "_tsLookupTreatment", -> Treatments.findOne(name: ""+@)

UI.registerHelper "_tsRenderTime", (timestamp) -> new Date(timestamp).toLocaleString()
UI.registerHelper "_tsRenderTimeMillis", (timestamp) ->
  m = moment(timestamp)
  m.format("L h:mm:ss.SSS A")

UI.registerHelper "_tsRenderTimeSince", TurkServer.Util.timeSince
UI.registerHelper "_tsRenderTimeUntil", TurkServer.Util.timeUntil

UI.registerHelper "_tsRenderISOTime", (isoString) ->
  m = moment(isoString)
  return m.format("L LT") + " (" + m.fromNow() + ")"

# https://github.com/kvz/phpjs/blob/master/functions/strings/nl2br.js
nl2br = (str) -> (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1<br>$2')

UI.registerHelper "_tsnl2br", nl2br

Template.tsBatchSelector.events =
  "change select": (e) ->
    unless Session.equals("_tsViewingBatchId", e.target.value)
      Session.set("_tsViewingBatchId", e.target.value)

Template.tsBatchSelector.helpers
  batches: -> Batches.find()
  noBatchSelection: -> not Session.get("_tsViewingBatchId")
  selected: -> Session.equals("_tsViewingBatchId", @_id)
  viewingBatchId: -> Session.get("_tsViewingBatchId")

Template.tsAdminInstance.rendered = ->
  # Subscribe to instance with whatever we rendered with
  this.autorun ->
    Meteor.subscribe "tsAdminInstance", Blaze.getData()

Template.tsAdminInstance.helpers
  instance: -> Experiments.findOne(@+"")

Template.tsAdminPayBonus.events<commit_msg>sort batch dropdown by name<commit_after>TurkServer.Util ?= {}

TurkServer.Util.duration = (millis) ->
  diff = moment.utc(millis)
  time = diff.format("H:mm:ss")
  days = +diff.format("DDD") - 1
  return (if days isnt 0 then days + "d " else "") + time

TurkServer.Util.timeSince = (timestamp) ->
  TurkServer.Util.duration(TimeSync.serverTime() - timestamp)
TurkServer.Util.timeUntil = (timestamp) ->
  TurkServer.Util.duration(timestamp - TimeSync.serverTime())

UI.registerHelper "_tsViewingBatch", -> Batches.findOne(Session.get("_tsViewingBatchId"))

UI.registerHelper "_tsLookupTreatment", -> Treatments.findOne(name: ""+@)

UI.registerHelper "_tsRenderTime", (timestamp) -> new Date(timestamp).toLocaleString()
UI.registerHelper "_tsRenderTimeMillis", (timestamp) ->
  m = moment(timestamp)
  m.format("L h:mm:ss.SSS A")

UI.registerHelper "_tsRenderTimeSince", TurkServer.Util.timeSince
UI.registerHelper "_tsRenderTimeUntil", TurkServer.Util.timeUntil

UI.registerHelper "_tsRenderISOTime", (isoString) ->
  m = moment(isoString)
  return m.format("L LT") + " (" + m.fromNow() + ")"

# https://github.com/kvz/phpjs/blob/master/functions/strings/nl2br.js
nl2br = (str) -> (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1<br>$2')

UI.registerHelper "_tsnl2br", nl2br

Template.tsBatchSelector.events =
  "change select": (e) ->
    unless Session.equals("_tsViewingBatchId", e.target.value)
      Session.set("_tsViewingBatchId", e.target.value)

Template.tsBatchSelector.helpers
  batches: -> Batches.find({}, {sort: {name: 1}})
  noBatchSelection: -> not Session.get("_tsViewingBatchId")
  selected: -> Session.equals("_tsViewingBatchId", @_id)
  viewingBatchId: -> Session.get("_tsViewingBatchId")

Template.tsAdminInstance.rendered = ->
  # Subscribe to instance with whatever we rendered with
  this.autorun ->
    Meteor.subscribe "tsAdminInstance", Blaze.getData()

Template.tsAdminInstance.helpers
  instance: -> Experiments.findOne(@+"")

Template.tsAdminPayBonus.events
{'fraction_non_alphanumeric': 0.16519099418163422, 'fraction_numerical': 0.0043005312420946115, 'mean_word_length': 5.101851851851852, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 41, 'https://': 2, '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': '2529177', 'n_tokens_mistral': 1354, 'n_tokens_neox': 1341, 'n_words': 222}