also looking at this
chore: wip stx improvements
#1649
241 files
+19022
-1323
| @@ -0,0 +1,187 @@ | ||
| 1 | import type { AnalyticsEvent, AnalyticsQuery, AnalyticsResult, AnalyticsStorage, PageView } from '../types' | |
| 2 | ||
| 3 | export class MemoryAnalyticsStorage implements AnalyticsStorage { | |
| 4 | pageViews: PageView[] = [] | |
| 5 | events: AnalyticsEvent[] = [] | |
| 6 | ||
| 7 | async savePageView(view: PageView): Promise<void> { | |
| 8 | this.pageViews.push(view) | |
| 9 | } | |
| 10 | ||
| 11 | async saveEvent(event: AnalyticsEvent): Promise<void> { | |
| 12 | this.events.push(event) | |
| 13 | } | |
| 14 | ||
| 15 | async getPageViews(query?: AnalyticsQuery): Promise<PageView[]> { | |
| 16 | let views = [...this.pageViews] | |
| 17 | views = filterByQuery(views, query) | |
| 18 | ||
| 19 | if (query?.limit) { | |
| 20 | views = views.slice(0, query.limit) | |
| 21 | } | |
| 22 | ||
| 23 | return views | |
| 24 | } | |
| 25 | ||
| 26 | async getEvents(query?: AnalyticsQuery): Promise<AnalyticsEvent[]> { | |
| 27 | let events = [...this.events] | |
| 28 | ||
| 29 | if (query?.from) { | |
| 30 | const fromTs = query.from.getTime() | |
| 31 | events = events.filter(e => e.timestamp >= fromTs) | |
| 32 | } | |
| 33 | if (query?.to) { | |
| 34 | const toTs = query.to.getTime() | |
| 35 | events = events.filter(e => e.timestamp <= toTs) | |
| 36 | } | |
| 37 | if (query?.event) { | |
| 38 | events = events.filter(e => e.name === query.event) | |
| 39 | } | |
| 40 | if (query?.path) { | |
| 41 | events = events.filter(e => e.path === query.path) | |
| 42 | } | |
| 43 | if (query?.limit) { | |
| 44 | events = events.slice(0, query.limit) | |
| 45 | } | |
| 46 | ||
| 47 | return events | |
| 48 | } | |
| 49 | ||
| 50 | async getStats(query?: AnalyticsQuery): Promise<AnalyticsResult> { | |
| 51 | const views = await this.getPageViews(query) | |
| 52 | const events = await this.getEvents(query) | |
| 53 | ||
| 54 | const totalPageViews = views.length | |
| 55 | ||
| 56 | const sessionIds = new Set<string>() | |
| 57 | for (const v of views) { | |
| 58 | if (v.sessionId) { | |
| 59 | sessionIds.add(v.sessionId) | |
| 60 | } | |
| 61 | } | |
| 62 | const uniqueVisitors = sessionIds.size | |
| 63 | ||
| 64 | // Top pages | |
| 65 | const pageCounts = new Map<string, number>() | |
| 66 | for (const v of views) { | |
| 67 | pageCounts.set(v.path, (pageCounts.get(v.path) || 0) + 1) | |
| 68 | } | |
| 69 | const topPages = Array.from(pageCounts.entries()) | |
| 70 | .map(([path, viewCount]) => ({ path, views: viewCount })) | |
| 71 | .sort((a, b) => b.views - a.views) | |
| 72 | ||
| 73 | // Top referrers | |
| 74 | const referrerCounts = new Map<string, number>() | |
| 75 | for (const v of views) { | |
| 76 | if (v.referrer) { | |
| 77 | referrerCounts.set(v.referrer, (referrerCounts.get(v.referrer) || 0) + 1) | |
| 78 | } | |
| 79 | } | |
| 80 | const topReferrers = Array.from(referrerCounts.entries()) | |
| 81 | .map(([referrer, count]) => ({ referrer, count })) | |
| 82 | .sort((a, b) => b.count - a.count) | |
| 83 | ||
| 84 | // Top countries | |
| 85 | const countryCounts = new Map<string, number>() | |
| 86 | for (const v of views) { | |
| 87 | if (v.country) { | |
| 88 | countryCounts.set(v.country, (countryCounts.get(v.country) || 0) + 1) | |
| 89 | } | |
| 90 | } | |
| 91 | const topCountries = Array.from(countryCounts.entries()) | |
| 92 | .map(([country, count]) => ({ country, count })) | |
| 93 | .sort((a, b) => b.count - a.count) | |
| 94 | ||
| 95 | // Average duration | |
| 96 | const viewsWithDuration = views.filter(v => v.duration != null) | |
| 97 | const averageDuration = viewsWithDuration.length > 0 | |
| 98 | ? viewsWithDuration.reduce((sum, v) => sum + (v.duration || 0), 0) / viewsWithDuration.length | |
| 99 | : undefined | |
| 100 | ||
| 101 | // Events aggregation | |
| 102 | const eventCounts = new Map<string, number>() | |
| 103 | for (const e of events) { | |
| 104 | eventCounts.set(e.name, (eventCounts.get(e.name) || 0) + 1) | |
| 105 | } | |
| 106 | const eventStats = Array.from(eventCounts.entries()) | |
| 107 | .map(([name, count]) => ({ name, count })) | |
| 108 | .sort((a, b) => b.count - a.count) | |
| 109 | ||
| 110 | // Group by period | |
| 111 | let pageViewsByPeriod: Array<{ period: string, count: number }> | undefined | |
| 112 | if (query?.groupBy === 'day' || query?.groupBy === 'week' || query?.groupBy === 'month') { | |
| 113 | const periodCounts = new Map<string, number>() | |
| 114 | for (const v of views) { | |
| 115 | const period = getPeriodKey(v.timestamp, query.groupBy) | |
| 116 | periodCounts.set(period, (periodCounts.get(period) || 0) + 1) | |
| 117 | } | |
| 118 | pageViewsByPeriod = Array.from(periodCounts.entries()) | |
| 119 | .map(([period, count]) => ({ period, count })) | |
| 120 | .sort((a, b) => a.period.localeCompare(b.period)) | |
| 121 | } | |
| 122 | ||
| 123 | return { | |
| 124 | totalPageViews, | |
| 125 | uniqueVisitors, | |
| 126 | topPages, | |
| 127 | topReferrers, | |
| 128 | topCountries, | |
| 129 | pageViewsByPeriod, | |
| 130 | events: eventStats.length > 0 ? eventStats : undefined, | |
| 131 | averageDuration, | |
| 132 | } | |
| 133 | } | |
| 134 | ||
| 135 | async clear(): Promise<void> { | |
| 136 | this.pageViews = [] | |
| 137 | this.events = [] | |
| 138 | } | |
| 139 | ||
| 140 | async count(): Promise<number> { | |
| 141 | return this.pageViews.length + this.events.length | |
| 142 | } | |
| 143 | } | |
| 144 | ||
| 145 | function filterByQuery(views: PageView[], query?: AnalyticsQuery): PageView[] { | |
| 146 | if (!query) | |
| 147 | return views | |
| 148 | ||
| 149 | if (query.from) { | |
| 150 | const fromTs = query.from.getTime() | |
| 151 | views = views.filter(v => v.timestamp >= fromTs) | |
| 152 | } | |
| 153 | if (query.to) { | |
| 154 | const toTs = query.to.getTime() | |
| 155 | views = views.filter(v => v.timestamp <= toTs) | |
| 156 | } | |
| 157 | if (query.path) { | |
| 158 | views = views.filter(v => v.path === query.path) | |
| 159 | } | |
| 160 | ||
| 161 | return views | |
| 162 | } | |
| 163 | ||
| 164 | function getPeriodKey(timestamp: number, groupBy: 'day' | 'week' | 'month'): string { | |
| 165 | const date = new Date(timestamp) | |
| 166 | const year = date.getFullYear() | |
| 167 | const month = String(date.getMonth() + 1).padStart(2, '0') | |
| 168 | const day = String(date.getDate()).padStart(2, '0') | |
| 169 | ||
| 170 | if (groupBy === 'day') { | |
| 171 | return `${year}-${month}-${day}` | |
| 172 | } | |
| 173 | ||
| 174 | if (groupBy === 'month') { | |
| 175 | return `${year}-${month}` | |
| 176 | } | |
| 177 | ||
| 178 | // week: use ISO week start (Monday) | |
| 179 | const d = new Date(timestamp) | |
| 180 | const dayOfWeek = d.getDay() | |
| 181 | const diff = d.getDate() - dayOfWeek + (dayOfWeek === 0 ? -6 : 1) | |
| 182 | const monday = new Date(d.setDate(diff)) | |
| 183 | const wYear = monday.getFullYear() | |
| 184 | const wMonth = String(monday.getMonth() + 1).padStart(2, '0') | |
| 185 | const wDay = String(monday.getDate()).padStart(2, '0') | |
| 186 | return `${wYear}-${wMonth}-${wDay}` | |
| 187 | } | |