9mm

9mm

Hello,

I have a collection like:

%{stats_a: [4, 5, 6], stats_b: [1, 2, 3]} # not actually integers (see next question)

Each of the 2 lists will be ~1 million items long.

I have a method which needs to push an item to beginning of each list. Ideally, it would in real-time chop the oldest/last items of the list so that it’s never longer than 1,000,000 items exactly

If theres no super fast way to do this however I will set a timer to do it every 1000ms… if there is an O(n) fast way to do it though I’ll do it in realtime. I need this timer anyway to aggregate stats so I’m not bothered if it needs to go there.

I will be pushing to it ~300-500 times/second.

Here’s what I had so far (without the limiting part yet…)

Is this crazy what I wrote?

  def handle_cast({:push_stats, key, stats}, state) do
    items = [stats | state[key]]
    new_state = state |> Map.put(key, items)
    {:noreply, new_state}
  end

Showing Posts 19 to 10

NobbZ

NobbZ

That attempt counts nils as 0 instead of skipping them, at least it like this from a first glance, is this really what you want?

peerreynders

peerreynders

Obviously I don’t care about line count - I like code that is easy to change:

defmodule Demo do
  defp initial_total(:response_time, time),
    do: if(is_number(time), do: {time, 1}, else: {0, 0})

  defp initial_total(_key, value),
    do: value || 0

  defp initial_aggregate({key, value}, aggregate),
    do: Map.put(aggregate, key, initial_total(key, value))

  defp merge_total(:response_time, {total_time, count} = total, time),
    do: if(is_number(time), do: {total_time + time, count + 1}, else: total)

  defp merge_total(_key, total, value),
    do: total + (value || 0)

  defp merge_aggregate({key, value}, aggregate) do
    new_total =
      case Map.fetch(aggregate, key) do
        {:ok, total} ->
          merge_total(key, total, value)

        _ ->
          initial_total(key, value)
      end

    Map.put(aggregate, key, new_total)
  end

  defp item_aggregator({key, data}, stats) do
    new_aggregate =
      case Map.fetch(stats, key) do
        {:ok, aggregate} ->
          Enum.reduce(data, aggregate, &merge_aggregate/2)

        _ ->
          Enum.reduce(data, %{}, &initial_aggregate/2)
      end

    Map.put(stats, key, new_aggregate)
  end

  defp finalize_total({:response_time, {total_time, count}}, aggregate) do
    if count > 0 do
      Map.put(aggregate, :response_time, div(total_time, count))
    else
      aggregate
    end
  end

  defp finalize_total({key, total}, aggregate) do
    Map.put(aggregate, key, total)
  end

  defp finalize_aggregate({key, aggregate}, stats),
    do: Map.put(stats, key, Enum.reduce(aggregate, %{}, &finalize_total/2))

  def make_stats(items) do
    items
    |> List.foldl(%{}, &item_aggregator/2)
    |> Enum.reduce(%{}, &finalize_aggregate/2)
  end
end

#
# item: {:group_1, data}
# data: %{timeout: 0, failure: 0, hits: 1, response_time: 100}
# value: associated with a "key" inside the "data" Map
# aggregate: %{failure: 0, hits: 2, response_time: {1100,2}, timeout: 0}
# total: value associated with a "key" inside the "aggregate" Map
# finalized_aggregate: %{failure: 0, hits: 2, response_time: 550, timeout: 0}
# stats: %{feed_key => (finalized_)aggregate}
#

feed_items = [
  {:group_1, %{timeout: 0, failure: 0, hits: 1, response_time: 100}},
  {:group_1, %{timeout: 0, failure: 0, hits: 1, response_time: 1000}},
  {:group_2, %{timeout: 0, failure: 0, hits: 1, response_time: 50}},
  {:group_2, %{timeout: 0, failure: 0, hits: 1, response_time: 2000}},
  {:group_3, %{timeout: 0, failure: 1, hits: 0}},
  {:group_3, %{timeout: 1, failure: 1, hits: 0, response_time: nil}}
]

IO.inspect(Demo.make_stats(feed_items))
$ elixir demo.exs
%{
  group_1: %{failure: 0, hits: 2, response_time: 550, timeout: 0},
  group_2: %{failure: 0, hits: 2, response_time: 1025, timeout: 0},
  group_3: %{failure: 2, hits: 0, timeout: 1}
}
9mm

9mm OP

