Skip to content
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

Rework info command for whole project #497

Merged
merged 1 commit into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 109 additions & 5 deletions blossom/api/slack/commands/info.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from datetime import timedelta
from datetime import datetime, timedelta
from typing import Dict

from django.utils import timezone

from blossom.api.models import TranscriptionCheck
from blossom.api.models import Submission, TranscriptionCheck
from blossom.api.slack import client
from blossom.api.slack.commands.utils import bool_str, format_stats_section, format_time
from blossom.api.slack.utils import dict_to_table, parse_user
Expand All @@ -14,15 +14,17 @@
i18n = translation()


QUEUE_TIMEOUT = timedelta(hours=18)


def info_cmd(channel: str, message: str) -> None:
"""Send info about a user to slack."""
parsed_message = message.split()
if len(parsed_message) == 1:
# they just sent an empty info message, create a summary response
data = Summary().generate_summary()
# they just sent an empty info message, get info about the whole project
client.chat_postMessage(
channel=channel,
text=i18n["slack"]["server_summary"].format("\n".join(dict_to_table(data))),
text=all_info_text(),
)
return

Expand Down Expand Up @@ -126,3 +128,105 @@ def user_debug_info(user: BlossomUser) -> Dict:
"Bot": bot,
"Accepted CoC": accepted_coc,
}


def all_info_text() -> str:
"""Get the info message for all users."""
now = datetime.now(tz=timezone.utc)

one_day_ago = now - timedelta(days=1)
one_week_ago = now - timedelta(weeks=1)
two_weeks_ago = now - timedelta(weeks=2)
one_month_ago = now - timedelta(days=30)
one_year_ago = now - timedelta(days=365)

queue_timeout = now - QUEUE_TIMEOUT

# Submission info

query_all = Submission.objects.filter(removed_from_queue=False)
query_one_day = query_all.filter(create_time__gte=one_day_ago)
query_one_week = query_all.filter(create_time__gte=one_week_ago)
query_one_month = query_all.filter(create_time__gte=one_month_ago)
query_one_year = query_all.filter(create_time__gte=one_year_ago)

query_all_queue = query_all.filter(create_time__gte=queue_timeout, archived=False)

total_all = query_all.count()
total_one_day = query_one_day.count()
total_one_week = query_one_week.count()
total_one_month = query_one_month.count()
total_one_year = query_one_year.count()

queue_all = query_all_queue.count()

submission_info = i18n["slack"]["info"]["submission_info"].format(
total_all=total_all,
queue_all=queue_all,
total_one_day=total_one_day,
total_one_week=total_one_week,
total_one_month=total_one_month,
total_one_year=total_one_year,
)

# Transcription info

transcribed_all = query_all.filter(completed_by__isnull=False).count()
transcribed_percentage_all = transcribed_all / total_all
transcribed_one_day = query_one_day.filter(completed_by__isnull=False).count()
transcribed_percentage_one_day = transcribed_one_day / total_one_day
transcribed_one_week = query_one_week.filter(completed_by__isnull=False).count()
transcribed_percentage_one_week = transcribed_one_week / total_one_week
transcribed_one_month = query_one_month.filter(completed_by__isnull=False).count()
transcribed_percentage_one_month = transcribed_one_month / total_one_month
transcribed_one_year = query_one_year.filter(completed_by__isnull=False).count()
transcribed_percentage_one_year = transcribed_one_year / total_one_year

transcription_info = i18n["slack"]["info"]["transcription_info"].format(
transcribed_all=transcribed_all,
transcribed_percentage_all=transcribed_percentage_all,
transcribed_one_day=transcribed_one_day,
transcribed_percentage_one_day=transcribed_percentage_one_day,
transcribed_one_week=transcribed_one_week,
transcribed_percentage_one_week=transcribed_percentage_one_week,
transcribed_one_month=transcribed_one_month,
transcribed_percentage_one_month=transcribed_percentage_one_month,
transcribed_one_year=transcribed_one_year,
transcribed_percentage_one_year=transcribed_percentage_one_year,
)

# Volunteer info

query_volunteers = BlossomUser.objects.filter(is_bot=False)

volunteers_all = query_volunteers.count()
volunteers_new_one_day = query_volunteers.filter(date_joined__gte=one_day_ago).count()
volunteers_new_one_week = query_volunteers.filter(date_joined__gte=one_week_ago).count()
volunteers_new_one_month = query_volunteers.filter(date_joined__gte=one_month_ago).count()
volunteers_new_one_year = query_volunteers.filter(date_joined__gte=one_year_ago).count()

volunteers_active_two_weeks = (
Submission.objects.filter(
completed_by__isnull=False,
complete_time__gte=two_weeks_ago,
)
.values("completed_by")
.distinct()
.count()
)

volunteer_info = i18n["slack"]["info"]["volunteer_info"].format(
volunteers_all=volunteers_all,
volunteers_new_one_day=volunteers_new_one_day,
volunteers_new_one_week=volunteers_new_one_week,
volunteers_new_one_month=volunteers_new_one_month,
volunteers_new_one_year=volunteers_new_one_year,
volunteers_active_two_weeks=volunteers_active_two_weeks,
)

# Putting it all together
return i18n["slack"]["info"]["message"].format(
submission_info=submission_info,
transcription_info=transcription_info,
volunteer_info=volunteer_info,
)
29 changes: 29 additions & 0 deletions blossom/strings/en_US.toml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,35 @@ no_warnings="u/{username} does not have a warning yet!"
[slack.submissions]
no_submissions="Sorry, no submissions were returned for *{username}* in the date range of *{start}* to *{end}*."

[slack.info]
message="""Info about *everything*:

{submission_info}

{transcription_info}

{volunteer_info}"""
submission_info="""*Submissions*:
- Total: {total_all:,g}
- Queue: {queue_all:,g}
- Last day: {total_one_day:,g}
- Last week: {total_one_week:,g}
- Last month: {total_one_month:,g}
- Last year: {total_one_year:,g}"""
transcription_info="""*Transcriptions*:
- Total: {transcribed_all:,g} ({transcribed_percentage_all:.2%} of submissions)
- Last day: {transcribed_one_day:,g} ({transcribed_percentage_one_day:.2%} of submissions)
- Last week: {transcribed_one_week:,g} ({transcribed_percentage_one_week:.2%} of submissions)
- Last month: {transcribed_one_month:,g} ({transcribed_percentage_one_month:.2%} of submissions)
- Last year: {transcribed_one_year:,g} ({transcribed_percentage_one_year:.2%} of submissions)"""
volunteer_info="""*Volunteers*:
- Total: {volunteers_all:,g}
- New (last day): {volunteers_new_one_day:,g}
- New (last week): {volunteers_new_one_week:,g}
- New (last month): {volunteers_new_one_month:,g}
- New (last year): {volunteers_new_one_year:,g}
- Active (last two weeks): {volunteers_active_two_weeks:,g}"""

[slack.subinfo]
message="""Info about *<https://reddit.com/r/{subreddit}|r/{subreddit}>*:

Expand Down