<commit_before># Description:
#   Your dog friend makes sure everything's in order
#
# Commands:
#
# Notes:
#
# Author:
#   aaronsky

moment = require 'moment'

constants = require '../helpers/constants'
HEADERS = constants.HEADERS
# TODO: FIX THIS FOR WIDE RELEASE
ADMINS = ['aaronsky', 'reid', 'ryan']

Organization = require('../models/organization').get()

module.exports = (robot) ->

  Logger = require('../helpers/logger')(robot)

  isAdminUser = (user) ->
    return user? and user in ADMINS

  # Org statistics
  robot.router.post '/ibizan/diagnostics/info', (req, res) ->
    body = req.body
    if body.token is process.env.SLASH_INFO_TOKEN
      res.status 200
      response = {
        "text": "#{Organization.name}'s Ibizan has been up since
                  #{Organization.initTime.toDate()}
                  (#{+moment()
                    .diff(Organization.initTime, 'minutes', true)
                    .toFixed(2)}
                  minutes)",
        "response_type": "in_channel"
      }
    else
      res.status 401
      response =  {
        "text": "Bad token in Ibizan configuration"
      }
    res.json response

  robot.router.post '/ibizan/diagnostics/users', (req, res) ->
    body = req.body
    if body.token is process.env.SLASH_USERS_TOKEN and
       isAdminUser body.user_name
      res.status 200
      response = ''
      for user in Organization.users
        response += user.description() + '\n\n'
    else
      res.status 401
      response = "Bad token in Ibizan configuration"
    res.json {
      "text": response
    }
    
  robot.router.post '/ibizan/diagnostics/projects', (req, res) ->
    body = req.body
    if body.token is process.env.SLASH_PROJECTS_TOKEN and
       isAdminUser body.user_name
      res.status 200
      response = ''
      for project in Organization.projects
        response += project.description() + '\n\n'
    else
      res.status 401
      response = "Bad token in Ibizan configuration"
    res.json {
      "text": response
    }
    
  robot.router.post '/ibizan/diagnostics/calendar', (req, res) ->
    body = req.body
    if body.token is process.env.SLASH_CALENDAR_TOKEN and
       isAdminUser body.user_name
      res.status 200
      response = Organization.calendar.description()
    else
      res.status 401
      response = "Bad token in Ibizan configuration"
    res.json {
      "text": response
    }
    
  robot.router.post '/ibizan/diagnostics/resethounding', (req, res) ->
    body = req.body
    if body.token is process.env.SLASH_RESETHOUNDING_TOKEN and
       isAdminUser body.user_name
      res.status 200
      count = Organization.resetHounding()
      response = "Reset #{count}
                  #{if count is 1 then "person's" else "peoples'"}
                  hound status"
    else
      res.status 401
      response = "Bad token in Ibizan configuration"
    res.json {
      "text": response
    }

  robot.router.post '/ibizan/diagnostics/payroll', (req, res) ->
    body = req.body
    if body.token is process.env.SLASH_PAYROLL_TOKEN and
       isAdminUser body.user_name
      response_url = body.response_url
      if response_url
        comps = body.text || []
        now = moment()
        start = if comps[0] then moment(comps[0]) else now.subtract(2, 'weeks')
        end = if comps[1] then moment(comps[1]) else now
        Organization.generateReport(start, end)
        .catch(
          (err) ->
            Logger.errorToSlack "Failed to produce a salary report", err
            Logger.log "POSTing to #{response_url}"
            payload =
              text: 'Failed to produce a salary report'
            robot.http(response_url)
            .header('Content-Type', 'application/json')
            .post(JSON.stringify(payload))
        )
        .done(
          (numberDone) ->
            Logger.log "Payroll has been generated"<commit_msg>Fixed bug where now-marker was being modified while creating range for payroll report<commit_after># Description:
#   Your dog friend makes sure everything's in order
#
# Commands:
#
# Notes:
#
# Author:
#   aaronsky

moment = require 'moment'

constants = require '../helpers/constants'
HEADERS = constants.HEADERS
# TODO: FIX THIS FOR WIDE RELEASE
ADMINS = ['aaronsky', 'reid', 'ryan']

Organization = require('../models/organization').get()

module.exports = (robot) ->

  Logger = require('../helpers/logger')(robot)

  isAdminUser = (user) ->
    return user? and user in ADMINS

  # Org statistics
  robot.router.post '/ibizan/diagnostics/info', (req, res) ->
    body = req.body
    if body.token is process.env.SLASH_INFO_TOKEN
      res.status 200
      response = {
        "text": "#{Organization.name}'s Ibizan has been up since
                  #{Organization.initTime.toDate()}
                  (#{+moment()
                    .diff(Organization.initTime, 'minutes', true)
                    .toFixed(2)}
                  minutes)",
        "response_type": "in_channel"
      }
    else
      res.status 401
      response =  {
        "text": "Bad token in Ibizan configuration"
      }
    res.json response

  robot.router.post '/ibizan/diagnostics/users', (req, res) ->
    body = req.body
    if body.token is process.env.SLASH_USERS_TOKEN and
       isAdminUser body.user_name
      res.status 200
      response = ''
      for user in Organization.users
        response += user.description() + '\n\n'
    else
      res.status 401
      response = "Bad token in Ibizan configuration"
    res.json {
      "text": response
    }
    
  robot.router.post '/ibizan/diagnostics/projects', (req, res) ->
    body = req.body
    if body.token is process.env.SLASH_PROJECTS_TOKEN and
       isAdminUser body.user_name
      res.status 200
      response = ''
      for project in Organization.projects
        response += project.description() + '\n\n'
    else
      res.status 401
      response = "Bad token in Ibizan configuration"
    res.json {
      "text": response
    }
    
  robot.router.post '/ibizan/diagnostics/calendar', (req, res) ->
    body = req.body
    if body.token is process.env.SLASH_CALENDAR_TOKEN and
       isAdminUser body.user_name
      res.status 200
      response = Organization.calendar.description()
    else
      res.status 401
      response = "Bad token in Ibizan configuration"
    res.json {
      "text": response
    }
    
  robot.router.post '/ibizan/diagnostics/resethounding', (req, res) ->
    body = req.body
    if body.token is process.env.SLASH_RESETHOUNDING_TOKEN and
       isAdminUser body.user_name
      res.status 200
      count = Organization.resetHounding()
      response = "Reset #{count}
                  #{if count is 1 then "person's" else "peoples'"}
                  hound status"
    else
      res.status 401
      response = "Bad token in Ibizan configuration"
    res.json {
      "text": response
    }

  robot.router.post '/ibizan/diagnostics/payroll', (req, res) ->
    body = req.body
    if body.token is process.env.SLASH_PAYROLL_TOKEN and
       isAdminUser body.user_name
      response_url = body.response_url
      if response_url
        comps = body.text || []
        now = moment()
        end = if comps[1] then moment(comps[1]) else now
        start = if comps[0] then moment(comps[0]) else now.subtract(2, 'weeks')
        Organization.generateReport(start, end)
        .catch(
          (err) ->
            Logger.errorToSlack "Failed to produce a salary report", err
            Logger.log "POSTing to #{response_url}"
            payload =
              text: 'Failed to produce a salary report'
            robot.http(response_url)
            .header('Content-Type', 'application/json')
            .post(JSON.stringify(payload))
        )
        .done(
          (numberDone) ->
            Logger.log "Payroll has been generated"
{'fraction_non_alphanumeric': 0.10908394998724165, 'fraction_numerical': 0.009441183975503956, 'mean_word_length': 2.1659935379644586, 'pattern_counts': {'":': 14, '<': 3, '<?xml version=': 0, '>': 23, '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': '7533452', 'n_tokens_mistral': 2436, 'n_tokens_neox': 2358, 'n_words': 613}