Commit Graph

107 Commits

Author SHA1 Message Date
Steve Howell 03c856ce10 Add create-stream management command.
The current version should only be used for testing; for example,
if you want to create a bunch of streams for stress testing, you
can run this in a loop.

(imported from commit ec51a431fb9679fc18379e4c6ecdba66bc75a395)
2013-10-19 09:28:59 -04:00
Kevin Mehall 7b8dea3d54 Add all_public_streams parameter to register API.
It makes the event queue return all messages on public streams, rather
than only the user's subscriptions. It's meant for use with chat bots.

(imported from commit 12d7e9e9586369efa7e7ff9eb060f25360327f71)
2013-10-18 14:55:32 -04:00
Jessica McKellar 6dbf7613a1 Supply a name when creating realms.
For our populate_db bulk creation, just use the domain.

(imported from commit 4fb756f6dfa2d8f90e55822e27891e84168d5d1c)
2013-10-17 13:47:51 -04:00
Steve Howell 8cc82c6cbe Optimize /json/update_message_flags.
I added filter() statements to do_update_message_flags().

Here is some context:

Steve Howell: Case 1, have AND clause to reduce work for DB.

humbug=> update zerver_usermessage set flags = (flags & ~1) where id > 9000;
UPDATE 382
humbug=> select count(*) from zerver_usermessage where (flags & 1) = 0;
 count
-------
   382
(1 row)

humbug=> explain analyze update zerver_usermessage set flags = (flags | 1) where (flags & 1) = 0;
                                                       QUERY PLAN
------------------------------------------------------------------------------------------------------------------------
 Update on zerver_usermessage  (cost=0.00..266.85 rows=47 width=27) (actual time=5.727..5.727 rows=0 loops=1)
   ->  Seq Scan on zerver_usermessage  (cost=0.00..266.85 rows=47 width=27) (actual time=0.045..2.751 rows=382 loops=1)
         Filter: ((flags & 1::bigint) = 0)
         Rows Removed by Filter: 9000
 Total runtime: 5.759 ms
(5 rows)

humbug=> select count(*) from zerver_usermessage where (flags & 1) = 0;
 count
-------
     0
(1 row)
Leo Franchi: Sounds reasonable, but I know way less than zev about DBs so I'll defer to his judgement :)

Steve Howell: Case 2, how the code works now:

humbug=> update zerver_usermessage set flags = (flags & ~1) where id > 9000;
UPDATE 382
humbug=> select count(*) from zerver_usermessage where (flags & 1) = 0;
 count
-------
   382
(1 row)

humbug=> explain analyze update zerver_usermessage set flags = (flags | 1);
                                                        QUERY PLAN
---------------------------------------------------------------------------------------------------------------------------
 Update on zerver_usermessage  (cost=0.00..243.28 rows=9382 width=27) (actual time=362.075..362.075 rows=0 loops=1)
   ->  Seq Scan on zerver_usermessage  (cost=0.00..243.28 rows=9382 width=27) (actual time=0.008..6.138 rows=9382 loops=1)
 Total runtime: 362.105 ms
(3 rows)

humbug=> select count(*) from zerver_usermessage where (flags & 1) = 0;
 count
-------
     0
(1 row)
Steve Howell: In both trials, we set it up so that only 382 of 9382 rows need to be updated. The first trial runs about 63x as fast. The second trial, if my theory is correct, is doing 24x as many writes as it needs. Both trials are reading all 9382 rows.

Steve Howell: The expense of the update statement seems to be proportional to the number of rows you "update", not the number of rows that you actually change.

Steve Howell: For now I created #1869.

Zev Benjamin: That sounds like a reasonable explanation. The disk IO can be expensive

(imported from commit d9090daee1f81cad76c430de0956f9bd504da075)
2013-10-15 11:30:13 -04:00
acrefoot d2b8fe6f57 fix earlier 'linter workaround'
(imported from commit 4ac9f1e57a241f95f53fa65d82c376a61de2c28c)
2013-10-11 11:54:05 -04:00
acrefoot 98897fe5e3 Queue the day 1 and day 2 Zulip followup emails
Handled by the queue processor for signups. Added a management command
that accomplishes the same task, in case it's needed for manually added users,
or in case we goof and need to remove queued emails for a given user.

This addresses Trac #1807

(imported from commit 6727b82a07fa6a3ea3d827860c9e60fd0602297a)
2013-10-10 19:32:21 -04:00
acrefoot f8662c16f7 Add Mandrill decorators, credentials, actions
You can queue email for future delivery or send immediately via mandrill now

