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

fix: online status middleware cache #955

Merged
merged 17 commits into from
May 13, 2019
Merged

fix: online status middleware cache #955

merged 17 commits into from
May 13, 2019

Conversation

MMFernando
Copy link
Contributor

@MMFernando MMFernando commented May 7, 2019

Replaced existing Django Online Status Middleware with the one cloned here . Integrated the code with the project and existing tests.

The code that was there before seemed to be failing to remove users from the online user list, causing an error as the cache was set to hold more than its data size limit of 1 MB.

Documentation was added.

@mrniket
Copy link
Contributor

mrniket commented May 7, 2019

This change is Reviewable

Copy link
Contributor

@mrniket mrniket left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 10 of 93 files at r1.
Reviewable status: 10 of 93 files reviewed, 8 unresolved discussions (waiting on @mariaFernando)


portal/autoconfig.py, line 107 at r1 (raw file):

        "django.contrib.auth.middleware.AuthenticationMiddleware",
        "django.contrib.auth.middleware.SessionAuthenticationMiddleware",
        "portal.middleware.django_online_status.online_status.middleware.OnlineStatusMiddleware",

the folder structure here seems a bit redundant.... portal -> middleware -> .... -> middleware -> ...

as a note, if this changes, you need to make this change in deploy-appengine possibly also


portal/middleware/django_online_status/.hgignore, line 2 at r1 (raw file):

syntax: glob

we don't use mercurial so we don't need this here


portal/middleware/django_online_status/.hgtags, line 1 at r1 (raw file):

c14b815efd477f233cf57c7605c14773ab6f12d5 0_1_0

we don't use mercurial, delete this


portal/middleware/django_online_status/AUTHORS, line 1 at r1 (raw file):

Jakub Zalewski http://zalew.tumblr.com

delete this file


portal/middleware/django_online_status/README, line 1 at r1 (raw file):

========================

delete this


portal/middleware/django_online_status/setup.py, line 1 at r1 (raw file):

#!/usr/bin/env python

this doesn't need to be its own package, delete this


portal/middleware/django_online_status/UNLICENSE, line 1 at r1 (raw file):

This is free and unencumbered software released into the public domain.

delete this


portal/middleware/django_online_status/.hg/00changelog.i, line 0 at r1 (raw file):
delete the .hg folder please

Copy link
Contributor

@mrniket mrniket left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 2 of 93 files at r1.
Reviewable status: 12 of 93 files reviewed, 13 unresolved discussions (waiting on @mariaFernando)

a discussion (no related file):
Can we have a more detailed description of the fix please? The middleware was already there, this PR should describe what was fixed in it



portal/middleware/django_online_status/online_status/middleware.py, line 15 at r1 (raw file):

class OnlineStatusMiddleware(MiddlewareMixin):

to make this a bit more futureproof, can we use the modern API for middleware here please?


portal/middleware/django_online_status/online_status/middleware.py, line 15 at r1 (raw file):

class OnlineStatusMiddleware(MiddlewareMixin):

Can we confirm that the behavioural differences between MIDDLEWARE and MIDDLEWARE_CLASSES do not affect the way this middleware works?


portal/middleware/django_online_status/online_status/models.py, line 0 at r1 (raw file):
afaik middleware do not need a models file, I would remove this


portal/middleware/django_online_status/online_status/status.py, line 96 at r1 (raw file):

def status_for_user(user):
    """Return status for user, duh?"""

Let's replace this with a more useful docstring

Copy link
Contributor

@mrniket mrniket left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 12 of 93 files reviewed, 14 unresolved discussions (waiting on @mariaFernando)

a discussion (no related file):
@dent50cent are your docs changes in this PR?


@MMFernando
Copy link
Contributor Author


portal/middleware/django_online_status/.hgignore, line 2 at r1 (raw file):

Previously, mrniket (Niket Shah) wrote…

we don't use mercurial so we don't need this here

Done.

@MMFernando
Copy link
Contributor Author


