-
Notifications
You must be signed in to change notification settings - Fork 21
/
metric_adapter.ex
319 lines (252 loc) · 9.92 KB
/
metric_adapter.ex
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
defmodule Sanbase.Clickhouse.Github.MetricAdapter do
@behaviour Sanbase.Metric.Behaviour
import Sanbase.Metric.Transform
import Sanbase.Metric.Utils
import Sanbase.Utils.ErrorHandling, only: [not_implemented_function_for_metric_error: 2]
alias Sanbase.Project
alias Sanbase.Clickhouse.Github
@aggregations [:sum]
@timeseries_metrics_function_mapping %{
"dev_activity" => :dev_activity,
"github_activity" => :github_activity,
"dev_activity_contributors_count" => :dev_activity_contributors_count,
"github_activity_contributors_count" => :github_activity_contributors_count
}
@aggregated_metrics_function_mapping %{
"dev_activity" => :total_dev_activity,
"github_activity" => :total_github_activity,
"dev_activity_contributors_count" => :total_dev_activity_contributors_count,
"github_activity_contributors_count" => :total_github_activity_contributors_count
}
@timeseries_metrics Map.keys(@timeseries_metrics_function_mapping)
@histogram_metrics []
@table_metrics []
@metrics @histogram_metrics ++ @timeseries_metrics ++ @table_metrics
# plan related - the plan is upcase string
@min_plan_map Enum.into(@metrics, %{}, fn metric -> {metric, "FREE"} end)
# restriction related - the restriction is atom :free or :restricted
@access_map Enum.into(@metrics, %{}, fn metric -> {metric, :free} end)
@free_metrics @metrics
@restricted_metrics []
@required_selectors Enum.into(@metrics, %{}, &{&1, []})
@default_complexity_weight 0.3
@impl Sanbase.Metric.Behaviour
def has_incomplete_data?(_), do: false
@impl Sanbase.Metric.Behaviour
def complexity_weight(_), do: @default_complexity_weight
@impl Sanbase.Metric.Behaviour
def required_selectors(), do: @required_selectors
@impl Sanbase.Metric.Behaviour
def broken_data(metric, selector, from, to) do
__MODULE__.BrokenData.get(metric, selector, from, to)
end
@impl Sanbase.Metric.Behaviour
def timeseries_data(metric, %{organization: organization}, from, to, interval, opts) do
timeseries_data(metric, %{organizations: [organization]}, from, to, interval, opts)
end
def timeseries_data(metric, %{organizations: organizations}, from, to, interval, _opts) do
apply(
Github,
Map.get(@timeseries_metrics_function_mapping, metric),
[organizations, from, to, interval, "None", nil]
)
|> transform_to_value_pairs()
end
def timeseries_data(metric, %{slug: slug_or_slugs}, from, to, interval, _opts) do
case Project.List.github_organizations_by_slug(slug_or_slugs) do
%{} = empty_map when map_size(empty_map) == 0 ->
{:ok, []}
%{} = organizations_map ->
organizations = Map.values(organizations_map) |> List.flatten()
apply(
Github,
Map.get(@timeseries_metrics_function_mapping, metric),
[organizations, from, to, interval, "None", nil]
)
|> transform_to_value_pairs()
{:error, error} ->
{:error, error}
end
end
@impl Sanbase.Metric.Behaviour
def timeseries_data_per_slug(metric, _selector, _from, _to, _interval, _opts) do
not_implemented_function_for_metric_error("timeseries_data_per_slug", metric)
end
@impl Sanbase.Metric.Behaviour
def aggregated_timeseries_data(metric, %{organization: organization}, from, to, opts) do
aggregated_timeseries_data(metric, %{organizations: [organization]}, from, to, opts)
end
def aggregated_timeseries_data(metric, %{organizations: organizations}, from, to, _opts)
when is_binary(organizations) or is_list(organizations) do
apply(
Github,
Map.get(@aggregated_metrics_function_mapping, metric),
[
List.wrap(organizations),
from,
to
]
)
end
def aggregated_timeseries_data(metric, %{slug: slug_or_slugs}, from, to, opts) do
slugs = slug_or_slugs |> List.wrap()
projects = Project.List.by_slugs(slugs, preload?: true, preload: [:github_organizations])
org_to_slug_map = github_organization_to_slug_map(projects)
organizations = github_organizatoins_of_projects(projects)
case aggregated_timeseries_data(metric, %{organizations: organizations}, from, to, opts) do
{:ok, map} ->
result =
Enum.reduce(map, %{}, fn {org, value}, acc ->
slug = Map.get(org_to_slug_map, org)
Map.update(acc, slug, value, &(&1 + value))
end)
{:ok, result}
{:error, error} ->
{:error, error}
end
end
defp github_organizatoins_of_projects(projects) do
projects
|> Enum.map(&Project.github_organizations/1)
|> Enum.filter(&match?({:ok, _}, &1))
|> Enum.map(&elem(&1, 1))
|> List.flatten()
end
defp github_organization_to_slug_map(projects) do
projects
|> Enum.flat_map(fn project ->
project.github_organizations
|> Enum.map(fn org -> {String.downcase(org.organization), project.slug} end)
end)
|> Map.new()
end
@impl Sanbase.Metric.Behaviour
def slugs_by_filter(_metric, _from, _to, _operator, _threshold, _opts) do
{:error, "Slugs filtering is not implemented for github data. Use `dev_activity_1d` instead"}
end
@impl Sanbase.Metric.Behaviour
def slugs_order(_metric, _from, _to, _direction, _opts) do
{:error, "Slugs ordering is not implemented for github data. Use `dev_activity_1d` instead"}
end
@impl Sanbase.Metric.Behaviour
def first_datetime(_metric, %{organization: organization}) when is_binary(organization) do
first_datetime_for_organizations([organization])
end
def first_datetime(_metric, %{slug: slug}) when is_binary(slug) do
case Project.github_organizations(slug) do
{:ok, organizations} when is_list(organizations) ->
first_datetime_for_organizations(organizations)
{:error, error} ->
{:error, error}
end
end
@impl Sanbase.Metric.Behaviour
def last_datetime_computed_at(_metric, %{organization: organization})
when is_binary(organization) do
last_datetime_computed_at_for_organizations([organization])
end
def last_datetime_computed_at(_metric, %{slug: slug}) when is_binary(slug) do
case Project.github_organizations(slug) do
{:ok, organizations} when is_list(organizations) ->
last_datetime_computed_at_for_organizations(organizations)
{:error, error} ->
{:error, error}
end
end
@impl Sanbase.Metric.Behaviour
def metadata(metric) do
{:ok,
%{
metric: metric,
internal_metric: metric,
has_incomplete_data: has_incomplete_data?(metric),
min_interval: "5m",
default_aggregation: :sum,
available_aggregations: @aggregations,
available_selectors: [:slug],
required_selectors: @required_selectors[metric],
data_type: :timeseries,
is_timebound: false,
complexity_weight: @default_complexity_weight,
docs: Enum.map(docs_links(metric), fn l -> %{link: l} end),
is_label_fqn_metric: false,
is_deprecated: false,
hard_deprecate_after: nil
}}
end
@impl Sanbase.Metric.Behaviour
def human_readable_name(metric) do
case metric do
"dev_activity" ->
{:ok, "Development Activity"}
"github_activity" ->
{:ok, "Github Activity"}
"dev_activity_contributors_count" ->
{:ok, "Number of Github contributors (related to dev activity events)"}
"github_activity_contributors_count" ->
{:ok, "Number of all Github contributors"}
end
end
def docs_links(metric) do
link = fn page -> "https://academy.santiment.net/metrics/development-activity/#{page}" end
case metric do
"dev_activity" -> [link.("development-activity")]
"dev_activity_contributors_count" -> [link.("development-activity-contributors-count")]
"github_activity" -> [link.("github-activity")]
"github_activity_contributors_count" -> [link.("github-activity-contributors-count")]
end
end
@impl Sanbase.Metric.Behaviour
def available_aggregations(), do: @aggregations
@impl Sanbase.Metric.Behaviour
def available_timeseries_metrics(), do: @timeseries_metrics
@impl Sanbase.Metric.Behaviour
def available_histogram_metrics(), do: @histogram_metrics
@impl Sanbase.Metric.Behaviour
def available_table_metrics(), do: @table_metrics
@impl Sanbase.Metric.Behaviour
def available_metrics(), do: @metrics
@impl Sanbase.Metric.Behaviour
def available_metrics(%{address: _address}), do: []
def available_metrics(%{contract_address: contract_address}) do
available_metrics_for_contract(__MODULE__, contract_address)
end
def available_metrics(%{slug: slug}) when is_binary(slug) do
case Project.github_organizations(slug) do
{:ok, []} ->
{:ok, []}
{:ok, organizations} when is_list(organizations) ->
{:ok, @metrics}
{:error, error} ->
{:error, error}
end
end
@impl Sanbase.Metric.Behaviour
def available_slugs() do
# Providing a 2 element tuple `{any, integer}` will use that second element
# as TTL for the cache key
cache_key = {__MODULE__, :slugs_with_github_org}
Sanbase.Cache.get_or_store({cache_key, 600}, fn ->
{:ok, Project.List.slugs_with_github_organization()}
end)
end
@impl Sanbase.Metric.Behaviour
def available_slugs(metric) when metric in @metrics do
available_slugs()
end
@impl Sanbase.Metric.Behaviour
def incomplete_metrics(), do: []
@impl Sanbase.Metric.Behaviour
def free_metrics(), do: @free_metrics
@impl Sanbase.Metric.Behaviour
def restricted_metrics(), do: @restricted_metrics
@impl Sanbase.Metric.Behaviour
def access_map(), do: @access_map
@impl Sanbase.Metric.Behaviour
def min_plan_map(), do: @min_plan_map
defp first_datetime_for_organizations([]), do: {:ok, nil}
defp first_datetime_for_organizations(organizations), do: Github.first_datetime(organizations)
defp last_datetime_computed_at_for_organizations([]), do: {:ok, nil}
defp last_datetime_computed_at_for_organizations(organizations),
do: Github.last_datetime_computed_at(organizations)
end