def parse_log_file(resp: requests.Response) -> int: """ Parse downloaded log file from requests' response, return inserted objects count. Raises exception if response's status code is not 200 or similar. """ if not (200 <= resp.status_code < 300): raise FileNotFound("Can't download file") regex = '([(\d\.)]+) - - \[(.*?)\] \"(.*?)\" (\d+) (\d+) \"(.*?)\" \"(.*?)\"' log_file_iterator = resp.iter_lines() log_entries = [] for line in log_file_iterator: line = line.decode() match = re.match(regex, line) if not match or not line: # Skip empty lines, or lines without necessary info. continue ip_address, date, req, status, size, url, agent = re.match( regex, line).groups() date = datetime.strptime(date, "%d/%b/%Y:%H:%M:%S %z") log_entries.append( LogEntry(ip=ip_address, date=date, http_method=req.split(' ')[0], status_code=int(status), response_size=int(size), request_uri=url)) LogEntry.objects.bulk_create(log_entries, ignore_conflicts=True) return LogEntry.objects.count()
async def get_logs_file(request, document): """Returns the html rendered log entry""" if document is None: abort(404) log_entry = LogEntry(app, document) return log_entry.render_html()
async def get_raw_logs_file(request, document): """Returns the plain text rendered log entry""" if document is None: abort(404) log_entry = LogEntry(app, document) return log_entry.render_plain_text()
async def getlogsfile(request, key): """Returned the plain text rendered log entry""" log = await app.db.logs.find_one({'key': key}) if log is None: return response.text('Not Found', status=404) else: return response.text(str(LogEntry(log)))
async def get_raw_logs_file(request, key): document = await app.db.logs.find_one({'key': key}) if document is None: abort(404) log_entry = LogEntry(app, document) return log_entry.render_plain_text()
async def get_logs_file(request, document): """Ritorna l'ingresso log renderizzato in HTML""" if document is None: abort(404) log_entry = LogEntry(app, document) return log_entry.render_html()
async def get_raw_logs_file(request, document): """Ritorna l'ingresso log renderizzato in testo""" if document is None: abort(404) log_entry = LogEntry(app, document) return log_entry.render_plain_text()
async def get_logs_file(request, key): """Returns the html rendered log entry""" document = await app.ctx.db.logs.find_one({"key": key}) if document is None: abort(404) log_entry = LogEntry(app, document) return log_entry.render_html()
async def get_raw_logs_file(request, key): """Returns the plain text rendered log entry""" document = await app.ctx.db.logs.find_one({"key": key}) if document is None: raise NotFound log_entry = LogEntry(app, document) return log_entry.render_plain_text()
async def get_logs_file(request, key): """Returned the plain text rendered log entry""" document = await app.db.logs.find_one({'key': key}) if document is None: abort(404) log_entry = LogEntry(app, document) return log_entry.render_html()