Skip to content

Commit 1aae619

Browse files
add export feature
1 parent 30e5b5b commit 1aae619

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "PyWSGIRef"
3-
version = "1.1.15"
3+
version = "1.1.16"
44
authors = [
55
{name="Leander Kafemann", email="[email protected]" },
66
]

src/PyWSGIRef/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def about():
1818
"""
1919
Returns information about your release and other projects by Leander Kafemann
2020
"""
21-
return {"Version": (1, 1, 15), "Author": "Leander Kafemann", "date": "05.09.2025",\
21+
return {"Version": (1, 1, 16), "Author": "Leander Kafemann", "date": "05.09.2025",\
2222
"recommend": ("pyimager"), "feedbackTo": "[email protected]"}
2323

2424
SCHABLONEN = TemplateDict()
@@ -82,7 +82,8 @@ def simpleApplication(environ, start_response) -> list:
8282
("Content-Length", str(len(content))),
8383
('Access-Control-Allow-Origin', '*')]
8484
start_response(status, headers)
85-
STATS.stopPerfTime(perfTime)
85+
if getStats:
86+
STATS.stopPerfTime(perfTime)
8687
if not vercelPythonHosting:
8788
return [content.encode("utf-8")]
8889
return simpleApplication

src/PyWSGIRef/stats.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,37 @@ def startPerfTime(self, title: str = "perfTime") -> PerformanceTime:
2727
return PerformanceTime(title)
2828
def stopPerfTime(self, perfTime: PerformanceTime):
2929
perfTime.stop()
30-
self.performanceTimes.append(perfTime)
30+
self.performanceTimes.append(perfTime)
31+
32+
def export_stats(self, filename: str = None) -> str:
33+
"""
34+
Exports all current stats as a formatted string.
35+
If filename is given, it will also save the stats to that file.
36+
"""
37+
lines = []
38+
lines.append("PyWSGIRef Statistic-Export")
39+
lines.append("=" * 30)
40+
lines.append(f"access counter: {self.count.count}")
41+
lines.append("")
42+
lines.append("Performance times:")
43+
if not self.performanceTimes:
44+
lines.append(" (no entrys)")
45+
else:
46+
for i, perf in enumerate(self.performanceTimes, 1):
47+
start = getattr(perf, "start", None)
48+
end = getattr(perf, "end", None)
49+
title = getattr(perf, "title", "unnamed")
50+
if start and end:
51+
duration = (end - start).total_seconds()
52+
lines.append(f" {i}. {title}: {start} - {end} (duration: {duration:.3f}s)")
53+
elif start:
54+
lines.append(f" {i}. {title}: started at {start} (not stopped until now)")
55+
else:
56+
lines.append(f" {i}. {title}: (no time data)")
57+
result = "\n".join(lines)
58+
if filename:
59+
if not filename.endswith(".pywsgirefstats"):
60+
filename += ".pywsgirefstats"
61+
with open(filename, "w", encoding="utf-8") as f:
62+
f.write(result)
63+
return result

0 commit comments

Comments
 (0)