Skip to content

Extend close time logic to differentiate between the claimant and other users#1470

Merged
jb3 merged 44 commits into
mainfrom
help-channel-closing-delay-changes
Mar 30, 2021
Merged

Extend close time logic to differentiate between the claimant and other users#1470
jb3 merged 44 commits into
mainfrom
help-channel-closing-delay-changes

Conversation

@ChrisLovering
Copy link
Copy Markdown
Member

@ChrisLovering ChrisLovering commented Mar 16, 2021

Closes #1451

This extends the closing logic for help channels to closed based on the claimant's last message time and last message in general.

This results in the help channel closing time being reduced if only others are chatting, and the claimant has already left.

Thanks to laundmo, Darr, Griff, LX and Hemlock for the group coding on this PR :D

This allows us to configure the idle time allowed for claiments
seperate from tohers.
As we have complicated this logic, we now don't specify
exactly how long until the channel will close.
@laundmo laundmo added a: help channels Related to the help channel system p: 1 - high High Priority labels Mar 16, 2021
laundmo
laundmo previously approved these changes Mar 16, 2021
Copy link
Copy Markdown
Contributor

@laundmo laundmo left a comment

Choose a reason for hiding this comment

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

I was there in VC watching this be implemented and tested, all seems good to me.

Copy link
Copy Markdown
Contributor

@Shivansh-007 Shivansh-007 left a comment

Choose a reason for hiding this comment

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

Few nitpicks:

Comment thread bot/exts/help_channels/_message.py Outdated
Comment thread bot/exts/help_channels/_cog.py Outdated
Comment thread bot/exts/help_channels/_channel.py Outdated
@Xithrius Xithrius added the t: feature New feature or request label Mar 17, 2021
Comment thread bot/exts/help_channels/_cog.py Outdated
Comment thread bot/exts/help_channels/_cog.py Outdated
Comment thread bot/exts/help_channels/_cog.py Outdated
Comment thread bot/exts/help_channels/_channel.py Outdated
Comment thread bot/exts/help_channels/_channel.py Outdated
Comment thread bot/exts/help_channels/_channel.py Outdated
Comment thread bot/exts/help_channels/_channel.py Outdated
claimant_last_message_time += timedelta(minutes=constants.HelpChannels.idle_minutes_claimant)

# The further away closing time is what we should use.
closing_time = max(claimant_last_message_time, last_message_time)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This breaks the priority that deleted_idle_minutes has because idle_minutes_claimant is configured to be greater than deleted_idle_minutes by default.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

If _message.is_empty(channel) returns True, this indicates that there isn't a message in the channel. So both cache's would be empty. This would mean we enter the if statement and return before this code is hit.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

That is incorrect because the cache doesn't get cleared when the channel goes dormant. is_empty doesn't mean the channel has no messages at all; it basically means the channel's latest message is the bot's embed.

Copy link
Copy Markdown
Member Author

@ChrisLovering ChrisLovering Mar 18, 2021

Choose a reason for hiding this comment

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

I added code to clear both caches when the channel is unclaimed in a recent commit, so would that mean it's now correct?
https://github.com/python-discord/bot/pull/1470/files#diff-c31098978b8d838eadb52b2bf23494072cc66686e738d7f99c4f59e6495bfe6fR380-R381

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe, but is there a way to fix it without relying on the cache being cleared?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

How do you feel about returning early, within the if await _message.is_empty(channel): block?

Comment thread bot/exts/help_channels/_channel.py Outdated
Comment thread bot/exts/help_channels/_channel.py Outdated
Comment thread bot/exts/help_channels/_cog.py Outdated
@MarkKoz
Copy link
Copy Markdown
Contributor

MarkKoz commented Mar 17, 2021

Has it been decided to "take the hit" when the bot is offline and can't cache messages that get sent?

@laundmo
Copy link
Copy Markdown
Contributor

laundmo commented Mar 17, 2021

@MarkKoz might need to add a condition to the old method fallback, that checks for bot uptime smaller than idle_minutes_claimant so that there is at least that much time after a restart after which channels wont be unduly closed.

that is, if i understood correctly what you were referring to.

@laundmo laundmo dismissed their stale review March 17, 2021 23:42

Issue with this breaking other functionality have been brought up.

@MarkKoz
Copy link
Copy Markdown
Contributor

MarkKoz commented Mar 18, 2021

@MarkKoz might need to add a condition to the old method fallback, that checks for bot uptime smaller than idle_minutes_claimant so that there is at least that much time after a restart after which channels wont be unduly closed.

that is, if i understood correctly what you were referring to.

What I was referring to is the cache not having the latest times due to the bot going offline and more messages being sent in channels. Upon going online again, the bot may prematurely close channels due to the outdated cache leading to the perception that the channels have been inactive.

I don't understand what you're proposing.

@laundmo
Copy link
Copy Markdown
Contributor

laundmo commented Mar 18, 2021

@MarkKoz as it stands currently, it will fall back to the old behavior if the cache isn't available (empty/ returns None)

I am proposing extending the fallback conditions to always use the old method for a while after startup, as it does not rely on cache. This way, instead of channels being closed prematurely they might take a bit longer to close.

