<commit_before>ElasticSearch = require "./elasticsearch"
_             = require "underscore"
checksum      = require "checksum"

{
  secure
  signature
} = require 'bongo'

uniqInt = 4

module.exports = class JErrorLog extends ElasticSearch
  @share()

  @set
    sharedMethods :
      static:
        create: (signature Object, Function)

  @errorsIndex:->
    @getIndexOptions("errorlogs", "errors")

  @create: secure (client, params, callback)->
    @getUserInfo client, (err, record)=>
      return callback err  if err

      _.extend record, params

      rawCurr = new Date

      year    = rawCurr.getFullYear()
      month   = rawCurr.getMonth() + 1
      date    = rawCurr.getDate()
      hour    = rawCurr.getHours()
      min     = Math.round(rawCurr.getMinutes() / uniqInt) * uniqInt # round to nearest time

      timeStr = "#{year}#{month}#{date}#{hour}#{min}"

      {error, reason} = params
      {username} = record

      # to reduce noise in logs, we don't log if there was the same error from
      # same user within `uniqInt`; es enforces unique constraint on `_id`
      idStr = "#{error}#{reason}#{username}#{timeStr}"
      _id   = checksum idStr

      # _.extends modifies source object, so we clone to keep them seperate<commit_msg>log: rename 'err' to 'error' to avoid possibe global collusion<commit_after>ElasticSearch = require "./elasticsearch"
_             = require "underscore"
checksum      = require "checksum"

{
  secure
  signature
} = require 'bongo'

uniqInt = 4

module.exports = class JErrorLog extends ElasticSearch
  @share()

  @set
    sharedMethods :
      static:
        create: (signature Object, Function)

  @errorsIndex:->
    @getIndexOptions("errorlogs", "errors")

  @create: secure (client, params, callback)->
    @getUserInfo client, (err, record)=>
      return callback err  if err

      _.extend record, params

      rawCurr = new Date

      year    = rawCurr.getFullYear()
      month   = rawCurr.getMonth() + 1
      date    = rawCurr.getDate()
      hour    = rawCurr.getHours()
      min     = Math.round(rawCurr.getMinutes() / uniqInt) * uniqInt # round to nearest time

      timeStr = "#{year}#{month}#{date}#{hour}#{min}"

      {err, reason} = params
      {username} = record

      # to reduce noise in logs, we don't log if there was the same error from
      # same user within `uniqInt`; es enforces unique constraint on `_id`
      idStr = "#{err}#{reason}#{username}#{timeStr}"
      _id   = checksum idStr

      # _.extends modifies source object, so we clone to keep them seperate
{'fraction_non_alphanumeric': 0.11240762349280435, 'fraction_numerical': 0.0015558148580318942, 'mean_word_length': 2.749271137026239, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '3624848', 'n_tokens_mistral': 812, 'n_tokens_neox': 794, 'n_words': 238}