This was my weak attempt, however it’s failing if there’s a single item which has a response_value of nil… which is showing me I have absolutely no idea how this works.. I thought value1 was the existing value from the accumulator and value2 was from the incoming map (from the list)

EDIT OK crap I see it, its because now it’s setting it for every value2…

IO.inspect List.foldl(items, %{}, fn ({feed_name, list_stats}, acc) ->
  Map.update(acc, feed_name, list_stats, fn acc_stats ->
    # handle the first item
    list_stats = %{list_stats | response_time: {list_stats.response_time || 0, 0}}
    Map.merge(acc_stats, list_stats, fn key, value1, value2 ->
      case key do
        :response_time ->
          {value1 + (value2 || 0), value1 + 1}
        _ ->
          (value1 || 0) + (value2 || 0)
      end
    end)
  end)
end)
NobbZ

NobbZ

I’m sure it does not work with your input directly, but if you want to retain the keys of your old data, this should work out:

data
|> Enum.map(fn {k, l} -> {k, Enum.reduce(from above)} end)
|> Map.new()

Sorry for only having such a half snippet, but I’m currently on my mobile.

9mm

9mm OP

Does this apply to my input data in post #2 of this thread?

This is what I had before adding averaging which seems to be the simplest way “so far” (thanks to help of peerreynders). I also need to combine it in the final output as well

IO.inspect List.foldl(items, %{}, fn ({date, feed_name, list_stats}, acc) ->
  Map.update(acc, feed_name, list_stats, fn acc_stats ->
    Map.merge(acc_stats, list_stats, fn _key, value1, value2 ->
      # case key do... check if :response_time and then return {total, count} instead of total
      (value1 || 0) + (value2 || 0)
    end)
  end)
end)
NobbZ

NobbZ

Wait? A simple average calculator with a reducer and you have 3 clauses? Then theres something wrong…

{sum, cnt} = Enum.reduce(list, {0, 0}, fn
  nil, acc -> acc
  n, {sum, cnt} -> {sum + n, cnt + 1}
end
if cnt > 0, do: sum / cnt, else: {:error, :no_elements_to_average_over}
9mm

9mm OP

This is what I ended up doing… it’s so insanely verbose but I just couldnt’ get the {total, count} method to work :frowning: I’m hoping it’s not super simple and I just missed something (very likely).

I first calculate the totals in a list, then i calculate the actual list, and then I combine them

feed_items = :queue.to_list(state[:feed_stats])

feed_counts = List.foldl(feed_items, %{}, fn ({feed_name, %{response_time: response_time}}, acc) ->
  count = if response_time, do: 1, else: 0
  Map.update(acc, feed_name, count, fn acc_total -> acc_total + count end)
end)

feed_stats = List.foldl(feed_items, %{}, fn ({feed_name, list_stats}, acc) ->
  # Helpful things to know:
  # - https://forum.elixirforum.com/t/how-to-quickly-add-to-list-in-map/18190
  # - Map.update/4 which runs a function on each value https://hexdocs.pm/elixir/Map.html#update/4
  # - Map.merge/3 which runs a function on each conflicting key https://hexdocs.pm/elixir/Map.html#merge/3
  Map.update(acc, feed_name, list_stats, fn acc_stats ->
    Map.merge(acc_stats, list_stats, fn _key, value1, value2 ->
      (value1 || 0) + (value2 || 0)
    end)
  end)
end) |> Enum.map(fn {feed_name, combined_stats} ->
  total = combined_stats[:response_time]
  feed_count = feed_counts[feed_name]
  stats_with_avg = case feed_count do
    # if there's not a single sucessful request just show -1
    0 -> combined_stats |> Map.put(:response_time, -1)
    _ -> combined_stats |> Map.put(:response_time, round(total / feed_count))
  end
    {feed_name, stats_with_avg}
end) |> Enum.into(%{})
9mm

9mm OP

Ok so i ended up thinking this was easy but it turned out to be really complicated because the input value can now be nil, integer, or {total, count}, and now the reducer function can accept any of those as input

I had about 3 guard clauses and I just stopped because it’s too confusing to reason about a month from now

If you are thinking of an easier way would you mind exposing my mind to your greatness?

9mm

9mm OP

Dude thats a great idea… ok thank you!

peerreynders

peerreynders

One possible work around is to store {total,count} instead of the average, so when you update you store {total+more,count+1} and when you need the average later you simply calculate total/count.

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews