api/events/index/MonitorId:1.json "limit" and sort order query parameters no longer work in recent versions

Current Development version likely to have breaking changes
Post Reply
MarsRover
Posts: 56
Joined: Sat Apr 30, 2022 10:34 am

api/events/index/MonitorId:1.json "limit" and sort order query parameters no longer work in recent versions

Post by MarsRover »

Hello
Starting from 1.37.66~20250612.323-focal call to

Code: Select all

api/events/index/MonitorId:1.json?sort=StartDateTime&direction=desc&limit=1
disregards `limit` query parameter and returns all events (which might take a while).

Code: Select all

5.15.0-139-generic #149~20.04.1-Ubuntu SMP Wed Apr 16 08:29:56 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux
Last edited by MarsRover on Wed Jul 09, 2025 7:15 pm, edited 2 times in total.
MarsRover
Posts: 56
Joined: Sat Apr 30, 2022 10:34 am

Re: api/events/index/MonitorId:1.json "limit" query parameter no longer works in recent versions

Post by MarsRover »

Had to downgrade to 1.37.66~20250520.297-focal, all higher versions completely ignore limit, sort and direction query parameters.
Last edited by MarsRover on Wed Jul 09, 2025 7:17 pm, edited 1 time in total.
User avatar
iconnor
Posts: 3403
Joined: Fri Oct 29, 2010 1:43 am
Location: Toronto
Contact:

