   end
 
   defp get_time do
-    Timex.Time.now(:usecs)
+    Timex.Time.now(:secs)
+  end
+
+  defp get_time_msecs do
+    Timex.Time.now(:msecs)
   end
 
   defp get_runner(fun_name) do

    {:noreply, mark_call_error(state, fun_name, delta)}
  end

  def register(fun_name, fun) do
    GenServer.call(__MODULE__, {:register, fun_name, fun})
  end

  def run(fun_name, args \\ []) when is_list(args) do
    case get_runner(fun_name) do
      {:ok, run_fun} ->
        start = get_time
        result = apply(run_fun, args)
        delta = get_time - start

        case result do
          :error -> track_error(fun_name, delta)

  end

  defp mark_call_ok(state, fun_name, delta) do
    fun_def = get_in(state, [:functions, fun_name])
    case fun_def do
      nil -> state
      _ ->
        requests = fun_def.requests ++ [{:ok, get_time, fun_name, delta}]
        |> prune_requests(fun_def.threshold_config)
        update_in(state, [:functions, fun_name, :requests], requests)
    end
  end

  defp mark_call_error(state, fun_name, delta) do
    fun_def = get_in(state, [:functions, fun_name])
    case fun_def do
      nil -> state
      _ ->
        requests = fun_def.requests ++ [{:error, get_time, fun_name, delta}]
        |> prune_requests(fun_def.threshold_config)
        update_in(state, [:functions, fun_name, :requests], requests)
    end
  end

  defp prune_requests(requests, threshold_config) do
    ts = get_time - threshold_config.window_length
    _prune_requests(requests, ts)
  end

  defp _prune_requests(requests, ts) do
    case requests do
      [{_, rts, _, _} | tail] when rts < ts -> _prune_requests(tail, ts)
      _ -> requests
    end
  end
end