(imported from commit e6b6d11a2d94fcdeaffab80793e7ba31955b9031)
2013-10-10 19:32:21 -04:00
acrefoot 851a6e391e change variable name to get linter to stop complaining
(imported from commit b61824c16b1e7ad88375eeebdd945827dfc7fd3c)
2013-10-10 19:24:50 -04:00
Tim Abbott 1a7ddd9ea3 Fix UserActivityInterval overlap bug.
The previous version was incorrect in the event that log_time >
last.start + 15.

(imported from commit 56aa619e8c86db1796cf936c2be5224a25ca2e1f)
2013-10-08 17:21:59 -04:00
Jessica McKellar 3b7143d3ec Linkify narrows in missed message emails.
This will hopefully incentivize people to click one and get back into
the app.

We'll also need this for digest emails.

(imported from commit 57191c3fcca3b12df93a81e4692bb7eb8ccc83b2)
2013-10-08 10:46:52 -04:00
Leo Franchi 72c7c5b836 [schema] Give new realms a default notifications stream, and send signups/creations to it
(imported from commit 19ad761f7d9eabb865ac3526bf66dff8b7ed0224)
2013-10-07 15:46:13 -04:00
Leo Franchi 7da3084465 Use name attribute in dictionary not property lookup
(imported from commit b2adae82f31b57d4e9e049225a662851b4364789)
2013-10-03 16:59:50 -04:00
Tim Abbott 4ae2ae5005 gather_subscriptions: Avoid fetching full stream objects.
(imported from commit 3052dd9c1df050e217949d36259416bc38469674)
2013-10-02 16:15:25 -04:00
Tim Abbott 356d33cfa0 gather_subscriptions: Use .values() for fetching all the user's subs.
(imported from commit 1f27841527bb5f49fab347071e40d289151fa2c5)
2013-10-02 16:15:25 -04:00
Tim Abbott 1eeb808a79 bulk_get_subscriber_emails: Use .values() to substantially improve performance.
(imported from commit b37738d670930cdf65687a8526d8a8f47835b42b)
2013-10-02 16:15:24 -04:00
Tim Abbott 382dd64ce9 gather_subscriptions: Tighten the query for Subscription objects.
(imported from commit a7caed32e52f68c7d6e33e15a7954df4fe90d797)
2013-10-02 16:15:24 -04:00
Tim Abbott b567630b5e Fix UserActivityInterval handling when events arrive out of order.
(imported from commit fa1cfbd67ac4c6c2864fb19db8a5221f90d028e7)
2013-10-01 17:41:30 -04:00
Tim Abbott e9ee05b2b2 gather_subscriptions: Avoid extra queries fetching realm objects.
(imported from commit 591083cfaff454b8ce48aacb5da7913b8fe0a38f)
2013-09-30 17:14:05 -04:00
Tim Abbott 5e420dc5c3 gather_subscriptions: Fetch subscribers using a bulk query.
(imported from commit 13c085d0ec55779279d31d3e301311ef1c9fd2c4)
2013-09-30 17:14:05 -04:00
Tim Abbott 3a3954a8c4 Extract validation for whether the user can access subscribers.
(imported from commit 4bff4c26f98a9c2ebdd11d931c977aae8e4ebffe)
2013-09-30 17:14:05 -04:00
Tim Abbott 4de0ad29b3 Remove confusing realm optional argument to get_subscribers.
The realm should always be the realm of the stream, and we should
always pass in a stream rather than sometimes passing in a stream name
and other times passing in a stream.

(imported from commit a098d6ed3db218a37c1b6b7c956e847c316c2d13)
2013-09-30 17:14:05 -04:00
Tim Abbott 543ba03ff6 gather_subscriptions: Fix confusing overwriting of local variable stream.
(imported from commit cfe369869de5f13579ff0e0b91ec0a735e2fe81c)
2013-09-30 17:14:05 -04:00
Steve Howell 49232d9173 Preserve muting preferences across reloads.
We have been persisting muting preferences on the back end for
a while, but we haven't been adding them to page_params for the
client to have at reload/startup time.

(imported from commit d9ca68aa0e4d22bfb0e6ce67fc0bc63981175c8b)
2013-09-28 15:48:37 -04:00
Leo Franchi 0fe3da55e3 Reduce number of queries done by bulk_add_subscriptions
We now bulk-fetch subscription information once from the database
and use it throughout bulk_add_subscriptions in order to avoid
hitting the db O(streams) times.

