# Path: stackimpact/runtime.py
# class runtime_info(object):
#     OS_LINUX = (sys.platform.startswith('linux'))
#     OS_DARWIN = (sys.platform == 'darwin')
#     OS_WIN = (sys.platform == 'win32')
#     PYTHON_2 = (sys.version_info.major == 2)
#     PYTHON_3 = (sys.version_info.major == 3)
#     GEVENT = False
# 
# def min_version(major, minor=0):
#     return (sys.version_info.major == major and sys.version_info.minor >= minor)
# 
# Path: stackimpact/metric.py
# class Metric(object):
# 
#     TYPE_STATE = 'state'
#     TYPE_COUNTER = 'counter'
#     TYPE_PROFILE = 'profile'
#     TYPE_TRACE = 'trace'
# 
#     CATEGORY_CPU = 'cpu'
#     CATEGORY_MEMORY = 'memory'
#     CATEGORY_GC = 'gc'
#     CATEGORY_RUNTIME = 'runtime'
#     CATEGORY_SPAN = 'span'
#     CATEGORY_CPU_PROFILE = 'cpu-profile'
#     CATEGORY_MEMORY_PROFILE = 'memory-profile'
#     CATEGORY_BLOCK_PROFILE = 'block-profile'
#     CATEGORY_ERROR_PROFILE = 'error-profile'
# 
#     NAME_CPU_TIME = 'CPU time'
#     NAME_CPU_USAGE = 'CPU usage'
#     NAME_MAIN_THREAD_CPU_USAGE = 'Main thread CPU usage'
#     NAME_MAX_RSS = 'Max RSS'
#     NAME_CURRENT_RSS = 'Current RSS'
#     NAME_VM_SIZE = 'VM Size'
#     NAME_GC_COUNT = 'Uncollected objects'
#     NAME_GC_COLLECTIONS = 'Collections'
#     NAME_GC_COLLECTED = 'Collected objects'
#     NAME_GC_UNCOLLECTABLE = 'Uncollectable objects'
#     NAME_THREAD_COUNT = 'Active threads'
#     NAME_UNCOLLECTED_ALLOCATIONS = 'Uncollected allocations'
#     NAME_BLOCKING_CALL_TIMES = 'Blocking call times'
#     NAME_HANDLED_EXCEPTIONS = 'Handled exceptions'
#     NAME_TF_OPERATION_TIMES = 'TensorFlow operation times'
#     NAME_TF_OPERATION_ALLOCATION_RATE = 'TensorFlow operation allocation rate'
#     
#     UNIT_NONE = ''
#     UNIT_MILLISECOND = 'millisecond'
#     UNIT_MICROSECOND = 'microsecond'
#     UNIT_NANOSECOND = 'nanosecond'
#     UNIT_BYTE = 'byte'
#     UNIT_KILOBYTE = 'kilobyte'
#     UNIT_PERCENT = 'percent'
# 
#     TRIGGER_TIMER = 'timer'
#     TRIGGER_API = 'api'
# 
# 
#     def __init__(self, agent, typ, category, name, unit):
#         metric_id = generate_sha1("{0}{1}{2}{3}{4}{5}{6}".format(
#             agent.get_option('app_name'), 
#             agent.get_option('app_environment'), 
#             agent.get_option('host_name'),
#             typ, category, name, unit))
# 
#         self.agent = agent
#         self.id = metric_id
#         self.typ = typ
#         self.category = category
#         self.name = name
#         self.unit = unit
#         self.measurement = None
#         self.has_last_value = False
#         self.last_value = None
# 
# 
#     def has_measurement(self):
#         return self.measurement != None
# 
# 
#     def create_measurement(self, trigger, value, duration = None, breakdown = None):
#         ready = True
# 
#         if self.typ == Metric.TYPE_COUNTER:
#             if not self.has_last_value:
#                 ready = False
#                 self.has_last_value = True
#                 self.last_value = value
#             else:
#                 tmp_value = value
#                 value = value - self.last_value
#                 self.last_value = tmp_value
# 
#         if ready:
#             self.measurement = Measurement(
#                 generate_uuid(),
#                 trigger,
#                 value,
#                 duration,
#                 breakdown,
#                 timestamp())
# 
# 
#     def to_dict(self):
#         measurement_map = None
#         if self.measurement:
#             measurement_map = self.measurement.to_dict()
# 
#         metric_map = {
#             'id': self.id,
#             'type': self.typ,
#             'category': self.category,
#             'name': self.name,
#             'unit': self.unit,
#             'measurement': measurement_map,
#         }
# 
#         return metric_map


