-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
af27379
commit 00c245d
Showing
7 changed files
with
113 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
defmodule T.Events do | ||
def bucket do | ||
:t |> Application.fetch_env!(__MODULE__) |> Keyword.fetch!(:bucket) | ||
end | ||
|
||
# TODO | ||
if Mix.env() == :prod do | ||
def save_seen_timings(by_user_id, user_id, timings) do | ||
alias T.Events.Buffer | ||
alias NimbleCSV.RFC4180, as: CSV | ||
|
||
row = CSV.dump_to_iodata([by_user_id, user_id, Jason.encode_to_iodata!(timings)]) | ||
GenServer.cast(Buffer, {:add, row}) | ||
end | ||
else | ||
def save_seen_timings(_by_user_id, _user_id, _timings) do | ||
:ok | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
defmodule T.Events.Buffer do | ||
@moduledoc false | ||
use GenServer | ||
|
||
alias T.Events | ||
|
||
def start_link(opts) do | ||
GenServer.start_link(__MODULE__, opts, name: opts[:name]) | ||
end | ||
|
||
@impl true | ||
def init(_opts) do | ||
# TODO upload when file size reaches some threshold like 10MB | ||
Process.send_after(self(), :upload, :timer.hours(1)) | ||
{:ok, open_buffer()} | ||
end | ||
|
||
@impl true | ||
def handle_cast({:add, row}, fd) do | ||
:file.write(fd, row) | ||
{:noreply, fd} | ||
end | ||
|
||
@impl true | ||
def handle_info(:upload, fd) do | ||
close_buffer(fd) | ||
to_upload = "#{:rand.uniform(1000)}.csv" | ||
:ok = File.rename("events-buffer.csv", to_upload) | ||
async_upload(to_upload) | ||
{:noreply, open_buffer()} | ||
end | ||
|
||
defp open_buffer do | ||
File.open!("events-buffer.csv", [:raw, :append, {:delayed_write, 512_000, 10_000}]) | ||
end | ||
|
||
defp close_buffer(fd) do | ||
case File.close(fd) do | ||
:ok -> :ok | ||
_error -> :ok = File.close(fd) | ||
end | ||
end | ||
|
||
def async_upload(filename) do | ||
Task.Supervisor.start_child(T.TaskSupervisor, fn -> | ||
bucket = Events.bucket() | ||
|
||
%DateTime{year: y, month: m, day: d, hour: h, minute: m, second: s} = DateTime.utc_now() | ||
s3_key = "seen/#{y}/#{m}/#{d}/#{h}/#{m}/#{s}/" <> filename | ||
|
||
filename | ||
|> ExAws.S3.Upload.stream_file() | ||
|> ExAws.S3.upload(bucket, s3_key) | ||
|> ExAws.request!(region: "eu-north-1") | ||
end) | ||
end | ||
|
||
@impl true | ||
def terminate(_reason, fd) do | ||
close_buffer(fd) | ||
{:ok, fd} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters