Skip to content

Commit

Permalink
helper(scalingo-logs): print the N longest recent queries
Browse files Browse the repository at this point in the history
  • Loading branch information
tristan-gueguen committed Mar 25, 2024
1 parent bfb5b8e commit f97c351
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
6 changes: 6 additions & 0 deletions app/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from app.domain.regulations import compute_regulation_for_user
from app.domain.vehicle import find_vehicle
from app.helpers.oauth.models import ThirdPartyApiKey
from app.helpers.scalingo_logs import get_long_requests
from app.helpers.xml.greco import temp_write_greco_xml
from app.models.controller_control import ControllerControl
from app.models.user import User
Expand Down Expand Up @@ -303,3 +304,8 @@ def load_company_stats():
def temp_command_generate_xm_control(id):
control = ControllerControl.query.get(id)
temp_write_greco_xml(control)


@app.cli.command("log_long_requests", with_appcontext=False)
def log_long_requests():
get_long_requests(nb_results=20, nb_lines=100000)
58 changes: 58 additions & 0 deletions app/helpers/scalingo_logs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import re
import subprocess


def extract_timestamp_from_log(log):
return log[:19]


def extract_endpoint_from_log(log):
endpoint_pattern = r'/graphql "(\w+)"'
endpoint_match = re.search(endpoint_pattern, log)
return endpoint_match.group(1) if endpoint_match else ""


def extract_time_from_log(log):
time_pattern = r"time=(\d+)ms"
time_match = re.search(time_pattern, log)
return int(time_match.group(1)) if time_match else None


def extract_user_from_log(log):
username_pattern = r"user=([\w\s]+) user_id"
userid_pattern = r"user_id=([\d]+)"

username_match = re.search(username_pattern, log)
userid_match = re.search(userid_pattern, log)

username = username_match.group(1) if username_match else ""
userid = userid_match.group(1) if userid_match else ""

return (username, userid)


def get_long_requests(nb_results=20, nb_lines=10000):
cmd = f"scalingo --region osc-fr1 --app mobilic-api logs --lines {nb_lines} | grep '\[web-' | awk 'NF'"

lines = [
l.decode("utf-8")
for l in subprocess.check_output(cmd, shell=True).splitlines()
]

data = []
for line in lines:
time = extract_time_from_log(line)
if time is None:
continue
data.append((time, line))
data.sort(key=lambda l: l[0], reverse=True)
for d in data[:nb_results]:
(ms, text) = d
(username, userid) = extract_user_from_log(text)
ts = extract_timestamp_from_log(text)
endpoint = extract_endpoint_from_log(text)
print(
" - ".join(
[f"{ms} ms", ts, endpoint, f"id={userid}", f"name={username}"]
)
)

0 comments on commit f97c351

Please sign in to comment.