Morzaram

Morzaram

Hey y’all I’m trying to figure out how to write this more eloquently…

I need to query a table of events that are happening this month (including things that are happening from the start of today). I have 3 types of events based off of column :type and depending on what’s in that column will determing whether or not I check that starts_at or ends_at

I have 3 types of events, :event, :action, :other

How do I write this so I check the following
if e.type == event: :starts_at
if e.type == action: :ends_at
if e.type == other: :ends_at

This is what I have now for and as you can see it looks horrible and only queries the start time. I’m hoping to get it in one query, otherwise I’ll write it in two, but it’s not the most ideal obviously

def events_at_date(month, year) do
    timezone = "America/Los_Angeles"

    from(e in Event,
      where:
        fragment(
          "EXTRACT(YEAR FROM (starts_at AT TIME ZONE 'UTC' AT TIME ZONE ?)) = ?",
          ^timezone,
          ^year
        ),
      where:
        fragment(
          "EXTRACT(MONTH FROM (starts_at AT TIME ZONE 'UTC' AT TIME ZONE ?)) = ?",
          ^timezone,
          ^month
        )
    )
  end

The edge of my sql knowledge stops here. I’m super weak with times and timezones…

Any help would be appreciated

Showing Posts 1 to 6

Morzaram

Morzaram OP

Still don’t like how much code there is so if anyone has any insight on how to make this better would love it, that being said, I think I’ve got it! The power of a 15m break

def events_at_date(month, year) do
    timezone = "America/Los_Angeles"

    from e in Event,
      where:
        fragment(
          """
          EXTRACT(YEAR FROM (
            CASE
              WHEN type = 'event' THEN starts_at
              ELSE ends_at
            END AT TIME ZONE 'UTC' AT TIME ZONE ?
          )) = ?
          """,
          ^timezone,
          ^year
        ),
      where:
        fragment(
          """
          EXTRACT(MONTH FROM (
            CASE
              WHEN type = 'event' THEN starts_at
              ELSE ends_at
            END AT TIME ZONE 'UTC' AT TIME ZONE ?
          )) = ?
          """,
          ^timezone,
          ^month
        )
  end
LostKobrakai

LostKobrakai

There are a few things here:

  • You need to switch based on type → that requires a CASE expression with sql.

  • This is ugly because many of the functions you’re using are not in the default ecto query api, so you end up with a huge fragment. Extracting those fragments to named functions/macros can help with better composability as well as making those things more readable: Ecto.Query.API — Ecto v3.11.0

  • Given you’re filtering based on a bunch of functions you cannot simply use an index here. It would be better to figure out a way to have the information you need in the db in the first place (either as a column or an index using expressions) and query based on that index – at least if this will grow to more events than just a few (hundred).

LostKobrakai

LostKobrakai

The simplest way to get this running would probably be something like this:

date_trunc('month', (CASE WHEN type = 'event' THEN starts_at
     WHEN type = 'action' THEN ends_at
     WHEN type = 'other' THEN ends_at
END AT TIME ZONE 'UTC' AT TIME ZONE ?)) == ?
Morzaram

Morzaram OP

Wow thank you, this makes a lot of sense. I think making another column would work of just making an month-year string "01-24" and querying that instead, but not sure if that’s a good idea.

Considering it’s just filtering by date and year, is the “month_year” column a decent way to go about it?

Is there a certain way that I don’t need to use AT TIME ZONE and still be accurate?

Thanks for the help by the way I really appreciate it

LostKobrakai

LostKobrakai

I’d ignore the “month” part. What is a month changes by timezone. But you want a stable (indexable) column of what datetime is relevant for your filtering. Then you can do a “before x, after y” search in the index with x and y specific to the tz you’re interested in.

Morzaram

Morzaram OP

Perfect! That’s crystal clear, and feels a lot better.

— All posts loaded —

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
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
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
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
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

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
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
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews