o
    va/                     @   sl   d Z ddlZddlZddlZddlZddlZddlmZ ddlm	Z	m
Z
mZ ddlmZ G dd deZdS )zThe stats manager.    N)logger)exports_pathplugins_pathsys_path)Counterc                   @   s   e Zd ZdZdZd1ddZdd Zdd	 Zd1d
dZd2ddZ	d2ddZ
d3ddZd3ddZd2ddZdd Zd2ddZdd Zdd Zd2dd Zd2d!d"Zd#d$ Zd2d%d&Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 ZdS )4GlancesStatsz+This class stores, updates and gives stats.glances_Nc                 C   s"   || _ || _d| _| | j d S )NT)configargsfirst_exportload_modules)selfr	   r
    r   //usr/lib/python3/dist-packages/glances/stats.py__init__(   s   zGlancesStats.__init__c                 C   s   | dr"|tdd  }| j| }t|drt|dS t|| drD|tdd  }| j| }t|dr@t|dS t|t|)a  Overwrite the getattr method in case of attribute is not found.

        The goal is to dynamically generate the following methods:
        - getPlugname(): return Plugname stat in JSON format
        - getViewsPlugname(): return views of the Plugname stat in JSON format
        ZgetViewsNZget_json_viewsgetZ	get_stats)
startswithlenlower_pluginshasattrgetattrAttributeError)r   itemZplugnamepluginr   r   r   __getattr__3   s   







zGlancesStats.__getattr__c                 C   sF   t t| _| j|d t t| _t t| _| j|d tt	_
dS )z,Wrapper to load: plugins and export modules.)r
   N)collectionsdefaultdictdictr   load_plugins_exports_exports_allload_exportsr   syspath)r   r
   r   r   r   r   U   s   
zGlancesStats.load_modulesc              
   C   s   |t | jd  }zt|dd }|j||d| j|< W n6 tyU } z*td	|| t
t  |durJt|d| d W Y d}~dS W Y d}~dS d}~ww |durjt|d| t|d| d dS dS )z>Load the plugin (script), init it and add to the _plugin dict.Nr
   r	   z+Error while initializing the {} plugin ({})Zdisable_F)r   headerr   
__import__ZPluginr   	Exceptionr   Zcriticalformaterror	traceback
format_excsetattrr   )r   Zplugin_scriptr
   r	   namer   er   r   r   _load_plugini   s*   zGlancesStats._load_pluginc                 C   s   t  }ttD ]0}|| jr8|dr8|| jd kr8|  | jtj	
||| jd td||  qtd|   dS )z)Load all plugins in the 'plugins' folder..pyz	plugin.pyr&   zPlugin {} started in {} secondszActive plugins list: {}N)r   oslistdirr   r   r'   endswithresetr1   r$   basenamer	   r   debugr*   r   getPluginsList)r   r
   Zstart_durationr   r   r   r   r      s    
zGlancesStats.load_pluginsc                 C   s  |du rdS d}t t d }ttD ]C}tj|t|d  }|	|rW|
drW||d krW||d krWtj|dd | j|< t| jd	| t| jd	| d q| jD ]%}t| jd	| drt| j| }|j|| jd
| j|< | j| | j|< q[td|   dS )z0Load all export modules in the 'exports' folder.NFr   r
   r%   r2   z	export.pyz
history.pyZexport_r&   zActive exports modules list: {}T)varslocalsr3   r4   r   r$   r7   r   r   r   r5   r!   r.   r
   r   r(   ZExportr	   r    r   r8   r*   getExportsList)r   r
   r'   Zargs_varr   Zexport_nameZexport_moduler   r   r   r"      s:   



zGlancesStats.load_exportsTc                    s(   |r fdd j D S dd  j D S )zReturn the plugins list.

        if enable is True, only return the active plugins (default)
        if enable is False, return all the plugins

        Return: list of plugin name
        c                    s   g | ]} j |  r|qS r   )r   Z
