2017-11-16 00:55:49 +01:00
|
|
|
import datetime
|
|
|
|
import logging
|
|
|
|
import time
|
2017-03-03 19:01:52 +01:00
|
|
|
from typing import Any, Dict
|
2016-06-04 16:52:18 +02:00
|
|
|
|
2016-11-03 10:22:19 +01:00
|
|
|
from django.core.management.base import BaseCommand, CommandParser
|
2017-11-16 00:55:49 +01:00
|
|
|
|
2013-07-29 23:03:31 +02:00
|
|
|
from zerver.lib.timestamp import timestamp_to_datetime
|
2017-11-16 00:55:49 +01:00
|
|
|
from zerver.models import Message, Recipient
|
2013-01-29 00:34:32 +01:00
|
|
|
|
2020-01-14 21:59:46 +01:00
|
|
|
|
2017-11-05 06:54:00 +01:00
|
|
|
def compute_stats(log_level: int) -> None:
|
2013-01-29 00:34:32 +01:00
|
|
|
logger = logging.getLogger()
|
|
|
|
logger.setLevel(log_level)
|
|
|
|
|
|
|
|
one_week_ago = timestamp_to_datetime(time.time()) - datetime.timedelta(weeks=1)
|
2017-03-04 09:19:37 +01:00
|
|
|
mit_query = Message.objects.filter(sender__realm__string_id="zephyr",
|
2013-01-29 00:34:32 +01:00
|
|
|
recipient__type=Recipient.STREAM,
|
2019-08-28 02:43:19 +02:00
|
|
|
date_sent__gt=one_week_ago)
|
2013-01-29 00:34:32 +01:00
|
|
|
for bot_sender_start in ["imap.", "rcmd.", "sys."]:
|
2013-03-28 20:43:34 +01:00
|
|
|
mit_query = mit_query.exclude(sender__email__startswith=(bot_sender_start))
|
2013-01-29 00:34:32 +01:00
|
|
|
# Filtering for "/" covers tabbott/extra@ and all the daemon/foo bots.
|
2013-03-28 20:43:34 +01:00
|
|
|
mit_query = mit_query.exclude(sender__email__contains=("/"))
|
|
|
|
mit_query = mit_query.exclude(sender__email__contains=("aim.com"))
|
2013-01-29 00:34:32 +01:00
|
|
|
mit_query = mit_query.exclude(
|
2013-03-28 20:43:34 +01:00
|
|
|
sender__email__in=["rss@mit.edu", "bash@mit.edu", "apache@mit.edu",
|
|
|
|
"bitcoin@mit.edu", "lp@mit.edu", "clocks@mit.edu",
|
|
|
|
"root@mit.edu", "nagios@mit.edu",
|
|
|
|
"www-data|local-realm@mit.edu"])
|
python: Convert assignment type annotations to Python 3.6 style.
This commit was split by tabbott; this piece covers the vast majority
of files in Zulip, but excludes scripts/, tools/, and puppet/ to help
ensure we at least show the right error messages for Xenial systems.
We can likely further refine the remaining pieces with some testing.
Generated by com2ann, with whitespace fixes and various manual fixes
for runtime issues:
- invoiced_through: Optional[LicenseLedger] = models.ForeignKey(
+ invoiced_through: Optional["LicenseLedger"] = models.ForeignKey(
-_apns_client: Optional[APNsClient] = None
+_apns_client: Optional["APNsClient"] = None
- notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- signup_notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ signup_notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- author: Optional[UserProfile] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
+ author: Optional["UserProfile"] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
- bot_owner: Optional[UserProfile] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
+ bot_owner: Optional["UserProfile"] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
- default_sending_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
- default_events_register_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_sending_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_events_register_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
-descriptors_by_handler_id: Dict[int, ClientDescriptor] = {}
+descriptors_by_handler_id: Dict[int, "ClientDescriptor"] = {}
-worker_classes: Dict[str, Type[QueueProcessingWorker]] = {}
-queues: Dict[str, Dict[str, Type[QueueProcessingWorker]]] = {}
+worker_classes: Dict[str, Type["QueueProcessingWorker"]] = {}
+queues: Dict[str, Dict[str, Type["QueueProcessingWorker"]]] = {}
-AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional[LDAPSearch] = None
+AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional["LDAPSearch"] = None
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-22 01:09:50 +02:00
|
|
|
user_counts: Dict[str, Dict[str, int]] = {}
|
2013-03-28 20:43:34 +01:00
|
|
|
for m in mit_query.select_related("sending_client", "sender"):
|
|
|
|
email = m.sender.email
|
2013-01-29 00:34:32 +01:00
|
|
|
user_counts.setdefault(email, {})
|
|
|
|
user_counts[email].setdefault(m.sending_client.name, 0)
|
|
|
|
user_counts[email][m.sending_client.name] += 1
|
|
|
|
|
python: Convert assignment type annotations to Python 3.6 style.
This commit was split by tabbott; this piece covers the vast majority
of files in Zulip, but excludes scripts/, tools/, and puppet/ to help
ensure we at least show the right error messages for Xenial systems.
We can likely further refine the remaining pieces with some testing.
Generated by com2ann, with whitespace fixes and various manual fixes
for runtime issues:
- invoiced_through: Optional[LicenseLedger] = models.ForeignKey(
+ invoiced_through: Optional["LicenseLedger"] = models.ForeignKey(
-_apns_client: Optional[APNsClient] = None
+_apns_client: Optional["APNsClient"] = None
- notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- signup_notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ signup_notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- author: Optional[UserProfile] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
+ author: Optional["UserProfile"] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
- bot_owner: Optional[UserProfile] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
+ bot_owner: Optional["UserProfile"] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
- default_sending_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
- default_events_register_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_sending_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_events_register_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
-descriptors_by_handler_id: Dict[int, ClientDescriptor] = {}
+descriptors_by_handler_id: Dict[int, "ClientDescriptor"] = {}
-worker_classes: Dict[str, Type[QueueProcessingWorker]] = {}
-queues: Dict[str, Dict[str, Type[QueueProcessingWorker]]] = {}
+worker_classes: Dict[str, Type["QueueProcessingWorker"]] = {}
+queues: Dict[str, Dict[str, Type["QueueProcessingWorker"]]] = {}
-AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional[LDAPSearch] = None
+AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional["LDAPSearch"] = None
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-22 01:09:50 +02:00
|
|
|
total_counts: Dict[str, int] = {}
|
|
|
|
total_user_counts: Dict[str, int] = {}
|
2013-01-29 00:34:32 +01:00
|
|
|
for email, counts in user_counts.items():
|
|
|
|
total_user_counts.setdefault(email, 0)
|
|
|
|
for client_name, count in counts.items():
|
|
|
|
total_counts.setdefault(client_name, 0)
|
|
|
|
total_counts[client_name] += count
|
|
|
|
total_user_counts[email] += count
|
|
|
|
|
2020-05-02 08:44:14 +02:00
|
|
|
logging.debug("%40s | %10s | %s", "User", "Messages", "Percentage Zulip")
|
python: Convert assignment type annotations to Python 3.6 style.
This commit was split by tabbott; this piece covers the vast majority
of files in Zulip, but excludes scripts/, tools/, and puppet/ to help
ensure we at least show the right error messages for Xenial systems.
We can likely further refine the remaining pieces with some testing.
Generated by com2ann, with whitespace fixes and various manual fixes
for runtime issues:
- invoiced_through: Optional[LicenseLedger] = models.ForeignKey(
+ invoiced_through: Optional["LicenseLedger"] = models.ForeignKey(
-_apns_client: Optional[APNsClient] = None
+_apns_client: Optional["APNsClient"] = None
- notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- signup_notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ signup_notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- author: Optional[UserProfile] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
+ author: Optional["UserProfile"] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
- bot_owner: Optional[UserProfile] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
+ bot_owner: Optional["UserProfile"] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
- default_sending_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
- default_events_register_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_sending_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_events_register_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
-descriptors_by_handler_id: Dict[int, ClientDescriptor] = {}
+descriptors_by_handler_id: Dict[int, "ClientDescriptor"] = {}
-worker_classes: Dict[str, Type[QueueProcessingWorker]] = {}
-queues: Dict[str, Dict[str, Type[QueueProcessingWorker]]] = {}
+worker_classes: Dict[str, Type["QueueProcessingWorker"]] = {}
+queues: Dict[str, Dict[str, Type["QueueProcessingWorker"]]] = {}
-AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional[LDAPSearch] = None
+AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional["LDAPSearch"] = None
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-22 01:09:50 +02:00
|
|
|
top_percents: Dict[int, float] = {}
|
2013-01-29 00:34:32 +01:00
|
|
|
for size in [10, 25, 50, 100, 200, len(total_user_counts.keys())]:
|
2016-01-26 04:08:05 +01:00
|
|
|
top_percents[size] = 0.0
|
2013-01-29 00:34:32 +01:00
|
|
|
for i, email in enumerate(sorted(total_user_counts.keys(),
|
|
|
|
key=lambda x: -total_user_counts[x])):
|
2013-08-06 22:20:02 +02:00
|
|
|
percent_zulip = round(100 - (user_counts[email].get("zephyr_mirror", 0)) * 100. /
|
2016-11-30 14:17:35 +01:00
|
|
|
total_user_counts[email], 1)
|
2013-01-29 00:34:32 +01:00
|
|
|
for size in top_percents.keys():
|
|
|
|
top_percents.setdefault(size, 0)
|
|
|
|
if i < size:
|
2013-08-06 22:20:02 +02:00
|
|
|
top_percents[size] += (percent_zulip * 1.0 / size)
|
2013-01-29 00:34:32 +01:00
|
|
|
|
2020-05-02 08:44:14 +02:00
|
|
|
logging.debug("%40s | %10s | %s%%", email, total_user_counts[email],
|
|
|
|
percent_zulip)
|
2013-01-29 00:34:32 +01:00
|
|
|
|
|
|
|
logging.info("")
|
|
|
|
for size in sorted(top_percents.keys()):
|
2020-05-02 08:44:14 +02:00
|
|
|
logging.info("Top %6s | %s%%", size, round(top_percents[size], 1))
|
2013-01-29 00:34:32 +01:00
|
|
|
|
|
|
|
grand_total = sum(total_counts.values())
|
2015-11-01 17:11:06 +01:00
|
|
|
print(grand_total)
|
2020-05-02 08:44:14 +02:00
|
|
|
logging.info("%15s | %s", "Client", "Percentage")
|
2013-01-29 00:34:32 +01:00
|
|
|
for client in total_counts.keys():
|
2020-05-02 08:44:14 +02:00
|
|
|
logging.info("%15s | %s%%", client, round(100. * total_counts[client] / grand_total, 1))
|
2013-01-29 00:34:32 +01:00
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
help = "Compute statistics on MIT Zephyr usage."
|
|
|
|
|
2017-11-05 06:54:00 +01:00
|
|
|
def add_arguments(self, parser: CommandParser) -> None:
|
2016-11-03 10:22:19 +01:00
|
|
|
parser.add_argument('--verbose', default=False, action='store_true')
|
|
|
|
|
2017-11-05 06:54:00 +01:00
|
|
|
def handle(self, *args: Any, **options: Any) -> None:
|
2013-01-29 00:34:32 +01:00
|
|
|
level = logging.INFO
|
|
|
|
if options["verbose"]:
|
|
|
|
level = logging.DEBUG
|
|
|
|
compute_stats(level)
|