๐Ÿ“ฆ navidrome / insights

๐Ÿ“„ summary_test.go ยท 176 lines
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176package summary

import (
	"maps"
	"slices"
	"testing"

	"github.com/navidrome/navidrome/core/metrics/insights"
	. "github.com/onsi/ginkgo/v2"
	. "github.com/onsi/gomega"
)

func TestSummary(t *testing.T) {
	RegisterFailHandler(Fail)
	RunSpecs(t, "Summary Suite")
}

var _ = Describe("Summary", func() {
	Describe("mapToBins", func() {
		var counters map[string]uint64
		var testBins = []int64{0, 1, 5, 10, 20, 50, 100, 200, 500, 1000}

		BeforeEach(func() {
			counters = make(map[string]uint64)
		})

		It("should map count to the correct bin", func() {
			mapToBins(0, testBins, counters)
			Expect(counters["0"]).To(Equal(uint64(1)))

			mapToBins(1, testBins, counters)
			Expect(counters["1"]).To(Equal(uint64(1)))

			mapToBins(10, testBins, counters)
			Expect(counters["10"]).To(Equal(uint64(1)))

			mapToBins(101, testBins, counters)
			Expect(counters["100"]).To(Equal(uint64(1)))

			mapToBins(1000, testBins, counters)
			Expect(counters["1000"]).To(Equal(uint64(1)))
		})

		It("should map count to the highest bin if count exceeds all bins", func() {
			mapToBins(2000, testBins, counters)
			Expect(counters["1000"]).To(Equal(uint64(1)))
		})

		It("should increment the correct bin count", func() {
			mapToBins(5, testBins, counters)
			mapToBins(5, testBins, counters)
			Expect(counters["5"]).To(Equal(uint64(2)))
		})

		It("should handle empty bins array", func() {
			mapToBins(5, []int64{}, counters)
			Expect(counters).To(BeEmpty())
		})
	})

	DescribeTable("mapVersion",
		func(expected string, data insights.Data) {
			Expect(mapVersion(data)).To(Equal(expected))
		},
		Entry("should map version", "0.54.2 (0b184893)", insights.Data{Version: "0.54.2 (0b184893)"}),
		Entry("should map version with long hash", "0.54.2 (0b184893)", insights.Data{Version: "0.54.2 (0b184893278620bb421a85c8b47df36900cd4df7)"}),
		Entry("should map version with no hash", "dev", insights.Data{Version: "dev"}),
		Entry("should map version with other values", "0.54.3 (source_archive)", insights.Data{Version: "0.54.3 (source_archive)"}),
		Entry("should map any version with a hash", "0.54.3-SNAPSHOT (734eb30a)", insights.Data{Version: "0.54.3-SNAPSHOT (734eb30a)"}),
	)

	DescribeTable("mapOS",
		func(expected string, data insights.Data) {
			Expect(mapOS(data)).To(Equal(expected))
		},
		Entry("should map darwin to macOS", "macOS - x86_64", insights.Data{OS: insightsOS{Type: "darwin", Arch: "x86_64"}}),
		Entry("should map linux to Linux", "Linux - x86_64", insights.Data{OS: insightsOS{Type: "linux", Arch: "x86_64"}}),
		Entry("should map containerized linux to Linux (containerized)", "Linux (containerized) - x86_64", insights.Data{OS: insightsOS{Type: "linux", Containerized: true, Arch: "x86_64"}}),
		Entry("should map bsd to BSD", "FreeBSD - x86_64", insights.Data{OS: insightsOS{Type: "freebsd", Arch: "x86_64"}}),
		Entry("should map unknown OS types", "Unknown - x86_64", insights.Data{OS: insightsOS{Type: "unknown", Arch: "x86_64"}}),
	)
	Describe("calcStats", func() {
		It("should return nil for empty slice", func() {
			Expect(calcStats([]int64{})).To(BeNil())
		})

		It("should calculate stats for a single value", func() {
			stats := calcStats([]int64{42})
			Expect(stats.Min).To(Equal(int64(42)))
			Expect(stats.Max).To(Equal(int64(42)))
			Expect(stats.Mean).To(Equal(float64(42)))
			Expect(stats.Median).To(Equal(float64(42)))
			Expect(stats.StdDev).To(Equal(float64(0)))
		})

		It("should calculate stats for odd number of values", func() {
			stats := calcStats([]int64{1, 2, 3, 4, 5})
			Expect(stats.Min).To(Equal(int64(1)))
			Expect(stats.Max).To(Equal(int64(5)))
			Expect(stats.Mean).To(Equal(float64(3)))
			Expect(stats.Median).To(Equal(float64(3)))
			Expect(stats.StdDev).To(BeNumerically("~", 1.414, 0.001))
		})

		It("should calculate stats for even number of values", func() {
			stats := calcStats([]int64{1, 2, 3, 4})
			Expect(stats.Min).To(Equal(int64(1)))
			Expect(stats.Max).To(Equal(int64(4)))
			Expect(stats.Mean).To(Equal(float64(2.5)))
			Expect(stats.Median).To(Equal(float64(2.5)))
			Expect(stats.StdDev).To(BeNumerically("~", 1.118, 0.001))
		})

		It("should handle unsorted input", func() {
			stats := calcStats([]int64{5, 1, 3, 2, 4})
			Expect(stats.Min).To(Equal(int64(1)))
			Expect(stats.Max).To(Equal(int64(5)))
			Expect(stats.Median).To(Equal(float64(3)))
		})

		It("should handle values with zeros", func() {
			stats := calcStats([]int64{0, 0, 10, 20})
			Expect(stats.Min).To(Equal(int64(0)))
			Expect(stats.Max).To(Equal(int64(20)))
			Expect(stats.Mean).To(Equal(float64(7.5)))
			Expect(stats.Median).To(Equal(float64(5)))
		})
	})

	DescribeTable("mapPlayerTypes",
		func(data insights.Data, expected map[string]uint64) {
			players := make(map[string]uint64)
			c := mapPlayerTypes(data, players)
			Expect(players).To(Equal(expected))
			values := slices.Collect(maps.Values(expected))
			var total uint64
			for _, v := range values {
				total += v
			}
			Expect(c).To(Equal(int64(total)))
		},
		Entry("Feishin player", insights.Data{Library: insightsLibrary{ActivePlayers: map[string]int64{"feishin_": 1, "Feishin": 1}}}, map[string]uint64{"Feishin": 1}),
		Entry("NavidromeUI player", insights.Data{Library: insightsLibrary{ActivePlayers: map[string]int64{"NavidromeUI_1.0": 2}}}, map[string]uint64{"NavidromeUI": 2}),
		Entry("play:Sub player", insights.Data{Library: insightsLibrary{ActivePlayers: map[string]int64{"playSub_iPhone11": 2, "playSub": 1}}}, map[string]uint64{"play:Sub": 2}),
		Entry("audrey player", insights.Data{Library: insightsLibrary{ActivePlayers: map[string]int64{"eu.callcc.audrey": 4}}}, map[string]uint64{"audrey": 4}),
		Entry("discard DSubCC player", insights.Data{Library: insightsLibrary{ActivePlayers: map[string]int64{"DSubCC": 5}}}, map[string]uint64{}),
		Entry("bonob player", insights.Data{Library: insightsLibrary{ActivePlayers: map[string]int64{"bonob": 6, "bonob+ogg": 4}}}, map[string]uint64{"bonob": 6}),
		Entry("Airsonic Refix player", insights.Data{Library: insightsLibrary{ActivePlayers: map[string]int64{"http://airsonic.netlify.app": 7}}}, map[string]uint64{"Airsonic Refix": 7}),
		Entry("Airsonic Refix player (HTTPS)", insights.Data{Library: insightsLibrary{ActivePlayers: map[string]int64{"https://airsonic.netlify.app": 7}}}, map[string]uint64{"Airsonic Refix": 7}),
		Entry("Multiple players", insights.Data{Library: insightsLibrary{ActivePlayers: map[string]int64{"Feishin": 1, "NavidromeUI_1.0": 2, "playSub_1.0": 3, "eu.callcc.audrey": 4, "DSubCC": 5, "bonob": 6, "bonob+ogg": 4, "http://airsonic.netlify.app": 7}}},
			map[string]uint64{"Feishin": 1, "NavidromeUI": 2, "play:Sub": 3, "audrey": 4, "bonob": 6, "Airsonic Refix": 7}),
	)
})

type insightsOS struct {
	Type          string `json:"type"`
	Distro        string `json:"distro,omitempty"`
	Version       string `json:"version,omitempty"`
	Containerized bool   `json:"containerized"`
	Arch          string `json:"arch"`
	NumCPU        int    `json:"numCPU"`
	Package       string `json:"package,omitempty"`
}

type insightsLibrary struct {
	Tracks        int64            `json:"tracks"`
	Albums        int64            `json:"albums"`
	Artists       int64            `json:"artists"`
	Playlists     int64            `json:"playlists"`
	Shares        int64            `json:"shares"`
	Radios        int64            `json:"radios"`
	Libraries     int64            `json:"libraries"`
	ActiveUsers   int64            `json:"activeUsers"`
	ActivePlayers map[string]int64 `json:"activePlayers,omitempty"`
}