MCPcopy Index your code
hub / github.com/ChatLab/ChatLab / getMemberActivity

Function getMemberActivity

packages/core/src/query/basic-queries.ts:106–151  ·  view source on GitHub ↗
(db: DatabaseAdapter, filter?: TimeFilter)

Source from the content-addressed store, hash-verified

104 * 获取成员活跃度排行
105 */
106export function getMemberActivity(db: DatabaseAdapter, filter?: TimeFilter): MemberActivity[] {
107 const { clause, params } = buildTimeFilter(filter)
108
109 const msgFilterBase = clause ? clause.replace('WHERE', 'AND') : ''
110 const msgFilterWithSystem = msgFilterBase + " AND COALESCE(m.account_name, '') != '系统消息'"
111
112 const totalClauseWithSystem = buildSystemMessageFilter(clause)
113 const totalMessages = (
114 db
115 .prepare(
116 `SELECT COUNT(*) as count
117 FROM message msg
118 JOIN member m ON msg.sender_id = m.id
119 ${totalClauseWithSystem}`
120 )
121 .get(...params) as { count: number }
122 ).count
123
124 const rows = db
125 .prepare(
126 `SELECT
127 m.id as memberId,
128 m.platform_id as platformId,
129 COALESCE(m.group_nickname, m.account_name, m.platform_id) as name,
130 m.avatar as avatar,
131 COUNT(msg.id) as messageCount
132 FROM member m
133 LEFT JOIN message msg ON m.id = msg.sender_id ${msgFilterWithSystem}
134 WHERE COALESCE(m.account_name, '') != '系统消息'
135 GROUP BY m.id
136 HAVING messageCount > 0
137 ORDER BY messageCount DESC`
138 )
139 .all(...params) as Array<{
140 memberId: number
141 platformId: string
142 name: string
143 avatar: string | null
144 messageCount: number
145 }>
146
147 return rows.map((row) => ({
148 ...row,
149 percentage: totalMessages > 0 ? Math.round((row.messageCount / totalMessages) * 10000) / 100 : 0,
150 }))
151}
152
153/**
154 * 获取每小时活跃度分布

Callers 5

getMemberStatsMethod · 0.90
registerAnalyticsRoutesFunction · 0.90
getChatOverviewFunction · 0.90
cli.tsFile · 0.90

Calls 5

buildTimeFilterFunction · 0.90
buildSystemMessageFilterFunction · 0.90
getMethod · 0.65
prepareMethod · 0.65
allMethod · 0.65

Tested by

no test coverage detected