The unanswered cache was previously just a boolen of whether a
non-claimant every replied to a help channel. With us now needing
to know the time at which a non-claimant messaged in a given channel,
we infer the answered status from this cache instead.
Also updates the doc string to reflect this new behaviour.
@MarkKoz
Copy link
Copy Markdown
Contributor

MarkKoz commented Mar 18, 2021

@MarkKoz as it stands currently, it will fall back to the old behavior if the cache isn't available (empty/ returns None)

I am proposing extending the fallback conditions to always use the old method for a while after startup, as it does not rely on cache. This way, instead of channels being closed prematurely they might take a bit longer to close.

Yes, that could work. It'd have to reschedule it for the maximum time.

Comment thread bot/exts/help_channels/_channel.py Outdated
@ChrisLovering
Copy link
Copy Markdown
Member Author

ChrisLovering commented Mar 18, 2021

@MarkKoz as it stands currently, it will fall back to the old behavior if the cache isn't available (empty/ returns None)
I am proposing extending the fallback conditions to always use the old method for a while after startup, as it does not rely on cache. This way, instead of channels being closed prematurely they might take a bit longer to close.

Yes, that could work. It'd have to reschedule it for the maximum time.

What about clearing the caches on init, so the bot uses the fallback on startup, until the caches have had time to populate?
Another open is populating the cache's on init too.

Comment thread bot/exts/help_channels/_channel.py Outdated
if claimed_timestamp:
claimed = datetime.utcfromtimestamp(claimed_timestamp)
return datetime.utcnow() - claimed
claimed = datetime.fromtimestamp(claimed_timestamp)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

discord.py still returns times in UTC; they're just naïve. It uses utcfromtimestamp. Sorry if my previous comment was misleading.

Comment thread bot/exts/help_channels/_cog.py Outdated
Comment thread bot/exts/help_channels/_stats.py
datetime.min cannot be converted to a timestamp as it's pre-epoch.
Instead wait until we actuall need it and then create the correct
datetime object depending on teh cache contents.
@MarkKoz
Copy link
Copy Markdown
Contributor

MarkKoz commented Mar 26, 2021

I'm trying this out with the following config:

    idle_minutes_claimant: 2
    idle_minutes_others: 1
    deleted_idle_minutes: 1

I simply claim a channel, and it correctly schedules it to close in 2 minutes. However, when the closing task runs, it calculates a wrong time. The log timestamps on the left are in UTC-7. Converted to UTC the times would be around 2021-03-26 20:58:06. Therefore, the time it calculates is off by 7 hours. It's clearly an issue with timezones somewhere.

2021-03-26 13:58:06 | bot.exts.help_channels._channel | TRACE | #help-burrito (800698210392997918) should be closed at 2021-03-27 03:58:05.915000 due to claimant timeout.
...
2021-03-26 13:58:06 | bot.exts.help_channels._cog | INFO | #help-burrito (800698210392997918) is still active; scheduling it to be moved after 25199 seconds.
...
2021-03-26 13:58:06 | bot.utils.scheduling.HelpChannels | TRACE | Waiting 25199 seconds before awaiting coroutine for #800698210392997918.

ChrisLovering and others added 5 commits March 26, 2021 21:57
Previously we were using `utcfromtimestamp()` which would compensate
the timestamp when converting to UTC even though the timestamp itself
was in UTC:

>>> datetime.utcnow()
datetime.datetime(2021, 3, 26, 22, 8, 47, 441603)
>>> a = datetime.utcnow().timestamp()
1616821624.207364
>>> a = datetime.utcfromtimestamp(a)
datetime.datetime(2021, 3, 27, 5, 7, 4, 207364)

By switching to `fromtimestamp()` this avoids that behaviour.
It has some API changes, so it's best to update now before the project
starts using the library more.
Fix issues converting timestamps to datetimes and vice-versa. The main
culprit id `datetime.timestamp()`, which always assumes naïve objects
are in local time. That behaviour conflicts with discord.py, which
returns naïve objects in UTC rather than local time. Switching from
`utcfromtimestamp` to `fromtimestamp` was incorrect since the latter
also assumes the timestamp is in local time.
Copy link
Copy Markdown
Member

@jb3 jb3 left a comment

Choose a reason for hiding this comment

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

Code looks good and functionality works, just a few documentation things.

Comment thread bot/exts/help_channels/_stats.py Outdated
"""
Report stats for a completed help session channel `channel_id`.

Set `is_auto` to True if the channel was automatically closed or False if manually closed.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This docstring needs updating with closed_on and the potential values.

Comment thread bot/exts/help_channels/_cog.py
Comment thread bot/exts/help_channels/_message.py
Copy link
Copy Markdown
Member

@jb3 jb3 left a comment

Choose a reason for hiding this comment

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

Looks good to me! :shipit:

Copy link
Copy Markdown
Member

@jb3 jb3 left a comment

Choose a reason for hiding this comment

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

Actual approval now, :shipit:.

@jb3 jb3 merged commit 07fb4ed into main Mar 30, 2021
@jb3 jb3 deleted the help-channel-closing-delay-changes branch March 30, 2021 20:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

a: help channels Related to the help channel system p: 1 - high High Priority t: feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve help channel closing by differentiating between the OP and other users.

6 participants