from __future__ import division
#
# Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
#

from builtins import object
from past.utils import old_div
import os
import psutil

from vrouter.cpuinfo.ttypes import *

class CpuInfoData(object):

    def __init__(self):
        self._process = psutil.Process(os.getpid())
        self._num_cpu = 0
    #end __init__

    def _get_num_cpu(self):
        return psutil.NUM_CPUS
    #end _get_num_cpu

    def _get_sys_mem_info(self):
        virtmem_info = psutil.virtual_memory()
        sys_mem_info = SysMemInfo()
        sys_mem_info.total = old_div(virtmem_info.total,1024)
        sys_mem_info.used = old_div(virtmem_info.used,1024)
        sys_mem_info.free = old_div(virtmem_info.free,1024)
        sys_mem_info.buffers = old_div(virtmem_info.buffers,1024)
        sys_mem_info.cached = old_div(virtmem_info.cached,1024)
        return sys_mem_info
    #end _get_sys_mem_info

    def _get_mem_info(self):
        mem_info = MemInfo()
        mem_info.virt = old_div(self._process.get_memory_info().vms,1024)
        mem_info.peakvirt = mem_info.virt
        mem_info.res = old_div(self._process.get_memory_info().rss,1024)
        return mem_info
    #end _get_mem_info

    def _get_cpu_load_avg(self):
        load_avg = os.getloadavg()
        cpu_load_avg = CpuLoadAvg()
        cpu_load_avg.one_min_avg = load_avg[0]
        cpu_load_avg.five_min_avg = load_avg[1]
        cpu_load_avg.fifteen_min_avg = load_avg[2]
        return cpu_load_avg
    #end _get_cpu_load_avg

    def _get_cpu_share(self):
        cpu_percent = self._process.get_cpu_percent(interval=0.1)
        return old_div(cpu_percent,self._get_num_cpu())
    #end _get_cpu_share

    def get_cpu_info(self, system=True):
        cpu_info = CpuLoadInfo()
        num_cpu = self._get_num_cpu()
        if self._num_cpu != num_cpu:
            self._num_cpu = num_cpu
            cpu_info.num_cpu = num_cpu
        if system:
            cpu_info.sys_mem_info = self._get_sys_mem_info()
            cpu_info.cpuload = self._get_cpu_load_avg()
        cpu_info.meminfo = self._get_mem_info()
        cpu_info.cpu_share = self._get_cpu_share()
        return cpu_info
    #end get_cpu_info

#end class CpuInfoData