On my machine this shaved the accounts_register API call from making
66 queries to making 37 queries.

(imported from commit 5dd5ad3f50b2a6edf85b5f1d55ebd697a1c60647)
2013-09-27 10:26:11 -04:00
Leo Franchi 1e6b83b6c8 Fetch stream.realm information when fetching default streams
(imported from commit 08eb32e6ecda75b17d6532b0421c371de2327027)
2013-09-27 10:25:23 -04:00
Steve Howell 93347be4af Send less presence info to Tornado in do_send_messages().
When we send a message, we send some presence information to Tornado
to help it figure out how to generate emails for idle recipients of
a message.  This change limits the presence info to being the
intersection of present users and recipients of the message.  It is
just an internal optimization to avoid queueing up unneeded data.

The history behind this feature is that I implemented it a while
back, but I think I made a rebase mistake that sent all the presence
data over the wire, despite having code to filter on recipients.
It was mostly harmless, just leading to some inefficiency which is
now fixed.

(imported from commit 7c8e97705afb299c67b99053909e952fbc823551)
2013-09-25 14:50:40 -04:00
Zev Benjamin 90ee96d456 Split out presence and activity interval events from user_activity queue
(imported from commit 3c22448491867a9e72659629d5d0faaf12b85b2c)
2013-09-24 20:44:15 -04:00
Jessica McKellar f058e9cac5 Add backend support for newer realms receiving stream notifications by default.
(imported from commit ea8ee65071423a94a462571aa30013969d9f3f7a)
2013-09-24 16:19:53 -04:00
Steve Howell 50641c3337 [perf] Reduce the number of DB queries in send_message.
For a 4-person stream, we were hitting the DB 8 times, and 4 of
those queries were to lazily get user.email for the 4 recipients
due to upstream code using only().  I added user_profile__email
to the only() call.

I believe this regression started 9/18, and after pushing this
to prod, we would should look at this graph:

https://stats1.zulip.net/graphs/8274cd84588

(imported from commit 70629cb69fe5955c674ba76482609dfe78e5faaf)
2013-09-24 16:19:52 -04:00
Waseem Daher 7afd3ecc81 Custom invite text for MIT invitations.
(imported from commit 21b72861e991159eb7faa74809edb8d01027c6c8)
2013-09-20 12:06:26 -04:00
Steve Howell 6eda9c3314 Rename function to send_pm_if_empty_stream().
This used to have a really long name.

(imported from commit d3027a75f49db428239d432cfaa8821aa733eb04)
2013-09-20 10:19:07 -04:00
Steve Howell a457135a25 Use efficient stream.num_subscribers() for bot PMs.
Use stream.num_subscribers() in check_if_a_bot_is_sending_a_message_to_an_empty_stream().

The num_subscribers() function using Django's count() method, which returns
a single row, vs. len() on an iterator of query rows.

(imported from commit 6157fe248945e9288ee71d8cc39fb6dda4e9a247)
2013-09-20 10:19:07 -04:00
Steve Howell 5ae5e3f881 Disable empty-stream PMs for MIT bots.
(imported from commit a855d612b6ba316fc183200c4f72102831b6179c)
2013-09-20 10:19:07 -04:00
Steve Howell 098720b6d1 Extract check_if_a_bot_is_sending_a_message_to_an_empty_stream().
(imported from commit 3312d70f6b94f4a13a42bbe0320f4bb1062262e7)
2013-09-20 10:19:07 -04:00
Waseem Daher 190390d8ea Allow MIT to invite coworkers.
(imported from commit 273b3abdd3dcc49c01c1a04001450983d4acb000)
2013-09-19 17:14:41 -04:00
Kevin Mehall 6695105bb9 Fix 2499d7b to not break user registration.
Some bots created by us do not have owners. Don't try to send a
message to the nonexistent owner.

(imported from commit ab952eccd7d6c4728e9477a106142214b5c81ca9)
2013-09-19 13:42:20 -04:00
Jessica McKellar 7e733a1b91 Don't rate-limit missed message e-mails.
Instead just rely on the 2-minute delay in the management command to
batch conversations.

We've had people report being confused or thinking the feature was
broken when they didn't get e-mails because of our rate-limiting, so
let's see if this is not too overwhelming.

(imported from commit 706ddb07b906b5c2edea1159c04acc2ee6f06e29)
2013-09-19 11:55:42 -04:00
Steve Howell de0d50d31a [important] Fix recent regression w/maybe_get_subscribers
(imported from commit 8069f729dcb2bfec1c1df8f50a88fe49545a9a87)
2013-09-19 09:37:39 -04:00
Steve Howell 3c69a6dd26 Bulk-send peer_add events in bulk_add_subscriptions().
Send a single peer_add event per stream that has multiple users.