portal/middleware/django_online_status/.hgtags, line 1 at r1 (raw file):

Previously, mrniket (Niket Shah) wrote…

we don't use mercurial, delete this

Done.

@MMFernando
Copy link
Contributor Author


portal/middleware/django_online_status/AUTHORS, line 1 at r1 (raw file):

Previously, mrniket (Niket Shah) wrote…

delete this file

Done.

@MMFernando
Copy link
Contributor Author


portal/middleware/django_online_status/README, line 1 at r1 (raw file):

Previously, mrniket (Niket Shah) wrote…

delete this

Done.

@MMFernando
Copy link
Contributor Author


portal/middleware/django_online_status/setup.py, line 1 at r1 (raw file):

Previously, mrniket (Niket Shah) wrote…

this doesn't need to be its own package, delete this

Done.

@MMFernando
Copy link
Contributor Author


portal/middleware/django_online_status/UNLICENSE, line 1 at r1 (raw file):

Previously, mrniket (Niket Shah) wrote…

delete this

Done.

@MMFernando
Copy link
Contributor Author


portal/middleware/django_online_status/online_status/models.py, line at r1 (raw file):

Previously, mrniket (Niket Shah) wrote…

afaik middleware do not need a models file, I would remove this

Done.

@MMFernando
Copy link
Contributor Author


portal/middleware/django_online_status/online_status/status.py, line 96 at r1 (raw file):

Previously, mrniket (Niket Shah) wrote…

Let's replace this with a more useful docstring

Done.

Copy link
Contributor

@mrniket mrniket left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 12 of 94 files reviewed, 14 unresolved discussions (waiting on @mariaFernando and @mrniket)


portal/middleware/django_online_status/.hgignore, line 2 at r1 (raw file):

Previously, mariaFernando (Maria Mafalda Fernando) wrote…

Done.

not done? perhaps you haven't pushed?

Copy link
Contributor

@mrniket mrniket left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 12 of 94 files reviewed, 14 unresolved discussions (waiting on @mariaFernando and @mrniket)


portal/middleware/django_online_status/.hgtags, line 1 at r1 (raw file):

Previously, mariaFernando (Maria Mafalda Fernando) wrote…

Done.

not done?

@MMFernando
Copy link
Contributor Author

a discussion (no related file):

Previously, mrniket (Niket Shah) wrote…

Can we have a more detailed description of the fix please? The middleware was already there, this PR should describe what was fixed in it

Is the description more fitting now, or should I go more in detail?


@MMFernando
Copy link
Contributor Author


portal/autoconfig.py, line 107 at r1 (raw file):

Previously, mrniket (Niket Shah) wrote…

the folder structure here seems a bit redundant.... portal -> middleware -> .... -> middleware -> ...

as a note, if this changes, you need to make this change in deploy-appengine possibly also

The folder structure is now portal/middleware/django_online_status. Should I rename middleware.py, too?

@MMFernando
Copy link
Contributor Author


portal/middleware/django_online_status/.hgignore, line 2 at r1 (raw file):

Previously, mrniket (Niket Shah) wrote…

not done? perhaps you haven't pushed?

I hadn't pushed, but it seems like the deletion didn't get properly pushed..?

@MMFernando
Copy link
Contributor Author


portal/middleware/django_online_status/.hg/00changelog.i, line at r1 (raw file):

Previously, mrniket (Niket Shah) wrote…

delete the .hg folder please

Done.

@MMFernando
Copy link
Contributor Author

a discussion (no related file):

Previously, mrniket (Niket Shah) wrote…

@dent50cent are your docs changes in this PR?

They are


Copy link
Contributor

@mrniket mrniket left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 1 of 2 files at r2.
Reviewable status: 2 of 20 files reviewed, 16 unresolved discussions (waiting on @dent50cent, @mariaFernando, and @mrniket)


docs/online_status_middleware.md, line 9 at r4 (raw file):

There are couple of template tags to access this information to help make use of this functionality.

