Skip to content
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions cognition_objects/conversation.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ def get_conversation_files_to_clean_up() -> List[Tuple[str, str, str]]:
return general.execute_all(query)


def get_conversations_older_than_24_hours() -> List[Tuple[str, str, str]]:
query = """
SELECT cc.id, cc.project_id, o.id organization_id
FROM cognition.conversation cc
INNER JOIN cognition.project cp
ON cc.project_id = cp.id
INNER JOIN PUBLIC.organization o
ON cp.organization_id = o.id
WHERE cc.created_at <= NOW() - INTERVAL '24 HOURS' AND cc.incognito_mode = TRUE"""
return general.execute_all(query)


def get_scoped(project_id: str, conversation_id: str, user_id) -> CognitionConversation:
return (
session.query(CognitionConversation)
Expand Down Expand Up @@ -159,10 +171,15 @@ def get_all_paginated_by_project_id(
order_asc: bool = True,
user_id: Optional[str] = None,
filter_dict: Optional[Dict[str, Any]] = None,
filter_incognito: bool = False,
) -> Tuple[int, int, List[CognitionConversation]]:
total_count_query = session.query(CognitionConversation.id).filter(
CognitionConversation.project_id == project_id
)
if filter_incognito:
total_count_query = total_count_query.filter(
CognitionConversation.incognito_mode == False
)
subquery = None
if filter_dict is not None:
subquery = __get_conversation_ids_by_filter(project_id, **filter_dict)
Expand Down Expand Up @@ -190,6 +207,8 @@ def get_all_paginated_by_project_id(
query = session.query(CognitionConversation).filter(
CognitionConversation.project_id == project_id
)
if filter_incognito:
query = query.filter(CognitionConversation.incognito_mode == False)
if user_id is not None:
query = query.filter(CognitionConversation.created_by == user_id)
if subquery is not None:
Expand Down Expand Up @@ -344,6 +363,7 @@ def create(
project_id: str,
user_id: str,
has_tmp_files: bool = False,
is_incognito: bool = False,
with_commit: bool = True,
created_at: Optional[datetime] = None,
) -> CognitionConversation:
Expand All @@ -353,6 +373,7 @@ def create(
created_at=created_at,
has_tmp_files=has_tmp_files,
scope_dict={},
incognito_mode=is_incognito,
)
general.add(conversation, with_commit)
return conversation
Expand All @@ -364,6 +385,7 @@ def update(
scope_dict: Optional[Dict[str, Any]] = None,
header: Optional[str] = None,
error: Optional[str] = None,
incognito_mode: Optional[bool] = None,
with_commit: bool = True,
) -> CognitionConversation:
conversation_entity = get(project_id, conversation_id)
Expand All @@ -373,6 +395,8 @@ def update(
conversation_entity.header = header
if error is not None:
conversation_entity.error = error
if incognito_mode is not None:
conversation_entity.incognito_mode = incognito_mode
general.flush_or_commit(with_commit)
return conversation_entity

Expand Down
30 changes: 17 additions & 13 deletions cognition_objects/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def get_message_feedback_overview(
FROM cognition.conversation C
INNER JOIN cognition.message mi
ON c.project_id = mi.project_id AND c.id = mi.conversation_id
WHERE C.project_id = '{project_id}'
WHERE C.project_id = '{project_id}' AND C.incognito_mode = FALSE
) x
GROUP BY project_id, conversation_id
) x
Expand Down Expand Up @@ -402,7 +402,7 @@ def get_response_time_messages(project_id: str) -> List[Dict[str, Any]]:
SELECT m.id, SUM(pl.time_elapsed)
FROM cognition.message m
INNER JOIN cognition.conversation c
ON c.id = m.conversation_id AND c.project_id = m.project_id
ON c.id = m.conversation_id AND c.project_id = m.project_id AND c.incognito_mode = FALSE
INNER JOIN cognition.pipeline_logs pl
ON m.id = pl.message_id
WHERE m.project_id = '{project_id}'
Expand All @@ -426,12 +426,13 @@ def get_conversations_messages_count(project_id: str) -> List[Dict[str, Any]]:
FROM (
SELECT conversation_id, COUNT(*) num_messages
FROM cognition.message as m
WHERE m.project_id = '{project_id}'
JOIN cognition.conversation con ON con.id = m.conversation_id
WHERE m.project_id = '{project_id}' AND con.incognito_mode = FALSE
GROUP BY conversation_id
) x
GROUP BY num_messages
)x,
(SELECT COUNT(*)::FLOAT c FROM cognition.conversation WHERE project_id = '{project_id}') conv_count
(SELECT COUNT(*)::FLOAT c FROM cognition.conversation WHERE project_id = '{project_id}' AND incognito_mode = FALSE) conv_count
ORDER BY 1
"""
return general.execute_all(query)
Expand All @@ -445,13 +446,13 @@ def get_feedback_distribution(
if start_date and end_date:
start_date = prevent_sql_injection(start_date, isinstance(start_date, str))
end_date = prevent_sql_injection(end_date, isinstance(end_date, str))
where_add += f"AND created_at BETWEEN '{start_date}' AND '{end_date}'"
where_add += f"AND m.created_at BETWEEN '{start_date}' AND '{end_date}'"
elif start_date:
start_date = prevent_sql_injection(start_date, isinstance(start_date, str))
where_add += f"AND created_at >= '{start_date}'"
where_add += f"AND m.created_at >= '{start_date}'"
elif end_date:
end_date = prevent_sql_injection(end_date, isinstance(end_date, str))
where_add += f"AND created_at <= '{end_date}'"
where_add += f"AND m.created_at <= '{end_date}'"

project_id = prevent_sql_injection(project_id, isinstance(project_id, str))
query = f"""
Expand All @@ -463,12 +464,13 @@ def get_feedback_distribution(
SELECT COUNT(*) feedbacks, feedback_value
FROM (
SELECT feedback_value
FROM cognition.message
WHERE project_id = '{project_id}' AND feedback_value IS NOT NULL {where_add}
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
)x
GROUP BY feedback_value
)x,
(SELECT COUNT(*)::FLOAT c FROM cognition.message WHERE project_id = '{project_id}' AND feedback_value IS NOT NULL {where_add} ) percentage_count
(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
"""
return general.execute_all(query)

Expand Down Expand Up @@ -515,13 +517,15 @@ def get_feedback_line_chart_data(
query = f"""
WITH base_select AS (
SELECT
date_trunc('{group_size}', created_at) time_group,
date_trunc('{group_size}', M.created_at) time_group,
feedback_value,
COUNT(*) c
FROM cognition.message M
WHERE project_id = '{project_id}'
AND created_at >= CURRENT_TIMESTAMP - INTERVAL '{interval}'
JOIN cognition.conversation con ON con.id = M.conversation_id
WHERE M.project_id = '{project_id}'
AND M.created_at >= CURRENT_TIMESTAMP - INTERVAL '{interval}'
AND feedback_value IS NOT NULL
AND con.incognito_mode = FALSE
GROUP BY 1,2
)
SELECT jsonb_object_agg(time_group, vals)
Expand Down
21 changes: 16 additions & 5 deletions global_objects/admin_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def __get_multitagged_conversations(
FROM cognition.conversation C
{filter_join}
INNER JOIN cognition.conversation_tag_association cta
ON c.id = cta.conversation_id
ON c.id = cta.conversation_id AND c.incognito_mode = FALSE
{org_join}
group BY 1, 2
HAVING COUNT(*) > 1
Expand Down Expand Up @@ -202,7 +202,7 @@ def __get_conversations_per_tag(
SELECT o.name organization_name, p.name project_name, COUNT({count_query}) tags_created
FROM cognition.conversation_tag_association cta
INNER JOIN cognition.conversation c
ON c.id = cta.conversation_id
ON c.id = cta.conversation_id AND c.incognito_mode = FALSE
{filter_join}
INNER JOIN cognition.project p
ON c.project_id = p.id
Expand Down Expand Up @@ -465,7 +465,7 @@ def __get_avg_messages_per_conversation(
{filter_join}
{org_where}
INNER JOIN cognition.conversation c
ON m.conversation_id = c.id
ON m.conversation_id = c.id AND c.incognito_mode = FALSE
INNER JOIN params p
ON c.created_at >= (
SELECT MIN(period_start)
Expand Down Expand Up @@ -539,6 +539,10 @@ def __get_global_messages_per_conversation(
ON m.created_by = u.id AND u.email NOT LIKE '%@kern.ai'
"""

incognito_join = """INNER JOIN cognition.conversation c
ON m.conversation_id = c.id AND c.incognito_mode = FALSE
"""

query = f"""
SELECT
o.name organization_name,
Expand All @@ -554,12 +558,13 @@ def __get_global_messages_per_conversation(
ROUND(AVG(cnt),2) avg_messages_per_conv
FROM (
SELECT
project_id,
m.project_id,
conversation_id,
COUNT(*) cnt
FROM cognition.message M
{filter_join}
{org_where}
{incognito_join}
GROUP BY
m.project_id,
m.conversation_id
Expand Down Expand Up @@ -638,7 +643,8 @@ def __get_messages_feedback_by_project(
SELECT MAX(period_start) + ( '1 ' || p.period )::interval
FROM periods, params
)
WHERE m.feedback_value IS NOT NULL
JOIN cognition.conversation c ON m.conversation_id = c.id
WHERE m.feedback_value IS NOT NULL AND c.incognito_mode = FALSE
),

agg AS (
Expand Down Expand Up @@ -776,6 +782,9 @@ def __get_messages_created_by_project(
ON m.created_at >= (SELECT MIN(period_start) FROM periods)
AND m.created_at < (SELECT MAX(period_start) + ( '1 ' || p.period )::interval
FROM periods, params)
JOIN cognition.conversation c ON m.conversation_id = c.id
WHERE c.incognito_mode = FALSE

),
aggregated AS (
SELECT
Expand Down Expand Up @@ -872,6 +881,8 @@ def __get_messages_created(
ON m.created_at >= (SELECT MIN(period_start) FROM periods)
AND m.created_at < (SELECT MAX(period_start) + ( '1 ' || p.period )::interval
FROM periods, params)
JOIN cognition.conversation c ON m.conversation_id = c.id
WHERE c.incognito_mode = FALSE
),
aggregated AS (
SELECT
Expand Down
1 change: 1 addition & 0 deletions models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1240,6 +1240,7 @@ class CognitionConversation(Base):
error = Column(String)
has_tmp_files = Column(Boolean, default=False)
archived = Column(Boolean, default=False)
incognito_mode = Column(Boolean, default=False)


class CognitionMessage(Base):
Expand Down