-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathwh_analytics.py
198 lines (166 loc) · 5.66 KB
/
wh_analytics.py
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# Wahoo! Results - https://github.com/JohnStrunk/wahoo-results
# Copyright (C) 2020 - John D. Strunk
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
"""Application usage analytics"""
import locale
import platform
import socket
import time
from pprint import pprint
from typing import Any, Dict, Optional, Tuple
import ipinfo # type: ignore
import requests
import sentry_sdk
from segment import analytics # type: ignore
import version
from model import Model
_CONTEXT: Dict[str, Any] = {}
def application_start(model: Model, screen_size: Tuple[int, int]) -> None:
"""Event for application startup"""
analytics.write_key = version.SEGMENT_WRITE_KEY
analytics.send = model.analytics.get()
global _CONTEXT # pylint: disable=global-statement
_CONTEXT = {
"context": _setup_context(screen_size),
"race_count": 0,
"race_count_with_names": 0,
"session_start": time.time(),
"user_id": model.client_id.get(),
}
analytics.identify(
user_id=_CONTEXT["user_id"],
context=_CONTEXT["context"],
traits=_CONTEXT["context"]["traits"],
)
_send_event("Scoreboard started")
def application_stop(model: Model) -> None:
"""Event for application shutdown"""
_send_event(
"Scoreboard stopped",
{
"runtime": time.time() - _CONTEXT["session_start"],
"race_count": _CONTEXT["race_count"],
"race_count_with_names": _CONTEXT["race_count_with_names"],
"lane_count": model.num_lanes.get(),
"time_threshold": model.time_threshold.get(),
"min_times": model.min_times.get(),
"bg_image": model.image_bg.get() != "",
"normal_font": model.font_normal.get(),
"time_font": model.font_time.get(),
},
)
analytics.shutdown()
def results_received(has_names: bool, chromecasts: int) -> None:
"""Event for race results"""
_CONTEXT["race_count"] += 1
if has_names:
_CONTEXT["race_count_with_names"] += 1
_send_event(
"Results received", {"has_names": has_names, "chromecast_count": chromecasts}
)
def documentation_link() -> None:
"""Follow link to docs event"""
_send_event("Documentation click")
def update_link() -> None:
"""Follow link to dl latest version event"""
_send_event("DownloadUpdate click")
def set_cts_directory(changed: bool) -> None:
"""Set CTS start list directory"""
_send_event(
"Browse CTS directory",
{
"directory_changed": changed,
},
)
def wrote_dolphin_csv(num_events: int) -> None:
"""Dolphin CSV event list written"""
_send_event(
"Write Dolphin CSV",
{
"event_count": num_events,
},
)
def set_do4_directory(changed: bool) -> None:
"""Set directory to watch for do4 files"""
_send_event(
"Browse D04 directory",
{
"directory_changed": changed,
},
)
def cc_toggle(enable: bool) -> None:
"""Enable/disable Chromecast"""
_send_event(
"Set Chromecast state",
{
"enable": enable,
},
)
def _send_event(name: str, kvparams: Optional[Dict[str, Any]] = None) -> None:
with sentry_sdk.start_span(op="analytics", description="Process analytics event"):
if "user_id" not in _CONTEXT:
return
if kvparams is None:
kvparams = {}
analytics.track(
_CONTEXT["user_id"], name, kvparams, context=_CONTEXT["context"]
)
if analytics.write_key == "unknown": # dev environment
print(f"Event: {name}")
pprint(kvparams)
def _setup_context(screen_size: Tuple[int, int]) -> Dict[str, Any]:
uname = platform.uname()
# https://segment.com/docs/connections/spec/identify/#traits
traits: Dict[str, Any] = {}
if hasattr(socket, "gethostname"):
traits["name"] = socket.gethostname()
# https://segment.com/docs/connections/spec/common/#context
context: Dict[str, Any] = {
"app": {
"version": version.WAHOO_RESULTS_VERSION,
},
"locale": locale.getlocale()[0],
"os": {
"name": uname.system,
"version": uname.version,
},
"screen": {
"height": screen_size[1],
"width": screen_size[0],
},
"traits": traits,
}
try:
iphandler = ipinfo.getHandler(version.IPINFO_TOKEN)
ipdetails = iphandler.getDetails()
traits["address"] = {
"city": ipdetails.city,
"state": ipdetails.region,
"country": ipdetails.country_name,
"postalCode": ipdetails.postal,
}
context["ip"] = ipdetails.ip
context["location"] = {
"city": ipdetails.city,
"region": ipdetails.region,
"country": ipdetails.country_name,
"latitude": ipdetails.latitude,
"longitude": ipdetails.longitude,
}
context["timezone"] = ipdetails.timezone
except requests.ConnectTimeout:
pass
print(context)
return context