(imported from commit 1027b212557f848d437128a03698ecd54575c3fd)
2013-09-19 08:52:54 -04:00
Steve Howell c8c64984d3 Eliminate redundant peer_add notifications.
Don't send peer_add notifications to users who are already
getting add notifications, because they will already know
about subscribers.

(imported from commit 726b54ae0e30b71440b17d9c51b026872ea96218)
2013-09-19 08:52:54 -04:00
Steve Howell c58d366178 Optimize notifications from do_add_subscription().
Instead of sending one notification per user, bulk them up with
the users parameter.

(imported from commit c296f23599a214a63ef6f09406a9deaea469a7f3)
2013-09-19 08:52:54 -04:00
Steve Howell 8182d60961 Optimize query used by do_add_subscription().
It only grabs the user_profile_id column now.  This leads to a
speedup of about 16x between grabbing large ORM objects vs.
small 1-column dictionaries.

(imported from commit 95150bff3fdcbe250b04f014062224af42a6644f)
2013-09-19 08:52:54 -04:00
Steve Howell e89c6f64bd Extract notify_peers().
Splitting out notify_peers() will give us flexibility for cleaning
up how we notify peers for bulk adds.

(imported from commit e108fa2c432cc1fe54d788c58c82c983e0f2394e)
2013-09-19 08:52:54 -04:00
Steve Howell d24d8e4b36 Optimize showing subscribers on the settings page.
If you expand subscribers on your settings page, you will now see
a query like this in your postgres logs:

SELECT "zerver_userprofile"."email"
FROM "zerver_subscription" INNER JOIN "zerver_recipient" ON ("zerver_subscription"."recipient_id" = "zerver_recipient"."id") INNER JOIN "zerver_userprofile" ON ("zerver_subscription"."user_profile_id" = "zerver_userprofile"."id") WHERE ("zerver_recipient"."type" = 2  AND "zerver_subscription"."active" = true  AND "zerver_recipient"."type_id" = 40  AND "zerver_userprofile"."is_active" = true )

The join's still complicated, but the list of fields is one instead of 40+.

(imported from commit 48de1f888193a4d23fcea52d0b633d134e4a3ff7)
2013-09-19 08:52:53 -04:00
Steve Howell 9cdedf7e6c Optimize adding subscriptions with maybe_get_subscriber_emails().
To get emails, query one field instead of 40-ish fields.

(imported from commit d2dbbe23871df76bc7d431dcd9b19ebd7a58c161)
2013-09-19 08:52:53 -04:00
Steve Howell 986dd8289e Optimize get_subscribers back end.
get_subscribers_backend() now calls the new get_subscriber_emails()
function, which just queries the email field:

  "zerver_userprofile"."email"

...instead of querying about 40 fields that it never uses.

I was able to verify the query slimming by watching my postgres server log.

Also, you can verify that the ORM does roughly 16x less work using values():

>>> def f(): return [sub.user_profile.email for sub in list(Subscription.objects.all().select_related())]
...
>>> def g(): return [row['user_profile__email'] for row in list(Subscription.objects.all().values('user_profile__email'))]
...
>>> def timeit(func): t = time.time(); func(); return time.time() - t
...
>>> timeit(f)
0.045198917388916016
>>> timeit(g)
0.002752065658569336

(imported from commit a69f690a96d076b323fdfc2f4821b0548bdfac7f)
2013-09-19 08:52:53 -04:00
Steve Howell db2c9ff8c2 Extract get_subscribers_query
(imported from commit fea253591093a1f0cf722c1d4bcfb0df4f881984)
2013-09-19 08:52:53 -04:00
Leo Franchi d88266b41d Warn bot owners when a bot sends a message to a nonexistent stream
(imported from commit 2499d7bd9d176a7ab43f751eb855813d0a479bd4)
2013-09-18 15:54:13 -04:00
Leo Franchi 5441e36167 Bulk update messages in deprecated cache when updating message
(imported from commit 5e68e4eec83c0ed02b9997b0cb3f93ce8f0a7fdd)
2013-09-18 13:27:58 -04:00
Leo Franchi c588c7938c Allow message topic changing to propagate backwards
(imported from commit 470178ef5f7aaf09d4528c88ae6e33f17538fcaf)
2013-09-18 13:27:58 -04:00