Skip to content

Commit

Permalink
feat(analytics): enhance average stats with early usage
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Dec 19, 2023
1 parent 97eb264 commit f328a68
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
11 changes: 9 additions & 2 deletions plugins/analytics/client/numbers/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<numeric icon="analytic:heart" title="今日 DAU">
<template #default>{{ store.analytics.dauHistory[0] }}</template>
<template #footer-left>近期 DAU</template>
<template #footer-right>{{ +(store.analytics.dauHistory.slice(1).reduce((a, b) => a + b, 0) / 7).toFixed(1) }}</template>
<template #footer-right>{{ +recentDau.toFixed(1) }}</template>
</numeric>
</k-slot-item>
</k-slot>
Expand All @@ -24,13 +24,20 @@

<script setup lang="ts">
import { provide } from 'vue'
import { provide, computed } from 'vue'
import { store } from '@koishijs/client'
import {} from '@koishijs/plugin-analytics/src'
import Numeric from './numeric.vue'
provide('component:analytic-number', Numeric)
const recentDau = computed(() => {
const data = store.analytics.dauHistory.slice(1)
const historyLength = store.analytics.dauHistory.length - 1
if (!historyLength) return 0
return data.reduce((a, b) => a + b, 0) / Math.min(data.length, historyLength)
})
</script>

<style lang="scss" scoped>
Expand Down
2 changes: 1 addition & 1 deletion plugins/analytics/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@koishijs/plugin-analytics",
"description": "Show statistics for Koishi application",
"version": "2.0.3",
"version": "2.0.5",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"files": [
Expand Down
38 changes: 25 additions & 13 deletions plugins/analytics/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class Analytics extends DataService<Analytics.Payload> {
}
}

private async getCommandRate() {
private async getCommandRate(lengthTask: Promise<number>) {
const data = await this.ctx.database
.select('analytics.command', {
date: this.queryRecent(),
Expand All @@ -157,32 +157,33 @@ class Analytics extends DataService<Analytics.Payload> {
count: row => $.sum(row.count),
})
.execute()
const length = await lengthTask
const result = {} as Dict<number>
data.forEach((stat) => {
result[stat.name] = stat.count / this.config.recentDayCount
result[stat.name] = stat.count / length
})
return result
}

private async getDauHistory() {
const data = await this.ctx.database
.select('analytics.command', {
date: { $gte: Time.getDateNumber() - 7 },
date: { $gte: Time.getDateNumber() - this.config.recentDayCount },
userId: { $gt: 0 },
})
.groupBy(['date'], {
count: row => $.count(row.userId),
})
.execute()
const result: number[] = new Array(8).fill(0)
const result: number[] = new Array(this.config.recentDayCount + 1).fill(0)
const today = Time.getDateNumber()
data.forEach((stat) => {
result[today - stat.date] = stat.count
})
return result
}

private async getMessageByBot() {
private async getMessageByBot(lengthTask: Promise<number>) {
const data = await this.ctx.database
.select('analytics.message', {
date: this.queryRecent(),
Expand All @@ -191,21 +192,24 @@ class Analytics extends DataService<Analytics.Payload> {
count: row => $.sum(row.count),
})
.execute()
const length = await lengthTask
const result = {} as Dict<Dict<MessageStats & Universal.User>>
data.forEach((stat) => {
const entry = (result[stat.platform] ||= {})[stat.selfId] ||= {
...this.ctx.bots[`${stat.platform}:${stat.selfId}`]?.user,
send: 0,
receive: 0,
}
entry[stat.type] = stat.count / this.config.recentDayCount
entry[stat.type] = stat.count / length
})
return result
}

private async getMessageByDate() {
const data = await this.ctx.database
.select('analytics.message')
.select('analytics.message', {
date: { $lt: Time.getDateNumber() },
})
.groupBy(['type', 'date'], {
count: row => $.sum(row.count),
})
Expand All @@ -217,10 +221,13 @@ class Analytics extends DataService<Analytics.Payload> {
const entry = result[today - stat.date] ||= { send: 0, receive: 0 }
entry[stat.type] = stat.count
})
for (let i = 0; i < result.length; i++) {
result[i] ||= { send: 0, receive: 0 }
}
return result
}

private async getMessageByHour() {
private async getMessageByHour(lengthTask: Promise<number>) {
const data = await this.ctx.database
.select('analytics.message', {
date: this.queryRecent(),
Expand All @@ -229,14 +236,19 @@ class Analytics extends DataService<Analytics.Payload> {
count: row => $.sum(row.count),
})
.execute()
const length = await lengthTask
const result = new Array(24).fill(null).map(() => ({ send: 0, receive: 0 }))
data.forEach((stat) => {
result[stat.hour][stat.type] = stat.count / this.config.recentDayCount
result[stat.hour][stat.type] = stat.count / length
})
return result
}

async download(): Promise<Analytics.Payload> {
const messageByDateTask = this.getMessageByDate()
const lengthTask = messageByDateTask.then((data) => {
return Math.min(Math.max(data.length - 1, 1), this.config.recentDayCount)
})
const [
userCount,
userIncrement,
Expand All @@ -261,11 +273,11 @@ class Analytics extends DataService<Analytics.Payload> {
$.gte(row.createdAt, Time.fromDateNumber(Time.getDateNumber() - 1)),
$.lt(row.createdAt, Time.fromDateNumber(Time.getDateNumber())),
)),
this.getCommandRate(),
this.getCommandRate(lengthTask),
this.getDauHistory(),
this.getMessageByBot(),
this.getMessageByDate(),
this.getMessageByHour(),
this.getMessageByBot(lengthTask),
messageByDateTask,
this.getMessageByHour(lengthTask),
])
return {
userCount,
Expand Down

0 comments on commit f328a68

Please sign in to comment.