Also, this middleware contains a couple of view/urls by default, the only one that should exist is the `/test` url as one of the others lists every single user currently online for everyone to see which may pose a security risk, and the other is an example which probably shouldn't exist in a production enviroment, if these are present in your project (or are not ignored) please make sure they don't end up in production.

I think the views and urls won't be there in the end


portal/autoconfig.py, line 107 at r1 (raw file):

Previously, mariaFernando (Maria Mafalda Fernando) wrote…

The folder structure is now portal/middleware/django_online_status. Should I rename middleware.py, too?

well, if you use the __init.py__ in django_online_status to expose the OnlineStatusMiddleware there, you don't need to specify the middleware.py part in the import path


portal/autoconfig.py, line 107 at r1 (raw file):

        "django.contrib.auth.middleware.AuthenticationMiddleware",
        "django.contrib.auth.middleware.SessionAuthenticationMiddleware",
        "portal.middleware.django_online_status.online_status.middleware.OnlineStatusMiddleware",

also, this doesn't make a lot of difference, but we don't need the django_ prefix here if we are already in a django project, right?

Copy link
Contributor

@mrniket mrniket left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 11 of 93 files at r1, 32 of 38 files at r3, 42 of 45 files at r4.
Reviewable status: 17 of 20 files reviewed, 19 unresolved discussions (waiting on @dent50cent, @mariaFernando, and @mrniket)


portal/middleware/django_online_status/.hg/undo.bookmarks, line 0 at r4 (raw file):
this file looks to still be present?


portal/middleware/django_online_status/.hg/store/phaseroots, line 0 at r4 (raw file):
this file seems to still be present?


portal/middleware/django_online_status/.hg/store/undo.phaseroots, line 0 at r4 (raw file):
this file seems to still be present?


portal/middleware/django_online_status/.hg/wcache/checkisexec, line 0 at r4 (raw file):
this file seems to still be there?


portal/middleware/django_online_status/.hg/wcache/checklink-target, line 0 at r4 (raw file):
this file seems to still be there?


portal/middleware/django_online_status/build/lib/online_status/models.py, line 0 at r4 (raw file):
this file seems to still be there?


portal/middleware/django_online_status/build/lib/online_status/templatetags/init.py, line 0 at r4 (raw file):
this file seems to still be there?


portal/middleware/django_online_status/online_status/templatetags/init.py, line 0 at r4 (raw file):
template tags should probably not be in the middleware folder right?

Copy link
Contributor

@mrniket mrniket left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 17 of 20 files reviewed, 19 unresolved discussions (waiting on @mariaFernando and @mrniket)


portal/tests/test_online_status.py, line 91 at r4 (raw file):

        self.client.get(reverse("dashboard"))
        useronline = cache.get(status.CACHE_PREFIX_USER % self.user1.pk)

maybe it's best to test user2 here as user1 is already testing in setup_conditions?

@MMFernando
Copy link
Contributor Author


portal/middleware/django_online_status/online_status/middleware.py, line 15 at r1 (raw file):

Previously, mrniket (Niket Shah) wrote…

Ah maybe I wasn't clear, I'm concerned and wanted to check about these differences:

image.png

We've checked it, it won't affect this middleware in particular

Copy link
Contributor Author

@MMFernando MMFernando left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: all files reviewed, 6 unresolved discussions (waiting on @mariaFernando and @mrniket)

a discussion (no related file):

Previously, mrniket (Niket Shah) wrote…

just wondering why the idle status was removed?

We came to the conclusion it wasn't needed and is unlikely to be needed in the future for this functionality (if it is, it's not hard to implement again)



portal/autoconfig.py, line 107 at r6 (raw file):

Previously, mrniket (Niket Shah) wrote…

can we have a PR ready to change this in deploy appengine too? https://github.com/ocadotechnology/codeforlife-deploy-appengine/blob/master/django_site/settings.py#L62

Will do

Copy link
Contributor

@mrniket mrniket left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: all files reviewed, 5 unresolved discussions (waiting on @mariaFernando)