Re: api/events/index/MonitorId:1.json "limit" query parameter no longer works in recent versions (and VNC monitor proble

Post by iconnor »

Events are now unlimited by default, you have to pass limits into the query.

How to downgrade? apt install zoneminder=whatever version you want

How to see what versions are available ? apt-cache madison zoneminder
User avatar
iconnor
Posts: 3403
Joined: Fri Oct 29, 2010 1:43 am
Location: Toronto
Contact:

Re: api/events/index/MonitorId:1.json "limit" query parameter no longer works in recent versions (and VNC monitor proble

Post by iconnor »

I realise that you are passing limit... I"m not sure it was ever actually honoured. Maybe it was. THe code that got removed use ZM_EVENTS_PER_PAGE and a hard limit of 100.

I hust added a line to respect a limit param... not sure about the direction= in your url... not sure that is honoured by anything.

Isaac
MarsRover
Posts: 56
Joined: Sat Apr 30, 2022 10:34 am

Re: api/events/index/MonitorId:1.json "limit" query parameter no longer works in recent versions (and VNC monitor proble

Post by MarsRover »

iconnor wrote: Sun Jun 22, 2025 1:47 am I hust added a line to respect a limit param... not sure about the direction= in your url... not sure that is honoured by anything.
Isaac
Thanks, I'll check if it works later.

The goal of this query is to get only the last/current event.
direction query parameter is documented here https://zoneminder.readthedocs.io/en/la ... ted-events, it's the sort field order, ascending or descending.

It did work before.
MarsRover
Posts: 56
Joined: Sat Apr 30, 2022 10:34 am

Re: api/events/index/MonitorId:1.json "limit" query parameter no longer works in recent versions (and VNC monitor proble

Post by MarsRover »

OK. limit in query is now regarded.

But no sorting!

I want to accomplish very simple task via API: get the list of last N events for a given monitor, including incomplete one (the current one being recorded).
It all worked fine until recent updates.

I don't want to mess-up with DB directly to achieve such a simple task.

What am I doing wrong?
MarsRover
Posts: 56
Joined: Sat Apr 30, 2022 10:34 am

Re: api/events/index/MonitorId:1.json "limit" query parameter no longer works in recent versions (and VNC monitor proble

Post by MarsRover »

MarsRover wrote: Tue Jul 08, 2025 7:06 pm I want to accomplish very simple task via API: get the list of last N events for a given monitor, including incomplete one (the current one being recorded).
It all worked fine until recent updates.
I don't want to mess-up with DB directly to achieve such a simple task.
OK, here's my solution
this bash code gives me 3 last events for MonitorID 1
)))

Code: Select all

#!/bin/bash
monId=1
nLastEvents=3
curl -sS --noproxy '*' -XGET "http://localhost/zm/api/events/index/MonitorId:${monId}/StartDateTime%20>=:$(mysql --batch --skip-column-names --skip-pager --execute="select REPLACE(StartDateTime, ' ', '%20') AS EncodedSDT from zm.Events where MonitorId = ${monId} ORDER BY Id DESC LIMIT $(( nLastEvents-1 )),1;").json"
All this instead of simple

Code: Select all

zm/api/events/index/MonitorId:1.json?sort=StartDateTime&direction=desc&limit=3
which worked fine at least in May (and still works in 1.37.66~20250520.297).

Maybe it's time to make my own events API, but I'm facing the risk of DB schema changes.
User avatar
iconnor
Posts: 3403
Joined: Fri Oct 29, 2010 1:43 am
Location: Toronto
Contact:

Re: api/events/index/MonitorId:1.json "limit" and sort order query parameters no longer work in recent versions

Post by iconnor »

If you put pagination=true in the query it should restore the old behaviour of returning paginated results.
MarsRover
Posts: 56
Joined: Sat Apr 30, 2022 10:34 am

Re: api/events/index/MonitorId:1.json "limit" and sort order query parameters no longer work in recent versions

Post by MarsRover »

iconnor wrote: Wed Jul 09, 2025 9:25 pm If you put pagination=true in the query it should restore the old behaviour of returning paginated results.
If you mean something like

Code: Select all

/zm/api/events/index/MonitorId:1.json?pagination=true&sort=StartDateTime&direction=desc&limit=3&page=0
than no, it doesn't work in recent 1.37.66~20250706231158

It disregards sort direction and returns 3 first events instead of 3 last events.
Also, with or without limit, it always returns single page and number of pageCount = 1

Code: Select all

"pagination": {
  "prevPage": false,
  "nextPage": true,
  "paramType": "named",
  "pageCount": 1,
  "options": [],
  "page": 1
}
pagination and page query parameters are completely ignored.


Could you please give a simple query example that returns just N last events for the given monitor? (with pagination or without, doesn't matter)
MarsRover
Posts: 56
Joined: Sat Apr 30, 2022 10:34 am

Re: api/events/index/MonitorId:1.json "limit" and sort order query parameters no longer work in recent versions

Post by MarsRover »

Found another way to achieve the same via ajax query, but is't way more complicated and performs way slover

Get last 3 events for MonitorId 1

Code: Select all

#!/bin/bash
monId=1
nLastEvents=3
curl "http://localhost/zm/index.php?view=request&request=events&task=query&filter%5BQuery%5D%5Bterms%5D%5B0%5D%5Battr%5D=MonitorId&filter%5BQuery%5D%5Bterms%5D%5B0%5D%5Bop%5D=%3D&filter%5BQuery%5D%5Bterms%5D%5B0%5D%5Bval%5D=${monId}&filter%5BQuery%5D%5Bsort_asc%5D=0&filter%5BQuery%5D%5Bsort_field%5D=StartDateTime&filter%5BQuery%5D%5Blimit%5D=${nLastEvents}" \
  -H 'Accept: */*' \
  -H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' \
  --data-raw "search=&sort=StartDateTime&order=desc&offset=0&limit=${nLastEvents}&filter%5BQuery%5D%5Bterms%5D%5B0%5D%5Battr%5D=MonitorId&filter%5BQuery%5D%5Bterms%5D%5B0%5D%5Bop%5D=%3D&filter%5BQuery%5D%5Bterms%5D%5B0%5D%5Bval%5D=${monId}"
Moreover, the event object model in this API response is slightly different from the regular API. To get media file path from it I'll have to further query Storage API and then construct absolute path from Storage "Path" field, event "Scheme" field and event "DefaultVideo" field.

Why it all should be so complicated for an API client that only needs simple things, like getting the list of last N events to do some postprocessing on stored mp4 files?
MarsRover
Posts: 56
Joined: Sat Apr 30, 2022 10:34 am

Re: api/events/index/MonitorId:1.json "limit" and sort order query parameters no longer work in recent versions

Post by MarsRover »

Yet another approach using mysqlsh.
I had some fun.

Usage: ./this_script.js <MonitorId> <NumberOfLastEvents>

Code: Select all

#! /usr/bin/env -S mysqlsh --js --file

const monitorId = Number(sys.argv[1] || 1);   // MonitorId
const nEvents   = Number(sys.argv[2] || 10);  // Last N Events

shell.connect('zmuser@localhost:3306/zm','zmpass');  // hardcoded, since it's not a secret

// SQL with a ? placeholders for parameter binding
const sqlText = `
  SELECT 
    Events.*,
    CASE Events.Scheme
      WHEN 'Medium'  THEN CONCAT_WS('/', Storage.Path, MonitorId, DATE(StartDateTime), Events.Id)
      WHEN 'Deep'    THEN CONCAT_WS('/', Storage.Path, MonitorId, DATE_FORMAT(StartDateTime, '%Y/%m/%d/%H/%i/%S'))
      WHEN 'Shallow' THEN CONCAT_WS('/', Storage.Path, Events.Id)
    END AS FileSystemPath
  FROM Events JOIN Storage ON Events.StorageId = Storage.Id
  WHERE Events.MonitorId = ?
  ORDER BY Events.Id DESC LIMIT ?
`;

// bind params & execute
let result = session.runSql(sqlText, [ monitorId, nEvents ]);

let docs = [], doc;
while ((doc = result.fetchOneObject())) {
  docs.push(doc);
}
print(JSON.stringify(docs)); // dump as a clean JSON array
// print(JSON.stringify(docs, null, 2)); // if you want a pretty-formatted JSON 
Last edited by MarsRover on Fri Jul 11, 2025 10:03 pm, edited 1 time in total.
User avatar
iconnor
Posts: 3403
Joined: Fri Oct 29, 2010 1:43 am
Location: Toronto
Contact:

Re: api/events/index/MonitorId:1.json "limit" and sort order query parameters no longer work in recent versions

Post by iconnor »

Took another look... most of the stuff I was doing is apparently not required. query params are all parsed by the paginator. But there is no way to get the paginator to return all results, so now we only do pagination if either limit or page is specified in the query. Your initial url works fine for me today.
User avatar
iconnor
Posts: 3403
Joined: Fri Oct 29, 2010 1:43 am
Location: Toronto
Contact:

Re: api/events/index/MonitorId:1.json "limit" and sort order query parameters no longer work in recent versions

Post by iconnor »

I think I can speed up the events listing api too...
MarsRover
Posts: 56
Joined: Sat Apr 30, 2022 10:34 am

Re: api/events/index/MonitorId:1.json "limit" and sort order query parameters no longer work in recent versions

Post by MarsRover »

iconnor wrote: Fri Jul 11, 2025 9:42 pm Your initial url works fine for me today.
Thanks, I'll check a bit later. Which build do you mean by "today"?
MarsRover
Posts: 56
Joined: Sat Apr 30, 2022 10:34 am

Re: api/events/index/MonitorId:1.json "limit" and sort order query parameters no longer work in recent versions

Post by MarsRover »

[/quote]
Thanks, I'll check a bit later. Which build do you mean by "today"?
[/quote]

Confirm that in 1.37.67~20250712005219-focal the sorting issue is fixed!
Big thanks!
Post Reply