Repository navigation
Expand file tree
/
Copy pathanalyze_stats.py
More file actions
256 lines (200 loc) · 7.82 KB
/
Copy pathanalyze_stats.py
File metadata and controls
256 lines (200 loc) · 7.82 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
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
251
252
253
254
255
256
from datetime import datetime, timedelta, timezone
import click
import matplotlib.pyplot as plt
import pandas as pd
from collect_stats import is_updater_asset
def _load_downloads():
df = pd.read_csv("data/stats.csv", index_col="timestamp", parse_dates=True)
df.index = pd.to_datetime(df.index, format="ISO8601", utc=True)
return df
def _load_firefox():
df = pd.read_csv("data/firefox-daily-users.csv", index_col="date", parse_dates=True)
df = df.tz_localize(tz=timezone.utc)
df.columns = ["Firefox DAU"]
return df
def _load_chrome():
df = pd.read_csv("data/chrome-weekly-users.csv", index_col="Date", parse_dates=True)
df = df.tz_localize(tz=timezone.utc)
df.columns = ["Chrome WAU"]
return df
def _load_android():
df = pd.read_csv("data/android/installed.csv", index_col="Date", parse_dates=True)
df = df.tz_localize(tz=timezone.utc)
df.drop(columns=["Notes"], inplace=True)
col_name = "Android installed devices"
df.columns = [col_name]
return df
def _load_android_crash_rate():
df = pd.read_csv("data/android-crash-rate.csv", index_col="date", parse_dates=True)
df = df.tz_localize(tz=timezone.utc)
df = df * 100 # fraction -> percent
df.columns = ["Android crash rate (%)"]
return df
def _load_android_rating():
df = pd.read_csv("data/android-ratings.csv", index_col="date", parse_dates=True)
df = df.tz_localize(tz=timezone.utc)
df.columns = ["Android Play Store rating"]
df = df[df.iloc[:, 0] > 0] # 0 = "no data" placeholder, not a rating
return df
def _load_assets(include_updater: bool = False):
"""Raw per-asset download counts over time (long format).
Columns: timestamp, tag, asset, platform, downloads. The collector logs an
asset only when its count changes, so this is sparse — see _asset_series()
for the forward-filled reconstruction.
Tauri updater assets (latest.json polls, .sig, .app.tar.gz) are dropped
unless `include_updater`: they count update checks, not downloads. See
updater_checks().
"""
df = pd.read_csv("data/stats-assets.csv")
df["timestamp"] = pd.to_datetime(df["timestamp"], utc=True)
if not include_updater:
df = df[~df["asset"].map(is_updater_asset)]
return df
def updater_checks(df=None):
"""Time series of Tauri update checks (latest.json fetches) per release.
Every running aw-tauri polls releases/latest/download/latest.json, so the
growth rate of the current release's count tracks active Tauri installs.
"""
df = _load_assets(include_updater=True) if df is None else df
df = df[df["asset"] == "latest.json"]
return _asset_series(df)
def _asset_series(df=None):
"""Wide per-(tag, asset) download counts, forward-filled over time.
Reconstructs each asset's running total at every observed timestamp from
the change-only log.
"""
df = _load_assets() if df is None else df
return (
df.pivot_table(index="timestamp", columns=["tag", "asset"], values="downloads")
.sort_index()
.ffill()
)
def _asset_meta(df):
"""Per-(tag, asset) metadata (platform), aligned to _asset_series columns."""
meta = df.drop_duplicates(["tag", "asset"]).set_index(["tag", "asset"])[["platform"]]
return meta.reindex(_asset_series(df).columns)
def platform_totals(df=None):
"""Time series of total downloads per platform."""
df = _load_assets() if df is None else df
wide = _asset_series(df)
plat = _asset_meta(df)["platform"]
totals = wide.T.groupby(plat.values).sum().T
totals.columns.name = "platform"
return totals
def version_platform_totals(df=None):
"""Time series of downloads per (version, platform)."""
df = _load_assets() if df is None else df
meta = _asset_meta(df)
wt = _asset_series(df).T
wt.index = pd.MultiIndex.from_arrays(
[meta.index.get_level_values("tag"), meta["platform"].values],
names=["tag", "platform"],
)
return wt.groupby(level=["tag", "platform"]).sum().T
def _load_data():
df = _load_downloads()
df = df.resample("1D").mean()
df = df.merge(_load_chrome(), how="outer", left_index=True, right_index=True)
df = df.merge(_load_firefox(), how="outer", left_index=True, right_index=True)
df = df.merge(_load_android(), how="outer", left_index=True, right_index=True)
df = df.merge(_load_android_crash_rate(), how="outer", left_index=True, right_index=True)
df = df.merge(_load_android_rating(), how="outer", left_index=True, right_index=True)
return df
def test_load():
_load_downloads()
_load_chrome()
_load_firefox()
_load_android()
_load_android_crash_rate()
_load_android_rating()
_load_assets()
platform_totals()
version_platform_totals()
def test_load_all():
_load_data()
@click.command()
@click.option("--column")
@click.option("--save")
@click.option("--since", type=click.DateTime(["%Y-%m-%d"]))
@click.option("--per-day", is_flag=True)
@click.option("--per-week", is_flag=True)
@click.option("--resample", default="1D")
@click.option("--title")
def main(
column: str | None = None,
save: str | None = None,
since: datetime | None = None,
per_day: bool = False,
per_week: bool = False,
resample: str = "1D",
title: str | None = None,
):
n_plots = 2 if per_day or per_week else 1
df = _load_data()
df = df.resample(resample).mean()
df = df.interpolate(method="time") # interpolate missing dates
if column:
if column not in df:
print(f"Error: No such column '{column}', try one of: {list(df.columns)}")
exit(1)
df = df[column]
if since:
df = df.truncate(before=since.astimezone(timezone.utc))
gridargs = dict(axis="both", linestyle="--", linewidth=1, alpha=0.4)
plt.figure(figsize=(8, 2.5 * n_plots))
ax1 = plt.subplot(n_plots, 1, 1)
df.plot(ax=ax1, title=column)
ax1.set_title(title if title is not None else "Cumulative")
ax1.set_ylim(0)
ax1.grid(True, which="major", **gridargs)
ax1.legend()
if n_plots >= 2:
ax = plt.subplot(n_plots, 1, 2) # , sharex=ax1)
df_w = df.diff()
df_w = df_w[df_w > 0] # Filter out the crazy outlier
df_w = df_w.resample("1D").mean()
# Always smooth out the day-to-day noise; per-week is the same daily
# rate scaled up by 7, not a different (jagged) series.
df_w = df_w.rolling("7D").mean()
if per_week:
df_w = df_w * 7
df_w.plot(ax=ax)
ax.set_title(f"Per {'week' if per_week else 'day'} (7d rolling mean)")
ax.set_ylim(0)
ax.grid(True, which="major", **gridargs)
ax.legend()
# if n_plots >= 2:
# ax2 = plt.subplot(n_plots, 1, 2, sharex=ax1)
# df_d = df.diff()
# df_d = df_d[df_d > 0] # Filter out the crazy outlier
# df_d.plot(ax=ax2)
# ax2.set_title("Per day")
# ax2.set_ylim(0)
plt.tight_layout()
# plt.subplots_adjust(hspace=0.10)
# print(df)
if save:
plt.savefig(save)
# plt.show()
else:
plt.show()
def calculate_goal_date(df, goal, sample_days=90):
# Get the last date and its corresponding downloads
last_date = df.index[-1]
last = df.iloc[-1]
# Calculate the daily download rate based on the last `sample_days` days
daily_rate = df[-sample_days:].diff().mean()
# Calculate the number of days until we reach the goal
days_until_goal = (goal - last) / daily_rate
# Calculate the date when we'll reach the goal
date_goal = last_date + timedelta(days=days_until_goal)
return date_goal
if __name__ == "__main__":
df = _load_data()
print(
f"Estimated date to reach 1,000,000 downloads: {calculate_goal_date(df['downloads'], 1000000)}"
)
print(
f"Estimated date to reach 10,000 stars: {calculate_goal_date(df['stars'], 10000)}"
)
main()