Get the last event matching something, before a given offset

Hi,

We need to find for a given event the corresponding predecessor.
Our idea is, to get the offset from the current event and query events, that are older, regarding the offset. And then take the latest of those, but we don’t want to do the selection of the latest in our code.

Is there an efficient way to query an event that matches a tag with an id that is the predecessor of another event?

If there is a solution without AQL, this would be finde es well :wink:

edit
So just to make more clear what I have:

FROM FEATURES(to(123)) & Machine:someId & SomeOtherTag

And from that, I only want the latest event, something like TOP in SQL would do.

Regards
Mike

Hi Mike,

getting the predecessor event of some known event can be done by querying with order: 'desc' in combination with an AQL expression like the one you propose. I’d interpolate the event ID into the query:

const predecessor = await new Promise((resolve) => {
  const cancel = actyx.queryAllKnownChunked(
    {
      order: 'desc',
      query: `FROM 'machine:someId' & to(${event.meta.eventId}) ...`
    },
    1, // chunk size one
    (chunk) => {
      resolve(chunk.events[0])
      cancel() // stop retrieving after getting the first result
    }
  )
})

This is a workaround in the sense that AQL should probably grow a LIMIT function or an aggregator of this kind, not sure yet which one is the better idea — the aggregator would make it clear whether it picks the first or last N elements, whereas the LIMIT transformation step would work in a nicely streaming fashion for large N but OTOH depend on the query order specified outside AQL in the API request (i.e. you get the first N for order: 'asc' and the last N for order: 'desc').

Regards,

Roland

1 Like

Small correction: this doesn’t yet work as queryAllKnownChunked doesn’t accept AQL — we’ll need to add queryAqlChunked for that (noted!).