#!/usr/bin/python3

import time
import psutil

#def net_usage(inf = 'wlan0'):
#    net_stat = psutil.net_io_counters(pernic=True, nowrap=True)[inf]
#    net_in_1 = net_stat.bytes_recv
#    net_out_1 = net_stat.bytes_sent
#    time.sleep(1)
#    net_stat = psutil.net_io_counters(pernic=True, nowrap=True)[inf]
#    #cory you must refresh the netstat to get the new bytes after the delay, in this istance its time.sleep(1)
#    net_in_2 = net_stat.bytes_recv
#    net_out_2 = net_stat.bytes_sent
#
#    net_in = round((net_in_2 - net_in_1) / 1024 / 1024, 3)
#    net_out = round((net_out_2 - net_out_1) / 1024 / 1024, 3)
#        
#    print(f'net in: {net_in}, net out: {net_out}')

#while True:
#    net_usage()

class Network:
    def __init__(self, inf = 'enp7s0'):
        self.net_stat = psutil.net_io_counters(pernic=True, nowrap=True)[inf]
        self.net_in_1 = self.net_stat.bytes_recv
        self.net_out_1 = self.net_stat.bytes_sent
        self.delay = 0

    def first_check(self, inf = 'enp7s0'):
        self.net_stat = psutil.net_io_counters(pernic=True, nowrap=True)[inf]
        self.net_in_1 = self.net_stat.bytes_recv
        self.net_out_1 = self.net_stat.bytes_sent


    def magik(self, inf = 'enp7s0'):
        self.net_stat = psutil.net_io_counters(pernic=True, nowrap=True)[inf]
        self.net_in_2 = self.net_stat.bytes_recv
        self.net_out_2 = self.net_stat.bytes_sent
        self.net_in = round((self.net_in_2 - self.net_in_1) / 1024, 3)
        print(self.net_in)

    def loop(self):
        while True:
            self.delay += 1
            if self.delay == 1000000:
                self.magik()
                self.first_check()
                self.delay = 0

    def getListOfProcessSortedByMemory(self):
        '''
        Get list of running process sorted by Memory Usage
        '''
        listOfProcObjects = []
        # Iterate over the list
        for proc in psutil.process_iter():
            try:
                # Fetch process details as dict
                pinfo = proc.as_dict(attrs=['pid', 'name', 'username'])
                #pinfo['vms'] = proc.memory_info().vms / (1024 * 1024)
                pinfo['network_in'] = psutil.net_io_counters(pernic=True, nowrap=True)['enp7s0'].bytes_recv
                # Append dict to list
                listOfProcObjects.append(pinfo);
            except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
                pass
        # Sort list of dict by key vms i.e. memory usage
        listOfProcObjects = sorted(listOfProcObjects, key=lambda procObj: procObj['network_in'], reverse=True)
        return listOfProcObjects
    def process_list(self):
        self.listOfRunningProcess = self.getListOfProcessSortedByMemory()
        time.sleep(3)
        secound_check = psutil.net_io_counters(pernic=True, nowrap=True)['enp7s0'].bytes_recv
        network_usage = []
        for elem in self.listOfRunningProcess:
            elem['network_in'] = secound_check - elem['network_in']
            network_usage.append(elem)
        #for elem in self.listOfRunningProcess[:5] :
        for elem in network_usage[:1]:
        #    elem['network_in'] = secound_check - elem['network_in']
            print(f"name: {elem['name']} pid: {elem['pid']} network_in: {elem['network_in']}")

network = Network()
while True:
    print('next \n')
    network.process_list()
