Skip to content

Commit 84da304

Browse files
authored
Create 20251222122848_fix_get_mining_stats.sql (#2563)
1 parent ab6b9e6 commit 84da304

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
-- fix "contacts.location" (was contacts.locations) --
2+
DROP FUNCTION IF EXISTS private.get_mining_stats(text);
3+
4+
CREATE FUNCTION private.get_mining_stats(mining_id text)
5+
RETURNS TABLE(
6+
user_id UUID,
7+
source text,
8+
total_contacts_mined BIGINT,
9+
total_reachable BIGINT,
10+
total_with_phone BIGINT,
11+
total_with_company BIGINT,
12+
total_with_location BIGINT
13+
) AS $$
14+
DECLARE
15+
user_id UUID;
16+
BEGIN
17+
-- Get the user_id first
18+
SELECT pt.user_id INTO user_id
19+
FROM private.tasks pt
20+
WHERE pt.details->>'miningId' = $1
21+
AND pt.status = 'done'
22+
ORDER BY pt.started_at DESC
23+
LIMIT 1;
24+
25+
-- Return the statistics
26+
RETURN QUERY
27+
SELECT
28+
user_id,
29+
(SELECT p.source FROM private.persons p WHERE p.mining_id = $1 LIMIT 1) AS source,
30+
COUNT(*) AS total_contacts_mined,
31+
COUNT(*) FILTER (WHERE contacts.status = 'VALID') AS total_reachable,
32+
COUNT(contacts.telephone) AS total_with_phone,
33+
COUNT(*) FILTER (WHERE contacts.job_title IS NOT NULL OR contacts.works_for IS NOT NULL) AS total_with_company,
34+
COUNT(*) FILTER (WHERE contacts.location IS NOT NULL AND contacts.location <> '') AS total_with_location
35+
FROM private.get_contacts_table(user_id) AS contacts
36+
WHERE contacts.mining_id = $1;
37+
END;
38+
$$ LANGUAGE plpgsql;

0 commit comments

Comments
 (0)