a discussion (no related file):

Previously, mariaFernando (Maria Mafalda Fernando) wrote…

We came to the conclusion it wasn't needed and is unlikely to be needed in the future for this functionality (if it is, it's not hard to implement again)

I don't remember this conversation... normally decisions to remove or add functionality go through the PO

I would add it back, we don't know to impact of that change and it shouldn't be in a bug fix PR. My guess is that it could be useful for teachers monitoring their classes.


@MMFernando
Copy link
Contributor Author

a discussion (no related file):

Previously, mrniket (Niket Shah) wrote…

I don't remember this conversation... normally decisions to remove or add functionality go through the PO

I would add it back, we don't know to impact of that change and it shouldn't be in a bug fix PR. My guess is that it could be useful for teachers monitoring their classes.

You're right, I'll add it back in. What should the offline and idle times be, 5 and 10 minutes like the original functionality?


Copy link
Contributor Author

@MMFernando MMFernando left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: all files reviewed, 5 unresolved discussions (waiting on @mariaFernando and @mrniket)


portal/tests/test_online_status.py, line 51 at r6 (raw file):

Previously, mrniket (Niket Shah) wrote…

can we make the sleep time as small as possible please

The override wasn't working properly, fixed now but it still seems to need rounded seconds, especially with the idle/offline distinction


portal/tests/test_online_status.py, line 100 at r6 (raw file):

Previously, mrniket (Niket Shah) wrote…

if we manually change user1.seen then we can probably test this without a sleep

the set_active method sets seen to datetime.now() so it can't be manually set

Maria Mafalda Fernando added 2 commits May 9, 2019 15:10
removed offline users from list of online users, which wasn't there before and might have caused the cache overflow. Online, offline and idle tests have been rewritten
@MMFernando MMFernando changed the title fix: online status fix: online status middleware cache May 9, 2019
@MMFernando
Copy link
Contributor Author


portal/middleware/online_status/status.py, line 92 at r6 (raw file):

Previously, mrniket (Niket Shah) wrote…

you can just return the expression here...

Removed that function

@MMFernando
Copy link
Contributor Author


portal/autoconfig.py, line 107 at r6 (raw file):

Previously, mariaFernando (Maria Mafalda Fernando) wrote…

Will do

Done.

Copy link
Member

@faucomte97 faucomte97 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

Reviewed 2 of 3 files at r7.
Reviewable status: all files reviewed, 4 unresolved discussions (waiting on @mrniket)

Copy link
Member

@faucomte97 faucomte97 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dismissed @mrniket from 2 discussions.
Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @mrniket)


portal/autoconfig.py, line 107 at r6 (raw file):

Previously, mariaFernando (Maria Mafalda Fernando) wrote…

Done.

Resolving.


portal/middleware/online_status/status.py, line 92 at r6 (raw file):

Previously, mariaFernando (Maria Mafalda Fernando) wrote…

Removed that function

Resolving.

Copy link
Contributor

@mrniket mrniket left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @mariaFernando and @mrniket)

a discussion (no related file):

Previously, mariaFernando (Maria Mafalda Fernando) wrote…

You're right, I'll add it back in. What should the offline and idle times be, 5 and 10 minutes like the original functionality?

sounds good 👍 thanks


Copy link
Contributor

@mrniket mrniket left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @mariaFernando)


portal/tests/test_online_status.py, line 100 at r6 (raw file):

Previously, mariaFernando (Maria Mafalda Fernando) wrote…

the set_active method sets seen to datetime.now() so it can't be manually set

there are two ways around this, you can either mock/patch the datetime.now() method for the first request or you can access the user object from the cache (which I believe you do below) and set the seen field on it

Copy link
Contributor

@mrniket mrniket left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: all files reviewed, 3 unresolved discussions (waiting on @mariaFernando)


portal/tests/test_online_status.py, line 51 at r6 (raw file):

Previously, mariaFernando (Maria Mafalda Fernando) wrote…

