Files
zoneminder/tests/zm_event_score.cpp
T
2c32cc6307 fix: keep recording when the Events insert fails, and let zmrecover give the event a normal id refs #7999 (#8409)
* fix: record an event under a time-based id when its insert keeps failing refs #7999

* fix: an event whose insert fails while zmc shuts down records under a fallback id too refs #7987

Event::Event stopped retrying the Events insert as soon as zm_terminate was
set, leaving the event with id 0, so its frames and video were written under
event 0. It now takes the same time-based fallback id it uses after three
failed attempts, so the recording is kept and zmrecover.pl can add it.
Monitor::openEvent never sees an id of 0 any more.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat: zmrecover gives an event recorded under a fallback id a normal id, and zmaudit keeps it refs #7999

When zmc cannot insert an event's row it records the event under a time-based
id far above the auto increment range. Recovering it as-is would leave that id
in the Events table and push auto increment past it.

ZoneMinder::Event::save_with_new_id() inserts the row without an Id, so it gets
the next auto increment id, then moves the event to match: the Medium or
Shallow directory, the Deep .<id> link or Shallow .<id> file, the
<id>-video.* file, the eid and file name in index.m3u8, DefaultVideo, the
recovered name, and any Frames, Stats, Event_Data, Events_Tags,
Snapshots_Events and Events_* summary rows zmc queued under the old id. If the
directory cannot be moved it removes the new row and keeps the old id, so a
later run can try again. zmrecover.pl saves every recovered event through it
when the id is in the fallback range.

zmaudit deleted the files of any event with no row once it was older than
ZM_AUDIT_MIN_AGE, which would throw away exactly the video the fallback id
exists to keep. It now warns and leaves those events for zmrecover.pl, and its
orphaned Frames, Stats and Event_Data cleanups stop below the fallback range.

The zmaudit and zmrecover tests that replace ZoneMinder::Event with a stub get
the two new names.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Jash <jashambaliya1@gmail.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-27 08:48:23 -05:00

149 lines
5.8 KiB
C++

