Spaces:
Sleeping
Sleeping
Captain Ezio
commited on
Commit
·
292d077
1
Parent(s):
96a3804
Update disable_db.py
Browse files- Powers/database/disable_db.py +91 -34
Powers/database/disable_db.py
CHANGED
@@ -10,7 +10,7 @@ DISABLED_CMDS = {}
|
|
10 |
class Disabling(MongoDB):
|
11 |
"""Class to manage database for Disabling for chats."""
|
12 |
|
13 |
-
# Database name to connect to
|
14 |
db_name = "disabled"
|
15 |
|
16 |
def __init__(self, chat_id: int) -> None:
|
@@ -20,20 +20,26 @@ class Disabling(MongoDB):
|
|
20 |
|
21 |
def check_cmd_status(self, cmd: str):
|
22 |
with INSERTION_LOCK:
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
# return bool(cmd in cmds)
|
26 |
-
return bool(cmd in cmds)
|
27 |
|
28 |
def add_disable(self, cmd: str):
|
29 |
with INSERTION_LOCK:
|
30 |
if not self.check_cmd_status(cmd):
|
31 |
-
# DISABLED_CMDS[self.chat_id]["commands"].append(cmd)
|
32 |
return self.update(
|
33 |
{"_id": self.chat_id},
|
34 |
{
|
35 |
"_id": self.chat_id,
|
36 |
-
"commands": self.chat_info["commands"]
|
37 |
},
|
38 |
)
|
39 |
|
@@ -41,7 +47,6 @@ class Disabling(MongoDB):
|
|
41 |
with INSERTION_LOCK:
|
42 |
if self.check_cmd_status(comm):
|
43 |
self.chat_info["commands"].remove(comm)
|
44 |
-
DISABLED_CMDS[self.chat_id]["commands"].remove(comm)
|
45 |
return self.update(
|
46 |
{"_id": self.chat_id},
|
47 |
{
|
@@ -52,20 +57,24 @@ class Disabling(MongoDB):
|
|
52 |
|
53 |
def get_disabled(self):
|
54 |
with INSERTION_LOCK:
|
55 |
-
global DISABLED_CMDS
|
56 |
try:
|
57 |
cmds = DISABLED_CMDS[self.chat_id]["commands"]
|
58 |
except KeyError:
|
59 |
cmds = self.chat_info["commands"]
|
60 |
-
DISABLED_CMDS[self.chat_id]
|
61 |
-
|
|
|
|
|
|
|
62 |
|
63 |
@staticmethod
|
64 |
def count_disabled_all():
|
65 |
with INSERTION_LOCK:
|
66 |
collection = MongoDB(Disabling.db_name)
|
67 |
curr = collection.find_all()
|
68 |
-
return sum(
|
|
|
|
|
69 |
|
70 |
@staticmethod
|
71 |
def count_disabling_chats():
|
@@ -76,33 +85,53 @@ class Disabling(MongoDB):
|
|
76 |
|
77 |
def set_action(self, action: str):
|
78 |
with INSERTION_LOCK:
|
79 |
-
|
80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
return self.update(
|
82 |
{"_id": self.chat_id},
|
83 |
-
{
|
|
|
|
|
|
|
84 |
)
|
85 |
|
86 |
def get_action(self):
|
87 |
with INSERTION_LOCK:
|
88 |
-
global DISABLED_CMDS
|
89 |
try:
|
90 |
-
|
91 |
except KeyError:
|
92 |
-
|
93 |
-
|
94 |
-
|
|
|
|
|
|
|
|
|
95 |
|
96 |
@staticmethod
|
97 |
def count_action_dis_all(action: str):
|
98 |
with INSERTION_LOCK:
|
99 |
collection = MongoDB(Disabling.db_name)
|
100 |
all_data = collection.find_all({"action": action})
|
101 |
-
return sum(
|
|
|
|
|
102 |
|
103 |
def rm_all_disabled(self):
|
104 |
with INSERTION_LOCK:
|
105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
return self.update(
|
107 |
{"_id": self.chat_id},
|
108 |
{"commands": []},
|
@@ -114,34 +143,62 @@ class Disabling(MongoDB):
|
|
114 |
except KeyError:
|
115 |
chat_data = self.find_one({"_id": self.chat_id})
|
116 |
if not chat_data:
|
117 |
-
new_data =
|
118 |
"_id": self.chat_id,
|
119 |
"commands": [],
|
120 |
"action": "none",
|
121 |
}
|
|
|
|
|
|
|
|
|
122 |
self.insert_one(new_data)
|
123 |
-
LOGGER.info(
|
|
|
124 |
return new_data
|
|
|
125 |
return chat_data
|
126 |
|
127 |
# Migrate if chat id changes!
|
128 |
def migrate_chat(self, new_chat_id: int):
|
129 |
-
global DISABLED_CMDS # global only when we are modifying the value
|
130 |
old_chat_db = self.find_one({"_id": self.chat_id})
|
131 |
new_data = old_chat_db.update({"_id": new_chat_id})
|
132 |
-
DISABLED_CMDS[new_chat_id] = DISABLED_CMDS
|
133 |
-
del DISABLED_CMDS[self.chat_id]
|
134 |
self.insert_one(new_data)
|
135 |
self.delete_one({"_id": self.chat_id})
|
136 |
|
137 |
-
|
138 |
-
def
|
139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
140 |
collection = MongoDB(Disabling.db_name)
|
141 |
-
|
142 |
-
|
143 |
-
i["_id"]: {"action": i["action"], "commands": i["commands"]} for i in all_data
|
144 |
-
}
|
145 |
|
146 |
|
147 |
-
|
|
|
10 |
class Disabling(MongoDB):
|
11 |
"""Class to manage database for Disabling for chats."""
|
12 |
|
13 |
+
# Database name to connect to perform operations
|
14 |
db_name = "disabled"
|
15 |
|
16 |
def __init__(self, chat_id: int) -> None:
|
|
|
20 |
|
21 |
def check_cmd_status(self, cmd: str):
|
22 |
with INSERTION_LOCK:
|
23 |
+
try:
|
24 |
+
cmds = DISABLED_CMDS[self.chat_id]["commands"]
|
25 |
+
except KeyError:
|
26 |
+
cmds = self.chat_info["commands"]
|
27 |
+
act = self.chat_info["action"]
|
28 |
+
DISABLED_CMDS[self.chat_id] = {
|
29 |
+
"command": cmds if cmds else [],
|
30 |
+
"action": act if act else "none",
|
31 |
+
}
|
32 |
# return bool(cmd in cmds)
|
33 |
+
return bool(cmd in cmds if cmds else [])
|
34 |
|
35 |
def add_disable(self, cmd: str):
|
36 |
with INSERTION_LOCK:
|
37 |
if not self.check_cmd_status(cmd):
|
|
|
38 |
return self.update(
|
39 |
{"_id": self.chat_id},
|
40 |
{
|
41 |
"_id": self.chat_id,
|
42 |
+
"commands": self.chat_info["commands"].append(cmd),
|
43 |
},
|
44 |
)
|
45 |
|
|
|
47 |
with INSERTION_LOCK:
|
48 |
if self.check_cmd_status(comm):
|
49 |
self.chat_info["commands"].remove(comm)
|
|
|
50 |
return self.update(
|
51 |
{"_id": self.chat_id},
|
52 |
{
|
|
|
57 |
|
58 |
def get_disabled(self):
|
59 |
with INSERTION_LOCK:
|
|
|
60 |
try:
|
61 |
cmds = DISABLED_CMDS[self.chat_id]["commands"]
|
62 |
except KeyError:
|
63 |
cmds = self.chat_info["commands"]
|
64 |
+
DISABLED_CMDS[self.chat_id] = {
|
65 |
+
"commands": cmds if cmds else [],
|
66 |
+
"action": self.chat_info["action"],
|
67 |
+
}
|
68 |
+
return cmds if cmds else []
|
69 |
|
70 |
@staticmethod
|
71 |
def count_disabled_all():
|
72 |
with INSERTION_LOCK:
|
73 |
collection = MongoDB(Disabling.db_name)
|
74 |
curr = collection.find_all()
|
75 |
+
return sum(
|
76 |
+
len(chat["commands"] if chat["commands"] else [])
|
77 |
+
for chat in curr)
|
78 |
|
79 |
@staticmethod
|
80 |
def count_disabling_chats():
|
|
|
85 |
|
86 |
def set_action(self, action: str):
|
87 |
with INSERTION_LOCK:
|
88 |
+
try:
|
89 |
+
DISABLED_CMDS[self.chat_id]["action"] = action
|
90 |
+
except KeyError:
|
91 |
+
cmds = self.chat_info["commands"]
|
92 |
+
DISABLED_CMDS[self.chat_id] = {
|
93 |
+
"commands": cmds if cmds else [],
|
94 |
+
"action": action,
|
95 |
+
}
|
96 |
return self.update(
|
97 |
{"_id": self.chat_id},
|
98 |
+
{
|
99 |
+
"_id": self.chat_id,
|
100 |
+
"action": action
|
101 |
+
},
|
102 |
)
|
103 |
|
104 |
def get_action(self):
|
105 |
with INSERTION_LOCK:
|
|
|
106 |
try:
|
107 |
+
val = DISABLED_CMDS[self.chat_id]["action"]
|
108 |
except KeyError:
|
109 |
+
cmds = self.chat_info["commands"]
|
110 |
+
val = self.chat_info["action"]
|
111 |
+
DISABLED_CMDS[self.chat_id] = {
|
112 |
+
"commands": cmds if cmds else [],
|
113 |
+
"action": val,
|
114 |
+
}
|
115 |
+
return val if val else "none"
|
116 |
|
117 |
@staticmethod
|
118 |
def count_action_dis_all(action: str):
|
119 |
with INSERTION_LOCK:
|
120 |
collection = MongoDB(Disabling.db_name)
|
121 |
all_data = collection.find_all({"action": action})
|
122 |
+
return sum(
|
123 |
+
len(i["commands"] if i["commands"] else []) >= 1
|
124 |
+
for i in all_data)
|
125 |
|
126 |
def rm_all_disabled(self):
|
127 |
with INSERTION_LOCK:
|
128 |
+
try:
|
129 |
+
DISABLED_CMDS[self.chat_id]["commands"] = []
|
130 |
+
except KeyError:
|
131 |
+
DISABLED_CMDS[self.chat_id] = {
|
132 |
+
"commands": [],
|
133 |
+
"action": self.chat_info["action"],
|
134 |
+
}
|
135 |
return self.update(
|
136 |
{"_id": self.chat_id},
|
137 |
{"commands": []},
|
|
|
143 |
except KeyError:
|
144 |
chat_data = self.find_one({"_id": self.chat_id})
|
145 |
if not chat_data:
|
146 |
+
new_data = {
|
147 |
"_id": self.chat_id,
|
148 |
"commands": [],
|
149 |
"action": "none",
|
150 |
}
|
151 |
+
DISABLED_CMDS[self.chat_id] = {
|
152 |
+
"commands": [],
|
153 |
+
"action": "none"
|
154 |
+
}
|
155 |
self.insert_one(new_data)
|
156 |
+
LOGGER.info(
|
157 |
+
f"Initialized Disabling Document for chat {self.chat_id}")
|
158 |
return new_data
|
159 |
+
DISABLED_CMDS[self.chat_id] = chat_data
|
160 |
return chat_data
|
161 |
|
162 |
# Migrate if chat id changes!
|
163 |
def migrate_chat(self, new_chat_id: int):
|
|
|
164 |
old_chat_db = self.find_one({"_id": self.chat_id})
|
165 |
new_data = old_chat_db.update({"_id": new_chat_id})
|
166 |
+
DISABLED_CMDS[new_chat_id] = DISABLED_CMDS.pop(self.chat_id)
|
|
|
167 |
self.insert_one(new_data)
|
168 |
self.delete_one({"_id": self.chat_id})
|
169 |
|
170 |
+
@staticmethod
|
171 |
+
def repair_db(collection):
|
172 |
+
global DISABLED_CMDS
|
173 |
+
all_data = collection.find_all()
|
174 |
+
DISABLED_CMDS = {
|
175 |
+
i["_id"]: {
|
176 |
+
"action": i["action"] if i["action"] else "none",
|
177 |
+
"commands": i["commands"] if i["commands"] else [],
|
178 |
+
}
|
179 |
+
for i in all_data
|
180 |
+
}
|
181 |
+
keys = {
|
182 |
+
"commands": [],
|
183 |
+
"action": "none",
|
184 |
+
}
|
185 |
+
for data in all_data:
|
186 |
+
for key, val in keys.items():
|
187 |
+
try:
|
188 |
+
_ = data[key]
|
189 |
+
except KeyError:
|
190 |
+
LOGGER.warning(
|
191 |
+
f"Repairing Disabling Database - setting '{key}:{val}' for {data['_id']}",
|
192 |
+
)
|
193 |
+
collection.update({"_id": data["_id"]}, {key: val})
|
194 |
+
|
195 |
+
|
196 |
+
def __pre_req_disabling():
|
197 |
+
start = time()
|
198 |
+
LOGGER.info("Starting disabling Database Repair ...")
|
199 |
collection = MongoDB(Disabling.db_name)
|
200 |
+
Disabling.repair_db(collection)
|
201 |
+
LOGGER.info(f"Done in {round((time() - start), 3)}s!")
|
|
|
|
|
202 |
|
203 |
|
204 |
+
__pre_req_disabling()
|