is_enabled.0pr   r   r   
<listcomp>   s    z/GlancesStats.getPluginsList.<locals>.<listcomp>c                 S      g | ]}|qS r   r   r=   r   r   r   rA          r   r   Zenabler   r@   r   r9      s   zGlancesStats.getPluginsListc                 C   s$   |r
dd | j D S dd | jD S )zReturn the exports list.

        if enable is True, only return the active exporters (default)
        if enable is False, return all the exporters

        Return: list of export module name
        c                 S   rB   r   r   r>   r0   r   r   r   rA      rC   z/GlancesStats.getExportsList.<locals>.<listcomp>c                 S   rB   r   r   rF   r   r   r   rA      rC   )r    r!   rE   r   r   r   r<      s   zGlancesStats.getExportsListc                 C   s    | j D ]
}| j | | qdS )z;Load the stats limits (except the one in the exclude list).N)r   load_limits)r   r	   r?   r   r   r   rG      s   
zGlancesStats.load_limitsc                 C   sJ   | j D ]}| j |  rq| j |   | j |   | j |   qdS )z#Wrapper method to update the stats.N)r   Zis_disabledupdateZupdate_stats_historyZupdate_views)r   r?   r   r   r   rH      s   
zGlancesStats.updatec                 C   s`   | j rtd d| _ dS |pi }| jD ]}td|  tj| j| j|fd}|  qdS )zXExport all the stats.

        Each export module is ran in a dedicated thread.
        zSDo not export stats during the first iteration because some information are missingFz Export stats using the %s module)targetr
   T)r   r   r8   r    	threadingZThreadrH   start)r   Zinput_statsr0   threadr   r   r   export   s   


zGlancesStats.exportc                        fdd j D S )zReturn all the stats (list).c                       g | ]	} j |  qS r   r   Zget_rawr=   r@   r   r   rA         z'GlancesStats.getAll.<locals>.<listcomp>rD   r@   r   r@   r   getAll      zGlancesStats.getAllc                    rN   )zReturn all the stats (dict).c                       i | ]
}| j |  qS r   rP   r=   r@   r   r   
<dictcomp>      z-GlancesStats.getAllAsDict.<locals>.<dictcomp>rD   r@   r   r@   r   getAllAsDict  rS   zGlancesStats.getAllAsDictc                    s"   |du r j } fdd j D S )
        Return all the stats to be exported (list).
        Default behavor is to export all the stat
        if plugin_list is provided, only export stats of given plugin (list)
        Nc                    rO   r   r   Z
get_exportr=   r@   r   r   rA     rQ   z.GlancesStats.getAllExports.<locals>.<listcomp>rD   r   Zplugin_listr   r@   r   getAllExports  s   zGlancesStats.getAllExportsc                        |du r j } fdd|D S )rX   Nc                    rT   r   rY   r=   r@   r   r   rU     rV   z4GlancesStats.getAllExportsAsDict.<locals>.<dictcomp>rD   rZ   r   r@   r   getAllExportsAsDict     z GlancesStats.getAllExportsAsDictc                    rN   )zReturn the plugins limits list.c                    s   g | ]} j | jqS r   r   Zlimitsr=   r@   r   r   rA      s    z-GlancesStats.getAllLimits.<locals>.<listcomp>rD   r@   r   r@   r   getAllLimits  rS   zGlancesStats.getAllLimitsc                    r\   )z
        Return all the stats limits (dict).
        Default behavor is to export all the limits
        if plugin_list is provided, only export limits of given plugin (list)
        Nc                    s   i | ]	}| j | jqS r   r_   r=   r@   r   r   rU   +  rQ   z3GlancesStats.getAllLimitsAsDict.<locals>.<dictcomp>rD   rZ   r   r@   r   getAllLimitsAsDict"  r^   zGlancesStats.getAllLimitsAsDictc                    rN   )zReturn the plugins views.c                    rO   r   r   Z	get_viewsr=   r@   r   r   rA   /  rQ   z,GlancesStats.getAllViews.<locals>.<listcomp>rD   r@   r   r@   r   getAllViews-  rS   zGlancesStats.getAllViewsc                    rN   )z"Return all the stats views (dict).c                    rT   r   rb   r=   r@   r   r   rU   3  rV   z2GlancesStats.getAllViewsAsDict.<locals>.<dictcomp>rD   r@   r   r@   r   getAllViewsAsDict1  rS   zGlancesStats.getAllViewsAsDictc                 C   s   | j S )zReturn the plugin list.rD   r@   r   r   r   get_plugin_list5  s   zGlancesStats.get_plugin_listc                 C   s   || j v r
| j | S dS )zReturn the plugin name.NrD   )r   Zplugin_namer   r   r   
get_plugin9  s   

zGlancesStats.get_pluginc                 C   s8   | j D ]	}| j |   q| jD ]	}| j|   qdS )zEnd of the Glances stats.N)r    exitr   )r   r0   r?   r   r   r   end@  s
   

zGlancesStats.end)NN)N)T)__name__
__module____qualname____doc__r'   r   r   r   r1   r   r"   r9   r<   rG   rH   rM   rR   rW   r[   r]   r`   ra   rc   rd   re   rf   rh   r   r   r   r   r   !   s2    
"



$





r   )rl   r   r3   r#   rJ   r,   Zglances.loggerr   Zglances.globalsr   r   r   Zglances.timerr   objectr   r   r   r   r   <module>   s   