-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollector_cog.py
196 lines (162 loc) · 6.41 KB
/
collector_cog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
from datetime import datetime, timedelta
import discord
from discord import app_commands, utils, ui
from discord.ext import commands
from bot import GwaffBot
from custom_logger import Logger
from database import DatabaseReader
from permissions import require_admin
from collector import record_data, update_profiles
from reducer import DatabaseReducer
logger = Logger('gwaff.bot.collector')
COLLECTION_MAX_TIME: int = 120 # The time in minutes that must go by before collection is said to be stopped.
REDUCER_TIMEOUT: int = 60 # Time in seconds before the reducer process times out and is halted.
COLLECTION_SMALL: int = 2 # Collect data from up to this page every collection event
COLLECTION_LARGE: int = 6 # Collect data from up to this page every second collection event
COLLECTION_LARGEST: int = 10 # Update names up to this page when updating names
class ReducerView(discord.ui.View):
def __init__(self, interaction: discord.Interaction):
super().__init__(timeout=REDUCER_TIMEOUT)
self.reducer = DatabaseReducer()
self.started = False
self.interaction = interaction
async def on_timeout(self):
"""
Called when the view times out. This will deactivate the buttons.
"""
await self.interaction.edit_original_response(
content="Timed out"
)
await self.remove_view()
return
async def remove_view(self):
"""
Removes and deactivates the view.
"""
for child in self.children:
child.disabled = True
await self.interaction.edit_original_response(view=self)
self.stop()
@ui.button(label="Proceed", style=discord.ButtonStyle.danger)
async def button_one_callback(
self, interaction: discord.Interaction, button: ui.Button
):
await interaction.response.defer()
if not self.started:
await self.interaction.edit_original_response(
content=f"Processing..."
)
count = self.reducer.reduce()
if not count:
await self.interaction.edit_original_response(
content=f"Something went wrong."
)
await self.remove_view()
return
elif count <= 1:
await self.interaction.edit_original_response(
content=f"There are no columns to remove."
)
await self.remove_view()
return
await self.interaction.edit_original_response(
content=f"Deleting {count} records\nAre you really sure?"
)
self.started = True
return
else:
self.reducer.commit()
await self.interaction.edit_original_response(
content=f"Saved the changes!"
)
await self.remove_view()
return
@ui.button(label="Cancel", style=discord.ButtonStyle.primary)
async def button_two_callback(
self, interaction: discord.Interaction, button: ui.Button
):
await interaction.response.defer()
self.reducer.rollback()
await self.interaction.edit_original_response(
content=f"Aborted!"
)
await self.remove_view()
return
class CollectorCog(commands.GroupCog, group_name='collector'):
def __init__(self, bot: GwaffBot):
self.bot = bot
self.bot.schedule_task(
self.collect_short,
hour='0-23/2',
minute=0
)
self.bot.schedule_task(
self.collect_long,
hour='1-23/2',
minute=0
)
self.bot.schedule_task(
self.update_profiles,
hour=0,
minute=10
)
# @app_commands.command(name="data",
# description="(Admin only) Gets the entire gwaff data as a csv")
# @app_commands.describe(hidden='Hide from others in this server (default True)')
# @require_admin
# async def send_data(self, interaction: discord.Interaction,
# hidden: bool = True):
# await interaction.response.defer(ephemeral=hidden)
# await interaction.followup.send(file=discord.File('gwaff.db'))
@app_commands.command(name="isalive",
description="When did I last collect data")
@app_commands.describe(hidden='Hide from others in this server (default True)')
async def is_alive(self, interaction: discord.Interaction,
hidden: bool = True):
await interaction.response.defer(ephemeral=hidden)
now = datetime.now()
dbr = DatabaseReader()
last = dbr.get_last_timestamp()
last_str = utils.format_dt(last, 'R')
prev_last = dbr.get_dates_in_range(now - timedelta(days=1))
if len(prev_last) <= 1:
prev_last_str = ""
else:
prev_last_str = f"(Before that {utils.format_dt(prev_last[-2], 'R')})\n"
alive: str
if (now - last).total_seconds() < 1.1 * COLLECTION_MAX_TIME * 60:
alive = ""
else:
alive = "Collection has halted!"
await interaction.followup.send(f"Data was last collected {last_str}\n"
f"{prev_last_str}{alive}")
@app_commands.command(name="reduce", description="(Admin only) Clean up old datapoints")
@require_admin
async def reducer_ui(self, interaction: discord.Interaction):
await interaction.response.send_message(
"Do you want to run the reducer?", view=ReducerView(interaction),
ephemeral=True
)
async def collect_short(self):
logger.info("Starting short data collection")
record_data(pages=range(1, COLLECTION_SMALL))
async def collect_long(self):
logger.info("Starting long data collection")
record_data(pages=range(1, COLLECTION_LARGE))
async def update_profiles(self):
logger.info("Starting profile update")
update_profiles()
async def reduce(self):
logger.info("Starting data reduction")
dr = DatabaseReducer()
count = dr.reduce()
if count > 1:
logger.info(f"Reduced {count} records")
async def setup(bot: GwaffBot):
"""
Sets up the CollectorCog and adds it to the bot.
Args:
bot (GwaffBot): The bot instance.
"""
cog = CollectorCog(bot)
await bot.add_cog(cog, guilds=bot.guilds)