/*
* This file is part of the ZoneMinder Project. See AUTHORS file for Copyright information
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "zm_catch2.h"
#include "zm_event.h"
TEST_CASE("EventMaxScoreForDb") {
// An event closed before its first frame still has max_score -1; the
// unsigned Events.MaxScore column must get 0.
REQUIRE(EventMaxScoreForDb(-1) == 0);
REQUIRE(EventMaxScoreForDb(0) == 0);
REQUIRE(EventMaxScoreForDb(42) == 42);
}
TEST_CASE("EventFallbackId") {
// When the Events insert keeps failing the event is still recorded to disk
// under this id, for zmrecover.pl to add to the database later. It has to be
// nonzero, distinct per monitor at the same moment, and grow with time.
REQUIRE(EventFallbackId(1795517294000, 1) != 0);
REQUIRE(EventFallbackId(1795517294000, 1) != EventFallbackId(1795517294000, 2));
REQUIRE(EventFallbackId(1795517294001, 1) > EventFallbackId(1795517294000, 999));
// Far above any id the Events table has handed out by auto increment.
REQUIRE(EventFallbackId(1795517294000, 1) > 1000000000000000ULL);
}
TEST_CASE("ClampSectionLength") {
// SectionLength=0 is accepted by the monitor form and used as a modulo.
REQUIRE(ClampSectionLength(Seconds(0)) == Seconds(1));
REQUIRE(ClampSectionLength(Seconds(1)) == Seconds(1));
REQUIRE(ClampSectionLength(Seconds(600)) == Seconds(600));
}
TEST_CASE("Event::BuildNotes") {
Event::StringSetMap notes;
REQUIRE(Event::BuildNotes(notes) == "");
notes["Motion"] = {"Zone1", "Zone2"};
REQUIRE(Event::BuildNotes(notes) == "Motion: Zone1, Zone2");
// Groups must not run together, e.g. "Motion: Zone2Signal: Lost".
notes["Signal"] = {"Lost"};
REQUIRE(Event::BuildNotes(notes) == "Motion: Zone1, Zone2; Signal: Lost");
}
TEST_CASE("EventNamePrefixLength") {
// Events.Name is varchar(64) and the id suffix must survive truncation.
REQUIRE(EventNamePrefixLength(0) == 63);
REQUIRE(EventNamePrefixLength(9) == 63);
REQUIRE(EventNamePrefixLength(10) == 62);
REQUIRE(EventNamePrefixLength(995000206) == 55);
REQUIRE(EventNamePrefixLength(UINT64_MAX) == 44);
}
TEST_CASE("EventCountsFrameStats") {
const auto t0 = std::chrono::system_clock::time_point(std::chrono::seconds(1000));
const auto boundary = t0 + std::chrono::seconds(5);
SECTION("no boundary counts everything") {
CHECK(EventCountsFrameStats(t0, SystemTimePoint()));
CHECK(EventCountsFrameStats(boundary, SystemTimePoint()));
CHECK(EventCountsFrameStats(boundary + std::chrono::seconds(1), SystemTimePoint()));
}
SECTION("earlier than boundary is not counted") {
CHECK_FALSE(EventCountsFrameStats(t0, boundary));
CHECK_FALSE(EventCountsFrameStats(boundary - std::chrono::milliseconds(1), boundary));
}
SECTION("equal to and later than boundary are counted") {
CHECK(EventCountsFrameStats(boundary, boundary));
CHECK(EventCountsFrameStats(boundary + std::chrono::milliseconds(1), boundary));
CHECK(EventCountsFrameStats(boundary + std::chrono::seconds(10), boundary));
}
}
TEST_CASE("EventKeepsDraining") {
const auto t0 = std::chrono::system_clock::time_point(std::chrono::seconds(1000));
const auto boundary = t0 + std::chrono::seconds(5);
ZMPacket packet;
packet.decoded = true;
packet.analyzed = true;
packet.timestamp = t0;
SECTION("not terminated keeps running") {
CHECK(EventKeepsDraining(false, SystemTimePoint(), nullptr));
CHECK(EventKeepsDraining(false, boundary, nullptr));
CHECK(EventKeepsDraining(false, boundary, &packet));
}
SECTION("terminated with zero drain_until stops immediately") {
CHECK_FALSE(EventKeepsDraining(true, SystemTimePoint(), nullptr));
CHECK_FALSE(EventKeepsDraining(true, SystemTimePoint(), &packet));
}
SECTION("terminated with non-zero drain_until stops on null or unanalyzed or boundary") {
CHECK_FALSE(EventKeepsDraining(true, boundary, nullptr));
packet.decoded = false;
CHECK_FALSE(EventKeepsDraining(true, boundary, &packet));
packet.decoded = true;
packet.analyzed = false;
CHECK_FALSE(EventKeepsDraining(true, boundary, &packet));
packet.analyzed = true;
packet.timestamp = boundary;
CHECK_FALSE(EventKeepsDraining(true, boundary, &packet));
packet.timestamp = boundary + std::chrono::seconds(1);
CHECK_FALSE(EventKeepsDraining(true, boundary, &packet));
}
SECTION("terminated with non-zero drain_until keeps draining earlier analyzed packet") {
packet.timestamp = t0;
CHECK(EventKeepsDraining(true, boundary, &packet));
packet.timestamp = boundary - std::chrono::milliseconds(1);
CHECK(EventKeepsDraining(true, boundary, &packet));
}
}
TEST_CASE("EventNameOnCloseSql escapes the event prefix") {
// The prefix is the expanded EventPrefix, so it can hold the monitor name or trigger text.
REQUIRE(EventNameOnCloseSql("Front-", 12) ==
"IF(Name='New Event', CONCAT(LEFT('Front-', 62), '12'), Name)");
REQUIRE(EventNameOnCloseSql("Joe's-", 12) ==
"IF(Name='New Event', CONCAT(LEFT('Joe\\'s-', 62), '12'), Name)");
REQUIRE(EventNameOnCloseSql("',Name),Cause=USER(),Name=IF(1,'", 7) ==
"IF(Name='New Event', CONCAT(LEFT('\\',Name),Cause=USER(),Name=IF(1,\\'', 63), '7'), Name)");
REQUIRE(EventNameOnCloseSql("a\\", 7) ==
"IF(Name='New Event', CONCAT(LEFT('a\\\\', 63), '7'), Name)");
}