Example #1
0
    def findValues( statsTypes, line = "", lineType = "[INFO]", fileType = "tx",logger = None ):
        """
            @summary : This method is used to find specific values within a line. 
            
            @note    : If stats type cannot be found the 0 value will be returned.  
                       A key named "valuesNotFound" will be added. It's value will 
                       be an array containing the list of statsType for which no 
                       values were found.
            
            @precondition: Requires _ translator to have been set prior to calling this function.
            
            @return : returns a dictionary containing the statsType->foundValue associations.
        
                       
            
        """
       
        global _ 
        
        values = {} #in case of an unknown type
        
        splitLine = line.split( " " )
        
        
        if line != "" and line != "\n" :
                       
            for statsType in statsTypes :  
                
                try:                    
                    
                    if statsType == "departure" : #is at the same place for every lineType 
                        values[ statsType ] =  line.split( "," )[0]   
                        
                    
                    elif lineType == "[INFO]" :
                        
                        if statsType == "latency":
                                                            
                            d1 = line[:19]
                            d2 = splitLine[6].split(":")[6]     
                                    
                            result = (datetime.datetime( int(d1[0:4]), int(d1[5:7]), int(d1[8:10]), int(d1[11:13]), int(d1[14:16]), int(d1[17:19])) - datetime.datetime( int(d2[0:4]),int(d2[4:6]),int(d2[6:8]),int(d2[8:10]),int(d2[10:12]),int(d2[12:14]) ) )                          
                            
                            values[statsType] = result.seconds + (result.days*24*60*60)
                            if values[statsType] < 0 :
                                values[statsType] = 0 
                                      
                        elif statsType == "arrival":                  
                        
                            values[statsType] = StatsDateLib.isoDateDashed( splitLine[6].split( ":" )[6] )    
    
                                
                        elif statsType == "bytecount":
                            start = line.find( "(" )
                            end   = line.find( "Bytes" )  
                            start = start +1
                            end = end -1                           
                            
                            values[statsType] = int( line[start:end] )
                        
                            
                        elif statsType == "fileName":
                            
                            if fileType == "tx" :
                                values[statsType] = os.path.basename(splitLine[6])#.split( ":" )[0]
                            else:
                                split     = line.split( "/" )
                                lastPart  = split[ len( split ) -1 ]
                                values[ statsType ] = lastPart.split( ":" )[0] #in case something is added after line ends.
                            
                        elif statsType == "productType":
                            
                            if fileType == "tx":
                                values[statsType] = os.path.basename(splitLine[6])#.split( ":" )[0]
                            else: # rx has a very different format for product.
                                split     = line.split( "/" )
                                lastPart  = split[ len( split ) -1 ]
                                values[ statsType ] = lastPart.split( ":" )[0] #in case something is added after line ends.
                                
                        elif statsType == "errors" :
                            values[statsType] = 0  
                            
                        
                    elif lineType == "[ERROR]":
                    
                        if statsType == "errors" :
                            values[statsType] = 1                   
            
                        
                        elif statsType == "productType" :     
                            values[statsType] = ""
                        else:
                            values[statsType] = 0
                        
                        #elif lineType == "[OTHER]" :               
            
                except:                
                    
                    if logger is not None :
                        logger.error(_("could not find %s value in line %s.") %( statsType,line ) ) 
                        logger.error(_("value was replaced by 0."))
                    
                    if "valuesNotFound" not in values:
                        values[ "valuesNotFound" ] = []
        
                    values[ "valuesNotFound" ].append( statsType )     
                    
                    values[statsType] = 0 
                    pass        

        else:
            for type in statsTypes :
                values[type] = 0
        

        return values