-
Notifications
You must be signed in to change notification settings - Fork 0
/
height_export.exs
303 lines (246 loc) · 8.1 KB
/
height_export.exs
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
Mix.install([:jason, :finch, :nimble_csv])
defmodule Height do
alias NimbleCSV.RFC4180, as: CSV
require Logger
def run do
Finch.start_link(name: Height)
:telemetry.attach(__MODULE__, [:finch, :request, :stop], &__MODULE__.handle_event/4, nil)
{opts, _argv} = OptionParser.parse!(System.argv(), strict: [list: :string])
list_name = Keyword.get(opts, :list, "stories")
status_map = get_status_map()
list = get_list(list_name, status_map)
list
|> get_tasks(status_map)
|> map("Getting activities", fn task ->
activities = get_activities(task, list, status_map)
Map.put(task, :activities, activities)
end)
|> tap(&write_to_json(&1, list))
|> tap(&write_to_csv(&1, list))
end
defp write_to_json(result, list) do
file_name = "height-#{list.key}-#{Date.utc_today()}.json"
file_path = Path.join(File.cwd!(), file_name)
result
|> Jason.encode!(pretty: true)
|> then(&File.write!(file_path, &1))
Logger.info("Results written to #{file_path}")
end
defp write_to_csv(result, list) do
file_name = "height-#{list.key}-#{Date.utc_today()}.csv"
file_path = Path.join(File.cwd!(), file_name)
result
|> Enum.map(fn task ->
[
task.index,
task.name
] ++ Enum.map(list.visible_sections, &get_date_of_status_change(task, &1))
end)
|> prepend(["#id", "item name"] ++ list.visible_sections)
|> CSV.dump_to_iodata()
|> then(&File.write!(file_path, &1))
Logger.info("Results written to #{file_path}")
end
defp get_date_of_status_change(task, status) do
case Enum.find(task.activities, fn a -> a.status == status end) do
nil -> nil
%{} = activity -> Calendar.strftime(activity.created_at, "%d/%m/%y")
end
end
defp prepend(list, item) do
[item | list]
end
defp map(list, action, fun) do
total = Enum.count(list)
list
|> Enum.with_index(1)
|> Enum.map(fn {item, index} ->
Logger.info("#{action} - #{index} of #{total}")
fun.(item)
end)
end
defp get_status_map do
"/fieldTemplates"
|> get()
|> Map.fetch!("list")
|> Enum.filter(fn template -> template["name"] == "Status" end)
|> Enum.flat_map(fn template -> template["labels"] end)
|> Map.new(fn template ->
{template["id"], %{value: template["value"], deleted: template["deleted"]}}
end)
end
defp get_tasks(list, status_map) do
filters = %{
"listIds" => %{"values" => [list.id]}
}
"/tasks"
|> get(%{"filters" => Jason.encode!(filters)})
|> Map.fetch!("list")
|> Enum.reject(& &1["deleted"])
|> Enum.map(fn task ->
{:ok, datetime, _tz} = DateTime.from_iso8601(task["createdAt"])
%{
id: task["id"],
created_at: DateTime.to_date(datetime),
status: Map.fetch!(status_map, task["status"]).value,
name: task["name"],
index: task["index"]
}
end)
|> Enum.filter(&supported_status?(&1.status, list))
|> Enum.sort_by(& &1.created_at, Date)
end
defp get_activities(task, list, status_map) do
"/activities"
|> get(%{"taskId" => task.id})
|> Map.fetch!("list")
|> Enum.filter(fn activity -> activity["type"] == "statusChange" end)
|> Enum.map(fn activity ->
{:ok, datetime, _tz} = DateTime.from_iso8601(activity["createdAt"])
%{
status: Map.fetch!(status_map, activity["newValue"]).value,
created_at: DateTime.to_date(datetime)
}
end)
|> Enum.sort_by(& &1.created_at, Date)
|> Enum.uniq_by(fn activity -> activity.status end)
|> Enum.filter(&supported_status?(&1.status, list))
|> Enum.reject(&status_surpassed?(&1.status, task.status, list))
|> fill_middle_status(list)
end
defp fill_middle_status(activities, list) do
reversed_sections = Enum.reverse(list.visible_sections)
indexed_activities = Map.new(activities, fn a -> {a.status, a} end)
reversed_sections
|> Enum.with_index()
|> Enum.reduce(indexed_activities, fn {section, index}, acc ->
previous_status = Enum.at(reversed_sections, index + 1)
if Map.has_key?(acc, section) and not Map.has_key?(acc, previous_status) and
Enum.count(reversed_sections) - 1 != index do
activity = Map.fetch!(acc, section)
Map.put(acc, previous_status, %{activity | status: previous_status})
else
acc
end
end)
|> Map.values()
|> Enum.sort_by(fn a ->
Enum.find_index(list.visible_sections, fn section -> section == a.status end)
end)
end
defp get_list(key, status_map) do
list =
"/lists"
|> get()
|> Map.fetch!("list")
|> Enum.find(fn item -> item["key"] == key end)
visible_sections =
list["viewBy"]["visibleSectionIds"]
|> Enum.map(fn status ->
Map.fetch!(status_map, status)
end)
|> Enum.reject(& &1.deleted)
|> Enum.map(& &1.value)
%{id: list["id"], visible_sections: visible_sections, key: key}
end
defp supported_status?(status, list) do
Enum.member?(list.visible_sections, status)
end
defp status_surpassed?(activity_status, task_status, list) do
Enum.find_index(list.visible_sections, fn status -> status == activity_status end) >
Enum.find_index(list.visible_sections, fn status -> status == task_status end)
end
defp get(path, query \\ %{}) do
url =
"https://api.height.app"
|> URI.new!()
|> Map.put(:path, path)
|> Map.put(:query, URI.encode_query(query))
|> URI.to_string()
method = :get
method
|> with_cache(url, fn ->
with_retry(fn -> request(method, url) end)
end)
|> Jason.decode!()
end
defp request(method, url) do
api_secret = System.fetch_env!("HEIGHT_SECRET_KEY")
method
|> Finch.build(url, [{"Authorization", "api-key #{api_secret}"}])
|> Finch.request(Height)
end
defp with_retry(request_fun, attempt \\ 1) do
case request_fun.() do
{:ok, %{status: 200, body: body}} ->
body
result ->
if attempt >= 10 do
raise "max attempts exceeded"
else
time_to_sleep =
case result do
{:ok, %{status: 429, headers: headers}} ->
Logger.info("Rate limited")
value = :proplists.get_value("retry-after", headers)
String.to_integer(value) * 1000
_other ->
attempt * 5000
end
new_attempt = attempt + 1
Logger.info("Sleeping for #{time_to_sleep}ms")
Process.sleep(time_to_sleep)
Logger.info("Retrying, attempt #{new_attempt}")
with_retry(request_fun, new_attempt)
end
end
end
defp with_cache(method, url, fun) do
file_name =
"#{Date.utc_today()}-#{method}-#{Base.encode32(url, padding: false, case: :lower)}.json"
base_path = Path.join(System.tmp_dir!(), "height-api-cache")
file_path = Path.join(base_path, file_name)
File.mkdir_p!(base_path)
if File.exists?(file_path) do
Logger.info("Reading from cache #{method} #{url} at #{file_path}")
file_path |> File.read!()
else
result = fun.()
Logger.info("Writing result to cache at #{file_path}")
File.write!(file_path, result)
result
end
end
def handle_event(
[:finch, :request, :stop],
%{duration: duration},
%{request: request, result: result},
nil
) do
url =
""
|> URI.new!()
|> Map.put(:scheme, to_string(request.scheme))
|> Map.put(:host, request.host)
|> Map.put(:path, request.path)
|> Map.put(:query, request.query)
|> Map.put(:port, request.port)
|> URI.to_string()
status =
case result do
{:ok, response} -> response.status
{:error, _reason} -> :error
end
Logger.info("#{request.method} #{url} -> #{status} #{format_duration(duration)}")
end
defp format_duration(duration) do
duration = System.convert_time_unit(duration, :native, :microsecond)
if duration > 1000 do
value = duration |> div(1000) |> Integer.to_string()
value <> "ms"
else
Integer.to_string(duration) <> "µs"
end
end
end
Height.run()