Skip to content

Commit ce7e748

Browse files
Custom cleanup conversations and files by org (#143)
* new oclumns * all by org joined * remove gdpr compliant field * default file lifespan * new joins * user count org
1 parent ef1d20b commit ce7e748

File tree

4 files changed

+49
-4
lines changed

4 files changed

+49
-4
lines changed

business_objects/organization.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66

77
from ..session import session
8-
from ..models import Organization, Project
8+
from ..models import Organization, Project, User
99
from ..business_objects import project, user, general
1010
from ..util import prevent_sql_injection
1111

@@ -62,6 +62,10 @@ def get_organization_overview_stats(
6262
return values[0]
6363

6464

65+
def get_user_count(organization_id: str) -> int:
66+
return session.query(User).filter(User.organization_id == organization_id).count()
67+
68+
6569
def __get_organization_overview_stats_query(organization_id: str):
6670
return f"""
6771
WITH labeled_records AS (

cognition_objects/conversation.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,35 @@ def get(project_id: str, conversation_id: str) -> CognitionConversation:
2525
)
2626

2727

28+
def get_conversations_to_clean_up() -> List[Tuple[str, str, str]]:
29+
query = """
30+
SELECT cc.id, cc.project_id, o.id organization_id
31+
FROM cognition.conversation cc
32+
INNER JOIN cognition.project cp
33+
ON cc.project_id = cp.id
34+
INNER JOIN (
35+
SELECT o.*, NOW() - INTERVAL '1 DAY' * conversation_lifespan_days conversation_delete_by
36+
FROM PUBLIC.organization o
37+
) o
38+
ON cp.organization_id = o.id AND cc.created_at <= o.conversation_delete_by"""
39+
return general.execute_all(query)
40+
41+
42+
def get_conversation_files_to_clean_up() -> List[Tuple[str, str, str]]:
43+
query = """
44+
SELECT cc.id, cc.project_id, o.id organization_id
45+
FROM cognition.conversation cc
46+
INNER JOIN cognition.project cp
47+
ON cc.project_id = cp.id
48+
INNER JOIN (
49+
SELECT o.*, NOW() - INTERVAL '1 DAY' * file_lifespan_days file_delete_by
50+
FROM PUBLIC.organization o
51+
) o
52+
ON cp.organization_id = o.id AND cc.created_at <= o.file_delete_by
53+
WHERE NOT cc.archived AND cc.has_tmp_files """
54+
return general.execute_all(query)
55+
56+
2857
def get_scoped(project_id: str, conversation_id: str, user_id) -> CognitionConversation:
2958
return (
3059
session.query(CognitionConversation)

cognition_objects/file_reference.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from ..business_objects import general
22
from ..session import session
33
from ..models import FileReference
4-
from typing import Dict, Any, List
4+
from typing import Dict, Any, List, Tuple
55

66

77
def get(org_id: str, hash: str, file_size_bytes) -> FileReference:
@@ -54,6 +54,18 @@ def get_all() -> List[FileReference]:
5454
return session.query(FileReference).all()
5555

5656

57+
def get_all_for_cleanup() -> List[Tuple[str, str]]:
58+
query = """
59+
SELECT fr.id, fr.organization_id
60+
FROM cognition.file_reference fr
61+
INNER JOIN (
62+
SELECT o.*, NOW() - INTERVAL '1 DAY' * file_lifespan_days file_delete_by
63+
FROM PUBLIC.organization o
64+
) o
65+
ON fr.organization_id = o.id AND fr.last_used <= o.file_delete_by"""
66+
return general.execute_all(query)
67+
68+
5769
def create(
5870
org_id: str,
5971
hash: str,

models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,6 @@ class Organization(Base):
141141
# database entry
142142
is_paying = Column(Boolean, default=False)
143143
created_at = Column(DateTime, default=sql.func.now())
144-
gdpr_compliant = Column(Boolean, default=False)
145-
146144
projects = parent_to_child_relationship(
147145
Tablenames.ORGANIZATION,
148146
Tablenames.PROJECT,
@@ -157,6 +155,8 @@ class Organization(Base):
157155

158156
# designed as opt out to ensure "forgotten" doesn't result in issues
159157
log_admin_requests = Column(String, default=AdminLogLevel.NO_GET.value)
158+
conversation_lifespan_days = Column(Integer)
159+
file_lifespan_days = Column(Integer, default=14)
160160

161161

162162
class User(Base):

0 commit comments

Comments
 (0)