
    #wg#              
       L   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 e	d      ZedZ ed	d
      Zej!                  dddd       ej!                  ddddd       ej!                  dddd       ej!                  dddd       ej!                  dddd       ej!                  dddd       ej!                  ddd dd! ee      z         ej#                         \  ZZ ee      d"k  r"ej+                           e j,                  d#       ed    Zed#   Zej2                  ed    Znej2                  Zej6                  &e j8                  j;                  d ej6                         	 d dlZejB                  &e j8                  j;                  d ejB                         	 d d%l"m#Z# ee#_$        ee#_%        d d'l&m'Z'm(Z(  e'       Z) e(       Z*d( Z+d) Z,d-d*Z-ej8                  j]                  e      rej_                  d+      r e+ee)e*       y ej`                  e      D ]D  \  Z1Z2Z3 ejh                  e3d,      D ](  Z5ej8                  jm                  e1e5      Z7 e+e7e)e*       * F y# e$ r	  e d$      w xY w# e$ r	  e d&      w xY w).    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                 `   d}d}d}d}t        j                  |       }t        |       }|D ]<  }	|	j                  |      s|	j                  D 
cg c]  }
|
j                          }}
 n |D ]$  }|j                  |      s|j                  \  }} n d}d}D ]  }|d   d|d   d	}||z  } |d
   D ]  }|d   d|d   d	}||z  } ||k7  rd}|d|d|dz  }dj                  |d         }dj                  |      }||k7  rd}||d|d|dz  }||d   k7  rd}||d|d   d|dz  }|du rkt        | g}t        j                  t        j                        D ]  }|j                  |        |j                  dt        |      z          |j                  dt        |      z          t        j                  |      D ]  }|j                  |        t        j                  dust        j                  du rt!        |       t!        |       t        j                  du r1t        d      t        _        t        j"                  du rt!        d       t        j"                  du r5t%        |      }|dkD  r$t!        d|z         t'        j(                  d       yyyyc c}
w )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   FNg      ?averager   :r    archivessecondsPerPointpointsTzupdating Retentions from: z to: z 
z{0:.2f}xFilesFactorz xFilesFactor differs real: z should be: aggregationMethodz" aggregation schema differs real: 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   getTupleformatwhisperResizeExecutableshlexsplitoptions
extra_argsappendstrquietconfirmprintdoitr   sysexit)fullPathschemasagg_schemasschema_config_argsschema_file_argsrebuildmessagesr    metricschemaarchivearchive_config
agg_schemar   r   	retentioncurrent_schemafileRetentioninfo_xFilesFactorstr_xFilesFactorcmdxexitcodes                          e/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/../../../bin/whisper-auto-resize.pyprocessMetricrG   f   s    GH <<!D x(F  >>&!@FPWg..0PNP	 " 
f%.8.A.A+L+
  % $ -	%.q\9Q<@n,- j) +%23D%E}U]G^_N*+
 	..%'9; 	;
 "((n)=> ''5 	--.0@B 	B 	T"566T"568IK 	K $&1W//0 	AJJqM	

$s<'889

)C0A,BBC/0 	AJJqM	 ==$4(?(O#J??d""#PQGL||u$+,<<4CyH1)S12   % a Qs   J+c                     t         j                  j                  t        j                        t         j
                  z   }| j                  |d      }|j                  dd      }|j                  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)filePathdata_dirmetric_names      rF   r!   r!      se     ww 7 78266AH ""8R0K%%fb1K%%c3/K    c                 b    	 t        |       j                         }|dv ry|dv ryt        |       /)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.
    T)yyes)nnoF)r   lowerr.   )questionerror_responseanswers      rF   r-   r-      s<     x&&(\![ n rU   rI   z*.wsp)zValid options : yes or no)8r0   rL   fnmatchr&   
subprocessr   optparser   distutils.spawnr   os.pathr   	six.movesr   r%   option_parser
add_option
parse_argsr(   argslen
print_helpr1   storagePath
configPathsubdirprocessPath
whisperlibrM   insertr   ImportError
SystemExit	carbonlibcarbon.confr   CONF_DIRrO   carbon.storager   r   r3   r4   rG   r!   r-   isfileendswithwalkroot_filesfilterfjoinfullpath rU   rF   <module>r      s*   
 	    ! +   **=> "-.>?&"5
    eL	*  ,   )U<	'  )   	@  B   4	_  a   D	`  b   |	@  B   ."8	9	)	*
+  ,
  **,$t9q=CHHQK1g!W
 >>q'K..K !HHOOAw))*D  HHOOAw(()C$  %  F 
$&`F*" 77>>+;#7#7#?+w4!"''+. :aw/ 	:Aww||D!,H(G[9	::O  D
 C D DD  C
 B C CCs   J ?J JJ#