defmodule Chr do @doc """ return command history """ def get_history_list() do shell = System.get_env("SHELL") |> String.split("/") |> List.last() history_file = "#{System.user_home!()}/.#{shell}_history" history_list = File.read!(history_file) |> String.split("\n") |> Enum.reject(&(String.first(&1) !== ":")) history_list end @doc """ pick up command from history record """ def pick_up_command(string) do string |> String.split(" ") |> List.delete_at(0) |> List.first() |> String.split(";") |> List.last() end @doc """ pick up directory from history record example: """ def pick_up_directory(string) do [head | tail] = string |> String.split(";") |> List.delete_at(0) |> List.first() |> String.split(" ") case head do "cd" -> tail |> List.first() "code" -> tail |> List.first() "vim" -> tail |> List.first() "zed" -> tail |> List.first() "open" -> tail |> List.first() _ -> nil end end @doc """ timestamp to datetime """ def timestamp_to_datetime(timestamp) do datetime = DateTime.from_unix!(timestamp |> String.to_integer()) datetime # datetime |> DateTime.to_string() end @doc """ top commands """ def top_commands(history_list) do history_list |> Enum.map(&pick_up_command/1) |> Enum.reduce(%{}, fn command, acc -> Map.update(acc, command, 1, &(&1 + 1)) end) |> Enum.into([]) |> Enum.sort(fn {_, count1}, {_, count2} -> count2 < count1 end) |> Enum.take(10) end @doc """ top directories """ def top_directories(history_list) do history_list |> Enum.map(&pick_up_directory/1) |> Enum.reject(&is_nil/1) |> Enum.reject(&(String.length(&1) < 2)) |> Enum.reject(&String.ends_with?(&1, "..")) # |> Enum.reject(&Path.expand/1) |> Enum.reduce(%{}, fn directory, acc -> Map.update(acc, directory, 1, &(&1 + 1)) end) |> Enum.into([]) |> Enum.sort(fn {_, count1}, {_, count2} -> count2 < count1 end) |> Enum.take(5) end @doc """ pick up date from history record """ def pick_up_datetime(string) do string |> String.split(":") |> List.delete_at(0) |> List.first() |> String.split(" ") |> List.last() |> String.to_integer() |> DateTime.from_unix!() end @doc """ pick up date from DateTime """ def pick_up_date(datetime) do datetime |> DateTime.to_date() end @doc """ pick up time from DateTime """ def pick_up_hour(%{hour: hour}) do hour end @doc """ pick weekday from DateTime """ def pick_up_weekday(datetime) do datetime |> DateTime.to_date() |> Date.day_of_week() end @doc """ busiest day """ def busiest_day(history_list) do history_list |> Enum.map(&pick_up_datetime/1) |> Enum.map(&pick_up_date/1) |> Enum.reduce(%{}, fn date, acc -> Map.update(acc, date, 1, &(&1 + 1)) end) |> Enum.into([]) |> Enum.sort(fn {_, count1}, {_, count2} -> count2 < count1 end) |> List.first() end @doc """ 📅Weekly Activity """ def weekly_activity(history_list) do map = history_list |> Enum.map(&pick_up_datetime/1) |> Enum.map(&pick_up_weekday/1) |> Enum.reduce(%{}, fn date, acc -> Map.update(acc, date, 1, &(&1 + 1)) end) max = map |> Enum.into([], fn {_, count} -> count end) |> Enum.max() [:monday, :tuesday, :wednesday, :thursday, :friday, :saturday, :sunday] |> Enum.with_index() |> Enum.map(fn {day, index} -> {day, (Map.get(map, index, 0) / max * 10) |> round()} end) end @doc """ Daily Activity """ def daily_activity(history_list) do map = history_list |> Enum.map(&pick_up_datetime/1) |> Enum.map(&pick_up_hour/1) |> Enum.reduce(%{}, fn date, acc -> Map.update(acc, date, 1, &(&1 + 1)) end) max = map |> Enum.into([], fn {_, count} -> count end) |> Enum.max() [ "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24" ] |> Enum.with_index() |> Enum.map(fn {time, index} -> {time, (Map.get(map, index, 0) / max * 10) |> round()} end) end # def hello() do # get_history_list() # |> daily_activity() # end end