Morzaram

Morzaram

Filtering Events by attribute with Different Time Conditions

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

Most Liked

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 ?)) == ?
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

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.

Where Next?

Popular in Questions Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
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
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New

We're in Beta

About us Mission Statement