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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
|
from __future__ import annotations
from collections import namedtuple
from dataclasses import dataclass, fields
from functools import cached_property
from typing import List, Optional
import numpy as np
from pydantic import (
BaseModel,
ConfigDict,
Field,
NonNegativeInt,
NonNegativeFloat,
PositiveFloat,
)
from typing_extensions import Self
from generalresearch.models.custom_types import AwareDatetimeISO, IPvAnyAddressStr
class Bounds(namedtuple("BoundsBase", ["left", "top", "width", "height"])):
__slots__ = ()
@property
def right(self):
return self.left + self.width
@property
def bottom(self):
return self.top + self.height
@dataclass(kw_only=True)
class Event:
type: str
# in microseconds, since page load (?)
timeStamp: float
# optional ID of the event target (e.g.: where the mouse is hovering)
_elementId: Optional[str] = None
# optional tag name of the event target
_elementTagName: Optional[str] = None
# extracted coordinates for the element being interacted with
_elementBounds: Optional[Bounds] = None
@classmethod
def from_dict(cls, data: dict) -> Self:
data = {k: v for k, v in data.items() if k in cls.__dataclass_fields__}
bounds = data.get("_elementBounds")
if bounds is not None and not isinstance(bounds, Bounds):
data = {**data, "_elementBounds": Bounds(**bounds)}
return cls(**data)
@dataclass
class PointerMove(Event):
# should always be 'pointermove'
type: str
# (mouse, touch, pen)
pointerType: str
# coordinate relative to the screen
screenX: float
screenY: float
# coordinate relative to the document (unaffected by scrolling)
pageX: float
pageY: float
# pageX/Y divided by the document Width/Height. This is calculated in JS and sent, which
# it must be b/c we don't know the document width/height at each time otherwise.
normalizedX: float
normalizedY: float
pointermove_keys = {f.name for f in fields(PointerMove)}
@dataclass
class MouseEvent(Event):
"""
More general than PointerMove. To be used for handling touch events/mobile
also, which don't generate pointermove events.
"""
# should be {'pointerdown', 'pointerup', 'pointermove', 'click'}
type: str
# Type of input (mouse, touch, pen)
pointerType: str
# coordinate relative to the document (unaffected by scrolling)
pageX: float
pageY: float
@dataclass
class KeyboardEvent(Event):
""" """
# should be {'keydown', 'input'}
type: str
# "insertText", "insertCompositionText", "deleteCompositionText",
# "insertFromComposition", "deleteContentBackward"
inputType: Optional[str]
# e.g., 'Enter', 'a', 'Backspace'
key: Optional[str] = None
# This is the actual text, if applicable
data: Optional[str] = None
@property
def key_text(self):
# if we get the input and keydown for a single char press, we don't need both
return (
f"<{self.key.upper()}>"
if self.key
and self.key.lower() not in {"unidentified", ""}
and len(self.key) > 1
else None
)
@property
def input_type_text(self):
return f"<{self.inputType.upper()}>" if self.inputType else None
@property
def text(self):
return self.data or self.key_text or self.input_type_text or ""
class TimingDataSummary(BaseModel):
"""
Summarizes the pings from a single TimingData
(measurements from a single websocket connection / session, for one user
on one IP)
"""
count: NonNegativeInt = Field(description="After filtering out outliers")
outlier_count: NonNegativeInt = Field()
outlier_frac: NonNegativeFloat = Field(ge=0, le=1)
median_log_rtt: PositiveFloat = Field()
mean_log_rtt: PositiveFloat = Field()
std_log_rtt: PositiveFloat = Field()
median_rtt: PositiveFloat = Field()
mean_rtt: PositiveFloat = Field()
std_rtt: PositiveFloat = Field()
class TimingData(BaseModel):
"""
Stores collected RTTs from websocket pings.
todo: can also store bandwidth info collected from router
"""
model_config = ConfigDict(extra="forbid", validate_assignment=True)
client_rtts: List[float] = Field()
server_rtts: List[float] = Field()
# Have to be optional for backwards-compatibility, but should always be set.
started_at: Optional[AwareDatetimeISO] = Field(default=None)
ended_at: Optional[AwareDatetimeISO] = Field(default=None)
client_ip: Optional[IPvAnyAddressStr] = Field(
description="This comes from the websocket request's headers",
examples=["72.39.217.116"],
default=None,
)
server_hostname: Optional[str] = Field(
description="The hostname of the server that handled this request",
examples=["grliq-web-0"],
default=None,
)
@property
def server_location(self) -> str:
# TODO: when we have more locations ...
return (
"fremont_ca"
if self.server_hostname in {"grliq-web-0", "grliq-web-1"}
else "fremont_ca"
)
@property
def has_data(self):
return len(self.client_rtts) > 0 and len(self.server_rtts) > 0
def filter_rtts(self, rtts):
# Skip the first 5 pings, unless we have <10 pings, then get the last
# 5 instead.
# The first couple pings are usually outliers as they are running
# when a lot of initial JS is also running.
if len(self.client_rtts) >= 10:
rtts = rtts[5:]
else:
rtts = rtts[-5:]
return rtts
@cached_property
def client_rtts_filtered(self):
return self.filter_rtts(self.client_rtts)
@property
def client_rtt_mean(self):
rtts = self.client_rtts_filtered
return sum(rtts) / len(rtts)
@cached_property
def server_rtts_filtered(self):
return self.filter_rtts(self.server_rtts)
@property
def server_rtt_mean(self):
rtts = self.server_rtts_filtered
return sum(rtts) / len(rtts)
@property
def filtered_rtts(self):
return self.server_rtts_filtered + self.client_rtts_filtered
@property
def cleaned_rtts(self):
# Trim outliers
rtts = np.array(self.filtered_rtts)
rtts = rtts[(rtts > 3) & (rtts < 1000)]
if rtts.size > 0:
p5, p95 = np.percentile(rtts, [5, 95])
rtts = rtts[(rtts >= p5) & (rtts <= p95)]
return rtts
@property
def summarize(self) -> Optional[TimingDataSummary]:
if len(self.filtered_rtts) < 5:
return None
orig_len = len(self.filtered_rtts)
rtts = np.array(self.cleaned_rtts)
if len(rtts) < 5:
# We started with 5 or more observations, but removed enough so that
# we have < 5 now. This is probably a signal of something
return None
log_rtts = np.log(rtts)
return TimingDataSummary(
count=len(rtts),
outlier_count=orig_len - len(rtts),
outlier_frac=(orig_len - len(rtts)) / orig_len,
median_rtt=float(np.median(rtts)),
mean_rtt=float(np.mean(rtts)),
std_rtt=float(np.std(rtts)),
mean_log_rtt=float(np.mean(log_rtts)),
median_log_rtt=float(np.median(log_rtts)),
std_log_rtt=float(np.std(log_rtts)),
)
|