#!/usr/bin/python -u

'''
------------------------------------------------------------------------
    Type:           Python 3.x script
    Author:         Milan Toman (milan.v.toman@gmail.com)
    Description:    System stats
    Version:        2.0 (SQLite)

------------------------------------------------------------------------
            Import libraries
------------------------------------------------------------------------
'''
# mandatory
import requests
import sys
import os
import re
import textwrap
import argparse
import time
import datetime
import simplejson as json
import influxdb
import psutil
import socket

# optionally, future modules, locally available, I hate dependencies
from pprint import pprint
_SCRIPT_PATH = os.path.dirname(os.path.abspath(sys.argv[0]))
sys.path.append(_SCRIPT_PATH + "/lib")
global _hostname
_hostname = socket.gethostname()

#import ventilLogger


'''
--------------------------------------------------------------------------------
            Define variables
--------------------------------------------------------------------------------
'''
_VERSION = 2.0
_NAME = u"Vantage Vue Decoding shite"
_LOG_DIR = _SCRIPT_PATH + '/log/'
_LOG_FILE_ROOT = re.sub(u'./', '', sys.argv[0])
_LOG_FILE = _LOG_DIR + _LOG_FILE_ROOT + u'.log'
_DEBUG_FILE = _LOG_DIR + _LOG_FILE_ROOT + u'.dbg'

influx_status_write = []

influx_host = 'localhost'
influx_port = 8086
influx_user = 'pi'
influx_pwd = 'Ventil6996'
status_db = 'status'

'''
--------------------------------------------------------------------------------
            Generic, standalone functions
--------------------------------------------------------------------------------
'''

# Obvious shit, set up the client class
influx_status_client = influxdb.client.InfluxDBClient(
    influx_host, influx_port, influx_user, influx_pwd, status_db
)

'''
--------------------------------------------------------------------------------
            Classes
--------------------------------------------------------------------------------
'''

class DBwriter(object):
    def __init__(self):
        __name__ = "Database writer class, Influx"

    def construct(self, connector, measurement, fields, tags):
        """ Takes values in a writes them to influxdb

        requires:   list(connector): connector with all ticks to be written
                                     at once
                    str(measurement): the measurement ID to be written
                    dict(fields): fields to be written in one tick
                    dict(tags): tags to be written with the fields

        returns:    list(result_connector)
        """
        result_connector = connector
        result_connector.append({"measurement": measurement,
                                 "fields": fields,
                                 "tags": tags}
                               )
        return result_connector

'''
--------------------------------------------------------------------------------
            Main
--------------------------------------------------------------------------------
'''
if '__main__':
    davis_writer = DBwriter()
    while True:
        averaged_cpu = psutil.cpu_percent()
        mem_consumption = psutil.virtual_memory()[2]
        disk_usage = psutil.disk_usage('/')[3]
        print(psutil)
        # CPU stats
        for timeout in range(1,15):
            if timeout == 1:
                averaged_cpu = (averaged_cpu + psutil.cpu_percent()) / 2
                mem_consumption = psutil.virtual_memory()[2]
                disk_usage = psutil.disk_usage('/')[3]
                if_counters = psutil.net_io_counters(pernic=True)
                interfaces = if_counters.keys()

                wlan_counters = if_counters["wlan0"]
                wlan_sent = wlan_counters[0]
                wlan_recv = wlan_counters[1]
                wlan_error_in = wlan_counters[4]
                wlan_error_out = wlan_counters[5]
                wlan_drop_in = wlan_counters[6]
                wlan_drop_out = wlan_counters[7]
                eth_counters = if_counters["eth0"]
                eth_sent = eth_counters[0]
                eth_recv = eth_counters[1]
                eth_error_in = eth_counters[4]
                eth_error_out = eth_counters[5]
                eth_drop_in = eth_counters[6]
                eth_drop_out = eth_counters[7]
            else:
                averaged_cpu = (averaged_cpu + psutil.cpu_percent()) / 2
            time.sleep(1)
        # Write the whole blob into Influx DB
        influx_status_write = davis_writer.construct(
                influx_status_write,
                "RasPI",
                {"usage": float(averaged_cpu)},
                {"type": "cpu"}
            )
        influx_status_write = davis_writer.construct(
                influx_status_write,
                "RasPI",
                {"usage": float(mem_consumption)},
                {"type": "mem"}
            )
        influx_status_write = davis_writer.construct(
                influx_status_write,
                "RasPI",
                {"usage": float(disk_usage)},
                {"type": "disk"}
            )
        influx_status_write = davis_writer.construct(
                influx_status_write,
                "net",
                {
                    "b_out": float(wlan_sent),
                    "b_in": float(wlan_recv),
                    "e_out": float(wlan_error_out),
                    "e_in": float(wlan_error_in),
                    "drop_out": float(wlan_drop_out),
                    "drop_in": float(wlan_drop_in),
                },
                {
                    "type": "wlan0",
                    "host": _hostname
                }
            )
        influx_status_write = davis_writer.construct(
                influx_status_write,
                "net",
                {
                    "b_out": float(eth_sent),
                    "b_in": float(eth_recv),
                    "e_out": float(eth_error_out),
                    "e_in": float(eth_error_in),
                    "drop_out": float(eth_drop_out),
                    "drop_in": float(eth_drop_in),
                },
                {
                    "type": "eth0",
                    "host": _hostname
                }
            )
        print("Writing values: {}".format(influx_status_write))
        influx_status_client.write_points(influx_status_write)
        influx_status_write = []
        averaged_cpu = 0
