# Path: bts_tools/core.py
# HERE = abspath(dirname(__file__))
# BTS_TOOLS_HOMEDIR = '~/.bts_tools'
# BTS_TOOLS_HOMEDIR = expanduser(BTS_TOOLS_HOMEDIR)
# BTS_TOOLS_CONFIG_FILE = join(BTS_TOOLS_HOMEDIR, 'config.yaml')
# BTS_TOOLS_DB_FILE = join(BTS_TOOLS_HOMEDIR, 'db.yaml')
# DB_VERSION = 1
# DEFAULT_BIN_FILENAMES = {'bts': ['witness_node/witness_node', 'cli_wallet/cli_wallet'],
#                          'bts-testnet': ['witness_node/witness_node', 'cli_wallet/cli_wallet'],
#                          'muse': ['mused/mused', 'cli_wallet/cli_wallet'],
#                          'steem': ['steemd/steemd', 'cli_wallet/cli_wallet'],
#                          'ppy': ['witness_node/witness_node', 'cli_wallet/cli_wallet'],
#                          'ppy-testnet': ['witness_node/witness_node', 'cli_wallet/cli_wallet']
#                          }
# DEFAULT_GUI_BIN_FILENAMES = {'bts': '',
#                              'bts-testnet': '',
#                              'muse': '',
#                              'steem': '',
#                              'ppy': '',
#                              'ppy-testnet': ''
#                              }
# _SIGNAL_NAMES = dict((k, v) for v, k in signal.__dict__.items()
#                      if v.startswith('SIG') and not v.startswith('SIG_'))
# VERSION = get_version()
# class AttributeDict(dict):
# class CaseInsensitiveAttributeDict(dict):
# class UnauthorizedError(Exception):
# class RPCError(Exception):
# class NoFeedData(Exception):
# class hashabledict(dict):
#     def __init__(self, *args, **kwargs):
#     def __init__(self, *args, **kwargs):
#     def __getitem__(self, item):
#     def __setitem__(self, item, value):
#     def __getattr__(self, item):
#     def __setattr__(self, key, value):
# def append_unique(l1, l2):
# def profile(f):
#     def wrapper(*args, **kwargs):
# def trace(f):
#     def wrapper(*args, **kwargs):
# def save_db():
# def load_db():
# def load_config(loglevels=None):
#     def recursive_update(a, b):
#             def add_cmdline_args(args):
#             def add_monitoring(l2):
# def affiliation(type):
# def get_data_dir(env):
# def get_gui_bin_name(build_env):
# def get_all_bin_names(client=None, build_env=None):
# def get_full_bin_name(client=None, build_env=None):
# def get_full_cli_bin_name(client=None, build_env=None):
# def get_bin_name(client=None, build_env=None):
# def get_cli_bin_name(client=None, build_env=None):
# def quote_shell_arg(arg):
# def join_shell_cmd(cmd):
# def split_shell_cmd(cmd):
# def _forward_signal(pid):
#     def wrapped(sig, frame):
# def _install_signal_forwarding(pid):
# def _run(cmd, capture_io=False, verbose=False, run_dir=None, forward_signals=False, pidfile=None, shell=False):
# def run(cmd, capture_io=False, verbose=True, log_on_fail=True,
#         run_dir=None, forward_signals=False, pidfile=None, shell=False):
# def get_version():
#     def __init__(self, *args, **kwargs):
#     def __key(self):
#     def __hash__(self):
#     def __eq__(self, other):
# def make_hashable(obj):
# def to_list(obj):
# def hash_salt_password(password):
# def list_valid_plugins(plugin_type):
# def get_plugin(plugin_type, plugin_name):
# def get_plugin_dict(plugin_type):
# def replace_in_file(filename, old, new, **kwargs):


from datetime import datetime
from collections import deque
from ..core import StatsFrame, GlobalStatsFrame
import psutil
import math
import logging
        #        actually, we have more problems related to that, as seed nodes, graphene, etc.
        #        don't show global stats anymore...

        # first monitoring thread that initializes its context gets to be
        # the one that will fill in the global cpu values
        cpu_total_ctx = ctx
        ctx.global_stats = deque(maxlen=min(desired_maxlen, maxlen))


def is_valid_node(node):
    return True


def monitor(node, ctx, cfg):
    # only monitor cpu and network if we are monitoring localhost
    if node.is_witness_localhost():
        p = node.process()
        if p is not None:
            try:
                connections = int(node.network_get_info()['connection_count'])
            except Exception:  # TimeoutError when we just closed the witness, but the process still exists
                connections = 0
            s = StatsFrame(cpu=p.cpu_percent(),
                           mem=p.memory_info().rss,
                           connections=connections,
                           timestamp=datetime.utcnow())
        else:
            s = StatsFrame(cpu=0, mem=0, connections=0, timestamp=datetime.utcnow())

    else:

