o
    va                     @   s   d Z ddlmZ ddlZddlZddlmZ ddlmZ dZ	dZ
dd	d
Zdd Zdd Zdd Zdd Zdd Zdd Zdd ZG dd deZdS )z#Fields description interface class.    )pformatN)logger)	iteritemszhttp://localhost:61208/api/3a  .. _api:

API (Restfull/JSON) documentation
=================================

The Glances Restfull/API server could be ran using the following command line:

.. code-block:: bash

    # glances -w --disable-webui

Note: Change request URL api/3 by api/2 if you use Glances 2.x.
    c                 C   s\   t | tr"t| dkr"t | d tr"|t| dd dd|  S |t| dd|  S )N   r      
)
isinstancelistlendictr   replace)statindent r   G/usr/lib/python3/dist-packages/glances/outputs/glances_stdout_apidoc.pyindent_stat/   s   $ r   c                 C   sZ   d}t | t dt|  t d t d t d t dt t t|  t d d S )NzGET plugins list- zGet the plugins list::z    # curl {}/pluginslist)printr   formatAPI_URLr   )r   	sub_titler   r   r   print_plugins_list8   s   r   c                 C   sb   d | }t| tdt|  td td td td t|  tt| td d S )NzGET {}r   r   zGet plugin stats::z    # curl {}/{})r   r   r   r   r   )pluginstat_exportr   r   r   r   print_plugin_exportD   s   
r   c                 C   s   |j r7td td t|j D ] \}}td||d dr'|d d d n|d |d  qtd d S td|  d S )	NzFields descriptions:r   z* **{}**: {} (unit is *{}*)description.Zunitz4No fields_description variable defined for plugin {})Zfields_descriptionr   r   r   endswithr   error)r   r   Zfieldr   r   r   r   print_plugin_descriptionQ   s   $r"   c                 C   s\  d }d }t |trt| d }d }n)t |tr=t|dkr=t |d tr=d|d v r3|d d }n
t|d  d }|ry||ryt||}t || trZ|| d }n|| }td td td	t
| | tt| td |r|r|||rtd td td	t
| || ttt||| td d S d S d S d S )Nr   keyzGet a specific field::r   z    # curl {}/{}/{}z7Get a specific item when field matchs the given value::z    # curl {}/{}/{}/{})r	   r   r
   keysr   Zget_stats_itemjsonloadsr   r   r   r   Zget_stats_value)r   r   r   itemvalueZ	stat_itemr   r   r   print_plugin_item_value_   s4   
$r)   c                  C   sV   d} t |  t dt|   t d t d t d t dt t d t d d S )NzGET all statsr   r   zGet all Glances stats::z    # curl {}/allzZ    Return a very big dictionnary (avoid using this request, performances will be poor)...)r   r   r   r   )r   r   r   r   	print_all}   s   r*   c                 C   sd  t d |   t d |   d}t| tdt|  td td td tdt ttt	| 
djdd	 td td
 td tdt ttt	| 
djdd	 td td td tdt ttt	| 
dd td td td tdt ttt	| 
djddd	 td d S )Nr   zGET stats historyr   r   zHistory of a plugin::z    # curl {}/cpu/historycpu   )nbz Limit history to last 2 values::z    # curl {}/cpu/history/2r   zHistory for a specific field::z     # curl {}/cpu/system/historysystemz5Limit history for a specific field to last 2 values::)timesleepupdater   r   r   r   r   r%   r&   
get_pluginZget_stats_historystatsr   r   r   r   print_history   s8   

  "r5   c                 C   s   d}t | t dt|  t d t d t d t dt t t|   t d t d t d t dt t t| dj t d d S )	Nz GET limits (used for thresholds)r   r   zAll limits/thresholds::z    # curl {}/all/limitsz&Limits/thresholds for the cpu plugin::z    # curl {}/cpu/limitsr+   )r   r   r   r   r   ZgetAllLimitsAsDictr2   Zlimitsr3   r   r   r   print_limits   s   r6   c                   @   s.   e Zd ZdZd
ddZdd Z	ddd	ZdS )GlancesStdoutApiDocz<
    This class manages the fields description display.
    Nc                 C   s   || _ || _d S N)configargs)selfr9   r:   r   r   r   __init__   s   
zGlancesStdoutApiDoc.__init__c                 C   s   d S r8   r   )r;   r   r   r   end   s   zGlancesStdoutApiDoc.endr   c                 C   s   t t tt|j t|jD ](}||}| }|du s'|g ks'|i kr(qt|| t|| t	||| qt
  t| t| dS )zDisplay issue
        NT)r   APIDOC_HEADERr   sortedZ_pluginsr2   Z
get_exportr   r"   r)   r*   r5   r6   )r;   r4   Zdurationr   r   r   r   r   r   r1      s   


zGlancesStdoutApiDoc.update)NN)r   )__name__
__module____qualname____doc__r<   r=   r1   r   r   r   r   r7      s    
r7   )r   )rC   Zpprintr   r%   r/   Zglances.loggerr   Zglances.compatr   r   r>   r   r   r   r"   r)   r*   r5   r6   objectr7   r   r   r   r   <module>   s"   
	