-
Notifications
You must be signed in to change notification settings - Fork 58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
frontend: add failed-to-succeeded-stats command #2798
Merged
+156
−0
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
154 changes: 154 additions & 0 deletions
154
frontend/coprs_frontend/commands/failed_to_succeeded_stats.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
""" | ||
Generate failed to succeeded stats | ||
""" | ||
|
||
import os | ||
from datetime import datetime | ||
|
||
import click | ||
import pygal | ||
from coprs import models | ||
|
||
|
||
@click.command() | ||
@click.option("--dest", "-D", required=True, help="Result directory") | ||
def failed_to_succeeded_stats(dest): | ||
""" | ||
Generate failed to succeeded stats | ||
""" | ||
print("Please wait, this will take at least 20 minutes.") | ||
categories = { | ||
"immediately": 0, | ||
"seconds": 0, | ||
"minutes": 0, | ||
"hours": 0, | ||
"days": 0, | ||
"weeks": 0, | ||
} | ||
tuples = failed_to_succeeded_tuples() | ||
for failed, succeeded in tuples: | ||
delta = datetime.fromtimestamp(succeeded) \ | ||
- datetime.fromtimestamp(failed) | ||
categories[delta_to_category(delta)] += 1 | ||
|
||
os.makedirs(dest, exist_ok=True) | ||
path = os.path.join(dest, "failed-to-succeeded-stats.svg") | ||
generate_graph(categories, path) | ||
print("Created: {0}".format(path)) | ||
|
||
|
||
def get_builds(): | ||
""" | ||
Return list of all builds | ||
""" | ||
query = ( | ||
models.Build.query | ||
.join(models.Package) | ||
.join(models.Copr) | ||
.order_by(models.Build.id) | ||
) | ||
|
||
# Only recent builds | ||
# start = datetime(2022, 1, 1).timestamp() | ||
# end = datetime(2023, 1, 1).timestamp() | ||
# query = (query | ||
# .filter(models.Build.submitted_on >= start) | ||
# .filter(models.Build.submitted_on <= end)) | ||
|
||
# Packit user | ||
# query = query.filter(models.Copr.user_id==5576) | ||
|
||
# For faster development | ||
# query = query.limit(1000) | ||
|
||
return query.all() | ||
|
||
|
||
def builds_per_package(): | ||
""" | ||
Return a `dict` where keys are package IDs and values are lists | ||
of all their builds. | ||
""" | ||
builds = get_builds() | ||
result = {} | ||
for build in builds: | ||
result.setdefault(build.package_id, []) | ||
result[build.package_id].append(build) | ||
return result | ||
|
||
|
||
def failed_to_succeeded_tuples(): | ||
""" | ||
Return a list of tuples. First value of each tuple is when the package | ||
failed, and the second value is when it succeeded. | ||
""" | ||
tuples = [] | ||
for builds in builds_per_package().values(): | ||
if len(builds) <= 1: | ||
# This package has only one build | ||
# Not dealing with this now. | ||
continue | ||
|
||
failed = None | ||
succeeded = None | ||
|
||
for build in builds: | ||
if not build.ended_on: | ||
continue | ||
|
||
if build.state == "failed" and not failed: | ||
failed = build | ||
|
||
elif build.state == "succeeded" and failed: | ||
succeeded = build | ||
|
||
if failed and succeeded: | ||
assert failed.id < succeeded.id | ||
tuples.append((failed.ended_on, succeeded.ended_on)) | ||
failed = None | ||
succeeded = None | ||
return tuples | ||
|
||
|
||
def delta_to_category(delta): | ||
""" | ||
Convert timedelta into a custom time category | ||
""" | ||
seconds = delta.total_seconds() | ||
if seconds < 0: | ||
return "immediately" | ||
if seconds < 60: | ||
return "seconds" | ||
if seconds < 60 * 60: | ||
return "minutes" | ||
if seconds < 60 * 60 * 24: | ||
return "hours" | ||
if seconds < 60 * 60 * 24 * 7: | ||
return "days" | ||
return "weeks" | ||
|
||
|
||
def generate_graph(data, path): | ||
""" | ||
Generate graph from the data | ||
""" | ||
title = "How long before devs submit a successfull package after a failure?" | ||
chart = pygal.Bar( | ||
title=title, | ||
print_values=True, | ||
legend_at_bottom=True, | ||
) | ||
for key, value in data.items(): | ||
label = label_for_group(key) | ||
chart.add(label, value) | ||
chart.render_to_file(path) | ||
return path | ||
|
||
|
||
def label_for_group(key): | ||
""" | ||
User-friendly labels for the graph | ||
""" | ||
if key == "immediately": | ||
return "Before it finished" | ||
return key.capitalize() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
with this, are the builds filtered via the Copr project owner? (or via the submitter of the build?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
afaik the
user_id
in Copr model is only project owner since this model represents the project as a whole, not the individual builds. To filter builds according to submitter:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exactly as @nikromen is saying.