The override wasn't working properly, fixed now but it still seems to need rounded seconds, especially with the idle/offline distinction

sorry, what do you mean by rounded seconds?


portal/tests/test_online_status.py, line 48 at r7 (raw file):

from portal.middleware.online_status import status

status.TIME_OFFLINE = 3

having this here will on the module level mean that these settings could be used elsewhere. We need to find a way of doing this only for the TestCase below

@MMFernando
Copy link
Contributor Author


portal/tests/test_online_status.py, line 51 at r6 (raw file):

Previously, mrniket (Niket Shah) wrote…

sorry, what do you mean by rounded seconds?

I used fractional values and it doesn't detect the difference between idle and offline times, possibly because of this line: seconds = (datetime.now() - online_status.seen).seconds . I might be wrong, though

@MMFernando
Copy link
Contributor Author


portal/tests/test_online_status.py, line 100 at r6 (raw file):

Previously, mrniket (Niket Shah) wrote…

there are two ways around this, you can either mock/patch the datetime.now() method for the first request or you can access the user object from the cache (which I believe you do below) and set the seen field on it

Accessing the user object from the cache won't work, since it's the request that simultaneously sets the status and seen to datetime.now(), so setting it manually would always be overriden when the status is set. But I'll look into that first option

@MMFernando
Copy link
Contributor Author


portal/tests/test_online_status.py, line 48 at r7 (raw file):

Previously, mrniket (Niket Shah) wrote…

having this here will on the module level mean that these settings could be used elsewhere. We need to find a way of doing this only for the TestCase below

Do you have any suggestion? override-settings doesn't seem to work. I'll keep looking for a solution and we can discuss it if I can't find one

Copy link
Contributor

@mrniket mrniket left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: all files reviewed, 3 unresolved discussions (waiting on @mariaFernando and @mrniket)


portal/tests/test_online_status.py, line 48 at r7 (raw file):

Previously, mariaFernando (Maria Mafalda Fernando) wrote…

Do you have any suggestion? override-settings doesn't seem to work. I'll keep looking for a solution and we can discuss it if I can't find one

let's discuss on Monday

Copy link
Contributor

@mrniket mrniket left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: all files reviewed, 3 unresolved discussions (waiting on @mariaFernando)


portal/tests/test_online_status.py, line 100 at r6 (raw file):

Previously, mariaFernando (Maria Mafalda Fernando) wrote…

Accessing the user object from the cache won't work, since it's the request that simultaneously sets the status and seen to datetime.now(), so setting it manually would always be overriden when the status is set. But I'll look into that first option

👍

@MMFernando
Copy link
Contributor Author

a discussion (no related file):

Previously, mrniket (Niket Shah) wrote…

sounds good 👍 thanks

Done.


@MMFernando
Copy link
Contributor Author


portal/tests/test_online_status.py, line 48 at r7 (raw file):

Previously, mrniket (Niket Shah) wrote…

let's discuss on Monday

Used mock patch, seems to work well and the sleep and setting override aren't needed anymore

@MMFernando
Copy link
Contributor Author


portal/tests/test_online_status.py, line 100 at r6 (raw file):

Previously, mrniket (Niket Shah) wrote…

👍

Done.

Copy link
Member

@faucomte97 faucomte97 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 1 of 3 files at r8.
Reviewable status: 7 of 8 files reviewed, 3 unresolved discussions (waiting on @faucomte97 and @mrniket)

Copy link
Contributor

@mrniket mrniket left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 1 of 3 files at r8.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @mariaFernando)


portal/tests/test_online_status.py, line 48 at r7 (raw file):

Previously, mariaFernando (Maria Mafalda Fernando) wrote…

Used mock patch, seems to work well and the sleep and setting override aren't needed anymore

great 👍

Copy link
Contributor

@mrniket mrniket left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: complete! all files reviewed, all discussions resolved

@MMFernando MMFernando merged commit 0ea74e8 into master May 13, 2019
@MMFernando MMFernando deleted the fix_online_status branch May 13, 2019 08:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants