Created
June 5, 2020 02:25
-
-
Save qhwa/889987134ad5f68d6dd299bdac737625 to your computer and use it in GitHub Desktop.
hit_rate
This file contains hidden or 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
defmodule HitRate do | |
@moduledoc """ | |
Documentation for `HitRate`. | |
""" | |
def calc(file_path) do | |
file_path | |
|> stream() | |
|> Enum.to_list() | |
|> IO.inspect(label: :stream) | |
|> acc_on_video_id() | |
|> print() | |
end | |
defp stream(file_path) do | |
File.stream!(file_path) | |
|> Stream.map(&parse_line/1) | |
|> Stream.filter(fn | |
{_, _} -> | |
true | |
_ -> | |
false | |
end) | |
end | |
# Fields: timestamp time-taken c-ip filesize s-ip s-port sc-status sc-bytes cs-method cs-uri-stem - rs-duration rs-bytes c-referrer c-user-agent customer-id x-ec_custom-1 | |
# 1523756544 3 86.45.165.83 1845784 152.195.141.240 80 TCP_HIT/200 1846031 GET http://c13.adrise.tv/04C0BF/v2/sources/content-owners/sgl-entertainment/275211/v0401185814-1389k.mp4+740005.ts - 0 486 "-" "TubiExoPlayer/2.12.9 (Linux;Android 6.0) ExoPlayerLib/2.4.2" 49343 "-" | |
defp parse_line(line) do | |
case line |> String.split(~r/\s+/) do | |
[_, _, _, _, _, _, sc_status, _, _, url | _] -> | |
case video_id(url) do | |
:error -> | |
:error | |
id -> | |
case sc_status do | |
"TCP_HIT" <> _ -> | |
{id, true} | |
_ -> | |
{id, false} | |
end | |
end | |
_ -> | |
:error | |
end | |
end | |
# http://c13.adrise.tv/04C0BF/v2/sources/content-owners/sgl-entertainment/275211/v0401185814-1389k.mp4+740005.ts | |
defp video_id(url) do | |
case Regex.run(~r{/(\d+)/[^/]+$}, url) |> IO.inspect(label: url) do | |
[_, id] -> | |
id | |
_ -> | |
:error | |
end | |
end | |
defp acc_on_video_id(stream) do | |
Enum.reduce(stream, %{}, fn | |
%{video_id: id, hit?: true}, acc -> | |
case acc do | |
%{^id => {hit_count, _}} -> | |
%{acc | hit_count: hit_count + 1} | |
%{} -> | |
Map.put_new(acc, id, {1, 0}) | |
end | |
%{video_id: id, hit?: false}, acc -> | |
case acc do | |
%{^id => {_, miss_count}} -> | |
%{acc | hit_count: miss_count + 1} | |
%{} -> | |
Map.put_new(acc, id, {0, 1}) | |
end | |
end) | |
end | |
defp print(result) do | |
IO.inspect(result, limit: :infinte) | |
end | |
def run do | |
"access.log" |> calc() | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment