U
    Tg"                     @   s  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 d dlm	Z	 d dl
mZ d dlmZ e	dZedkre	dZedkrdZed	d
dZejddddd ejdddddd ejddddd ejddddd ejddddd ejddddd ejddd dd!ee d e \ZZeed"k rBe  e d# ed  Zed# Zejdkrhed  ZnejZejdk	re jd ej zd dlZW n ek
r   e d$Y nX ej!dk	re jd ej! zd d%l"m#Z# W n ek
r    e d&Y nX ee#_$ee#_%d d'l&m'Z'm(Z( e' Z)e( Z*d(d) Z+d*d+ Z,d1d-d.Z-ej.erle/d/rle+ee)e* nFe0eD ]:\Z1Z2Z3e4e3d0D ] Z5ej6e1e5Z7e+e7e)e* qqvdS )2    N)call)OptionParser)find_executable)basename)inputzwhisper-resize.pyzwhisper-resizez%prog storagePath configPath

storagePath   the Path to the directory containing whisper files (CAN NOT BE A
              SUBDIR, use --subdir for that)
configPath    the path to your carbon config files
z	%prog 0.1)usageversionz--doitF
store_truezThis is not a drill, lets do it)defaultactionhelpz-qz--quietzDisplay extra debugging infoz--subdirstringz&only process a subdir of whisper files)r
   typer   z--carbonlibzEfolder where the carbon lib files are if its not in your path alreadyz--whisperlibzFfolder where the whisper lib files are if its not in your path alreadyz	--confirmz5ask for comfirmation prior to resizing a whisper filez-xz--extra_args z.pass any additional arguments to the %s script      z\[ERROR] Can't find the whisper module, try using --whisperlib to explicitly include the path)settingszZ[ERROR] Can't find the carbon module, try using --carbonlib to explicitly include the path)loadStorageSchemasloadAggregationSchemasc                 C   sJ  d}d}d}d}t | }t| }|D ]"}	|	|r&dd |	jD }
 qJq&|D ]}||rN|j\}} qlqN|dkrxd}|dkrd}|
D ] }d|d	 |d
 f }||7 }q|d D ] }d|d |d f }||7 }q||krd}|d||f 7 }d|d }d|}||kr(d}|d|||f 7 }||d krPd}|d||d |f 7 }|dkrFt| g}tt	j
D ]}|| qn|dt|  |dt|  t|D ]}|| qt	jdk	st	jdkrt| t| t	jdkrtdt	_t	jdkrtd t	jdkrFt|}|d	krFtd|  td
 dS )a0  
        method to process a given metric, and resize it if necessary

        Parameters:
            fullPath    - full path to the metric whisper file
            schemas     - carbon storage schemas loaded from config
            agg_schemas - carbon storage aggregation schemas load from confg

    r   Fc                 S   s   g | ]}|  qS  )ZgetTuple).0archiver   r   c/home/ankuromar296_gmail_com/.local/lib/python3.8/site-packages/../../../bin/whisper-auto-resize.py
<listcomp>   s     z!processMetric.<locals>.<listcomp>Ng      ?Zaveragez%s:%s r   r   archivesZsecondsPerPointZpointsTz%updating Retentions from: %s to: %s 
z{0:.2f}xFilesFactorz0%s xFilesFactor differs real: %s should be: %s 
aggregationMethodz6%s aggregation schema differs real: %s should be: %s 
z--xFilesFactor=z--aggregationMethod=z+Would you like to run this command? [y/n]: zSkipping command 
zError running: %s)whisperinfogetMetricFromPathmatchesr   formatwhisperResizeExecutableshlexsplitoptions
extra_argsappendstrquietconfirmprintZdoitr   sysexit)ZfullPathschemasagg_schemasZschema_config_argsZschema_file_argsZrebuildmessagesr   ZmetricZschemaZarchive_configZ
agg_schemar   r   Z	retentionZcurrent_schemaZfileRetentionZinfo_xFilesFactorZstr_xFilesFactorcmdxexitcoder   r   r   processMetricf   sz    











r4   c                 C   s<   t jtjt j }| |d}|dd}|dd}|S )z
        this method takes the full file path of a whisper file an converts it
        to a gaphite metric name

        Parameters:
            filePath - full file path to a whisper file

        Returns a string representing the metric name
    r   .wsp/.)ospathnormpathr   LOCAL_DATA_DIRsepreplace)ZfilePathdata_dirZmetric_namer   r   r   r      s
    r   Valid options : yes or noc                 C   s2   t |  }|dkrdS |dkr$dS t| q dS )a  
         ask the user if they would like to perform the action

         Parameters:
             question       - the question you would like to ask the user to confirm.
             error_response - the message to display if an invalid option is given.
    )yyesT)nnoFN)r   lowerr+   )questionZerror_responseZanswerr   r   r   r*      s    	r*   r5   z*.wsp)r?   )8r,   r8   fnmatchr#   
subprocessr   optparser   distutils.spawnr   os.pathr   Z	six.movesr   r"   Zoption_parser
add_option
parse_argsr%   argslen
print_helpr-   ZstoragePathZ
configPathsubdirZprocessPathZ
whisperlibr9   insertr   ImportError
SystemExitZ	carbonlibZcarbon.confr   ZCONF_DIRr;   Zcarbon.storager   r   r.   r/   r4   r   r*   isfileendswithwalkroot_filesfilterfjoinfullpathr   r   r   r   <module>   s                   

c
