My requirement is to monitor a kafka topic for the messages & filter or search for a keyword - state
from the arrived messages and print, Active Brands and Non active brands(Disables & Booted) separately. This has to be done within a timeout of 15 minutes or else break out of the script saying TimedOut.
Below is my piece of code and the sample message format from Kafka topic. I'm using confluent kafka.
Problems being, I'm unsure, where to add the timeout logic, should I do it in to_get_msg_from_kafka()
or do it as part of status_check(
). Secondly, ideally, All the messages until 15 minutes should be processed right or how should that be handled ?
Code Snippet:
START_TIME = datetime.now()
lap_list = ['Apple', 'Acer']
TIMEOUT = 15
#Function that polls kafka topic and retrieve messages..
def get_msg_from_kafka(args):
pass
#Function that has to validate, whether keyword is present and also have to capture
#which laptops are in Active state and which aren't..
def check_state():
success = True
msg_status = get_msg_from_kafka(args) # This will return a JSON output as posted below.
if bool(msg_status) == False:
return False
for val in msg_status['Brand']:
if val['name'] in lap_list and val['state'] != "Active":
success = False
print(f"Status of {val['name']}: {val['Brand']}")
return success
#Is it possible to check how long the validation took, i.e. to check the laptops are in active state.
def status_check():
stop_time = START_TIME + timedelta(minutes=TIMEOUT)
while True:
success = check_state()
if success:
print("All the Laptops are in Active State..")
break
current_time = datetime.now()
if current_time > stop_time:
print("Exceeded the timeout ...")
break
if __name__ == '__main__':
status_check()
msg_status
looks like below (Message format from Kafka)
msg_status = { "case": "2nmi",
"id": "6.00c",
"key": "subN",
"Brand":[
{
"state": "Active",
"name": "Apple",
"date": "2021-01-20T08:35:33.382532",
"Loc": "GA",
},
{
"state": "Disabled",
"name": "HP",
"date": "2018-01-09T08:25:90.382",
"Loc": "TX",
},
{
"state": "Active",
"name": "Acer",
"date": "2022-01-2T8:35:03.5432",
"Loc": "IO"
},
{
"state": "Booted",
"name": "Toshiba",
"date": "2023-09-29T9:5:03.3298",
"Loc": "TX"
}
],
"DHL_ID":"a3288ec45c82"
}