nallwhy

nallwhy

How to properly use aggregates on a resource?

I’m a bit stuck on how to correctly use aggregates on a resource.

Let’s say I have the following resource:

defmodule MyApp.Invoice do
  use Ash.Resource

  attributes do
    uuid_v7_primary_key :id
    attribute :amount, :decimal, allow_nil?: false
    attribute :settled, :boolean, default: false, allow_nil?: false
  end
end

I want to get the sum of all amounts where `settled is false, so I defined this aggregate:

aggregates do
  sum :total_amount, [], :amount do
    filter expr(not settled)
  end
end

But:

  1. I’m not sure what the proper way to fetch this aggregate is.
  2. When I tried using Ash.aggregate(MyApp.Invoice, {:total_amount, :sum}), it didn’t seem to apply the filter.

I’m not sure if this is a bug or if I’m misunderstanding how it’s supposed to work.

I only shared the relevant code here, but if it helps, I can create a minimal repo to reproduce it.

Thanks in advance!

Marked As Solved

zachdaniel

zachdaniel

Creator of Ash

Ash.aggregate(Resource, {:total_amount, :sum}) should be requiring that you specify a field to be summing, i.e Ash.aggregate(Resource, {:total_amount, :sum, field: :amount}).

Ash.aggregate is for aggregating information about a query/resource as a whole. i.e

Resource
|> Ash.Query.filter(not settled)
|> Ash.aggregate(Resource, {:total_amount, :sum, field: :amount}

will get you back something like this:
{:ok, %{total_amount: #Decimal<10.0>}}, and is the “sum of of all amounts that are not settled”.

The aggregates declared in a resource are different, they are more like a calculation. They are for summary information about related data.

# on a customer resource for example
sum :total_unsettled_invoices_amount, [:invoices], :total_amount do
  filter expr(not settled)
end

This allows you to say:

customer |> Ash.load!(:total_unsettled_invoices_amount)

which return something like

%Customer{total_unsettled_invoices_amount: #Decimal<10.0>}

That total_invoices_amount is “for the given customer(s), the sum of all related invoices, which are not settled”

Hopefully that helps!

Also Liked

FlyingNoodle

FlyingNoodle

Aggregates are “per instance of this resource”.

I think something like this:

# in your resource
defmodule MyApp.Invoice do
  actions do
    read :not_settled do
      filter expr(not settled)
    end
  end
end

# in your domain
defmodule MyApp.MyDomain do
  resource MyApp.Invoice do
    read :get_not_settled_invoices, action: :not_settled
  end
end

# consumer
MyApp.MyDomain.get_not_settled_invoices() |> Ash.sum(:amount)

but I don’t know if thats the best way to do it? I mean you can also put this function in your domain or wherever you like.

Where Next?

Popular in Questions Top

hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

Other popular topics Top

AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54120 245
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement