pup-py commited on
Commit
14f478a
·
1 Parent(s): 7de90bf

subprocess debug

Browse files
Files changed (1) hide show
  1. fetch.py +10 -8
fetch.py CHANGED
@@ -92,11 +92,9 @@ async def log_request(request: Request, call_next: Any):
92
  "method": request.method,
93
  "headers": str(request.headers),
94
  }
95
- output = json.dumps(
96
- obj=data, default=str, indent=None, separators=(", ", ":"), newline="\n"
97
- )
98
  with open(LOGFILE, "a") as f:
99
- f.write(output)
100
 
101
  response = await call_next(request)
102
  return response
@@ -107,10 +105,14 @@ def get_analytics(n: int = 5):
107
  if n == 0:
108
  cmd = f"tac {LOGFILE.as_posix()}"
109
  else:
110
- cmd = f"tail -{n} {LOGFILE.as_posix()} | tac"
111
- json_lines = subprocess.run(cmd.split(), capture_output=True).stdout
112
- content = json.loads(f"[ {json_lines.replace(b"\n", b",").decode()} ]")
113
- return content
 
 
 
 
114
 
115
 
116
  @app.api_route("/qa", response_class=FileResponse, methods=["GET", "HEAD"])
 
92
  "method": request.method,
93
  "headers": str(request.headers),
94
  }
95
+ output = json.dumps(obj=data, default=str, indent=None, separators=(", ", ":"))
 
 
96
  with open(LOGFILE, "a") as f:
97
+ f.write(output + "\n")
98
 
99
  response = await call_next(request)
100
  return response
 
105
  if n == 0:
106
  cmd = f"tac {LOGFILE.as_posix()}"
107
  else:
108
+ cmd = f"tail {n} {LOGFILE.as_posix()} | tac"
109
+ _subprocess = subprocess.run(cmd.split(), capture_output=True)
110
+ json_lines, stderr = _subprocess.stdout, _subprocess.stderr
111
+ try:
112
+ content = json.loads(f"[ {json_lines.replace(b"\n", b",").decode()} ]")
113
+ return content
114
+ except Exception as e:
115
+ return {"error": str(e), "stderr": stderr, "json_lines": json_lines}
116
 
117
 
118
  @app.api_route("/qa", response_class=FileResponse, methods=["GET", "HEAD"])