Home > Enterprise >  Date filtering when filtering data from .log file via gui Tkinter
Date filtering when filtering data from .log file via gui Tkinter

Time:07-27

In the interface I designed, I Insert the data on the textbox as I filtered in the log file. Features I've done so far: I filter by word only and filter by start date.

However, I want to output the lines containing the word I want to search between the exact property start date and the end date I want.

For example, my keyword is AppEngine. Also, I want to filter between 07/19/22 08:40:49 to 07/19/22 08:50:49.

So the output should be like this

    [07/19/22 08:40:49.785667 ] [debug  ] [1422] [AppEngine.c:257] [eventNotify] ENTERED 
    [07/19/22 08:40:49.785702 ] [debug  ] [1422] [AppEngine.c:262] [eventNotify] Event notified eventId: 290 
    [07/19/22 08:40:49.785736 ] [debug  ] [1422] [AppEngine.c:271] [eventNotify] Event identified eventId: 290 
    [07/19/22 08:40:49.785770 ] [debug  ] [1422] [AppEngine.c:282] [eventNotify] LEAVED 

How can I write the ending date state?

My log file example here:

[07/19/22 08:40:49.785667 ] [debug  ] [1422] [AppEngine.c:257] [eventNotify] ENTERED 
[07/19/22 08:40:49.785702 ] [debug  ] [1422] [AppEngine.c:262] [eventNotify] Event notified eventId: 290 
[07/19/22 08:40:49.785736 ] [debug  ] [1422] [AppEngine.c:271] [eventNotify] Event identified eventId: 290 
[07/19/22 08:40:49.785770 ] [debug  ] [1422] [AppEngine.c:282] [eventNotify] LEAVED 
[07/19/22 08:40:49.785936 ] [debug  ] [1422] [SoftwareManagement.c:236] [requestAppToProcessRruSoftwareInformation] LEAVED 
[07/19/22 08:40:49.785959 ] [debug  ] [1422] [SwmUkaEventHandler.c:1501] [handleUkaSWMRRUVersionListInformRequestReceived] handleUkaSWMRRUVersionListInformRequestReceived return value = 0 
[07/19/22 08:40:49.785971 ] [debug  ] [1422] [SwmUkaEventHandler.c:1501] [handleUkaSWMRRUVersionListInformRequestReceived] LEAVED 
[07/19/22 08:40:49.785983 ] [debug  ] [1426] [UKAInterface.c:483] [receiveMessageFromAKA] Received data size: 14 
[07/19/22 08:40:49.785995 ] [debug  ] [1426] [UKAInterface.c:687] [mergeMessageChunks] uChunkSize: 5 

My Function is :

#function to search string in text
def search(msg, startingDate, endingDate):
    with open(r"OAM.log", 'r', encoding='latin1') as fp:
        for l_no, line in enumerate(fp):
            lineNum = l_no   1
            if msg in line and var1.get() == 1 and (var2.get() == 0):
                text.insert('1.0', f"Line Number: {lineNum} Log: {line} \n ")
            elif startingDate in line and (var2.get() == 1):
                if msg in line and (var1.get() == 1):
                    text.insert('1.0', f"Line Number: {lineNum} Log: {line} \n ")
    
def getInfo():
    msg = edit.get()
    startingDate = tarih1.get()
    endingDate = tarih2.get()

    search(msg, startingDate, endingDate)  

butt.config(command=getInfo)

My Gui: Gui

CodePudding user response:

You need to convert the timestamp to datetime object for comparing with the startingDate or endingDate:

...
from datetime import datetime
...

def parse_date(date_str):
    # format mm/dd/yy HH:MM:SS[.NNNNNN]
    date_fmt = '%m/%d/%y %H:%M:%S'
    if '.' in date_str:
        date_fmt  = '.%f'
    return datetime.strptime(date_str, date_fmt)

#function to search string in text
def search(msg, startingDate, endingDate):
    # clear current result
    text.delete('1.0', 'end')
    with open(r"OAM.log", 'r', encoding='latin1') as fp:
        for l_no, line in enumerate(fp, 1):
            if msg and msg not in line:
                # does not contain search message, skip it
                continue
            if startingDate or endingDate:
                # get the timestamp
                timestamp = parse_date(line[1:25])
                # within startingDate and endingDate ?
                if startingDate and timestamp < startingDate:
                    # before given starting date, skip it
                    continue
                if endingDate and timestamp > endingDate:
                    # after given ending date, skip it
                    continue
            # insert the log
            text.insert('end', f'Line Number: {l_no} Log: {line}')

def getInfo():
    msg = edit.get() if var1.get() == 1 else None
    startingDate = parse_date(tarih1.get()) if var2.get() == 1 else None
    endingDate = parse_date(tarih2.get()) if var3.get() == 1 else None

    search(msg, startingDate, endingDate)
  • Related