Skip to content

Commit 25b5d67

Browse files
Incognito mode to conversations (#195)
* Incognito mode to conversations * Added filter for incognito mode * Filters for incognito in admin queries * Update incognito mode request * Delete conversations older than 24 hours * PR comments * PR comments
1 parent 1a11059 commit 25b5d67

File tree

4 files changed

+57
-20
lines changed

4 files changed

+57
-20
lines changed

cognition_objects/conversation.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import Dict, List, Optional, Tuple, Any, Union
22

3-
from datetime import datetime
3+
from datetime import datetime, timedelta
44

55
from ..cognition_objects import message
66
from ..business_objects import general
@@ -120,7 +120,7 @@ def get_overview_list(
120120
FROM (
121121
SELECT id, project_id, created_at, error IS NOT NULL has_error
122122
FROM cognition.conversation c
123-
WHERE c.project_id = '{project_id}' {basic_where_add}
123+
WHERE c.project_id = '{project_id}' {basic_where_add} AND c.incognito_mode = FALSE
124124
ORDER BY c.created_at {order_key}
125125
{pagination_add}
126126
) x
@@ -159,10 +159,15 @@ def get_all_paginated_by_project_id(
159159
order_asc: bool = True,
160160
user_id: Optional[str] = None,
161161
filter_dict: Optional[Dict[str, Any]] = None,
162+
filter_incognito: bool = False,
162163
) -> Tuple[int, int, List[CognitionConversation]]:
163164
total_count_query = session.query(CognitionConversation.id).filter(
164165
CognitionConversation.project_id == project_id
165166
)
167+
if filter_incognito:
168+
total_count_query = total_count_query.filter(
169+
CognitionConversation.incognito_mode == False
170+
)
166171
subquery = None
167172
if filter_dict is not None:
168173
subquery = __get_conversation_ids_by_filter(project_id, **filter_dict)
@@ -190,6 +195,8 @@ def get_all_paginated_by_project_id(
190195
query = session.query(CognitionConversation).filter(
191196
CognitionConversation.project_id == project_id
192197
)
198+
if filter_incognito:
199+
query = query.filter(CognitionConversation.incognito_mode == False)
193200
if user_id is not None:
194201
query = query.filter(CognitionConversation.created_by == user_id)
195202
if subquery is not None:
@@ -344,6 +351,7 @@ def create(
344351
project_id: str,
345352
user_id: str,
346353
has_tmp_files: bool = False,
354+
is_incognito: bool = False,
347355
with_commit: bool = True,
348356
created_at: Optional[datetime] = None,
349357
) -> CognitionConversation:
@@ -353,6 +361,7 @@ def create(
353361
created_at=created_at,
354362
has_tmp_files=has_tmp_files,
355363
scope_dict={},
364+
incognito_mode=is_incognito,
356365
)
357366
general.add(conversation, with_commit)
358367
return conversation
@@ -364,6 +373,7 @@ def update(
364373
scope_dict: Optional[Dict[str, Any]] = None,
365374
header: Optional[str] = None,
366375
error: Optional[str] = None,
376+
incognito_mode: Optional[bool] = None,
367377
with_commit: bool = True,
368378
) -> CognitionConversation:
369379
conversation_entity = get(project_id, conversation_id)
@@ -373,6 +383,8 @@ def update(
373383
conversation_entity.header = header
374384
if error is not None:
375385
conversation_entity.error = error
386+
if incognito_mode is not None:
387+
conversation_entity.incognito_mode = incognito_mode
376388
general.flush_or_commit(with_commit)
377389
return conversation_entity
378390

@@ -435,3 +447,12 @@ def delete_many(
435447
CognitionConversation.id.in_(conversation_ids),
436448
).delete(synchronize_session=False)
437449
general.flush_or_commit(with_commit)
450+
451+
452+
def delete_incognito_conversations_older_than_24_hours() -> None:
453+
time_to_delete = datetime.now() - timedelta(hours=24)
454+
session.query(CognitionConversation).filter(
455+
CognitionConversation.incognito_mode == True,
456+
CognitionConversation.created_at <= time_to_delete,
457+
).delete(synchronize_session=False)
458+
general.flush_or_commit(True)

cognition_objects/message.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ def get_message_feedback_overview(
246246
FROM cognition.conversation C
247247
INNER JOIN cognition.message mi
248248
ON c.project_id = mi.project_id AND c.id = mi.conversation_id
249-
WHERE C.project_id = '{project_id}'
249+
WHERE C.project_id = '{project_id}' AND C.incognito_mode = FALSE
250250
) x
251251
GROUP BY project_id, conversation_id
252252
) x
@@ -402,7 +402,7 @@ def get_response_time_messages(project_id: str) -> List[Dict[str, Any]]:
402402
SELECT m.id, SUM(pl.time_elapsed)
403403
FROM cognition.message m
404404
INNER JOIN cognition.conversation c
405-
ON c.id = m.conversation_id AND c.project_id = m.project_id
405+
ON c.id = m.conversation_id AND c.project_id = m.project_id AND c.incognito_mode = FALSE
406406
INNER JOIN cognition.pipeline_logs pl
407407
ON m.id = pl.message_id
408408
WHERE m.project_id = '{project_id}'
@@ -426,12 +426,13 @@ def get_conversations_messages_count(project_id: str) -> List[Dict[str, Any]]:
426426
FROM (
427427
SELECT conversation_id, COUNT(*) num_messages
428428
FROM cognition.message as m
429-
WHERE m.project_id = '{project_id}'
429+
JOIN cognition.conversation con ON con.id = m.conversation_id
430+
WHERE m.project_id = '{project_id}' AND con.incognito_mode = FALSE
430431
GROUP BY conversation_id
431432
) x
432433
GROUP BY num_messages
433434
)x,
434-
(SELECT COUNT(*)::FLOAT c FROM cognition.conversation WHERE project_id = '{project_id}') conv_count
435+
(SELECT COUNT(*)::FLOAT c FROM cognition.conversation WHERE project_id = '{project_id}' AND incognito_mode = FALSE) conv_count
435436
ORDER BY 1
436437
"""
437438
return general.execute_all(query)
@@ -445,13 +446,13 @@ def get_feedback_distribution(
445446
if start_date and end_date:
446447
start_date = prevent_sql_injection(start_date, isinstance(start_date, str))
447448
end_date = prevent_sql_injection(end_date, isinstance(end_date, str))
448-
where_add += f"AND created_at BETWEEN '{start_date}' AND '{end_date}'"
449+
where_add += f"AND m.created_at BETWEEN '{start_date}' AND '{end_date}'"
449450
elif start_date:
450451
start_date = prevent_sql_injection(start_date, isinstance(start_date, str))
451-
where_add += f"AND created_at >= '{start_date}'"
452+
where_add += f"AND m.created_at >= '{start_date}'"
452453
elif end_date:
453454
end_date = prevent_sql_injection(end_date, isinstance(end_date, str))
454-
where_add += f"AND created_at <= '{end_date}'"
455+
where_add += f"AND m.created_at <= '{end_date}'"
455456

456457
project_id = prevent_sql_injection(project_id, isinstance(project_id, str))
457458
query = f"""
@@ -463,12 +464,13 @@ def get_feedback_distribution(
463464
SELECT COUNT(*) feedbacks, feedback_value
464465
FROM (
465466
SELECT feedback_value
466-
FROM cognition.message
467-
WHERE project_id = '{project_id}' AND feedback_value IS NOT NULL {where_add}
467+
FROM cognition.message m
468+
JOIN cognition.conversation con ON con.id = m.conversation_id
469+
WHERE m.project_id = '{project_id}' AND m.feedback_value IS NOT NULL {where_add} AND con.incognito_mode = FALSE
468470
)x
469471
GROUP BY feedback_value
470472
)x,
471-
(SELECT COUNT(*)::FLOAT c FROM cognition.message WHERE project_id = '{project_id}' AND feedback_value IS NOT NULL {where_add} ) percentage_count
473+
(SELECT COUNT(*)::FLOAT c FROM cognition.message m JOIN cognition.conversation con ON con.id = m.conversation_id WHERE m.project_id = '{project_id}' AND m.feedback_value IS NOT NULL {where_add} AND con.incognito_mode = FALSE) percentage_count
472474
"""
473475
return general.execute_all(query)
474476

@@ -515,13 +517,15 @@ def get_feedback_line_chart_data(
515517
query = f"""
516518
WITH base_select AS (
517519
SELECT
518-
date_trunc('{group_size}', created_at) time_group,
520+
date_trunc('{group_size}', M.created_at) time_group,
519521
feedback_value,
520522
COUNT(*) c
521523
FROM cognition.message M
522-
WHERE project_id = '{project_id}'
523-
AND created_at >= CURRENT_TIMESTAMP - INTERVAL '{interval}'
524+
JOIN cognition.conversation con ON con.id = M.conversation_id
525+
WHERE M.project_id = '{project_id}'
526+
AND M.created_at >= CURRENT_TIMESTAMP - INTERVAL '{interval}'
524527
AND feedback_value IS NOT NULL
528+
AND con.incognito_mode = FALSE
525529
GROUP BY 1,2
526530
)
527531
SELECT jsonb_object_agg(time_group, vals)

global_objects/admin_queries.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ def __get_multitagged_conversations(
158158
FROM cognition.conversation C
159159
{filter_join}
160160
INNER JOIN cognition.conversation_tag_association cta
161-
ON c.id = cta.conversation_id
161+
ON c.id = cta.conversation_id AND c.incognito_mode = FALSE
162162
{org_join}
163163
group BY 1, 2
164164
HAVING COUNT(*) > 1
@@ -202,7 +202,7 @@ def __get_conversations_per_tag(
202202
SELECT o.name organization_name, p.name project_name, COUNT({count_query}) tags_created
203203
FROM cognition.conversation_tag_association cta
204204
INNER JOIN cognition.conversation c
205-
ON c.id = cta.conversation_id
205+
ON c.id = cta.conversation_id AND c.incognito_mode = FALSE
206206
{filter_join}
207207
INNER JOIN cognition.project p
208208
ON c.project_id = p.id
@@ -465,7 +465,7 @@ def __get_avg_messages_per_conversation(
465465
{filter_join}
466466
{org_where}
467467
INNER JOIN cognition.conversation c
468-
ON m.conversation_id = c.id
468+
ON m.conversation_id = c.id AND c.incognito_mode = FALSE
469469
INNER JOIN params p
470470
ON c.created_at >= (
471471
SELECT MIN(period_start)
@@ -539,6 +539,10 @@ def __get_global_messages_per_conversation(
539539
ON m.created_by = u.id AND u.email NOT LIKE '%@kern.ai'
540540
"""
541541

542+
incognito_join = """INNER JOIN cognition.conversation c
543+
ON m.conversation_id = c.id AND c.incognito_mode = FALSE
544+
"""
545+
542546
query = f"""
543547
SELECT
544548
o.name organization_name,
@@ -554,12 +558,13 @@ def __get_global_messages_per_conversation(
554558
ROUND(AVG(cnt),2) avg_messages_per_conv
555559
FROM (
556560
SELECT
557-
project_id,
561+
m.project_id,
558562
conversation_id,
559563
COUNT(*) cnt
560564
FROM cognition.message M
561565
{filter_join}
562566
{org_where}
567+
{incognito_join}
563568
GROUP BY
564569
m.project_id,
565570
m.conversation_id
@@ -638,7 +643,8 @@ def __get_messages_feedback_by_project(
638643
SELECT MAX(period_start) + ( '1 ' || p.period )::interval
639644
FROM periods, params
640645
)
641-
WHERE m.feedback_value IS NOT NULL
646+
JOIN cognition.conversation c ON m.conversation_id = c.id
647+
WHERE m.feedback_value IS NOT NULL AND c.incognito_mode = FALSE
642648
),
643649
644650
agg AS (
@@ -776,6 +782,9 @@ def __get_messages_created_by_project(
776782
ON m.created_at >= (SELECT MIN(period_start) FROM periods)
777783
AND m.created_at < (SELECT MAX(period_start) + ( '1 ' || p.period )::interval
778784
FROM periods, params)
785+
JOIN cognition.conversation c ON m.conversation_id = c.id
786+
WHERE c.incognito_mode = FALSE
787+
779788
),
780789
aggregated AS (
781790
SELECT
@@ -872,6 +881,8 @@ def __get_messages_created(
872881
ON m.created_at >= (SELECT MIN(period_start) FROM periods)
873882
AND m.created_at < (SELECT MAX(period_start) + ( '1 ' || p.period )::interval
874883
FROM periods, params)
884+
JOIN cognition.conversation c ON m.conversation_id = c.id
885+
WHERE c.incognito_mode = FALSE
875886
),
876887
aggregated AS (
877888
SELECT

models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,6 +1240,7 @@ class CognitionConversation(Base):
12401240
error = Column(String)
12411241
has_tmp_files = Column(Boolean, default=False)
12421242
archived = Column(Boolean, default=False)
1243+
incognito_mode = Column(Boolean, default=False)
12431244

12441245

12451246
class CognitionMessage(Base):

0 commit comments

Comments
 (0)