<commit_before>os = require 'os'
fs = require 'fs'
path = require 'path'
util = require 'util'

Jolokia = require 'jolokia-client'
async = require 'async'
mkdirp = require 'mkdirp'
Gmetric = require 'gmetric'

config = require 'nconf'
logger = require './logger'

###*
 * Jolokia server client wrapper.
###
class JolokiaSrv
  constructor: (@interval=20, @gmond_enabled=true) ->
    @jclients  = new Object()
    @templates = new Object()

    @gmond_interval_id = null
    @gmetric = new Gmetric()

    @setup_template_dir()

  ###*
   * Sets up the template directory and loads the current templates
  ###
  setup_template_dir: =>
    mkdirp config.get('template_dir'), (err) =>
      if (err)
        logger.error "Error creating template directory: #{err}"
        process.exit(1)
      else
        @watch_templates()

  ###*
   * Watch template directory and update @templates on changes.
   * @note watching with inotify is current only supported on linux,
   * other operating systems will drop to an initial-load only.
  ###
  watch_templates: =>
    @load_all_templates (err) =>
      if os.platform() == 'linux'
        fs.exists config.get('template_dir'), (exists) =>
          if exists
            fs.watch config.get('template_dir')
            , (event, filename) =>
              fs.exists path.resolve(config.get('template_dir'), filename)
              , (exists) =>
                if exists
                  @load_template(filename)
                else
                  @unload_template(filename)

  ###*
   * Loads all of the current templates in the template directory.
   * @param {Function} (fn) The callback function
  ###
  load_all_templates: (fn) =>
    @stop_gmond()
    fs.readdir path.resolve(config.get('template_dir')), (err, files) =>
      if files == undefined
        json_files = []
      else
        json_files = files.filter (x) -> x.match /\.json/
      async.each json_files, @load_template, (err) =>
        @start_gmond()
        unless fn == undefined then fn(err)

  ###*
   * Removes the given template from the available templates.
   * @param {String}   (template) The template to remove
  ###
  load_template: (template, fn) =>
    fs.readFile path.resolve(config.get('template_dir'), template)
      , 'utf8', (err, data) =>
        if (err)
          logger.error "Error reading file: #{template}: #{err}"
        else
          try
            json_data = JSON.parse(data)
            @templates[json_data.name] =
              inherits: json_data.inherits
              mappings: json_data.mappings
          catch error
            logger.error "Error parsing `#{template}`: #{error}"
        unless fn == undefined then fn(err)

  ###*
   * Removes the given template from the available templates.
   * @param {String} (template) The template to remove
  ###
  unload_template: (template) =>
    delete @templates[template]<commit_msg>Added fix for template_dir<commit_after>os = require 'os'
fs = require 'fs'
path = require 'path'
util = require 'util'

Jolokia = require 'jolokia-client'
async = require 'async'
mkdirp = require 'mkdirp'
Gmetric = require 'gmetric'

config = require 'nconf'
logger = require './logger'

###*
 * Jolokia server client wrapper.
###
class JolokiaSrv
  constructor: (@interval=20, @gmond_enabled=true) ->
    @jclients  = new Object()
    @templates = new Object()

    @gmond_interval_id = null
    @gmetric = new Gmetric()

    @setup_template_dir()

  ###*
   * Sets up the template directory and loads the current templates
  ###
  setup_template_dir: =>
    mkdirp config.get('template_dir'), (err) =>
      if (err)
        logger.error "Error creating template directory: #{err}"
        process.exit(1)
      else
        @watch_templates()

  ###*
   * Watch template directory and update @templates on changes.
   * @note watching with inotify is current only supported on linux,
   * other operating systems will drop to an initial-load only.
  ###
  watch_templates: =>
    @load_all_templates (err) =>
      if os.platform() == 'linux'
        fs.exists config.get('template_dir'), (exists) =>
          if exists
            fs.watch config.get('template_dir')
            , (event, filename) =>
              fs.exists path.resolve(config.get('template_dir'), filename)
              , (exists) =>
                if exists
                  @load_template(filename)
                else
                  @unload_template(filename)

  ###*
   * Loads all of the current templates in the template directory.
   * @param {Function} (fn) The callback function
  ###
  load_all_templates: (fn) =>
    @stop_gmond()
    fs.exists config.get('template_dir'), (exists) =>
      unless exists then return fn("Template dir does not exist.")
      fs.readdir path.resolve(config.get('template_dir')), (err, files) =>
        if files == undefined
          json_files = []
        else
          json_files = files.filter (x) -> x.match /\.json/
        async.each json_files, @load_template, (err) =>
          @start_gmond()
          unless fn == undefined then fn(err)

  ###*
   * Removes the given template from the available templates.
   * @param {String}   (template) The template to remove
  ###
  load_template: (template, fn) =>
    fs.exists config.get('template_dir'), (exists) =>
      unless exists then return fn("Template dir does not exist.")
      fs.readFile path.resolve(config.get('template_dir'), template)
        , 'utf8', (err, data) =>
          if (err)
            logger.error "Error reading file: #{template}: #{err}"
          else
            try
              json_data = JSON.parse(data)
              @templates[json_data.name] =
                inherits: json_data.inherits
                mappings: json_data.mappings
            catch error
              logger.error "Error parsing `#{template}`: #{error}"
          unless fn == undefined then fn(err)

  ###*
   * Removes the given template from the available templates.
   * @param {String} (template) The template to remove
  ###
  unload_template: (template) =>
    delete @templates[template]
{'fraction_non_alphanumeric': 0.12251601248152406, 'fraction_numerical': 0.0013138446378715717, 'mean_word_length': 2.5, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 35, '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': '731374', 'n_tokens_mistral': 1779, 'n_tokens_neox': 1734, 'n_words': 510}