diff --git a/src/prerna/collaboration/BrainTopicUtils.java b/src/prerna/collaboration/BrainTopicUtils.java index a994bcf1fe..ea0e614306 100644 --- a/src/prerna/collaboration/BrainTopicUtils.java +++ b/src/prerna/collaboration/BrainTopicUtils.java @@ -102,9 +102,10 @@ public static Map listTopics(User user, List statuses, S Map threads = countThreadsByTopic(ownerId, ownerType); Map openItems = WorkTaskUtils.openByTopic(ownerId, ownerType); + Map chats = countChatsByTopic(ownerId, ownerType); List> items = CollaborationDbUtils.query(CollaborationDbUtils.page( "SELECT " + SUMMARY_COLUMNS + " FROM BRAIN_TOPIC" + where + " ORDER BY NAME, TOPIC_ID", limit, offset), - rs -> mapSummary(rs, threads, openItems), params.toArray()); + rs -> mapSummary(rs, threads, openItems, chats), params.toArray()); Map page = new LinkedHashMap<>(); page.put("items", items); @@ -134,11 +135,12 @@ public static Map getTopic(User user, String topicId, String top public static Map getTopic(String ownerId, String ownerType, String topicId) { Map threads = Map.of(topicId, countThreads(ownerId, ownerType, topicId)); Map openItems = WorkTaskUtils.openByTopic(ownerId, ownerType); + Map chats = Map.of(topicId, countChats(ownerId, ownerType, topicId)); Map topic = CollaborationDbUtils.queryOne( "SELECT " + SUMMARY_COLUMNS + ", DESCRIPTION, KEYWORDS_JSON, CALENDAR_SERIES_JSON FROM BRAIN_TOPIC " + "WHERE OWNER_ID = ? AND OWNER_TYPE = ? AND TOPIC_ID = ?", rs -> { - Map row = mapSummary(rs, threads, openItems); + Map row = mapSummary(rs, threads, openItems, chats); row.put("description", CollaborationDbUtils.getString(rs, "DESCRIPTION")); row.put("keywords", CollaborationDbUtils.parseList(CollaborationDbUtils.getString(rs, "KEYWORDS_JSON"))); @@ -692,6 +694,20 @@ private static Map countThreadsByTopic(String ownerId, String o + "WHERE OWNER_ID = ? AND OWNER_TYPE = ? GROUP BY TOPIC_ID", ownerId, ownerType); } + // chats (rooms) linked to the topic, including AI-suggested ones + private static int countChats(String ownerId, String ownerType, String topicId) { + return CollaborationDbUtils.count( + "SELECT COUNT(DISTINCT ROOM_ID) FROM BRAIN_TOPIC_ROOM " + + "WHERE OWNER_ID = ? AND OWNER_TYPE = ? AND TOPIC_ID = ? AND STATE IN (?, ?)", + ownerId, ownerType, topicId, BrainTopicRoomUtils.LINKED, BrainTopicRoomUtils.SUGGESTED); + } + + private static Map countChatsByTopic(String ownerId, String ownerType) { + return countByTopic("SELECT TOPIC_ID, COUNT(DISTINCT ROOM_ID) FROM BRAIN_TOPIC_ROOM " + + "WHERE OWNER_ID = ? AND OWNER_TYPE = ? AND STATE IN (?, ?) GROUP BY TOPIC_ID", + ownerId, ownerType, BrainTopicRoomUtils.LINKED, BrainTopicRoomUtils.SUGGESTED); + } + private static Map countByTopic(String sql, Object... params) { Map counts = new HashMap<>(); for (Pair row : CollaborationDbUtils.query(sql, rs -> Pair.with(rs.getString(1), rs.getInt(2)), @@ -704,11 +720,12 @@ private static Map countByTopic(String sql, Object... params) { // ---- mapping ---- private static Map mapSummary(ResultSet rs, Map threads, - Map openItems) throws SQLException { + Map openItems, Map chats) throws SQLException { String topicId = CollaborationDbUtils.getString(rs, "TOPIC_ID"); Map stats = new LinkedHashMap<>(); stats.put("threads", threads.getOrDefault(topicId, 0)); stats.put("openItems", openItems.getOrDefault(topicId, 0)); + stats.put("chats", chats.getOrDefault(topicId, 0)); stats.put("lastActivity", CollaborationDbUtils.getTimestamp(rs, "LAST_ACTIVITY_AT")); Map row = new LinkedHashMap<>();