Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
*Web Log Sessions solution in Clear category for Web Log Sessions by JimmyCarlos
import datetime
class Session:
def __init__(self,timestamp,username,site):
self.username = username
self.site = site
self.first_time = timestamp
self.last_time = timestamp
self.quantity_of_requests = 1
def __repr__(self):
return "{};;{};;{};;{}".format(self.username,self.site,self.duration,self.quantity_of_requests)
@property
def duration(self): return int((self.last_time-self.first_time).total_seconds())+1
def add_log(self,new_timestamp):
self.last_time = new_timestamp
self.quantity_of_requests += 1
def find_second_level_domain(URL):
if "://" in URL: URL = URL.split("://")[1]
if "/" in URL: URL = URL.split("/")[0]
if len(URL.split(".")) == 3: URL = ".".join(URL.split(".")[1:])
return URL
def checkio(log_text):
sessions = []
for log in log_text.split():
timestamp_str,username,URL = log.lower().split(";;")
timestamp = datetime.datetime(*[int(x) for x in timestamp_str.split("-")])
site = find_second_level_domain(URL)
for session in sessions:
if username != session.username: continue
if site != session.site: continue
if timestamp - session.last_time > datetime.timedelta(minutes=30): continue
session.add_log(timestamp)
break
else:
sessions.append(Session(timestamp,username,site))
sessions.sort(key=lambda x:(x.username,x.site,x.duration,x.quantity_of_requests))
return "\n".join(str(x) for x in sessions)
June 15, 2019