QueryAggregateChatSession101220.java 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import java.sql.*;
  2. /** Analyze aggregate chat session 101220 in tenant DB */
  3. public class QueryAggregateChatSession101220 {
  4. public static void main(String[] args) throws Exception {
  5. String host = "jdbc:mysql://cq-cdb-8fjmemkb.sql.tencentcdb.com:27220/";
  6. String user = "root";
  7. String pass = "Ylrz_1q2w3e4r5t6y";
  8. String db = "fs_tenant_cs1";
  9. try (Connection c = DriverManager.getConnection(host + db
  10. + "?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8", user, pass)) {
  11. querySession(c, 101220L);
  12. queryMsgByContact(c, 755L);
  13. queryDuplicateOutbound(c);
  14. querySilentInstances(c);
  15. queryInstanceForSession(c, 101220L);
  16. queryPrompts(c);
  17. queryTasks(c);
  18. }
  19. }
  20. private static void querySession(Connection c, long sessionId) throws SQLException {
  21. System.out.println("--- chat_session " + sessionId + " ---");
  22. try (Statement st = c.createStatement()) {
  23. try (ResultSet rs = st.executeQuery(
  24. "SELECT session_id, company_id, contact_id, channel_type, external_user_id, "
  25. + "instance_id, last_msg, last_msg_time, status, create_time, update_time "
  26. + "FROM chat_session WHERE session_id=" + sessionId)) {
  27. if (rs.next()) {
  28. for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
  29. System.out.println(rs.getMetaData().getColumnLabel(i) + "=" + rs.getString(i));
  30. }
  31. } else {
  32. System.out.println("(not found)");
  33. }
  34. }
  35. System.out.println("--- chat_msg chronological ---");
  36. try (ResultSet rs = st.executeQuery(
  37. "SELECT msg_id, send_type, LEFT(content,160) AS content, create_time "
  38. + "FROM chat_msg WHERE session_id=" + sessionId + " ORDER BY create_time ASC")) {
  39. int n = 0, cust = 0, ai = 0;
  40. while (rs.next()) {
  41. n++;
  42. int stype = rs.getInt("send_type");
  43. if (stype == 1) cust++;
  44. else if (stype == 2) ai++;
  45. System.out.printf("%s | send_type=%s | %s%n",
  46. rs.getString("create_time"), stype, rs.getString("content"));
  47. }
  48. System.out.println("total=" + n + " customer=" + cust + " ai=" + ai);
  49. }
  50. }
  51. }
  52. private static void queryMsgByContact(Connection c, long contactId) throws SQLException {
  53. System.out.println("\n--- sessions contact_id=" + contactId + " ---");
  54. String sql = "SELECT session_id, channel_type, instance_id, last_msg, last_msg_time "
  55. + "FROM chat_session WHERE contact_id=" + contactId + " ORDER BY last_msg_time DESC LIMIT 5";
  56. try (Statement st = c.createStatement(); ResultSet rs = st.executeQuery(sql)) {
  57. while (rs.next()) {
  58. System.out.printf("sess=%s ch=%s inst=%s time=%s msg=%s%n",
  59. rs.getString("session_id"), rs.getString("channel_type"),
  60. rs.getString("instance_id"), rs.getString("last_msg_time"),
  61. rs.getString("last_msg"));
  62. }
  63. }
  64. }
  65. private static void queryDuplicateOutbound(Connection c) throws SQLException {
  66. System.out.println("\n--- duplicate outbound send_type=2 (7d, cnt>=3) ---");
  67. String sql = "SELECT LEFT(content,90) AS snippet, COUNT(*) AS cnt, "
  68. + "MIN(create_time) AS first_at, MAX(create_time) AS last_at "
  69. + "FROM chat_msg WHERE send_type=2 "
  70. + "AND create_time >= DATE_SUB(NOW(), INTERVAL 7 DAY) "
  71. + "GROUP BY LEFT(content,90) HAVING cnt >= 3 ORDER BY cnt DESC LIMIT 12";
  72. try (Statement st = c.createStatement(); ResultSet rs = st.executeQuery(sql)) {
  73. while (rs.next()) {
  74. System.out.printf("cnt=%s | %s ~ %s | %s%n",
  75. rs.getString("cnt"), rs.getString("first_at"),
  76. rs.getString("last_at"), rs.getString("snippet"));
  77. }
  78. }
  79. }
  80. private static void querySilentInstances(Connection c) throws SQLException {
  81. System.out.println("\n--- running instances: 0 inbound chat, >=2 outbound ---");
  82. String sql = "SELECT i.id, i.workflow_id, i.contact_id, i.current_node_name, i.start_time, "
  83. + "(SELECT COUNT(*) FROM chat_msg m JOIN chat_session s ON m.session_id=s.session_id "
  84. + " WHERE s.instance_id=i.id AND m.send_type=1) AS inbound, "
  85. + "(SELECT COUNT(*) FROM chat_msg m JOIN chat_session s ON m.session_id=s.session_id "
  86. + " WHERE s.instance_id=i.id AND m.send_type=2) AS outbound "
  87. + "FROM lobster_workflow_instance i WHERE i.status='running' "
  88. + "HAVING inbound=0 AND outbound>=2 ORDER BY outbound DESC LIMIT 8";
  89. try (Statement st = c.createStatement(); ResultSet rs = st.executeQuery(sql)) {
  90. int n = 0;
  91. while (rs.next()) {
  92. n++;
  93. System.out.printf("inst=%s wf=%s contact=%s out=%s in=%s node=%s%n",
  94. rs.getString("id"), rs.getString("workflow_id"), rs.getString("contact_id"),
  95. rs.getString("outbound"), rs.getString("inbound"), rs.getString("current_node_name"));
  96. }
  97. if (n == 0) System.out.println("(none)");
  98. } catch (SQLException e) {
  99. System.out.println("skip: " + e.getMessage());
  100. }
  101. }
  102. private static void queryInstanceForSession(Connection c, long sessionId) throws SQLException {
  103. System.out.println("\n--- workflow instance for session ---");
  104. try (Statement st = c.createStatement()) {
  105. try (ResultSet rs = st.executeQuery(
  106. "SELECT id, workflow_id, contact_id, status, current_node_code, current_node_name, "
  107. + "start_time, last_activity_time, context_json "
  108. + "FROM lobster_workflow_instance WHERE id=(SELECT instance_id FROM chat_session WHERE session_id="
  109. + sessionId + ")")) {
  110. if (rs.next()) {
  111. System.out.println("id=" + rs.getString("id"));
  112. System.out.println("workflow_id=" + rs.getString("workflow_id"));
  113. System.out.println("status=" + rs.getString("status"));
  114. System.out.println("node=" + rs.getString("current_node_code") + " / " + rs.getString("current_node_name"));
  115. System.out.println("start=" + rs.getString("start_time"));
  116. System.out.println("last_activity=" + rs.getString("last_activity_time"));
  117. String ctx = rs.getString("context_json");
  118. if (ctx != null) {
  119. System.out.println("context_json(len)=" + ctx.length());
  120. if (ctx.contains("_proactiveGreetCount")) System.out.println(" has _proactiveGreetCount");
  121. if (ctx.contains("_lastProactiveGreetText")) System.out.println(" has _lastProactiveGreetText");
  122. }
  123. } else {
  124. System.out.println("(no instance)");
  125. }
  126. }
  127. } catch (SQLException e) {
  128. System.out.println("skip: " + e.getMessage());
  129. }
  130. }
  131. private static void queryPrompts(Connection c) throws SQLException {
  132. System.out.println("\n--- lobster_system_prompt (instruction keys) ---");
  133. try (Statement st = c.createStatement();
  134. ResultSet rs = st.executeQuery(
  135. "SELECT prompt_key, LEFT(prompt_content,120) AS c FROM lobster_system_prompt "
  136. + "WHERE prompt_key LIKE '%instruction%' OR prompt_key='system' "
  137. + "OR prompt_content LIKE '%Please answer%' LIMIT 15")) {
  138. while (rs.next()) {
  139. System.out.printf("%s | %s%n", rs.getString("prompt_key"), rs.getString("c"));
  140. }
  141. } catch (SQLException e) {
  142. System.out.println("skip: " + e.getMessage());
  143. }
  144. }
  145. private static void queryTasks(Connection c) throws SQLException {
  146. System.out.println("\n--- recent lobster tasks (pending/done) ---");
  147. try (Statement st = c.createStatement();
  148. ResultSet rs = st.executeQuery(
  149. "SELECT id, workflow_id, task_type, execute_status, cron_expression, "
  150. + "last_execute_time, create_time FROM company_workflow_lobster_task "
  151. + "ORDER BY id DESC LIMIT 8")) {
  152. while (rs.next()) {
  153. System.out.printf("task=%s wf=%s type=%s status=%s cron=%s last=%s%n",
  154. rs.getString("id"), rs.getString("workflow_id"), rs.getString("task_type"),
  155. rs.getString("execute_status"), rs.getString("cron_expression"),
  156. rs.getString("last_execute_time"));
  157. }
  158. } catch (SQLException e) {
  159. System.out.println("skip: " + e.getMessage());
  160. }
  161. }
  162. }