This repository was archived by the owner on Nov 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathAnalyticsAdminController.swift
More file actions
74 lines (64 loc) · 2.58 KB
/
AnalyticsAdminController.swift
File metadata and controls
74 lines (64 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//
// File.swift
//
//
// Created by Tibor Bodecs on 2021. 12. 26..
//
import FeatherCore
import Vapor
import SQLKit
import FeatherIcons
struct AnalyticsAdminController {
struct GroupCount: Decodable {
let name: String?
let count: Int
var finalName: String {
name ?? "Unknown"
}
}
struct MetricsGroup {
let icon: Svg
let name: String
let groups: [GroupCount]
var total: Int { groups.reduce(0, { $0 + $1.count }) }
}
/// This won't work with the MongoDB driver yet, see https://github.com/vapor/fluent-kit/issues/206
func count(_ req: Request, icon: Svg, name: String, groupBy group: String) async throws -> MetricsGroup? {
guard let db = req.db as? SQLDatabase else {
return nil
}
let sql = """
SELECT
count(id) AS `count`,
`\(group)` AS name
FROM analytics_logs
GROUP BY `\(group)`
ORDER BY count(id) DESC
LIMIT 10
"""
// WHERE name IS NOT NULL
// print(try await db.raw(SQLQueryString(sql)).all())
let values = try await db.raw(SQLQueryString(sql)).all(decoding: GroupCount.self).get()
return MetricsGroup(icon: icon, name: name, groups: values)
}
func overviewView(_ req: Request) async throws -> Response {
let totalPageViews = try await AnalyticsLogModel.query(on: req.db).count()
let browser = try await count(req, icon: .compass, name: "Browsers", groupBy: "browser_name")
let os = try await count(req, icon: .monitor, name: "Operating systems", groupBy: "os_name")
let lang = try await count(req, icon: .messageSquare, name: "Languages", groupBy: "language")
let path = try await count(req, icon: .anchor, name: "Pages", groupBy: "path")
let m = [browser, os, lang, path].compactMap { $0 }.map { value -> AnalyticsOverviewContext.Metrics in
let groups = value.groups.map { g -> AnalyticsOverviewContext.Metrics.Group in
.init(name: g.finalName,
count: g.count,
percent: String(format: "%.0f", Double(g.count) / Double(value.total) * 100))
}
return .init(icon: value.icon,
name: value.name,
total: value.total,
groups: groups)
}
let ctx = AnalyticsOverviewContext(totalPageViews: totalPageViews, metrics: m)
return req.templates.renderHtml(AnalyticsOverviewTemplate(ctx))
}
}