<commit_before># Statistics
# ======
#
# **Statistics** makes use of [process -> hrtime](http://nodejs.org/docs/v0.8.0/api/all.html#all_process_hrtime)
# to record processing times.
# Keeps also track of which methods are currently executed
#
# Copyright &copy; <NAME>, GPL v3.0 Licensed.

# Module dependencies
nconf       = require 'nconf'
fs          = require 'fs'
logger      = require '../logging/logger'


statistics = exports = module.exports = class Statistics

  @currentExecutions = []
  @currentStatistics = {}
  @startTime = 0

  constructor ->
    startTime = 0

  @isRunning :(reqPath) ->
    executionInfo = @currentExecutions.filter (x) -> x.path == reqPath
    return executionInfo.length > 0

  @startRecording: (reqPath) ->
    logger.log 'info', 'reqPath: ' + reqPath
    @startTime = process.hrtime()
    rand = Math.random()
    @currentExecutions.push({
      rand : rand
      startTime: @startTime
      path: reqPath
    })
    return rand

  @endRecording: (rand, reqPath) ->
    executionInfo = @currentExecutions.filter (x) -> x.rand == rand
    @endTime = process.hrtime(executionInfo[0].startTime)
    delete @currentExecutions[rand]
    if(@currentStatistics[reqPath]?)
      @currentStatistics[reqPath] =
       runtime: (@currentStatistics[reqPath].runtime + @endTime[0]) / 2
       executions: @currentStatistics[reqPath].executions+1
    else
      @currentStatistics[reqPath] =
        runtime: @endTime[0]
        executions: 1
    return @endTime[0]

  @getMeanExecutionTime: (reqPath) ->
    if(@currentStatistics[reqPath]?)
      return @currentStatistics[reqPath].runtime
    else
      return 'No runtime information available'
  @loadStatistics: () ->
    if(Object.keys(@currentStatistics).length is 0)
      try
        @currentStatistics = JSON.parse(fs.readFileSync(nconf.get('paths:statisticsFile'),'utf-8'))
      catch error
        #Should only happen at first startup
        logger.log 'error', 'No statistics file found'
  @saveStatistics: (callback) ->
    fs.writeFile nconf.get('paths:statisticsFile'), JSON.stringify(@currentStatistics), (err) ->
      if(err?)
        callback err
      else
        callback null
      return<commit_msg>fixed a bug in statistics<commit_after># Statistics
# ======
#
# **Statistics** makes use of [process -> hrtime](http://nodejs.org/docs/v0.8.0/api/all.html#all_process_hrtime)
# to record processing times.
# Keeps also track of which methods are currently executed
#
# Copyright &copy; <NAME>, GPL v3.0 Licensed.

# Module dependencies
nconf       = require 'nconf'
fs          = require 'fs'
logger      = require '../logging/logger'


statistics = exports = module.exports = class Statistics

  @currentExecutions = []
  @currentStatistics = {}
  @startTime = 0

  constructor ->
    startTime = 0

  @isRunning :(reqPath) ->
    executionInfo = @currentExecutions.filter (x) -> x.path == reqPath
    return executionInfo.length > 0

  @startRecording: (reqPath) ->
    logger.log 'info', 'reqPath: ' + reqPath
    @startTime = process.hrtime()
    rand = Math.random()
    @currentExecutions.push({
      rand : rand
      startTime: @startTime
      path: reqPath
    })
    return rand

  @endRecording: (rand, reqPath) ->
    executionInfo = @currentExecutions.filter (x) -> x.rand == rand
    @endTime = process.hrtime(executionInfo[0].startTime)
    delete @currentExecutions[rand]
    if(@currentStatistics[reqPath]?)
      @currentStatistics[reqPath] =
       runtime: (@currentStatistics[reqPath].runtime + @endTime[0]) / 2
       executions: @currentStatistics[reqPath].executions+1
    else
      @currentStatistics[reqPath] =
        runtime: @endTime[0]
        executions: 1
    #remove the call from current executions
    @currentExecutions = @currentExecutions.filter (x) -> x.rand == rand
    return @endTime[0]

  @getMeanExecutionTime: (reqPath) ->
    if(@currentStatistics[reqPath]?)
      return @currentStatistics[reqPath].runtime
    else
      return 'No runtime information available'
  @loadStatistics: () ->
    if(Object.keys(@currentStatistics).length is 0)
      try
        @currentStatistics = JSON.parse(fs.readFileSync(nconf.get('paths:statisticsFile'),'utf-8'))
      catch error
        #Should only happen at first startup
        logger.log 'error', 'No statistics file found'
  @saveStatistics: (callback) ->
    fs.writeFile nconf.get('paths:statisticsFile'), JSON.stringify(@currentStatistics), (err) ->
      if(err?)
        callback err
      else
        callback null
      return
{'fraction_non_alphanumeric': 0.12582781456953643, 'fraction_numerical': 0.0075055187637969095, 'mean_word_length': 3.218808193668529, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 30, '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': '6937407', 'n_tokens_mistral': 1363, 'n_tokens_neox': 1334, 'n_words': 318}