Skip to content

Commit c996e22

Browse files
authored
some admin extension (#196)
* Adds timed exec table & new fields * Removed tracking flag since all will be tracked atm * Collect data for admin dashboard * Remove oidc identifier
1 parent 25b5d67 commit c996e22

File tree

4 files changed

+77
-2
lines changed

4 files changed

+77
-2
lines changed

business_objects/user.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def delete(user_id: str, with_commit: bool = False) -> None:
137137

138138
def get_missing_users(user_ids: List[str]):
139139
query = f"""
140-
SELECT jsonb_object_agg(u.id, u.last_interaction)
140+
SELECT jsonb_object_agg(u.id, jsonb_build_object('last_interaction', u.last_interaction,'messages_created_this_month', u.messages_created_this_month))
141141
FROM public.user u
142142
WHERE id IN ({','.join([f"'{user_id}'" for user_id in user_ids])})
143143
"""

enums.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ class Tablenames(Enum):
177177
SUMS_TABLE = "sums_table"
178178
ADMIN_QUERY_MESSAGE_SUMMARY = "admin_query_message_summary"
179179
RELEASE_NOTIFICATION = "release_notification"
180+
TIMED_EXECUTIONS = "timed_executions"
180181

181182
def snake_case_to_pascal_case(self):
182183
# the type name (written in PascalCase) of a table is needed to create backrefs
@@ -1012,3 +1013,7 @@ class MessageInitiationType(Enum):
10121013
UI = "UI"
10131014
API = "API"
10141015
MACRO = "MACRO"
1016+
1017+
1018+
class TimedExecutionKey(Enum):
1019+
LAST_RESET_USER_MESSAGE_COUNT = "LAST_RESET_USER_MESSAGE_COUNT"

global_objects/timed_executions.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
from typing import List
2+
from datetime import datetime
3+
4+
from ..business_objects import general
5+
from ..session import session
6+
from ..models import TimedExecutions
7+
from ..enums import TimedExecutionKey
8+
from ..models import User
9+
10+
11+
def get(key: TimedExecutionKey) -> TimedExecutions:
12+
return (
13+
session.query(TimedExecutions)
14+
.filter(
15+
TimedExecutions.time_key == key.value,
16+
)
17+
.first()
18+
)
19+
20+
21+
def get_all() -> List[TimedExecutions]:
22+
return session.query(TimedExecutions).all()
23+
24+
25+
def execute_time_key_update(
26+
with_commit: bool = True,
27+
) -> None:
28+
# potential optimization by only getting "needed" keys so the time check is already done in the query
29+
all_keys = {key for key in TimedExecutionKey}
30+
all_existing = {obj.time_key: obj for obj in get_all()}
31+
32+
for key in all_keys:
33+
obj = all_existing.get(key.value)
34+
if obj:
35+
last_executed_at = obj.last_executed_at
36+
else:
37+
last_executed_at = datetime.min
38+
if __execute_timed_execution_by_key(key, last_executed_at):
39+
if obj:
40+
obj.last_executed_at = datetime.now()
41+
else:
42+
obj = TimedExecutions(
43+
time_key=key.value,
44+
last_executed_at=datetime.now(),
45+
)
46+
general.add(obj, False)
47+
general.flush_or_commit(with_commit)
48+
49+
50+
def __execute_timed_execution_by_key(
51+
key: TimedExecutionKey, last_executed_at: datetime
52+
) -> bool:
53+
if key == TimedExecutionKey.LAST_RESET_USER_MESSAGE_COUNT:
54+
# check if month has changed since last execution
55+
now = datetime.now()
56+
if last_executed_at.year == now.year and last_executed_at.month == now.month:
57+
return False # already executed this month
58+
59+
session.query(User).update({User.messages_created_this_month: 0})
60+
general.flush_or_commit(False)
61+
return True
62+
63+
raise NotImplementedError(f"Timed execution for key {key} is not implemented yet")

models.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,9 @@ class User(Base):
226226
created_at = Column(DateTime, default=sql.func.now())
227227
metadata_public = Column(JSON)
228228
sso_provider = Column(String)
229-
oidc_identifier = Column(String)
230229
use_new_cognition_ui = Column(Boolean, default=True)
231230
auto_logout_minutes = Column(Integer)
231+
messages_created_this_month = Column(BigInteger, default=0)
232232

233233

234234
class Team(Base):
@@ -2505,3 +2505,10 @@ class ReleaseNotification(Base):
25052505
)
25062506
link = Column(String, nullable=False)
25072507
config = Column(JSON) # e.g. {"en": {"headline":"", "description":""}, "de": {...}}
2508+
2509+
2510+
class TimedExecutions(Base):
2511+
__tablename__ = Tablenames.TIMED_EXECUTIONS.value
2512+
__table_args__ = {"schema": "global"}
2513+
time_key = Column(String, unique=True, primary_key=True) # enums.TimedExecutionKey
2514+
last_executed_at = Column(DateTime)

0 commit comments

Comments
 (0)