import time
import unittest
import sys
import stackimpact
from stackimpact.runtime import runtime_info, min_version
from stackimpact.metric import Metric




class ProcessReporterTestCase(unittest.TestCase):

    def test_report(self):
        stackimpact._agent = None
        agent = stackimpact.start(
            dashboard_address = 'http://localhost:5001',
            agent_key = 'key1',
            app_name = 'TestPythonApp',
            debug = True
        )
        agent.process_reporter.start()

        agent.process_reporter.report()
        time.sleep(0.1)
        agent.process_reporter.report()

        metrics = agent.process_reporter.metrics

        if not runtime_info.OS_WIN:
            self.is_valid(metrics, Metric.TYPE_COUNTER, Metric.CATEGORY_CPU, Metric.NAME_CPU_TIME, 0, float("inf"))
            self.is_valid(metrics, Metric.TYPE_STATE, Metric.CATEGORY_CPU, Metric.NAME_CPU_USAGE, 0, float("inf"))

        if not runtime_info.OS_WIN:
            self.is_valid(metrics, Metric.TYPE_STATE, Metric.CATEGORY_MEMORY, Metric.NAME_MAX_RSS, 0, float("inf"))


# Path: stackimpact/runtime.py
# class runtime_info(object):
#     OS_LINUX = (sys.platform.startswith('linux'))
#     OS_DARWIN = (sys.platform == 'darwin')
#     OS_WIN = (sys.platform == 'win32')
#     PYTHON_2 = (sys.version_info.major == 2)
#     PYTHON_3 = (sys.version_info.major == 3)
#     GEVENT = False
# 
# def min_version(major, minor=0):
#     return (sys.version_info.major == major and sys.version_info.minor >= minor)
# 
# Path: stackimpact/metric.py
# class Metric(object):
# 
#     TYPE_STATE = 'state'
#     TYPE_COUNTER = 'counter'
#     TYPE_PROFILE = 'profile'
#     TYPE_TRACE = 'trace'
# 
#     CATEGORY_CPU = 'cpu'
#     CATEGORY_MEMORY = 'memory'
#     CATEGORY_GC = 'gc'
#     CATEGORY_RUNTIME = 'runtime'
#     CATEGORY_SPAN = 'span'
#     CATEGORY_CPU_PROFILE = 'cpu-profile'
#     CATEGORY_MEMORY_PROFILE = 'memory-profile'
#     CATEGORY_BLOCK_PROFILE = 'block-profile'
#     CATEGORY_ERROR_PROFILE = 'error-profile'
# 
#     NAME_CPU_TIME = 'CPU time'
#     NAME_CPU_USAGE = 'CPU usage'
#     NAME_MAIN_THREAD_CPU_USAGE = 'Main thread CPU usage'
#     NAME_MAX_RSS = 'Max RSS'
#     NAME_CURRENT_RSS = 'Current RSS'
#     NAME_VM_SIZE = 'VM Size'
#     NAME_GC_COUNT = 'Uncollected objects'
#     NAME_GC_COLLECTIONS = 'Collections'
#     NAME_GC_COLLECTED = 'Collected objects'
#     NAME_GC_UNCOLLECTABLE = 'Uncollectable objects'
#     NAME_THREAD_COUNT = 'Active threads'
#     NAME_UNCOLLECTED_ALLOCATIONS = 'Uncollected allocations'
#     NAME_BLOCKING_CALL_TIMES = 'Blocking call times'
#     NAME_HANDLED_EXCEPTIONS = 'Handled exceptions'
#     NAME_TF_OPERATION_TIMES = 'TensorFlow operation times'
#     NAME_TF_OPERATION_ALLOCATION_RATE = 'TensorFlow operation allocation rate'
#     
#     UNIT_NONE = ''
#     UNIT_MILLISECOND = 'millisecond'
#     UNIT_MICROSECOND = 'microsecond'
#     UNIT_NANOSECOND = 'nanosecond'
#     UNIT_BYTE = 'byte'
#     UNIT_KILOBYTE = 'kilobyte'
#     UNIT_PERCENT = 'percent'
# 
#     TRIGGER_TIMER = 'timer'
#     TRIGGER_API = 'api'
# 
# 
#     def __init__(self, agent, typ, category, name, unit):
#         metric_id = generate_sha1("{0}{1}{2}{3}{4}{5}{6}".format(
#             agent.get_option('app_name'), 
#             agent.get_option('app_environment'), 
#             agent.get_option('host_name'),
#             typ, category, name, unit))
# 
#         self.agent = agent
#         self.id = metric_id
#         self.typ = typ
#         self.category = category
#         self.name = name
#         self.unit = unit
#         self.measurement = None
#         self.has_last_value = False
#         self.last_value = None
# 
# 
#     def has_measurement(self):
#         return self.measurement != None
# 
# 
#     def create_measurement(self, trigger, value, duration = None, breakdown = None):
#         ready = True
# 
#         if self.typ == Metric.TYPE_COUNTER:
#             if not self.has_last_value:
#                 ready = False
#                 self.has_last_value = True
#                 self.last_value = value
#             else:
#                 tmp_value = value
#                 value = value - self.last_value
#                 self.last_value = tmp_value
# 
#         if ready:
#             self.measurement = Measurement(
#                 generate_uuid(),
#                 trigger,
#                 value,
#                 duration,
#                 breakdown,
#                 timestamp())
# 
# 
#     def to_dict(self):
#         measurement_map = None
#         if self.measurement:
#             measurement_map = self.measurement.to_dict()
# 
#         metric_map = {
#             'id': self.id,
#             'type': self.typ,
#             'category': self.category,
#             'name': self.name,
#             'unit': self.unit,
#             'measurement': measurement_map,
#         }
# 
#         return metric_map


# Path: tests/reporters/process_reporter_test.py
import time
import unittest
import sys
import stackimpact
from stackimpact.runtime import runtime_info, min_version
from stackimpact.metric import Metric





class ProcessReporterTestCase(unittest.TestCase):

    def test_report(self):
        stackimpact._agent = None
        agent = stackimpact.start(
            dashboard_address = 'http://localhost:5001',
            agent_key = 'key1',
            app_name = 'TestPythonApp',
            debug = True
        )
        agent.process_reporter.start()

        agent.process_reporter.report()
        time.sleep(0.1)
        agent.process_reporter.report()

        metrics = agent.process_reporter.metrics

        if not runtime_info.OS_WIN:
            self.is_valid(metrics, Metric.TYPE_COUNTER, Metric.CATEGORY_CPU, Metric.NAME_CPU_TIME, 0, float("inf"))
            self.is_valid(metrics, Metric.TYPE_STATE, Metric.CATEGORY_CPU, Metric.NAME_CPU_USAGE, 0, float("inf"))

        if not runtime_info.OS_WIN:
            self.is_valid(metrics, Metric.TYPE_STATE, Metric.CATEGORY_MEMORY, Metric.NAME_MAX_RSS, 0, float("inf"))


        if runtime_info.OS_LINUX: