/*
 * @copyright Copyright (c) Sematext Group, Inc. - All Rights Reserved
 *
 * @licence SPM for NodeJS is free-to-use, proprietary software.
 * THIS IS PROPRIETARY SOURCE CODE OF Sematext Group, Inc. (Sematext)
 * This source code may not be copied, reverse engineered, or altered for any purpose.
 * This source code is to be used exclusively by users and customers of Sematext.
 * Please see the full license (found in LICENSE in this distribution) for details on its license and the licenses of its dependencies.
 */
'use strict'
const { Agent, Config } = require('spm-agent')
const pidUsageTree = require('pidusage-tree')
const addMetrics = function (agent, pidToCheck) {
  const timestamp = new Date()
  pidUsageTree(pidToCheck, function (err, results) {
    if (err) {
      return console.error(err)
    }

    const processes = Object.keys(results)
      .map(key => results[key])
      .filter(proc => proc)

    // console.log(processes)

    const masterProcess = processes.filter(proc => proc.pid === pidToCheck).pop()
    const masterProcessMetric = {
      timestamp: timestamp,
      measurement: 'nodejs.process',
      tags: {
        'nodejs.process.name': 'master',
        'nodejs.process.type': 'master'
      },
      fields: {
        'cpu.percent': masterProcess.cpu,
        memory: masterProcess.memory,
        uptime: masterProcess.elapsed
      }
    }
    console.log(masterProcessMetric)
    agent.addMetrics(masterProcessMetric)

    const childProcesses = processes.filter(proc => proc.pid !== pidToCheck)
    if (childProcesses && childProcesses.length) {
      childProcesses.forEach((proc, counter) => {
        const childProcessMetric = {
          timestamp: timestamp,
          measurement: 'nodejs.process',
          tags: {
            'nodejs.process.name': `child-${counter + 1}`,
            'nodejs.process.type': 'child'
          },
          fields: {
            'cpu.percent': proc.cpu,
            memory: proc.memory,
            uptime: proc.elapsed
          }
        }
        console.log(childProcessMetric)
        agent.addMetrics(childProcessMetric)
      })
    }

    const processCountMetric = {
      timestamp: timestamp,
      measurement: 'nodejs.process',
      tags: {
        'nodejs.process.type': 'master'
      },
      fields: {
        count: 1
      }
    }
    agent.addMetrics(processCountMetric)
    const processChildCountMetric = {
      timestamp: timestamp,
      measurement: 'nodejs.process',
      tags: {
        'nodejs.process.type': 'child'
      },
      fields: {
        count: Math.max(processes.length - 1, 0) || 0
      }
    }
    agent.addMetrics(processChildCountMetric)
  })
}

function getDefaultProcessMetrics (agent) {
  if (process.send !== undefined) {
    return // child process, not collecting stats
  }

  addMetrics(agent, process.pid)
}
function getPm2Metrics (agent) {
  if (process.send === undefined) {
    return // master pm2 process, not collecting stats
  }

  addMetrics(agent, process.ppid)
}

function getProcessStats () {
  return {
    start: function (agent) {
      const timerId = setInterval(function () {
        if (
          Boolean(process.env.PM2) === true
        ) {
          getPm2Metrics(agent)
          return
        }
        getDefaultProcessMetrics(agent)
      }, Config.collectionInterval)
      if (timerId.unref) {
        timerId.unref()
      }
    },
    stop: function () { }
  }
}

module.exports = function () {
  return new Agent(getProcessStats())
}

@@ -916,39 +916,8 @@
 c)%0A%0A
-    // console.log(processes)%0A%0A
     
@@ -1349,45 +1349,8 @@
   %7D%0A
-    console.log(masterProcessMetric)%0A
     
@@ -1958,48 +1958,8 @@
   %7D%0A
-        console.log(childProcessMetric)%0A
     

remove console