Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
115 changes: 67 additions & 48 deletions DSL/Resql/analytics/POST/csa-chats-count.sql
Original file line number Diff line number Diff line change
@@ -1,53 +1,72 @@
WITH LastValidMessages AS (
SELECT DISTINCT ON (message.chat_base_id)
message.id,
message.chat_base_id,
message.author_id,
message.author_role,
message.created AS message_created
FROM message
WHERE message.author_role IN ('backoffice-user', 'end-user')
AND message.author_id IS NOT NULL
AND message.author_id <> ''
AND message.author_id <> 'null'
ORDER BY message.chat_base_id, message.created DESC
),
FilteredMessages AS (
SELECT
lvm.chat_base_id,
lvm.author_id,
lvm.message_created,
chat.created AS chat_created
FROM LastValidMessages lvm
JOIN chat ON chat.base_id = lvm.chat_base_id
WHERE (
array_length(ARRAY[:urls]::TEXT[], 1) IS NULL
OR chat.end_user_url LIKE ANY(ARRAY[:urls]::TEXT[])
)
AND (
:showTest = TRUE
OR chat.test = FALSE
)
AND chat.created::timestamptz BETWEEN :start::timestamptz AND :end::timestamptz
),
FinalData AS (
SELECT
date_trunc(:metric, fm.chat_created) AS date_time,
fm.chat_base_id,
fm.author_id,
"user".display_name,
"user".first_name,
"user".last_name,
"user".id_code
FROM FilteredMessages fm
LEFT JOIN "user" ON "user".id_code = fm.author_id
WHERE "user".id_code NOT IN (:excluded_csas)
)
WITH latest_per_base AS (
SELECT DISTINCT ON (c.base_id)
c.base_id,
c.ended,
c.test,
c.end_user_url
FROM chat c
WHERE c.status = 'ENDED'
AND c.ended::timestamptz BETWEEN :start::timestamptz AND :end::timestamptz
ORDER BY c.base_id, c.updated DESC
),
csa_chats AS (
SELECT lp.base_id, lp.ended
FROM latest_per_base lp
WHERE (:showTest = TRUE OR lp.test = FALSE)
AND (
array_length(ARRAY[:urls]::TEXT[], 1) IS NULL
OR lp.end_user_url LIKE ANY(ARRAY[:urls]::TEXT[])
)
AND EXISTS (
SELECT 1 FROM message m
WHERE m.chat_base_id = lp.base_id
AND m.author_role = 'backoffice-user'
)
AND EXISTS (
SELECT 1 FROM message m
WHERE m.chat_base_id = lp.base_id
AND (m.event = 'taken-over' OR m.event = 'pending-assigned')
)
),
latest_user AS (
SELECT DISTINCT ON (id_code)
id_code, display_name, first_name, last_name
FROM "user"
ORDER BY id_code, id DESC
),
last_known_csa AS (
SELECT DISTINCT ON (m.chat_base_id)
m.chat_base_id,
u.display_name,
u.first_name,
u.last_name,
u.id_code
FROM message m
JOIN latest_user u ON u.id_code = m.author_id
WHERE m.author_role = 'backoffice-user'
AND m.author_id IS NOT NULL
AND m.author_id <> ''
AND m.author_id <> 'null'
ORDER BY m.chat_base_id, m.created DESC
),
FinalData AS (
SELECT
date_trunc(:metric, cc.ended AT TIME ZONE :timezone) AT TIME ZONE :timezone AS date_time,
cc.base_id AS chat_base_id,
COALESCE(lkc.id_code, 'unknown') AS csa_id,
COALESCE(lkc.display_name, 'Unknown') AS display_name,
lkc.first_name,
lkc.last_name
FROM csa_chats cc
LEFT JOIN last_known_csa lkc ON lkc.chat_base_id = cc.base_id
WHERE COALESCE(lkc.id_code, 'unknown') NOT IN (:excluded_csas)
)
SELECT
date_time,
MAX(display_name) AS customer_support_display_name,
MAX(id_code) AS customer_support_id,
MAX(CONCAT(first_name, ' ', last_name)) AS customer_support_full_name,
csa_id AS customer_support_id,
COALESCE(MAX(first_name || ' ' || last_name), 'Unknown') AS customer_support_full_name,
COUNT(DISTINCT chat_base_id) AS count
FROM FinalData
GROUP BY date_time, author_id;
GROUP BY date_time, csa_id
ORDER BY date_time ASC;
4 changes: 4 additions & 0 deletions DSL/Ruuter/analytics/POST/agents/chats/total.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ declaration:
- field: showTest
type: boolean
description: "Body field 'showTest'"
- field: timezone
type: string
description: "Body field 'timezone'"

check_for_required_parameters:
switch:
Expand All @@ -44,6 +47,7 @@ post_step:
excluded_csas: ${incoming.body.excluded_csas}
urls: ${incoming.body.urls}
showTest: ${incoming.body.showTest}
timezone: ${incoming.body.timezone}
result: result

return_value:
Expand Down
1 change: 1 addition & 0 deletions GUI/src/pages/ChatsPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ const ChatsPage: React.FC = () => {
excluded_csas: excluded_csas.length > 0 ? excluded_csas : [''],
urls: getDomainsArray(),
showTest: getShowTestData(),
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
},
});
const res = response.response;
Expand Down
Loading