# Path: bts_tools/core.py
# HERE = abspath(dirname(__file__))
# BTS_TOOLS_HOMEDIR = '~/.bts_tools'
# BTS_TOOLS_HOMEDIR = expanduser(BTS_TOOLS_HOMEDIR)
# BTS_TOOLS_CONFIG_FILE = join(BTS_TOOLS_HOMEDIR, 'config.yaml')
# BTS_TOOLS_DB_FILE = join(BTS_TOOLS_HOMEDIR, 'db.yaml')
# DB_VERSION = 1
# DEFAULT_BIN_FILENAMES = {'bts': ['witness_node/witness_node', 'cli_wallet/cli_wallet'],
#                          'bts-testnet': ['witness_node/witness_node', 'cli_wallet/cli_wallet'],
#                          'muse': ['mused/mused', 'cli_wallet/cli_wallet'],
#                          'steem': ['steemd/steemd', 'cli_wallet/cli_wallet'],
#                          'ppy': ['witness_node/witness_node', 'cli_wallet/cli_wallet'],
#                          'ppy-testnet': ['witness_node/witness_node', 'cli_wallet/cli_wallet']
#                          }
# DEFAULT_GUI_BIN_FILENAMES = {'bts': '',
#                              'bts-testnet': '',
#                              'muse': '',
#                              'steem': '',
#                              'ppy': '',
#                              'ppy-testnet': ''
#                              }
# _SIGNAL_NAMES = dict((k, v) for v, k in signal.__dict__.items()
#                      if v.startswith('SIG') and not v.startswith('SIG_'))
# VERSION = get_version()
# class AttributeDict(dict):
# class CaseInsensitiveAttributeDict(dict):
# class UnauthorizedError(Exception):
# class RPCError(Exception):
# class NoFeedData(Exception):
# class hashabledict(dict):
#     def __init__(self, *args, **kwargs):
#     def __init__(self, *args, **kwargs):
#     def __getitem__(self, item):
#     def __setitem__(self, item, value):
#     def __getattr__(self, item):
#     def __setattr__(self, key, value):
# def append_unique(l1, l2):
# def profile(f):
#     def wrapper(*args, **kwargs):
# def trace(f):
#     def wrapper(*args, **kwargs):
# def save_db():
# def load_db():
# def load_config(loglevels=None):
#     def recursive_update(a, b):
#             def add_cmdline_args(args):
#             def add_monitoring(l2):
# def affiliation(type):
# def get_data_dir(env):
# def get_gui_bin_name(build_env):
# def get_all_bin_names(client=None, build_env=None):
# def get_full_bin_name(client=None, build_env=None):
# def get_full_cli_bin_name(client=None, build_env=None):
# def get_bin_name(client=None, build_env=None):
# def get_cli_bin_name(client=None, build_env=None):
# def quote_shell_arg(arg):
# def join_shell_cmd(cmd):
# def split_shell_cmd(cmd):
# def _forward_signal(pid):
#     def wrapped(sig, frame):
# def _install_signal_forwarding(pid):
# def _run(cmd, capture_io=False, verbose=False, run_dir=None, forward_signals=False, pidfile=None, shell=False):
# def run(cmd, capture_io=False, verbose=True, log_on_fail=True,
#         run_dir=None, forward_signals=False, pidfile=None, shell=False):
# def get_version():
#     def __init__(self, *args, **kwargs):
#     def __key(self):
#     def __hash__(self):
#     def __eq__(self, other):
# def make_hashable(obj):
# def to_list(obj):
# def hash_salt_password(password):
# def list_valid_plugins(plugin_type):
# def get_plugin(plugin_type, plugin_name):
# def get_plugin_dict(plugin_type):
# def replace_in_file(filename, old, new, **kwargs):


# Path: bts_tools/monitoring/cpu_ram_usage.py
from datetime import datetime
from collections import deque
from ..core import StatsFrame, GlobalStatsFrame
import psutil
import math
import logging

        #        actually, we have more problems related to that, as seed nodes, graphene, etc.
        #        don't show global stats anymore...

        # first monitoring thread that initializes its context gets to be
        # the one that will fill in the global cpu values
        cpu_total_ctx = ctx
        ctx.global_stats = deque(maxlen=min(desired_maxlen, maxlen))


def is_valid_node(node):
    return True


def monitor(node, ctx, cfg):
    # only monitor cpu and network if we are monitoring localhost
    if node.is_witness_localhost():
        p = node.process()
        if p is not None:
            try:
                connections = int(node.network_get_info()['connection_count'])
            except Exception:  # TimeoutError when we just closed the witness, but the process still exists
                connections = 0
            s = StatsFrame(cpu=p.cpu_percent(),
                           mem=p.memory_info().rss,
                           connections=connections,
                           timestamp=datetime.utcnow())
        else:
            s = StatsFrame(cpu=0, mem=0, connections=0, timestamp=datetime.utcnow())

    else:

        s = StatsFrame(cpu=0, mem=0, connections=0, timestamp=datetime.utcnow())