Aggregate the average story point for the week

How to count weekly average points from tasks resource?

defmodule Ash.Assignments.Milestone do
  ...

  attributes do
    uuid_primary_key :id

    attribute :title, :string
    attribute :description, :string
    attribute :due_date, :utc_datetime
    timestamps()
  end

  relationships do
    has_many :tasks, Ash.Task,
      destination_attribute: :milestone_id,
      public?: true
  end

  aggregates do
    avg :week_avg_story_point, :tasks, :story_point do
      public? true
      filter expr(inserted_at >= start_date() and inserted_at <= end_date())
    end
  end
end


defmodule Ash.Task do
  ...

  attributes do
    uuid_primary_key :id

    attribute :title, :string
    attribute :description, :string
    attribute :status, :atom, public?: true, constraints: [one_of: [:todo, :in_progress, :done]]
    attribute :due_date, :utc_datetime, public?: true
    attribute :story_point, :integer
    timestamps()
  end

  relationships do
    belongs_to :milestone, Tuesday.Assignments.Milestone,
      allow_nil?: true,
      public?: true
  end
end

Ash.Milestone
|> Ash.Query.load(:week_avg_story_point)
|> Ash.read!()

How to give proper filter expression for between two dates:
eg: filter expr(inserted_at >= start_date() and inserted_at <= end_date())

I would write a custom expression for a start_of_week and end_of_week or similar. See: Ash.CustomExpression — ash v3.4.68

1 Like

Sure I will try CustomExpression and let you know.

Is the idea that start_date and end_date are provided when asking for the avg story points for the week? Or does that just mean “this current week”?

Yes, just I need the current week.

Gotcha, then yeah a custom expression, or a fragment, or lazy would work. i.e

fragment("postgres_code_that_gets_you_start_of_week") or
lazy({SomeModule, :start_of_week, []})

1 Like