#2742 Events tab: decode events from all channels stored in the database

- EventManager collects decode events from all channels (tracked by identity, re-checked for in-place updates)
  and saves/broadcasts EventRecord snapshots for the Events tab and future API subscribers.
- Schema version 3: events and event_patched_talkgroups tables; IEventRepository/EventRepository for H2,
  MariaDB and PostgreSQL.
- Events tab after Calls: paging, page size, talkgroup/radio alias search, category, event type, protocol,
  system, details and date filters, live updates, talkgroup hold context menu.
- Aliases resolved at display time from the stored alias list name (shared IdentifierRecord base with calls,
  SqlSupport filter helpers).
- Database switch transfers events; event age-off rule (default 30 days) in the database preferences.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
denny
2026-11-22 02:29:09 -05:00
parent 6dc1f0fdcb
commit d92070bd11
27 changed files with 3744 additions and 588 deletions

View File

@@ -19,7 +19,7 @@
package io.github.dsheirer.audio.playback;
import io.github.dsheirer.audio.AudioSegment;
import io.github.dsheirer.database.call.CallRecord;
import io.github.dsheirer.database.IdentifierRecord;
import io.github.dsheirer.identifier.Identifier;
import io.github.dsheirer.identifier.IdentifierCollection;
import io.github.dsheirer.identifier.Role;
@@ -140,7 +140,7 @@ public record TalkgroupHold(Protocol protocol, int talkgroup, String label)
* @param toAlias current alias name of the TO talkgroup for the label, or null
* @return hold or null if the call doesn't have a TO talkgroup
*/
public static TalkgroupHold from(CallRecord call, String toAlias)
public static TalkgroupHold from(IdentifierRecord call, String toAlias)
{
if(call == null || !call.hasTalkgroup() || call.getToValue() > Integer.MAX_VALUE)
{

View File

@@ -261,7 +261,7 @@ public class CallManager implements Listener<AudioSegment>
/**
* Talkgroups patched into the patch group TO identifier, or an empty list.
*/
static List<PatchedTalkgroup> getPatchedTalkgroups(Identifier to)
public static List<PatchedTalkgroup> getPatchedTalkgroups(Identifier to)
{
if(!(to instanceof PatchGroupIdentifier pgi) || pgi.getValue() == null)
{
@@ -292,7 +292,7 @@ public class CallManager implements Listener<AudioSegment>
/**
* Numeric value of a talkgroup, patch group (the patch group talkgroup) or radio identifier, or null.
*/
static Long getValue(Identifier identifier)
public static Long getValue(Identifier identifier)
{
if(identifier instanceof PatchGroupIdentifier pgi && pgi.getValue() != null &&
pgi.getValue().getPatchGroup() != null)
@@ -328,7 +328,7 @@ public class CallManager implements Listener<AudioSegment>
{
private final AudioSegment mAudioSegment;
private final CallRecord mCall = new CallRecord();
private final DatabaseManager.CallHandle mHandle;
private final DatabaseManager.RowHandle mHandle;
private final CallRecorder.Recording mRecording;
private String mLastSignature;
private boolean mPublished;

View File

@@ -22,6 +22,8 @@ import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import io.github.dsheirer.database.call.CallRepository;
import io.github.dsheirer.database.call.ICallRepository;
import io.github.dsheirer.database.event.EventRepository;
import io.github.dsheirer.database.event.IEventRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -47,12 +49,13 @@ public class CallDatabase implements AutoCloseable
* Version of the sdrtrunk tables that this release creates. Increment and add an upgrade step to
* {@link #getUpgradeStatements(int)} when the tables change.
*/
public static final int SCHEMA_VERSION = 2;
public static final int SCHEMA_VERSION = 3;
public static final String SCHEMA_INFO_TABLE = "schema_info";
private final DatabaseConfiguration mConfiguration;
private final HikariDataSource mDataSource;
private final ICallRepository mCallRepository;
private final IEventRepository mEventRepository;
/**
* Opens a connection pool for the configuration. The schema isn't created or checked.
@@ -64,6 +67,7 @@ public class CallDatabase implements AutoCloseable
{
mConfiguration = configuration;
mCallRepository = new CallRepository(configuration.schema());
mEventRepository = new EventRepository(configuration.schema());
if(configuration.type() == DatabaseType.H2)
{
@@ -137,6 +141,14 @@ public class CallDatabase implements AutoCloseable
return mCallRepository;
}
/**
* Repository for the events tables in this schema
*/
public IEventRepository getEventRepository()
{
return mEventRepository;
}
/**
* Pooled connection. Close it to return it to the pool.
*/
@@ -208,6 +220,22 @@ public class CallDatabase implements AutoCloseable
}
}
/**
* Number of events stored in the schema, or 0 if the schema doesn't have sdrtrunk tables (or no events table yet).
*/
public long getEventCount() throws SQLException
{
if(getSchemaStatus() != SchemaStatus.SDRTRUNK)
{
return 0;
}
try(Connection connection = getConnection())
{
return getVersion(connection) >= 3 ? mEventRepository.count(connection) : 0;
}
}
/**
* Creates the schema and tables when they don't exist and upgrades the tables of an older version.
* @throws SQLException on error, or if the schema was created by a newer version of sdrtrunk
@@ -273,6 +301,13 @@ public class CallDatabase implements AutoCloseable
statements.addAll(CallRepository.getPatchedTalkgroupCreateStatements(mConfiguration.schema()));
}
//Version 3: decode events
if(fromVersion < 3)
{
statements.addAll(EventRepository.getCreateStatements(mConfiguration.type().getDialect(),
mConfiguration.schema()));
}
return statements;
}

View File

@@ -40,6 +40,10 @@ public record DatabaseEvent(Type type, DatabaseConfiguration configuration, Stri
/**
* Stored calls changed outside of the live call updates, for example by an age-off rule
*/
CALLS_CHANGED
CALLS_CHANGED,
/**
* Stored events changed outside of the live event updates, for example by the event age-off rule
*/
EVENTS_CHANGED
}
}

View File

@@ -23,6 +23,9 @@ import io.github.dsheirer.database.call.CallRecord;
import io.github.dsheirer.database.call.ICallRepository;
import io.github.dsheirer.database.call.CallSearchCriteria;
import io.github.dsheirer.database.call.CallState;
import io.github.dsheirer.database.event.EventRecord;
import io.github.dsheirer.database.event.EventSearchCriteria;
import io.github.dsheirer.database.event.IEventRepository;
import io.github.dsheirer.preference.UserPreferences;
import io.github.dsheirer.sample.Broadcaster;
import io.github.dsheirer.sample.Listener;
@@ -49,14 +52,15 @@ import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
/**
* Manages the call database: opens the selected database at startup, writes call records in the background, runs the
* age-off rules and switches between databases (with optional transfer of the existing calls).
* Manages the call database: opens the selected database at startup, writes call and decode event records in the
* background, runs the age-off rules and switches between databases (with optional transfer of the existing calls and
* events).
*
* All writes, the age-off rules and database switches run on a single database thread, so they never overlap.
* Searches run on a separate thread so that a long write doesn't block the user interface.
*
* Call recording doesn't depend on the database: when the database is unavailable, call updates are held in memory
* (newest update per call) and written when the database becomes available again.
* Call recording doesn't depend on the database: when the database is unavailable, call and event updates are held in
* memory (newest update per call or event) and written when the database becomes available again.
*/
public class DatabaseManager
{
@@ -64,6 +68,7 @@ public class DatabaseManager
private static final long FLUSH_INTERVAL_MS = 250;
private static final long RETRY_INTERVAL_MS = 30_000;
private static final int MAX_PENDING_CALLS = 20_000;
private static final int MAX_PENDING_EVENTS = 50_000;
private static final int TRANSFER_PAGE_SIZE = 1000;
private final UserPreferences mUserPreferences;
@@ -73,7 +78,9 @@ public class DatabaseManager
Executors.newSingleThreadExecutor(new NamingThreadFactory("sdrtrunk call database query"));
private final Broadcaster<DatabaseEvent> mEventBroadcaster = new Broadcaster<>();
private final Broadcaster<CallRecord> mSavedCallBroadcaster = new Broadcaster<>();
private final Map<CallHandle,CallRecord> mPendingCalls = new LinkedHashMap<>();
private final Broadcaster<EventRecord> mSavedEventBroadcaster = new Broadcaster<>();
private final Map<RowHandle,CallRecord> mPendingCalls = new LinkedHashMap<>();
private final Map<RowHandle,EventRecord> mPendingEvents = new LinkedHashMap<>();
private final AgeOffManager mAgeOffManager;
private volatile CallDatabase mDatabase;
private volatile String mErrorMessage;
@@ -239,11 +246,11 @@ public class DatabaseManager
}
/**
* Creates a handle that tracks the database row of one call across updates.
* Creates a handle that tracks the database row of one call or event across updates.
*/
public CallHandle createHandle()
public RowHandle createHandle()
{
return new CallHandle();
return new RowHandle();
}
/**
@@ -252,7 +259,7 @@ public class DatabaseManager
* @param handle for the call
* @param snapshot of the call that is not modified after this call
*/
public void save(CallHandle handle, CallRecord snapshot)
public void save(RowHandle handle, CallRecord snapshot)
{
synchronized(mPendingCalls)
{
@@ -261,7 +268,7 @@ public class DatabaseManager
//Bound memory use if the database is unavailable for a long time. Drop the oldest completed calls first.
if(mPendingCalls.size() > MAX_PENDING_CALLS)
{
Iterator<Map.Entry<CallHandle,CallRecord>> it = mPendingCalls.entrySet().iterator();
Iterator<Map.Entry<RowHandle,CallRecord>> it = mPendingCalls.entrySet().iterator();
while(it.hasNext() && mPendingCalls.size() > MAX_PENDING_CALLS)
{
@@ -277,7 +284,35 @@ public class DatabaseManager
}
/**
* Writes the pending call updates. Database thread only.
* Queues a snapshot of the decode event for insertion (first time) or update. Only the newest snapshot for each
* event is written, so this can be invoked as often as the event changes.
* @param handle for the event
* @param snapshot of the event that is not modified after this call
*/
public void save(RowHandle handle, EventRecord snapshot)
{
synchronized(mPendingEvents)
{
mPendingEvents.put(handle, snapshot);
//Bound memory use if the database is unavailable for a long time. Drop the oldest events first.
if(mPendingEvents.size() > MAX_PENDING_EVENTS)
{
Iterator<RowHandle> it = mPendingEvents.keySet().iterator();
while(it.hasNext() && mPendingEvents.size() > MAX_PENDING_EVENTS)
{
it.next();
it.remove();
}
LOGGER.warn("Call database is unavailable - discarding the oldest pending event updates");
}
}
}
/**
* Writes the pending call and event updates. Database thread only.
*/
private void flush()
{
@@ -298,65 +333,160 @@ public class DatabaseManager
}
}
List<Map.Entry<CallHandle,CallRecord>> batch;
ICallRepository callRepository = database.getCallRepository();
IEventRepository eventRepository = database.getEventRepository();
synchronized(mPendingCalls)
if(flush(database, mPendingCalls, callRepository::insert, callRepository::update, mSavedCallBroadcaster,
false))
{
if(mPendingCalls.isEmpty())
flush(database, mPendingEvents, eventRepository::insert, eventRepository::update, mSavedEventBroadcaster,
true);
}
}
/**
* Inserts a row and returns its generated id
*/
private interface IRowInsert<R>
{
long insert(Connection connection, R record) throws SQLException;
}
/**
* Updates a row and indicates if the row exists
*/
private interface IRowUpdate<R>
{
boolean update(Connection connection, R record) throws SQLException;
}
/**
* Writes the pending updates of one table. Database thread only.
* @param database to write to
* @param pending updates by row handle
* @param inserter for new rows
* @param updater for existing rows
* @param saved broadcaster for the written records
* @param transaction to write the whole batch in one transaction (faster for many small rows)
* @return true if the updates were written, false on error
*/
private <R extends IdentifierRecord> boolean flush(CallDatabase database, Map<RowHandle,R> pending,
IRowInsert<R> inserter, IRowUpdate<R> updater,
Broadcaster<R> saved,
boolean transaction)
{
List<Map.Entry<RowHandle,R>> batch;
synchronized(pending)
{
if(pending.isEmpty())
{
return;
return true;
}
batch = new ArrayList<>(mPendingCalls.entrySet());
mPendingCalls.clear();
batch = new ArrayList<>(pending.entrySet());
pending.clear();
}
ICallRepository repository = database.getCallRepository();
int written = 0;
List<R> writtenRecords = new ArrayList<>();
List<RowHandle> inserted = new ArrayList<>();
try(Connection connection = database.getConnection())
{
for(Map.Entry<CallHandle,CallRecord> entry: batch)
boolean autoCommit = connection.getAutoCommit();
if(transaction)
{
CallHandle handle = entry.getKey();
CallRecord call = entry.getValue();
connection.setAutoCommit(false);
}
if(handle.mGeneration == mGeneration && handle.mId > 0)
try
{
for(Map.Entry<RowHandle,R> entry: batch)
{
call.setId(handle.mId);
RowHandle handle = entry.getKey();
R record = entry.getValue();
if(repository.update(connection, call))
if(handle.mGeneration == mGeneration && handle.mId > 0)
{
written++;
mSavedCallBroadcaster.broadcast(call);
continue;
record.setId(handle.mId);
if(updater.update(connection, record))
{
written++;
writtenRecords.add(record);
continue;
}
}
//New row, a row that started before a database switch, or a row removed by an age-off rule.
record.setId(0);
handle.mId = inserter.insert(connection, record);
handle.mGeneration = mGeneration;
inserted.add(handle);
written++;
writtenRecords.add(record);
}
if(transaction)
{
connection.commit();
}
}
catch(SQLException se)
{
if(transaction)
{
//Nothing in the batch was written
rollback(connection);
written = 0;
writtenRecords.clear();
for(RowHandle handle: inserted)
{
handle.mId = 0;
}
}
//New call, a call that started before a database switch, or a row removed by an age-off rule.
call.setId(0);
handle.mId = repository.insert(connection, call);
handle.mGeneration = mGeneration;
written++;
mSavedCallBroadcaster.broadcast(call);
throw se;
}
finally
{
if(transaction)
{
restoreAutoCommit(connection, autoCommit);
}
}
for(R record: writtenRecords)
{
saved.broadcast(record);
}
return true;
}
catch(SQLException se)
{
LOGGER.error("Error writing call updates to the database - " + se.getMessage());
LOGGER.error("Error writing updates to the database - " + se.getMessage());
for(R record: writtenRecords)
{
saved.broadcast(record);
}
//Put back the updates that weren't written, unless a newer update arrived in the meantime
synchronized(mPendingCalls)
synchronized(pending)
{
Map<CallHandle,CallRecord> newer = new LinkedHashMap<>(mPendingCalls);
mPendingCalls.clear();
Map<RowHandle,R> newer = new LinkedHashMap<>(pending);
pending.clear();
for(int x = written; x < batch.size(); x++)
{
mPendingCalls.put(batch.get(x).getKey(), batch.get(x).getValue());
pending.put(batch.get(x).getKey(), batch.get(x).getValue());
}
mPendingCalls.putAll(newer);
pending.putAll(newer);
}
if(!isReachable(database))
@@ -367,6 +497,32 @@ public class DatabaseManager
mNextRetry = System.currentTimeMillis() + RETRY_INTERVAL_MS;
broadcast(DatabaseEvent.Type.ERROR);
}
return false;
}
}
private static void rollback(Connection connection)
{
try
{
connection.rollback();
}
catch(SQLException se)
{
LOGGER.warn("Database rollback failed - " + se.getMessage());
}
}
private static void restoreAutoCommit(Connection connection, boolean autoCommit)
{
try
{
connection.setAutoCommit(autoCommit);
}
catch(SQLException se)
{
LOGGER.warn("Unable to restore database auto-commit - " + se.getMessage());
}
}
@@ -416,6 +572,41 @@ public class DatabaseManager
*/
public record SearchResult(List<CallRecord> calls, long total) {}
/**
* Searches the events table on the query thread.
*/
public CompletableFuture<EventSearchResult> searchEvents(EventSearchCriteria criteria, int offset, int limit)
{
return CompletableFuture.supplyAsync(() -> {
CallDatabase database = mDatabase;
if(database == null)
{
throw new IllegalStateException("Call database is not available" +
(mErrorMessage != null ? ": " + mErrorMessage : ""));
}
try(Connection connection = database.getConnection())
{
IEventRepository repository = database.getEventRepository();
List<EventRecord> events = repository.search(connection, criteria, offset, limit);
long total = repository.count(connection, criteria);
return new EventSearchResult(events, total);
}
catch(SQLException se)
{
throw new IllegalStateException(se.getMessage(), se);
}
}, mQueryExecutor);
}
/**
* Event search results
* @param events in the requested page
* @param total count of matching events
*/
public record EventSearchResult(List<EventRecord> events, long total) {}
/**
* Connects to the database configuration and reports the state of its schema. Runs on the query thread.
*/
@@ -426,9 +617,10 @@ public class DatabaseManager
{
SchemaStatus status = database.getSchemaStatus();
long count = status == SchemaStatus.SDRTRUNK ? database.getCallCount() : 0;
long eventCount = status == SchemaStatus.SDRTRUNK ? database.getEventCount() : 0;
String suggestion = status == SchemaStatus.MISSING ? configuration.schema() :
database.suggestSchemaName();
return new ProbeResult(status, count, suggestion);
return new ProbeResult(status, count, eventCount, suggestion);
}
catch(SQLException se)
{
@@ -441,9 +633,10 @@ public class DatabaseManager
* Result of probing a database configuration
* @param status of the configured schema
* @param callCount in the configured schema
* @param eventCount in the configured schema
* @param suggestedSchema an unused schema name, for the create separate schema option
*/
public record ProbeResult(SchemaStatus status, long callCount, String suggestedSchema) {}
public record ProbeResult(SchemaStatus status, long callCount, long eventCount, String suggestedSchema) {}
/**
* Checks whether a schema exists in the database of the configuration. Runs on the query thread.
@@ -515,6 +708,8 @@ public class DatabaseManager
long transferred = 0;
long skipped = 0;
long eventsTransferred = 0;
long eventsSkipped = 0;
CallDatabase current = mDatabase;
if(transfer && current != null && !current.getConfiguration().isSameLocation(target))
@@ -524,6 +719,9 @@ public class DatabaseManager
long[] counts = transfer(current, targetDatabase, progress);
transferred = counts[0];
skipped = counts[1];
long[] eventCounts = transferEvents(current, targetDatabase, progress);
eventsTransferred = eventCounts[0];
eventsSkipped = eventCounts[1];
}
try(Connection connection = targetDatabase.getConnection())
@@ -557,9 +755,11 @@ public class DatabaseManager
}
LOGGER.info("Switched call database to " + target.getDescription() + " - transferred " +
transferred + " calls" + (skipped > 0 ? ", skipped " + skipped + " existing calls" : ""));
transferred + " calls" + (skipped > 0 ? ", skipped " + skipped + " existing calls" : "") +
" and " + eventsTransferred + " events" + (eventsSkipped > 0 ? ", skipped " + eventsSkipped +
" existing events" : ""));
broadcast(DatabaseEvent.Type.CONNECTED);
future.complete(new SwitchResult(transferred, skipped));
future.complete(new SwitchResult(transferred, skipped, eventsTransferred, eventsSkipped));
}
catch(Exception e)
{
@@ -581,8 +781,10 @@ public class DatabaseManager
* Result of a database switch
* @param transferred count of calls copied to the new database
* @param skipped count of calls that already existed in the new database
* @param eventsTransferred count of events copied to the new database
* @param eventsSkipped count of events that already existed in the new database
*/
public record SwitchResult(long transferred, long skipped) {}
public record SwitchResult(long transferred, long skipped, long eventsTransferred, long eventsSkipped) {}
/**
* Copies the calls from the source to the target database, skipping calls in progress (they are written to the
@@ -651,6 +853,71 @@ public class DatabaseManager
return new long[]{transferred, skipped};
}
/**
* Copies the events from the source to the target database, skipping events that already exist in the target.
* @return transferred and skipped counts
*/
private long[] transferEvents(CallDatabase source, CallDatabase target, Consumer<String> progress)
throws SQLException
{
IEventRepository sourceRepository = source.getEventRepository();
IEventRepository targetRepository = target.getEventRepository();
long transferred = 0;
long skipped = 0;
try(Connection sourceConnection = source.getConnection(); Connection targetConnection = target.getConnection())
{
long total = sourceRepository.count(sourceConnection);
Set<String> existing = targetRepository.count(targetConnection) > 0 ?
targetRepository.getTransferKeys(targetConnection) : Collections.emptySet();
boolean autoCommit = targetConnection.getAutoCommit();
targetConnection.setAutoCommit(false);
try
{
List<EventRecord> page = sourceRepository.readPage(sourceConnection, 0, TRANSFER_PAGE_SIZE);
while(!page.isEmpty())
{
List<EventRecord> toInsert = new ArrayList<>();
for(EventRecord event: page)
{
if(existing.contains(event.getTransferKey()))
{
skipped++;
}
else
{
toInsert.add(event);
}
}
if(!toInsert.isEmpty())
{
targetRepository.insertBatch(targetConnection, toInsert);
targetConnection.commit();
transferred += toInsert.size();
}
progress.accept("Transferring events: " + (transferred + skipped) + " of " + total);
page = sourceRepository.readPage(sourceConnection, page.getLast().getId(), TRANSFER_PAGE_SIZE);
}
}
catch(SQLException se)
{
targetConnection.rollback();
throw se;
}
finally
{
targetConnection.setAutoCommit(autoCommit);
}
}
return new long[]{transferred, skipped};
}
/**
* Age-off rule manager
*/
@@ -675,6 +942,14 @@ public class DatabaseManager
broadcast(DatabaseEvent.Type.CALLS_CHANGED);
}
/**
* Notifies listeners that the stored events changed outside of the normal event updates (age-off).
*/
public void notifyEventsChanged()
{
broadcast(DatabaseEvent.Type.EVENTS_CHANGED);
}
private void broadcast(DatabaseEvent.Type type)
{
mEventBroadcaster.broadcast(new DatabaseEvent(type, getConfiguration(), mErrorMessage));
@@ -708,9 +983,23 @@ public class DatabaseManager
}
/**
* Tracks the database row of a call. Accessed only on the database thread.
* Registers the listener to receive each event after it is written to the database, with its row id assigned.
* Notified on the database thread.
*/
public static class CallHandle
public void addEventSavedListener(Listener<EventRecord> listener)
{
mSavedEventBroadcaster.addListener(listener);
}
public void removeEventSavedListener(Listener<EventRecord> listener)
{
mSavedEventBroadcaster.removeListener(listener);
}
/**
* Tracks the database row of a call or event. Accessed only on the database thread.
*/
public static class RowHandle
{
private long mId;
private int mGeneration;

View File

@@ -0,0 +1,370 @@
/*
* *****************************************************************************
* Copyright (C) 2014-2026 Dennis Sheirer
*
* 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 3 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/>
* ****************************************************************************
*/
package io.github.dsheirer.database;
import io.github.dsheirer.database.call.PatchedTalkgroup;
import io.github.dsheirer.protocol.Protocol;
import java.util.List;
/**
* Base class for a stored record (call or decode event) with the channel details and the TO and FROM identifiers.
*
* The TO and FROM identifiers are stored as the formatted identifier, the numeric value, the identifier form and the
* fully qualified address, together with the alias list name of the channel, so that the aliases can be looked up in
* the current alias list when the record is displayed and so that records can be searched by alias.
*/
public abstract class IdentifierRecord
{
public static final String FORM_TALKGROUP = "TALKGROUP";
public static final String FORM_PATCH_GROUP = "PATCH_GROUP";
public static final String FORM_RADIO = "RADIO";
private long mId;
private long mStartTime;
private long mEndTime;
private String mProtocol;
private String mSystem;
private String mSite;
private String mChannel;
private long mFrequency;
private int mTimeslot;
private String mToId;
private Long mToValue;
private String mToForm;
private String mToFullyQualified;
private String mFromId;
private Long mFromValue;
private String mFromForm;
private String mFromFullyQualified;
private String mAliasList;
private List<PatchedTalkgroup> mPatchedTalkgroups = List.of();
/**
* Copies the fields of this base class to the target, for creating a snapshot.
*/
protected void copyTo(IdentifierRecord target)
{
target.mId = mId;
target.mStartTime = mStartTime;
target.mEndTime = mEndTime;
target.mProtocol = mProtocol;
target.mSystem = mSystem;
target.mSite = mSite;
target.mChannel = mChannel;
target.mFrequency = mFrequency;
target.mTimeslot = mTimeslot;
target.mToId = mToId;
target.mToValue = mToValue;
target.mToForm = mToForm;
target.mToFullyQualified = mToFullyQualified;
target.mFromId = mFromId;
target.mFromValue = mFromValue;
target.mFromForm = mFromForm;
target.mFromFullyQualified = mFromFullyQualified;
target.mAliasList = mAliasList;
target.mPatchedTalkgroups = mPatchedTalkgroups;
}
/**
* Database row identifier, or 0 if this record hasn't been inserted yet.
*/
public long getId()
{
return mId;
}
public void setId(long id)
{
mId = id;
}
/**
* Start time, milliseconds since epoch
*/
public long getStartTime()
{
return mStartTime;
}
public void setStartTime(long startTime)
{
mStartTime = startTime;
}
/**
* End time, milliseconds since epoch, or 0 when there is no end time (yet).
*/
public long getEndTime()
{
return mEndTime;
}
public void setEndTime(long endTime)
{
mEndTime = endTime;
}
/**
* Protocol enumeration entry name (e.g. APCO25)
*/
public String getProtocol()
{
return mProtocol;
}
public void setProtocol(String protocol)
{
mProtocol = protocol;
}
/**
* Protocol, or UNKNOWN if the stored value isn't recognized.
*/
public Protocol getProtocolValue()
{
if(mProtocol != null)
{
try
{
return Protocol.valueOf(mProtocol);
}
catch(IllegalArgumentException iae)
{
//Fall through
}
}
return Protocol.UNKNOWN;
}
public String getSystem()
{
return mSystem;
}
public void setSystem(String system)
{
mSystem = system;
}
public String getSite()
{
return mSite;
}
public void setSite(String site)
{
mSite = site;
}
/**
* Channel name from the channel configuration
*/
public String getChannel()
{
return mChannel;
}
public void setChannel(String channel)
{
mChannel = channel;
}
/**
* Channel frequency in Hertz, or 0 if unknown.
*/
public long getFrequency()
{
return mFrequency;
}
public void setFrequency(long frequency)
{
mFrequency = frequency;
}
public int getTimeslot()
{
return mTimeslot;
}
public void setTimeslot(int timeslot)
{
mTimeslot = timeslot;
}
/**
* Formatted TO identifier (talkgroup, patch group or radio)
*/
public String getToId()
{
return mToId;
}
public void setToId(String toId)
{
mToId = toId;
}
/**
* Numeric TO identifier value, or null. For a patch group, this is the patch group talkgroup.
*/
public Long getToValue()
{
return mToValue;
}
public void setToValue(Long toValue)
{
mToValue = toValue;
}
/**
* Indicates if the TO identifier is a talkgroup or patch group that can be used as a hold talkgroup.
*/
public boolean hasTalkgroup()
{
return mToValue != null && mToValue > 0 && isToTalkgroup();
}
/**
* Indicates if the TO identifier is a talkgroup or patch group (as opposed to a radio, for a private call).
*/
public boolean isToTalkgroup()
{
return FORM_TALKGROUP.equals(mToForm) || FORM_PATCH_GROUP.equals(mToForm);
}
/**
* Indicates if the TO identifier is a radio
*/
public boolean isToRadio()
{
return FORM_RADIO.equals(mToForm);
}
/**
* Identifier form (Form enumeration name, e.g. TALKGROUP, PATCH_GROUP or RADIO) of the TO identifier, or null.
*/
public String getToForm()
{
return mToForm;
}
public void setToForm(String toForm)
{
mToForm = toForm;
}
/**
* Fully qualified address (wacn.system.id) of a fully qualified (roaming) TO identifier, or null.
*/
public String getToFullyQualified()
{
return mToFullyQualified;
}
public void setToFullyQualified(String toFullyQualified)
{
mToFullyQualified = toFullyQualified;
}
public String getFromId()
{
return mFromId;
}
public void setFromId(String fromId)
{
mFromId = fromId;
}
public Long getFromValue()
{
return mFromValue;
}
public void setFromValue(Long fromValue)
{
mFromValue = fromValue;
}
/**
* Identifier form (Form enumeration name, e.g. RADIO) of the FROM identifier, or null.
*/
public String getFromForm()
{
return mFromForm;
}
public void setFromForm(String fromForm)
{
mFromForm = fromForm;
}
/**
* Indicates if the FROM identifier is a radio
*/
public boolean isFromRadio()
{
return FORM_RADIO.equals(mFromForm);
}
/**
* Fully qualified address (wacn.system.id) of a fully qualified (roaming) FROM radio, or null.
*/
public String getFromFullyQualified()
{
return mFromFullyQualified;
}
public void setFromFullyQualified(String fromFullyQualified)
{
mFromFullyQualified = fromFullyQualified;
}
/**
* Name of the alias list that was assigned to the channel, or null. Aliases for the TO and FROM identifiers are
* looked up in the current version of this alias list when the record is displayed.
*/
public String getAliasList()
{
return mAliasList;
}
public void setAliasList(String aliasList)
{
mAliasList = aliasList;
}
/**
* Talkgroups that are patched into the TO patch group, or an empty list. Searching for a talkgroup finds the
* records that it participated in through a patch.
*/
public List<PatchedTalkgroup> getPatchedTalkgroups()
{
return mPatchedTalkgroups;
}
public void setPatchedTalkgroups(List<PatchedTalkgroup> patchedTalkgroups)
{
mPatchedTalkgroups = patchedTalkgroups == null ? List.of() : List.copyOf(patchedTalkgroups);
}
}

View File

@@ -0,0 +1,187 @@
/*
* *****************************************************************************
* Copyright (C) 2014-2026 Dennis Sheirer
*
* 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 3 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/>
* ****************************************************************************
*/
package io.github.dsheirer.database;
import io.github.dsheirer.database.call.CallSearchCriteria;
import io.github.dsheirer.database.call.IdentifierFilter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
/**
* Portable SQL helpers shared by the calls and events repositories.
*/
public final class SqlSupport
{
private SqlSupport()
{
}
/**
* WHERE clause for a TO or FROM identifier filter.
* @param prefix of the identifier columns (to or from)
* @param filter to apply
* @param formClause that restricts an alias filter to the identifier forms that the alias identifiers apply to
* @param patchedTable to also match the talkgroups patched into a patch group (TO filter), or null
* @param patchedKey column of the patched table that references the id of the outer row (alias c)
* @param parameters to add the parameter values to
*/
public static String getIdentifierClause(String prefix, IdentifierFilter filter, String formClause,
String patchedTable, String patchedKey, List<Object> parameters)
{
String valueColumn = prefix + "_value";
String fqColumn = prefix + "_fq";
String patched = patchedTable != null ? "EXISTS (SELECT 1 FROM " + patchedTable + " p WHERE p." + patchedKey + " = c.id AND " :
null;
if(filter.isAlias())
{
List<String> matches = new ArrayList<>();
List<Object> matchParameters = new ArrayList<>();
for(IdentifierFilter.ValueRange range: filter.ranges())
{
matches.add("(" + fqColumn + " IS NULL AND protocol = ? AND " + valueColumn + " BETWEEN ? AND ?)");
matchParameters.add(range.protocol().name());
matchParameters.add((long)range.min());
matchParameters.add((long)range.max());
if(patched != null)
{
matches.add("(protocol = ? AND " + patched + "p.fq IS NULL AND p.talkgroup BETWEEN ? AND ?))");
matchParameters.add(range.protocol().name());
matchParameters.add((long)range.min());
matchParameters.add((long)range.max());
}
}
for(IdentifierFilter.FullyQualifiedAddress address: filter.fullyQualified())
{
matches.add("(protocol = ? AND " + fqColumn + " = ?)");
matchParameters.add(address.protocol().name());
matchParameters.add(address.address());
if(patched != null)
{
matches.add("(protocol = ? AND " + patched + "p.fq = ?))");
matchParameters.add(address.protocol().name());
matchParameters.add(address.address());
}
}
if(matches.isEmpty())
{
return "1 = 0";
}
parameters.add(filter.aliasList() != null ? filter.aliasList().toLowerCase(Locale.ROOT) : "");
parameters.addAll(matchParameters);
return "(LOWER(alias_list) = ? AND " + formClause + " AND (" + String.join(" OR ", matches) + "))";
}
Long value = CallSearchCriteria.parseValue(filter.text());
parameters.add(getLikePattern(filter.text()));
if(value != null)
{
parameters.add(value);
if(patched != null)
{
parameters.add(value);
return "(LOWER(" + prefix + "_id) LIKE ? ESCAPE '!' OR " + valueColumn + " = ? OR " + patched +
"p.talkgroup = ?))";
}
return "(LOWER(" + prefix + "_id) LIKE ? ESCAPE '!' OR " + valueColumn + " = ?)";
}
return "LOWER(" + prefix + "_id) LIKE ? ESCAPE '!'";
}
/**
* Case-insensitive 'contains' LIKE pattern with the LIKE wildcard characters escaped.
*/
public static String getLikePattern(String text)
{
String escaped = text.toLowerCase(Locale.ROOT).replace("!", "!!").replace("%", "!%").replace("_", "!_");
return "%" + escaped + "%";
}
public static void bindParameters(PreparedStatement statement, List<Object> parameters) throws SQLException
{
for(int x = 0; x < parameters.size(); x++)
{
Object parameter = parameters.get(x);
if(parameter instanceof String s)
{
statement.setString(x + 1, s);
}
else if(parameter instanceof Integer i)
{
statement.setInt(x + 1, i);
}
else if(parameter instanceof Long l)
{
statement.setLong(x + 1, l);
}
else
{
throw new IllegalArgumentException("Unsupported parameter type: " + parameter);
}
}
}
public static void setString(PreparedStatement statement, int index, String value, int maxLength)
throws SQLException
{
if(value == null)
{
statement.setNull(index, Types.VARCHAR);
}
else
{
statement.setString(index, value.length() > maxLength ? value.substring(0, maxLength) : value);
}
}
public static void setLong(PreparedStatement statement, int index, Long value) throws SQLException
{
if(value == null)
{
statement.setNull(index, Types.BIGINT);
}
else
{
statement.setLong(index, value);
}
}
public static Long getLong(ResultSet rs, String column) throws SQLException
{
long value = rs.getLong(column);
return rs.wasNull() ? null : value;
}
}

View File

@@ -47,6 +47,7 @@ import java.util.concurrent.TimeUnit;
* their recordings, after the retention period (default 1 hour).</li>
* <li>Age rule: removes calls older than a number of days.</li>
* <li>Size rule: removes the oldest recordings when the total size of the recordings exceeds a limit.</li>
* <li>Event rule: deletes decode events older than a number of days (enabled by default, 30 days).</li>
* </ul>
* The age and size rules either delete the call row and its recording file, or delete only the recording file and
* keep the call row.
@@ -138,12 +139,13 @@ public class AgeOffManager
RetentionPeriod retention = mPreference.getDuplicateDoNotMonitorRetention();
if(database == null || (!retention.isEnabled() && !mPreference.isAgeRuleEnabled() &&
!mPreference.isSizeRuleEnabled()))
!mPreference.isSizeRuleEnabled() && !mPreference.isEventRuleEnabled()))
{
return new AgeOffResult(0, 0, 0);
return new AgeOffResult(0, 0, 0, 0);
}
AgeOffResult result = new AgeOffResult(0, 0, 0);
AgeOffResult result = new AgeOffResult(0, 0, 0, 0);
long events = 0;
try(Connection connection = database.getConnection())
{
@@ -166,6 +168,19 @@ public class AgeOffManager
long limit = mPreference.getSizeRuleLimitMB() * BYTES_PER_MB;
result = result.add(applySizeRule(connection, repository, limit, mPreference.getSizeRuleAction()));
}
if(mPreference.isEventRuleEnabled())
{
long cutoff = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(mPreference.getEventRuleDays());
events = database.getEventRepository().deleteStartedBefore(connection, cutoff);
result = result.add(new AgeOffResult(0, 0, 0, events));
}
}
if(events > 0)
{
LOGGER.info("Event age-off removed " + events + " events");
mDatabaseManager.notifyEventsChanged();
}
if(result.calls() > 0 || result.recordings() > 0)
@@ -185,7 +200,7 @@ public class AgeOffManager
static AgeOffResult applyRetentionRule(Connection connection, ICallRepository repository, long cutoff)
throws SQLException
{
AgeOffResult result = new AgeOffResult(0, 0, 0);
AgeOffResult result = new AgeOffResult(0, 0, 0, 0);
List<CallRecord> page = repository.getDuplicateAndDoNotMonitorCalls(connection, cutoff, PAGE_SIZE);
while(!page.isEmpty())
@@ -203,7 +218,7 @@ public class AgeOffManager
static AgeOffResult applyAgeRule(Connection connection, ICallRepository repository, long cutoff,
AgeOffAction action) throws SQLException
{
AgeOffResult result = new AgeOffResult(0, 0, 0);
AgeOffResult result = new AgeOffResult(0, 0, 0, 0);
List<CallRecord> page = action == AgeOffAction.DELETE_CALL_AND_RECORDING ?
repository.getCallsStartedBefore(connection, cutoff, PAGE_SIZE) :
repository.getRecordedCalls(connection, cutoff, PAGE_SIZE);
@@ -226,7 +241,7 @@ public class AgeOffManager
static AgeOffResult applySizeRule(Connection connection, ICallRepository repository, long limitBytes,
AgeOffAction action) throws SQLException
{
AgeOffResult result = new AgeOffResult(0, 0, 0);
AgeOffResult result = new AgeOffResult(0, 0, 0, 0);
long total = repository.getRecordedFileSize(connection);
while(total > limitBytes)
@@ -281,11 +296,11 @@ public class AgeOffManager
if(action == AgeOffAction.DELETE_CALL_AND_RECORDING)
{
repository.delete(connection, ids);
return new AgeOffResult(ids.size(), recordings, bytes);
return new AgeOffResult(ids.size(), recordings, bytes, 0);
}
repository.markRecordingsDeleted(connection, ids);
return new AgeOffResult(0, recordings, bytes);
return new AgeOffResult(0, recordings, bytes, 0);
}
/**
@@ -318,12 +333,14 @@ public class AgeOffManager
* @param calls deleted
* @param recordings deleted
* @param bytes of recordings deleted
* @param events deleted
*/
public record AgeOffResult(long calls, long recordings, long bytes)
public record AgeOffResult(long calls, long recordings, long bytes, long events)
{
public AgeOffResult add(AgeOffResult other)
{
return new AgeOffResult(calls + other.calls, recordings + other.recordings, bytes + other.bytes);
return new AgeOffResult(calls + other.calls, recordings + other.recordings, bytes + other.bytes,
events + other.events);
}
}
}

View File

@@ -18,9 +18,7 @@
*/
package io.github.dsheirer.database.call;
import io.github.dsheirer.protocol.Protocol;
import java.util.List;
import io.github.dsheirer.database.IdentifierRecord;
/**
* A call audio event as stored in the calls table.
@@ -28,33 +26,10 @@ import java.util.List;
* Instances are mutable while they are being built, but a record that has been handed to the database writer or to
* the user interface is a snapshot that is never changed afterwards. Use {@link #copy()} to create a new snapshot.
*/
public class CallRecord
public class CallRecord extends IdentifierRecord
{
public static final String FORM_TALKGROUP = "TALKGROUP";
public static final String FORM_PATCH_GROUP = "PATCH_GROUP";
public static final String FORM_RADIO = "RADIO";
private long mId;
private long mCallKey;
private long mStartTime;
private long mEndTime;
private CallState mCallState = CallState.ACTIVE;
private String mProtocol;
private String mSystem;
private String mSite;
private String mChannel;
private long mFrequency;
private int mTimeslot;
private String mToId;
private Long mToValue;
private String mToForm;
private String mToFullyQualified;
private String mFromId;
private Long mFromValue;
private String mFromForm;
private String mFromFullyQualified;
private String mAliasList;
private List<PatchedTalkgroup> mPatchedTalkgroups = List.of();
private String mTalkerAlias;
private boolean mEncrypted;
private boolean mDuplicate;
@@ -71,27 +46,9 @@ public class CallRecord
public CallRecord copy()
{
CallRecord copy = new CallRecord();
copy.mId = mId;
copyTo(copy);
copy.mCallKey = mCallKey;
copy.mStartTime = mStartTime;
copy.mEndTime = mEndTime;
copy.mCallState = mCallState;
copy.mProtocol = mProtocol;
copy.mSystem = mSystem;
copy.mSite = mSite;
copy.mChannel = mChannel;
copy.mFrequency = mFrequency;
copy.mTimeslot = mTimeslot;
copy.mToId = mToId;
copy.mToValue = mToValue;
copy.mToForm = mToForm;
copy.mToFullyQualified = mToFullyQualified;
copy.mFromId = mFromId;
copy.mFromValue = mFromValue;
copy.mFromForm = mFromForm;
copy.mFromFullyQualified = mFromFullyQualified;
copy.mAliasList = mAliasList;
copy.mPatchedTalkgroups = mPatchedTalkgroups;
copy.mTalkerAlias = mTalkerAlias;
copy.mEncrypted = mEncrypted;
copy.mDuplicate = mDuplicate;
@@ -104,19 +61,6 @@ public class CallRecord
return copy;
}
/**
* Database row identifier, or 0 if this call hasn't been inserted yet.
*/
public long getId()
{
return mId;
}
public void setId(long id)
{
mId = id;
}
/**
* In-memory key that identifies a live call across snapshots, or 0 for records loaded from the database.
*/
@@ -130,43 +74,17 @@ public class CallRecord
mCallKey = callKey;
}
/**
* Call start time, milliseconds since epoch
*/
public long getStartTime()
{
return mStartTime;
}
public void setStartTime(long startTime)
{
mStartTime = startTime;
}
/**
* Call end time, milliseconds since epoch, or 0 while the call is active.
*/
public long getEndTime()
{
return mEndTime;
}
public void setEndTime(long endTime)
{
mEndTime = endTime;
}
/**
* Call duration in milliseconds. For an active call this is the elapsed time up to now.
*/
public long getDuration()
{
if(mEndTime > 0)
if(getEndTime() > 0)
{
return Math.max(0, mEndTime - mStartTime);
return Math.max(0, getEndTime() - getStartTime());
}
return isActive() ? Math.max(0, System.currentTimeMillis() - mStartTime) : 0;
return isActive() ? Math.max(0, System.currentTimeMillis() - getStartTime()) : 0;
}
public CallState getCallState()
@@ -187,248 +105,12 @@ public class CallRecord
return mCallState == CallState.ACTIVE;
}
/**
* Protocol enumeration entry name (e.g. APCO25)
*/
public String getProtocol()
{
return mProtocol;
}
public void setProtocol(String protocol)
{
mProtocol = protocol;
}
/**
* Protocol, or UNKNOWN if the stored value isn't recognized.
*/
public Protocol getProtocolValue()
{
if(mProtocol != null)
{
try
{
return Protocol.valueOf(mProtocol);
}
catch(IllegalArgumentException iae)
{
//Fall through
}
}
return Protocol.UNKNOWN;
}
public String getSystem()
{
return mSystem;
}
public void setSystem(String system)
{
mSystem = system;
}
public String getSite()
{
return mSite;
}
public void setSite(String site)
{
mSite = site;
}
public String getChannel()
{
return mChannel;
}
public void setChannel(String channel)
{
mChannel = channel;
}
/**
* Channel frequency in Hertz, or 0 if unknown.
*/
public long getFrequency()
{
return mFrequency;
}
public void setFrequency(long frequency)
{
mFrequency = frequency;
}
public int getTimeslot()
{
return mTimeslot;
}
public void setTimeslot(int timeslot)
{
mTimeslot = timeslot;
}
/**
* Formatted TO identifier (talkgroup, patch group or radio)
*/
public String getToId()
{
return mToId;
}
public void setToId(String toId)
{
mToId = toId;
}
/**
* Numeric TO identifier value, or null. For a patch group, this is the patch group talkgroup.
*/
public Long getToValue()
{
return mToValue;
}
public void setToValue(Long toValue)
{
mToValue = toValue;
}
/**
* Indicates if the TO identifier of this call is a talkgroup or patch group that can be used as a hold talkgroup.
*/
public boolean hasTalkgroup()
{
return mToValue != null && mToValue > 0 && isToTalkgroup();
}
/**
* Indicates if the TO identifier is a talkgroup or patch group (as opposed to a radio, for a private call).
*/
public boolean isToTalkgroup()
{
return FORM_TALKGROUP.equals(mToForm) || FORM_PATCH_GROUP.equals(mToForm);
}
/**
* Call type (group, patch, private or telephone), derived from the TO and FROM identifier forms.
*/
public CallType getCallType()
{
return CallType.from(mToForm, mFromForm);
}
/**
* Identifier form (Form enumeration name, e.g. TALKGROUP, PATCH_GROUP or RADIO) of the TO identifier, or null.
*/
public String getToForm()
{
return mToForm;
}
public void setToForm(String toForm)
{
mToForm = toForm;
}
/**
* Fully qualified address (wacn.system.id) of a fully qualified (roaming) TO talkgroup, or null.
*/
public String getToFullyQualified()
{
return mToFullyQualified;
}
public void setToFullyQualified(String toFullyQualified)
{
mToFullyQualified = toFullyQualified;
}
/**
* Identifier form (Form enumeration name, e.g. RADIO) of the FROM identifier, or null.
*/
public String getFromForm()
{
return mFromForm;
}
public void setFromForm(String fromForm)
{
mFromForm = fromForm;
}
/**
* Indicates if the FROM identifier is a radio
*/
public boolean isFromRadio()
{
return FORM_RADIO.equals(mFromForm);
}
/**
* Fully qualified address (wacn.system.id) of a fully qualified (roaming) FROM radio, or null.
*/
public String getFromFullyQualified()
{
return mFromFullyQualified;
}
public void setFromFullyQualified(String fromFullyQualified)
{
mFromFullyQualified = fromFullyQualified;
}
/**
* Talkgroups that are patched into the TO patch group of the call, or an empty list. Searching for a talkgroup
* finds the calls that it participated in through a patch.
*/
public List<PatchedTalkgroup> getPatchedTalkgroups()
{
return mPatchedTalkgroups;
}
public void setPatchedTalkgroups(List<PatchedTalkgroup> patchedTalkgroups)
{
mPatchedTalkgroups = patchedTalkgroups == null ? List.of() : List.copyOf(patchedTalkgroups);
}
/**
* Name of the alias list that was assigned to the channel for this call, or null. Aliases for the TO and FROM
* identifiers are looked up in the current version of this alias list when the call is displayed.
*/
public String getAliasList()
{
return mAliasList;
}
public void setAliasList(String aliasList)
{
mAliasList = aliasList;
}
public String getFromId()
{
return mFromId;
}
public void setFromId(String fromId)
{
mFromId = fromId;
}
public Long getFromValue()
{
return mFromValue;
}
public void setFromValue(Long fromValue)
{
mFromValue = fromValue;
return CallType.from(getToForm(), getFromForm());
}
public String getTalkerAlias()
@@ -556,13 +238,13 @@ public class CallRecord
return "F:" + mFilePath;
}
return "C:" + mStartTime + "|" + mChannel + "|" + mTimeslot + "|" + mToId + "|" + mFromId;
return "C:" + getStartTime() + "|" + getChannel() + "|" + getTimeslot() + "|" + getToId() + "|" + getFromId();
}
@Override
public String toString()
{
return "Call " + mId + " " + mProtocol + " TO:" + mToId + " FROM:" + mFromId + " " + mCallState + " " +
return "Call " + getId() + " " + getProtocol() + " TO:" + getToId() + " FROM:" + getFromId() + " " + mCallState + " " +
mRecordingState + (mFilePath != null ? " " + mFilePath : "");
}
}

View File

@@ -21,18 +21,23 @@ package io.github.dsheirer.database.call;
import io.github.dsheirer.alias.id.priority.Priority;
import io.github.dsheirer.database.dialect.IDatabaseDialect;
import static io.github.dsheirer.database.SqlSupport.bindParameters;
import static io.github.dsheirer.database.SqlSupport.getIdentifierClause;
import static io.github.dsheirer.database.SqlSupport.getLikePattern;
import static io.github.dsheirer.database.SqlSupport.getLong;
import static io.github.dsheirer.database.SqlSupport.setLong;
import static io.github.dsheirer.database.SqlSupport.setString;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
@@ -360,31 +365,6 @@ public class CallRepository implements ICallRepository
statement.setString(index, call.getRecordingState().name());
}
private static void setString(PreparedStatement statement, int index, String value, int maxLength)
throws SQLException
{
if(value == null)
{
statement.setNull(index, Types.VARCHAR);
}
else
{
statement.setString(index, value.length() > maxLength ? value.substring(0, maxLength) : value);
}
}
private static void setLong(PreparedStatement statement, int index, Long value) throws SQLException
{
if(value == null)
{
statement.setNull(index, Types.BIGINT);
}
else
{
statement.setLong(index, value);
}
}
/**
* Creates a call record from the current result set row.
*/
@@ -422,12 +402,6 @@ public class CallRepository implements ICallRepository
return call;
}
private static Long getLong(ResultSet rs, String column) throws SQLException
{
long value = rs.getLong(column);
return rs.wasNull() ? null : value;
}
/**
* Marks any calls left in the active state, by a previous session that didn't shut down cleanly, as interrupted.
* Recordings that were being written are left as recorded if the file has data.
@@ -523,13 +497,13 @@ public class CallRepository implements ICallRepository
if(!criteria.to().isEmpty())
{
clauses.add(getIdentifierClause("to", criteria.to(), "to_form IN ('" + CallRecord.FORM_TALKGROUP + "', '" +
CallRecord.FORM_PATCH_GROUP + "')", mPatchedTable, parameters));
CallRecord.FORM_PATCH_GROUP + "')", mPatchedTable, "call_id", parameters));
}
if(!criteria.from().isEmpty())
{
clauses.add(getIdentifierClause("from", criteria.from(), "from_form = '" + CallRecord.FORM_RADIO + "'",
null, parameters));
null, null, parameters));
}
if(!criteria.talkerAlias().isEmpty())
@@ -570,121 +544,6 @@ public class CallRepository implements ICallRepository
return clauses.isEmpty() ? "" : " WHERE " + String.join(" AND ", clauses);
}
/**
* WHERE clause for a TO or FROM identifier filter.
* @param prefix of the identifier columns (to or from)
* @param filter to apply
* @param formClause that restricts an alias filter to the identifier forms that the alias identifiers apply to
* @param patchedTable to also match the talkgroups patched into a patch group call (TO filter), or null
* @param parameters to add the parameter values to
*/
private static String getIdentifierClause(String prefix, IdentifierFilter filter, String formClause,
String patchedTable, List<Object> parameters)
{
String valueColumn = prefix + "_value";
String fqColumn = prefix + "_fq";
String patched = patchedTable != null ? "EXISTS (SELECT 1 FROM " + patchedTable + " p WHERE p.call_id = c.id AND " :
null;
if(filter.isAlias())
{
List<String> matches = new ArrayList<>();
List<Object> matchParameters = new ArrayList<>();
for(IdentifierFilter.ValueRange range: filter.ranges())
{
matches.add("(" + fqColumn + " IS NULL AND protocol = ? AND " + valueColumn + " BETWEEN ? AND ?)");
matchParameters.add(range.protocol().name());
matchParameters.add((long)range.min());
matchParameters.add((long)range.max());
if(patched != null)
{
matches.add("(protocol = ? AND " + patched + "p.fq IS NULL AND p.talkgroup BETWEEN ? AND ?))");
matchParameters.add(range.protocol().name());
matchParameters.add((long)range.min());
matchParameters.add((long)range.max());
}
}
for(IdentifierFilter.FullyQualifiedAddress address: filter.fullyQualified())
{
matches.add("(protocol = ? AND " + fqColumn + " = ?)");
matchParameters.add(address.protocol().name());
matchParameters.add(address.address());
if(patched != null)
{
matches.add("(protocol = ? AND " + patched + "p.fq = ?))");
matchParameters.add(address.protocol().name());
matchParameters.add(address.address());
}
}
if(matches.isEmpty())
{
return "1 = 0";
}
parameters.add(filter.aliasList() != null ? filter.aliasList().toLowerCase(Locale.ROOT) : "");
parameters.addAll(matchParameters);
return "(LOWER(alias_list) = ? AND " + formClause + " AND (" + String.join(" OR ", matches) + "))";
}
Long value = CallSearchCriteria.parseValue(filter.text());
parameters.add(getLikePattern(filter.text()));
if(value != null)
{
parameters.add(value);
if(patched != null)
{
parameters.add(value);
return "(LOWER(" + prefix + "_id) LIKE ? ESCAPE '!' OR " + valueColumn + " = ? OR " + patched +
"p.talkgroup = ?))";
}
return "(LOWER(" + prefix + "_id) LIKE ? ESCAPE '!' OR " + valueColumn + " = ?)";
}
return "LOWER(" + prefix + "_id) LIKE ? ESCAPE '!'";
}
/**
* Case-insensitive 'contains' LIKE pattern with the LIKE wildcard characters escaped.
*/
static String getLikePattern(String text)
{
String escaped = text.toLowerCase(Locale.ROOT).replace("!", "!!").replace("%", "!%").replace("_", "!_");
return "%" + escaped + "%";
}
private static void bindParameters(PreparedStatement statement, List<Object> parameters) throws SQLException
{
for(int x = 0; x < parameters.size(); x++)
{
Object parameter = parameters.get(x);
if(parameter instanceof String s)
{
statement.setString(x + 1, s);
}
else if(parameter instanceof Integer i)
{
statement.setInt(x + 1, i);
}
else if(parameter instanceof Long l)
{
statement.setLong(x + 1, l);
}
else
{
throw new IllegalArgumentException("Unsupported parameter type: " + parameter);
}
}
}
/**
* Reads a page of calls in id order, for transferring calls between databases.
* @param afterId only return calls with an id greater than this value

View File

@@ -80,7 +80,7 @@ public record CallSearchCriteria(IdentifierFilter to, IdentifierFilter from, Str
/**
* Parses the text as a decimal identifier value, or null if it isn't a number.
*/
static Long parseValue(String text)
public static Long parseValue(String text)
{
if(text == null || text.isEmpty() || text.length() > 18)
{

View File

@@ -26,6 +26,7 @@ import io.github.dsheirer.alias.id.radio.RadioRange;
import io.github.dsheirer.alias.id.talkgroup.P25FullyQualifiedTalkgroup;
import io.github.dsheirer.alias.id.talkgroup.Talkgroup;
import io.github.dsheirer.alias.id.talkgroup.TalkgroupRange;
import io.github.dsheirer.database.IdentifierRecord;
import io.github.dsheirer.protocol.Protocol;
import java.util.ArrayList;
@@ -195,7 +196,7 @@ public record IdentifierFilter(String text, String aliasName, String aliasList,
* @param formMatches true if the identifier form is the form that the alias identifiers apply to
* @param patched talkgroups patched into the TO patch group, which also match, or an empty list
*/
boolean matches(CallRecord call, String formatted, Long value, String fullyQualifiedAddress, boolean formMatches,
public boolean matches(IdentifierRecord call, String formatted, Long value, String fullyQualifiedAddress, boolean formMatches,
List<PatchedTalkgroup> patched)
{
if(isAlias())

View File

@@ -0,0 +1,95 @@
/*
* *****************************************************************************
* Copyright (C) 2014-2026 Dennis Sheirer
*
* 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 3 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/>
* ****************************************************************************
*/
package io.github.dsheirer.database.event;
import io.github.dsheirer.module.decode.event.DecodeEventType;
import java.util.EnumSet;
import java.util.Set;
import java.util.stream.Collectors;
/**
* Event categories for filtering, using the same groupings of decode event types as the Now Playing event filters.
*/
public enum EventCategory
{
VOICE_CALL("Voice Call", DecodeEventType.VOICE_CALLS),
ENCRYPTED_CALL("Encrypted Call", DecodeEventType.VOICE_CALLS_ENCRYPTED),
DATA("Data", DecodeEventType.DATA_CALLS),
COMMAND("Command", DecodeEventType.COMMANDS),
REGISTRATION("Registration", DecodeEventType.REGISTRATION),
OTHER("Other", DecodeEventType.OTHERS);
private final String mLabel;
private final Set<DecodeEventType> mTypes;
EventCategory(String label, EnumSet<DecodeEventType> types)
{
mLabel = label;
mTypes = EnumSet.copyOf(types);
}
/**
* Event types in this category
*/
public Set<DecodeEventType> getTypes()
{
return mTypes;
}
/**
* Indicates if the event type belongs to this category
*/
public boolean contains(DecodeEventType type)
{
return mTypes.contains(type);
}
/**
* Category of the event type
*/
public static EventCategory from(DecodeEventType type)
{
for(EventCategory category: values())
{
if(category.contains(type))
{
return category;
}
}
return OTHER;
}
/**
* SQL condition that matches the events in this category. The values are enumeration names, so they are safe
* to include in the statement.
*/
public String getWhereClause()
{
return "event_type IN (" + mTypes.stream().map(type -> "'" + type.name() + "'").sorted()
.collect(Collectors.joining(", ")) + ")";
}
@Override
public String toString()
{
return mLabel;
}
}

View File

@@ -0,0 +1,175 @@
/*
* *****************************************************************************
* Copyright (C) 2014-2026 Dennis Sheirer
*
* 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 3 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/>
* ****************************************************************************
*/
package io.github.dsheirer.database.event;
import io.github.dsheirer.database.IdentifierRecord;
import io.github.dsheirer.module.decode.event.DecodeEventType;
import java.util.Objects;
/**
* A decode event as stored in the events table.
*
* Instances are mutable while they are being built, but a record that has been handed to the database writer or to
* the user interface is a snapshot that is never changed afterwards. Use {@link #copy()} to create a new snapshot.
*/
public class EventRecord extends IdentifierRecord
{
private long mEventKey;
private long mDuration;
private String mEventType;
private String mChannelDescriptor;
private String mTalkerAlias;
private String mDetails;
/**
* Creates a copy (snapshot) of this record.
*/
public EventRecord copy()
{
EventRecord copy = new EventRecord();
copyTo(copy);
copy.mEventKey = mEventKey;
copy.mDuration = mDuration;
copy.mEventType = mEventType;
copy.mChannelDescriptor = mChannelDescriptor;
copy.mTalkerAlias = mTalkerAlias;
copy.mDetails = mDetails;
return copy;
}
/**
* In-memory key that identifies a live event across snapshots, or 0 for records loaded from the database.
*/
public long getEventKey()
{
return mEventKey;
}
public void setEventKey(long eventKey)
{
mEventKey = eventKey;
}
/**
* Event duration in milliseconds, or 0 when the event has no duration.
*/
public long getDuration()
{
return mDuration;
}
public void setDuration(long duration)
{
mDuration = Math.max(0, duration);
}
/**
* Decode event type enumeration entry name (e.g. CALL_GROUP)
*/
public String getEventType()
{
return mEventType;
}
public void setEventType(String eventType)
{
mEventType = eventType;
}
/**
* Decode event type, or UNKNOWN when the stored value isn't recognized.
*/
public DecodeEventType getEventTypeValue()
{
if(mEventType != null)
{
try
{
return DecodeEventType.valueOf(mEventType);
}
catch(IllegalArgumentException iae)
{
//Fall through
}
}
return DecodeEventType.UNKNOWN;
}
/**
* Event category derived from the event type
*/
public EventCategory getCategory()
{
return EventCategory.from(getEventTypeValue());
}
/**
* Channel descriptor for the event (e.g. the traffic channel number of a call), or null.
*/
public String getChannelDescriptor()
{
return mChannelDescriptor;
}
public void setChannelDescriptor(String channelDescriptor)
{
mChannelDescriptor = channelDescriptor;
}
public String getTalkerAlias()
{
return mTalkerAlias;
}
public void setTalkerAlias(String talkerAlias)
{
mTalkerAlias = talkerAlias;
}
/**
* Decoder supplied details for the event, or null.
*/
public String getDetails()
{
return mDetails;
}
public void setDetails(String details)
{
mDetails = details;
}
/**
* Key used to detect an event that already exists in a target database during an append transfer.
*/
public String getTransferKey()
{
return "E:" + getStartTime() + "|" + mEventType + "|" + getChannel() + "|" + getTimeslot() + "|" + getToId() +
"|" + getFromId() + "|" + Objects.hashCode(mDetails);
}
@Override
public String toString()
{
return "Event " + getId() + " " + mEventType + " " + getProtocol() + " TO:" + getToId() + " FROM:" +
getFromId() + (mDetails != null ? " " + mDetails : "");
}
}

View File

@@ -0,0 +1,527 @@
/*
* *****************************************************************************
* Copyright (C) 2014-2026 Dennis Sheirer
*
* 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 3 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/>
* ****************************************************************************
*/
package io.github.dsheirer.database.event;
import io.github.dsheirer.database.IdentifierRecord;
import io.github.dsheirer.database.call.PatchedTalkgroup;
import io.github.dsheirer.database.dialect.IDatabaseDialect;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static io.github.dsheirer.database.SqlSupport.bindParameters;
import static io.github.dsheirer.database.SqlSupport.getIdentifierClause;
import static io.github.dsheirer.database.SqlSupport.getLikePattern;
import static io.github.dsheirer.database.SqlSupport.getLong;
import static io.github.dsheirer.database.SqlSupport.setLong;
import static io.github.dsheirer.database.SqlSupport.setString;
/**
* JDBC implementation of the events table, using SQL that is portable across all supported database engines. All
* methods take a connection from the caller and qualify the table names with the schema.
*/
public class EventRepository implements IEventRepository
{
public static final String TABLE = "events";
public static final String PATCHED_TABLE = "event_patched_talkgroups";
/**
* Columns in insert/select order, excluding the generated id.
*/
private static final String COLUMNS = "start_time, end_time, duration_ms, event_type, protocol, system_name, " +
"site_name, channel_name, channel_descriptor, frequency, timeslot, to_id, to_value, to_form, to_fq, from_id, " +
"from_value, from_form, from_fq, alias_list, talker_alias, details";
private static final int COLUMN_COUNT = 22;
/**
* Maximum stored lengths of the text columns, so that a long value can't fail an insert. Events can have more
* than one TO or FROM identifier, so the identifier columns are longer than in the calls table.
*/
private static final int LENGTH_SHORT = 48;
private static final int LENGTH_ID = 64;
private static final int LENGTH_NAME = 255;
static final int LENGTH_DETAILS = 2048;
private static final String TO_TALKGROUP_FORMS = "to_form IN ('" + IdentifierRecord.FORM_TALKGROUP + "', '" +
IdentifierRecord.FORM_PATCH_GROUP + "')";
private static final String TO_RADIO_FORM = "to_form = '" + IdentifierRecord.FORM_RADIO + "'";
private static final String FROM_RADIO_FORM = "from_form = '" + IdentifierRecord.FORM_RADIO + "'";
private final String mSchema;
private final String mTable;
private final String mPatchedTable;
/**
* Constructs an instance
* @param schema that holds the events tables
*/
public EventRepository(String schema)
{
mSchema = schema;
mTable = schema + "." + TABLE;
mPatchedTable = schema + "." + PATCHED_TABLE;
}
/**
* Statements that create the events tables and their indexes (schema version 3).
*/
public static List<String> getCreateStatements(IDatabaseDialect dialect, String schema)
{
String table = schema + "." + TABLE;
String patchedTable = schema + "." + PATCHED_TABLE;
List<String> statements = new ArrayList<>();
statements.add("CREATE TABLE IF NOT EXISTS " + table + " (" +
"id " + dialect.getIdentityColumn() + ", " +
"start_time BIGINT NOT NULL, " +
"end_time BIGINT NOT NULL, " +
"duration_ms BIGINT NOT NULL, " +
"event_type VARCHAR(" + LENGTH_SHORT + ") NOT NULL, " +
"protocol VARCHAR(" + LENGTH_SHORT + "), " +
"system_name VARCHAR(" + LENGTH_NAME + "), " +
"site_name VARCHAR(" + LENGTH_NAME + "), " +
"channel_name VARCHAR(" + LENGTH_NAME + "), " +
"channel_descriptor VARCHAR(" + LENGTH_NAME + "), " +
"frequency BIGINT NOT NULL, " +
"timeslot INT NOT NULL, " +
"to_id VARCHAR(" + LENGTH_NAME + "), " +
"to_value BIGINT, " +
"to_form VARCHAR(" + LENGTH_SHORT + "), " +
"to_fq VARCHAR(" + LENGTH_ID + "), " +
"from_id VARCHAR(" + LENGTH_NAME + "), " +
"from_value BIGINT, " +
"from_form VARCHAR(" + LENGTH_SHORT + "), " +
"from_fq VARCHAR(" + LENGTH_ID + "), " +
"alias_list VARCHAR(" + LENGTH_NAME + "), " +
"talker_alias VARCHAR(" + LENGTH_NAME + "), " +
"details VARCHAR(" + LENGTH_DETAILS + "))");
//Index names are scoped to the schema (H2, PostgreSQL) or the table (MariaDB), so prefix them with the table
statements.add("CREATE INDEX IF NOT EXISTS events_start_time_idx ON " + table + " (start_time)");
statements.add("CREATE INDEX IF NOT EXISTS events_to_value_idx ON " + table + " (to_value)");
statements.add("CREATE INDEX IF NOT EXISTS events_from_value_idx ON " + table + " (from_value)");
statements.add("CREATE INDEX IF NOT EXISTS events_event_type_idx ON " + table + " (event_type, start_time)");
statements.add("CREATE TABLE IF NOT EXISTS " + patchedTable + " (" +
"event_id BIGINT NOT NULL REFERENCES " + table + "(id) ON DELETE CASCADE, " +
"talkgroup BIGINT NOT NULL, " +
"fq VARCHAR(" + LENGTH_ID + "))");
statements.add("CREATE INDEX IF NOT EXISTS event_patched_event_idx ON " + patchedTable + " (event_id)");
statements.add("CREATE INDEX IF NOT EXISTS event_patched_talkgroup_idx ON " + patchedTable + " (talkgroup)");
return statements;
}
private String getInsertStatement()
{
return "INSERT INTO " + mTable + " (" + COLUMNS + ") VALUES (" + "?, ".repeat(COLUMN_COUNT - 1) + "?)";
}
@Override
public long insert(Connection connection, EventRecord event) throws SQLException
{
try(PreparedStatement statement = connection.prepareStatement(getInsertStatement(), new String[]{"id"}))
{
bind(statement, event);
statement.executeUpdate();
try(ResultSet keys = statement.getGeneratedKeys())
{
if(keys.next())
{
long id = keys.getLong(1);
event.setId(id);
insertPatchedTalkgroups(connection, id, event.getPatchedTalkgroups());
return id;
}
}
}
throw new SQLException("Database didn't return a generated id for the inserted event");
}
@Override
public void insertBatch(Connection connection, List<EventRecord> events) throws SQLException
{
List<EventRecord> patched = new ArrayList<>();
try(PreparedStatement statement = connection.prepareStatement(getInsertStatement()))
{
for(EventRecord event: events)
{
//Events with patched talkgroups need their generated id for the patched talkgroup rows
if(!event.getPatchedTalkgroups().isEmpty())
{
patched.add(event);
continue;
}
bind(statement, event);
statement.addBatch();
}
statement.executeBatch();
}
for(EventRecord event: patched)
{
insert(connection, event);
}
}
@Override
public boolean update(Connection connection, EventRecord event) throws SQLException
{
String assignments = String.join(" = ?, ", COLUMNS.split(", ")) + " = ?";
try(PreparedStatement statement = connection.prepareStatement("UPDATE " + mTable + " SET " + assignments +
" WHERE id = ?"))
{
bind(statement, event);
statement.setLong(COLUMN_COUNT + 1, event.getId());
if(statement.executeUpdate() == 0)
{
return false;
}
}
if(IdentifierRecord.FORM_PATCH_GROUP.equals(event.getToForm()) || !event.getPatchedTalkgroups().isEmpty())
{
try(PreparedStatement statement = connection.prepareStatement("DELETE FROM " + mPatchedTable +
" WHERE event_id = ?"))
{
statement.setLong(1, event.getId());
statement.executeUpdate();
}
insertPatchedTalkgroups(connection, event.getId(), event.getPatchedTalkgroups());
}
return true;
}
private void insertPatchedTalkgroups(Connection connection, long eventId, List<PatchedTalkgroup> talkgroups)
throws SQLException
{
if(talkgroups.isEmpty())
{
return;
}
try(PreparedStatement statement = connection.prepareStatement("INSERT INTO " + mPatchedTable +
" (event_id, talkgroup, fq) VALUES (?, ?, ?)"))
{
for(PatchedTalkgroup talkgroup: talkgroups)
{
statement.setLong(1, eventId);
statement.setLong(2, talkgroup.value());
setString(statement, 3, talkgroup.fullyQualified(), LENGTH_ID);
statement.addBatch();
}
statement.executeBatch();
}
}
/**
* Loads the patched talkgroups of the patch group events in the list.
*/
private void loadPatchedTalkgroups(Connection connection, List<EventRecord> events) throws SQLException
{
Map<Long,EventRecord> patchEvents = new HashMap<>();
for(EventRecord event: events)
{
if(IdentifierRecord.FORM_PATCH_GROUP.equals(event.getToForm()))
{
patchEvents.put(event.getId(), event);
}
}
if(patchEvents.isEmpty())
{
return;
}
Map<Long,List<PatchedTalkgroup>> talkgroups = new HashMap<>();
String placeholders = String.join(", ", Collections.nCopies(patchEvents.size(), "?"));
try(PreparedStatement statement = connection.prepareStatement("SELECT event_id, talkgroup, fq FROM " +
mPatchedTable + " WHERE event_id IN (" + placeholders + ") ORDER BY event_id, talkgroup"))
{
int index = 1;
for(Long id: patchEvents.keySet())
{
statement.setLong(index++, id);
}
try(ResultSet rs = statement.executeQuery())
{
while(rs.next())
{
talkgroups.computeIfAbsent(rs.getLong(1), id -> new ArrayList<>())
.add(new PatchedTalkgroup(rs.getLong(2), rs.getString(3)));
}
}
}
for(Map.Entry<Long,List<PatchedTalkgroup>> entry: talkgroups.entrySet())
{
patchEvents.get(entry.getKey()).setPatchedTalkgroups(entry.getValue());
}
}
/**
* Binds the event column values to statement parameters 1 - COLUMN_COUNT.
*/
private static void bind(PreparedStatement statement, EventRecord event) throws SQLException
{
int index = 1;
statement.setLong(index++, event.getStartTime());
statement.setLong(index++, event.getEndTime());
statement.setLong(index++, event.getDuration());
statement.setString(index++, event.getEventTypeValue().name());
setString(statement, index++, event.getProtocol(), LENGTH_SHORT);
setString(statement, index++, event.getSystem(), LENGTH_NAME);
setString(statement, index++, event.getSite(), LENGTH_NAME);
setString(statement, index++, event.getChannel(), LENGTH_NAME);
setString(statement, index++, event.getChannelDescriptor(), LENGTH_NAME);
statement.setLong(index++, event.getFrequency());
statement.setInt(index++, event.getTimeslot());
setString(statement, index++, event.getToId(), LENGTH_NAME);
setLong(statement, index++, event.getToValue());
setString(statement, index++, event.getToForm(), LENGTH_SHORT);
setString(statement, index++, event.getToFullyQualified(), LENGTH_ID);
setString(statement, index++, event.getFromId(), LENGTH_NAME);
setLong(statement, index++, event.getFromValue());
setString(statement, index++, event.getFromForm(), LENGTH_SHORT);
setString(statement, index++, event.getFromFullyQualified(), LENGTH_ID);
setString(statement, index++, event.getAliasList(), LENGTH_NAME);
setString(statement, index++, event.getTalkerAlias(), LENGTH_NAME);
setString(statement, index, event.getDetails(), LENGTH_DETAILS);
}
/**
* Creates an event record from the current result set row.
*/
private static EventRecord read(ResultSet rs) throws SQLException
{
EventRecord event = new EventRecord();
event.setId(rs.getLong("id"));
event.setStartTime(rs.getLong("start_time"));
event.setEndTime(rs.getLong("end_time"));
event.setDuration(rs.getLong("duration_ms"));
event.setEventType(rs.getString("event_type"));
event.setProtocol(rs.getString("protocol"));
event.setSystem(rs.getString("system_name"));
event.setSite(rs.getString("site_name"));
event.setChannel(rs.getString("channel_name"));
event.setChannelDescriptor(rs.getString("channel_descriptor"));
event.setFrequency(rs.getLong("frequency"));
event.setTimeslot(rs.getInt("timeslot"));
event.setToId(rs.getString("to_id"));
event.setToValue(getLong(rs, "to_value"));
event.setToForm(rs.getString("to_form"));
event.setToFullyQualified(rs.getString("to_fq"));
event.setFromId(rs.getString("from_id"));
event.setFromValue(getLong(rs, "from_value"));
event.setFromForm(rs.getString("from_form"));
event.setFromFullyQualified(rs.getString("from_fq"));
event.setAliasList(rs.getString("alias_list"));
event.setTalkerAlias(rs.getString("talker_alias"));
event.setDetails(rs.getString("details"));
return event;
}
private List<EventRecord> query(Connection connection, String sql, List<Object> parameters) throws SQLException
{
List<EventRecord> events = new ArrayList<>();
try(PreparedStatement statement = connection.prepareStatement(sql))
{
bindParameters(statement, parameters);
try(ResultSet rs = statement.executeQuery())
{
while(rs.next())
{
events.add(read(rs));
}
}
}
loadPatchedTalkgroups(connection, events);
return events;
}
@Override
public List<EventRecord> search(Connection connection, EventSearchCriteria criteria, int offset, int limit)
throws SQLException
{
List<Object> parameters = new ArrayList<>();
String sql = "SELECT id, " + COLUMNS + " FROM " + mTable + " c" + getWhereClause(criteria, parameters) +
" ORDER BY start_time DESC, id DESC LIMIT ? OFFSET ?";
parameters.add(limit);
parameters.add(offset);
return query(connection, sql, parameters);
}
@Override
public long count(Connection connection, EventSearchCriteria criteria) throws SQLException
{
List<Object> parameters = new ArrayList<>();
try(PreparedStatement statement = connection.prepareStatement("SELECT COUNT(*) FROM " + mTable + " c" +
getWhereClause(criteria, parameters)))
{
bindParameters(statement, parameters);
try(ResultSet rs = statement.executeQuery())
{
return rs.next() ? rs.getLong(1) : 0;
}
}
}
@Override
public long count(Connection connection) throws SQLException
{
return count(connection, EventSearchCriteria.all());
}
/**
* Builds the WHERE clause (with a leading space) for the criteria and adds the parameter values to the list.
*/
private String getWhereClause(EventSearchCriteria criteria, List<Object> parameters)
{
List<String> clauses = new ArrayList<>();
if(!criteria.talkgroup().isEmpty())
{
clauses.add(getIdentifierClause("to", criteria.talkgroup(), TO_TALKGROUP_FORMS, mPatchedTable,
"event_id", parameters));
}
if(!criteria.radio().isEmpty())
{
//A radio can be the FROM identifier or the TO identifier (e.g. unit-to-unit call, page, radio check)
String from = getIdentifierClause("from", criteria.radio(), FROM_RADIO_FORM, null, null, parameters);
String to = getIdentifierClause("to", criteria.radio(), TO_RADIO_FORM, null, null, parameters);
clauses.add("(" + from + " OR (" + TO_RADIO_FORM + " AND " + to + "))");
}
if(criteria.category() != null)
{
clauses.add(criteria.category().getWhereClause());
}
if(criteria.eventType() != null)
{
clauses.add("event_type = ?");
parameters.add(criteria.eventType().name());
}
if(criteria.protocol() != null)
{
clauses.add("protocol = ?");
parameters.add(criteria.protocol().name());
}
if(!criteria.system().isEmpty())
{
clauses.add("LOWER(system_name) LIKE ? ESCAPE '!'");
parameters.add(getLikePattern(criteria.system()));
}
if(!criteria.details().isEmpty())
{
clauses.add("LOWER(details) LIKE ? ESCAPE '!'");
parameters.add(getLikePattern(criteria.details()));
}
if(criteria.startTime() > 0)
{
clauses.add("start_time >= ?");
parameters.add(criteria.startTime());
}
if(criteria.endTime() > 0)
{
clauses.add("start_time <= ?");
parameters.add(criteria.endTime());
}
return clauses.isEmpty() ? "" : " WHERE " + String.join(" AND ", clauses);
}
@Override
public List<EventRecord> readPage(Connection connection, long afterId, int limit) throws SQLException
{
List<Object> parameters = new ArrayList<>();
parameters.add(afterId);
parameters.add(limit);
return query(connection, "SELECT id, " + COLUMNS + " FROM " + mTable + " WHERE id > ? ORDER BY id LIMIT ?",
parameters);
}
@Override
public Set<String> getTransferKeys(Connection connection) throws SQLException
{
Set<String> keys = new HashSet<>();
List<EventRecord> page = readPage(connection, 0, 5000);
while(!page.isEmpty())
{
for(EventRecord event: page)
{
keys.add(event.getTransferKey());
}
page = readPage(connection, page.getLast().getId(), 5000);
}
return keys;
}
@Override
public int deleteStartedBefore(Connection connection, long startedBefore) throws SQLException
{
try(PreparedStatement statement = connection.prepareStatement("DELETE FROM " + mTable +
" WHERE start_time < ?"))
{
statement.setLong(1, startedBefore);
return statement.executeUpdate();
}
}
@Override
public String getSchema()
{
return mSchema;
}
}

View File

@@ -0,0 +1,131 @@
/*
* *****************************************************************************
* Copyright (C) 2014-2026 Dennis Sheirer
*
* 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 3 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/>
* ****************************************************************************
*/
package io.github.dsheirer.database.event;
import io.github.dsheirer.database.call.IdentifierFilter;
import io.github.dsheirer.module.decode.event.DecodeEventType;
import io.github.dsheirer.protocol.Protocol;
import java.util.List;
import java.util.Locale;
/**
* Search criteria for the events table. Empty or null values don't filter.
*
* @param talkgroup filter for the TO talkgroup or patch group, including the talkgroups patched into a patch group
* @param radio filter for a radio identifier in either the FROM or the TO role (e.g. a unit-to-unit call or a page)
* @param category of the event type, or null for any category
* @param eventType decode event type, or null for any type
* @param protocol or null for any protocol
* @param system text that the system name contains
* @param details text that the event details contain
* @param startTime earliest event start time, or 0
* @param endTime latest event start time, or 0
*/
public record EventSearchCriteria(IdentifierFilter talkgroup, IdentifierFilter radio, EventCategory category,
DecodeEventType eventType, Protocol protocol, String system, String details,
long startTime, long endTime)
{
public EventSearchCriteria
{
talkgroup = talkgroup == null ? IdentifierFilter.none() : talkgroup;
radio = radio == null ? IdentifierFilter.none() : radio;
system = clean(system);
details = clean(details);
}
/**
* Criteria that match all events
*/
public static EventSearchCriteria all()
{
return new EventSearchCriteria(null, null, null, null, null, null, null, 0, 0);
}
private static String clean(String value)
{
return value == null ? "" : value.trim();
}
/**
* Indicates if these criteria match all events
*/
public boolean isEmpty()
{
return talkgroup.isEmpty() && radio.isEmpty() && category == null && eventType == null && protocol == null &&
system.isEmpty() && details.isEmpty() && startTime == 0 && endTime == 0;
}
/**
* Indicates if the event matches these criteria, for live events that aren't in the database yet. Matches the
* same events as the database search.
*/
public boolean matches(EventRecord event)
{
if(!talkgroup.isEmpty() && !talkgroup.matches(event, event.getToId(), event.getToValue(),
event.getToFullyQualified(), event.isToTalkgroup(), event.getPatchedTalkgroups()))
{
return false;
}
if(!radio.isEmpty() && !radio.matches(event, event.getFromId(), event.getFromValue(),
event.getFromFullyQualified(), event.isFromRadio(), List.of()) && !(event.isToRadio() &&
radio.matches(event, event.getToId(), event.getToValue(), event.getToFullyQualified(), true, List.of())))
{
return false;
}
if(category != null && !category.contains(event.getEventTypeValue()))
{
return false;
}
if(eventType != null && !eventType.name().equals(event.getEventType()))
{
return false;
}
if(protocol != null && !protocol.name().equals(event.getProtocol()))
{
return false;
}
if(!system.isEmpty() && !contains(event.getSystem(), system))
{
return false;
}
if(!details.isEmpty() && !contains(event.getDetails(), details))
{
return false;
}
if(startTime > 0 && event.getStartTime() < startTime)
{
return false;
}
return endTime <= 0 || event.getStartTime() <= endTime;
}
private static boolean contains(String value, String text)
{
return value != null && value.toLowerCase(Locale.ROOT).contains(text.toLowerCase(Locale.ROOT));
}
}

View File

@@ -0,0 +1,90 @@
/*
* *****************************************************************************
* Copyright (C) 2014-2026 Dennis Sheirer
*
* 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 3 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/>
* ****************************************************************************
*/
package io.github.dsheirer.database.event;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Set;
/**
* Storage operations for decode events in one database schema. Each method uses the connection from the caller so
* that the caller controls pooling and transactions.
*/
public interface IEventRepository
{
/**
* Inserts the event and assigns the generated row id to the event.
* @return generated row id
*/
long insert(Connection connection, EventRecord event) throws SQLException;
/**
* Inserts the events in a batch without retrieving the generated ids.
*/
void insertBatch(Connection connection, List<EventRecord> events) throws SQLException;
/**
* Updates the existing event row identified by the event's id.
* @return true if the row exists and was updated
*/
boolean update(Connection connection, EventRecord event) throws SQLException;
/**
* Searches for events matching the criteria, newest first.
* @param offset of the first row to return
* @param limit maximum rows to return
*/
List<EventRecord> search(Connection connection, EventSearchCriteria criteria, int offset, int limit)
throws SQLException;
/**
* Count of events matching the criteria
*/
long count(Connection connection, EventSearchCriteria criteria) throws SQLException;
/**
* Total number of event rows
*/
long count(Connection connection) throws SQLException;
/**
* Reads a page of events in id order, for transferring events between databases.
* @param afterId only return events with an id greater than this value
* @param limit maximum number of events
*/
List<EventRecord> readPage(Connection connection, long afterId, int limit) throws SQLException;
/**
* Transfer keys (see {@link EventRecord#getTransferKey()}) of all events, for skipping events that already exist
* when appending.
*/
Set<String> getTransferKeys(Connection connection) throws SQLException;
/**
* Deletes the events that started before the time, for the event age-off rule.
* @return count of events deleted
*/
int deleteStartedBefore(Connection connection, long startedBefore) throws SQLException;
/**
* Schema name
*/
String getSchema();
}

View File

@@ -0,0 +1,442 @@
/*
* *****************************************************************************
* Copyright (C) 2014-2026 Dennis Sheirer
*
* 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 3 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/>
* ****************************************************************************
*/
package io.github.dsheirer.event;
import io.github.dsheirer.call.CallManager;
import io.github.dsheirer.channel.IChannelDescriptor;
import io.github.dsheirer.controller.NamingThreadFactory;
import io.github.dsheirer.database.DatabaseManager;
import io.github.dsheirer.database.event.EventRecord;
import io.github.dsheirer.identifier.Form;
import io.github.dsheirer.identifier.Identifier;
import io.github.dsheirer.identifier.IdentifierClass;
import io.github.dsheirer.identifier.IdentifierCollection;
import io.github.dsheirer.identifier.Role;
import io.github.dsheirer.identifier.configuration.DecoderTypeConfigurationIdentifier;
import io.github.dsheirer.identifier.configuration.FrequencyConfigurationIdentifier;
import io.github.dsheirer.identifier.radio.FullyQualifiedRadioIdentifier;
import io.github.dsheirer.identifier.talkgroup.FullyQualifiedTalkgroupIdentifier;
import io.github.dsheirer.module.decode.event.DecodeEventType;
import io.github.dsheirer.module.decode.event.IDecodeEvent;
import io.github.dsheirer.preference.identifier.TalkgroupFormatPreference;
import io.github.dsheirer.protocol.Protocol;
import io.github.dsheirer.sample.Broadcaster;
import io.github.dsheirer.sample.Listener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.WeakHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedTransferQueue;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
/**
* Collects the decode events from all channels: saves each event to the database and broadcasts it to the event
* listeners (Events tab, and later external subscribers), so they see the same event activity.
*
* Decoders update an event after it is first broadcast (for example the duration of a call, or the identifiers as
* they are decoded), sometimes without broadcasting it again. The manager keeps checking a received event for
* changes until it hasn't changed for {@link #ACTIVE_WINDOW_MS}, and a later broadcast of the same event resumes the
* checks. Events are identified by object identity, so each event has one row that is updated as it changes.
*
* Events are processed on a single event manager thread every 500 milliseconds.
*/
public class EventManager implements Listener<IDecodeEvent>
{
private static final Logger LOGGER = LoggerFactory.getLogger(EventManager.class);
private static final long PROCESSING_INTERVAL_MS = 500;
static final long ACTIVE_WINDOW_MS = 30_000;
private final LinkedTransferQueue<IDecodeEvent> mQueue = new LinkedTransferQueue<>();
/**
* Tracking state by event. Weak keys, so that the entry is removed when the decoders no longer reference the
* event. The tracking state must not reference the event.
*/
private final Map<IDecodeEvent,TrackedEvent> mTrackedEvents = new WeakHashMap<>();
private final List<ActiveEvent> mActiveEvents = new ArrayList<>();
private final Broadcaster<EventRecord> mEventBroadcaster = new Broadcaster<>();
private final ScheduledExecutorService mExecutor =
Executors.newSingleThreadScheduledExecutor(new NamingThreadFactory("sdrtrunk event manager"));
private final TalkgroupFormatPreference mTalkgroupFormatPreference;
private final DatabaseManager mDatabaseManager;
private long mNextEventKey = 1;
private ScheduledFuture<?> mProcessorHandle;
/**
* Constructs an instance
* @param talkgroupFormatPreference for formatting the TO and FROM identifiers
* @param databaseManager to store the events, or null to only broadcast the events
*/
public EventManager(TalkgroupFormatPreference talkgroupFormatPreference, DatabaseManager databaseManager)
{
mTalkgroupFormatPreference = talkgroupFormatPreference;
mDatabaseManager = databaseManager;
}
/**
* Starts processing events
*/
public void start()
{
if(mProcessorHandle == null)
{
mProcessorHandle = mExecutor.scheduleWithFixedDelay(this::processSafely, PROCESSING_INTERVAL_MS,
PROCESSING_INTERVAL_MS, TimeUnit.MILLISECONDS);
}
}
/**
* Stops processing, after saving the final state of the events received so far.
*/
public void stop()
{
if(mProcessorHandle != null)
{
mProcessorHandle.cancel(false);
mProcessorHandle = null;
try
{
mExecutor.submit(this::process).get(10, TimeUnit.SECONDS);
}
catch(Exception e)
{
LOGGER.error("Error processing the remaining events during shutdown", e);
}
}
mExecutor.shutdownNow();
}
/**
* Registers the listener to receive a snapshot of each event when it is first received and when it changes.
* Snapshots of the same event share the same {@link EventRecord#getEventKey()}. Notified on the event manager
* thread.
*/
public void addEventListener(Listener<EventRecord> listener)
{
mEventBroadcaster.addListener(listener);
}
public void removeEventListener(Listener<EventRecord> listener)
{
mEventBroadcaster.removeListener(listener);
}
/**
* Receives decode events from all channels. Invoked on the channel processing threads.
*/
@Override
public void receive(IDecodeEvent event)
{
if(event != null)
{
mQueue.add(event);
}
}
private void processSafely()
{
try
{
process();
}
catch(Throwable t)
{
LOGGER.error("Error while processing decode events", t);
}
}
/**
* Processes new events and checks the active events for changes. Event manager thread only.
*/
void process()
{
long now = System.currentTimeMillis();
IDecodeEvent event = mQueue.poll();
while(event != null)
{
TrackedEvent tracked = mTrackedEvents.get(event);
if(tracked == null)
{
tracked = new TrackedEvent(mNextEventKey++);
mTrackedEvents.put(event, tracked);
}
tracked.mLastActivity = now;
if(!tracked.mActive)
{
tracked.mActive = true;
mActiveEvents.add(new ActiveEvent(event, tracked));
}
event = mQueue.poll();
}
Iterator<ActiveEvent> it = mActiveEvents.iterator();
while(it.hasNext())
{
ActiveEvent active = it.next();
TrackedEvent tracked = active.tracked();
try
{
if(tracked.update(active.event()))
{
tracked.mLastActivity = now;
}
}
catch(RuntimeException re)
{
//The decoder may be changing the event's identifiers on another thread - try again next time
LOGGER.debug("Unable to read decode event - " + re.getMessage());
}
if(now - tracked.mLastActivity > ACTIVE_WINDOW_MS)
{
tracked.mActive = false;
it.remove();
}
}
}
/**
* Number of events that are checked for changes (for testing).
*/
int getActiveEventCount()
{
return mActiveEvents.size();
}
/**
* Updates the event record fields from the decode event.
*/
void update(EventRecord record, IDecodeEvent event)
{
IdentifierCollection collection = event.getIdentifierCollection();
Identifier to = collection != null ? collection.getToIdentifier() : null;
Identifier from = collection != null ? collection.getFromIdentifier() : null;
record.setStartTime(event.getTimeStart());
record.setEndTime(Math.max(0, event.getTimeEnd()));
record.setDuration(event.getDuration());
record.setEventType((event.getEventType() != null ? event.getEventType() : DecodeEventType.UNKNOWN).name());
Protocol protocol = event.getProtocol();
if(protocol == null || protocol == Protocol.UNKNOWN)
{
protocol = to != null ? to.getProtocol() : (from != null ? from.getProtocol() : null);
}
if((protocol == null || protocol == Protocol.UNKNOWN) && collection != null)
{
Identifier decoder = collection.getIdentifier(IdentifierClass.CONFIGURATION, Form.DECODER_TYPE, Role.ANY);
if(decoder instanceof DecoderTypeConfigurationIdentifier dtci && dtci.getValue() != null)
{
protocol = dtci.getValue().getProtocol();
}
}
record.setProtocol(protocol != null && protocol != Protocol.UNKNOWN ? protocol.name() : null);
record.setSystem(getString(collection, Form.SYSTEM));
record.setSite(getString(collection, Form.SITE));
record.setChannel(getString(collection, Form.CHANNEL));
record.setAliasList(getString(collection, Form.ALIAS_LIST));
IChannelDescriptor descriptor = event.getChannelDescriptor();
record.setChannelDescriptor(descriptor != null ? descriptor.toString() : null);
long frequency = descriptor != null ? descriptor.getDownlinkFrequency() : 0;
if(frequency <= 0 && collection != null)
{
Identifier configured = collection.getIdentifier(IdentifierClass.CONFIGURATION, Form.CHANNEL_FREQUENCY,
Role.ANY);
if(configured instanceof FrequencyConfigurationIdentifier fci && fci.getValue() != null)
{
frequency = fci.getValue();
}
}
record.setFrequency(Math.max(0, frequency));
record.setTimeslot(event.hasTimeslot() ? event.getTimeslot() : -1);
record.setToId(format(collection, Role.TO));
record.setToValue(CallManager.getValue(to));
record.setToForm(to != null ? to.getForm().name() : null);
record.setToFullyQualified(to instanceof FullyQualifiedTalkgroupIdentifier fqt ?
fqt.getFullyQualifiedTalkgroupAddress() : (to instanceof FullyQualifiedRadioIdentifier fqr ?
fqr.getFullyQualifiedRadioAddress() : null));
record.setFromId(format(collection, Role.FROM));
record.setFromValue(CallManager.getValue(from));
record.setFromForm(from != null ? from.getForm().name() : null);
record.setFromFullyQualified(from instanceof FullyQualifiedRadioIdentifier fqr ?
fqr.getFullyQualifiedRadioAddress() : null);
record.setPatchedTalkgroups(CallManager.getPatchedTalkgroups(to));
record.setTalkerAlias(getTalkerAlias(collection));
record.setDetails(event.getDetails() != null && !event.getDetails().isBlank() ? event.getDetails() : null);
}
private static String getString(IdentifierCollection collection, Form form)
{
if(collection == null)
{
return null;
}
Identifier identifier = collection.getIdentifier(IdentifierClass.CONFIGURATION, form, Role.ANY);
return identifier != null && identifier.getValue() != null ? identifier.getValue().toString() : null;
}
/**
* Formats the identifiers with the role as a comma separated list, without talker aliases, like the Now Playing
* events table.
*/
private String format(IdentifierCollection collection, Role role)
{
if(collection == null)
{
return null;
}
StringBuilder sb = new StringBuilder();
for(Identifier identifier: collection.getIdentifiers(role))
{
if(identifier.getForm() == Form.TALKER_ALIAS)
{
continue;
}
if(!sb.isEmpty())
{
sb.append(",");
}
if(identifier.getForm() == Form.TALKGROUP || identifier.getForm() == Form.RADIO ||
identifier.getForm() == Form.PATCH_GROUP)
{
sb.append(mTalkgroupFormatPreference != null ? mTalkgroupFormatPreference.format(identifier) :
identifier.toString());
}
else
{
sb.append(identifier);
}
}
return sb.isEmpty() ? null : sb.toString();
}
private static String getTalkerAlias(IdentifierCollection collection)
{
if(collection == null)
{
return null;
}
List<String> aliases = new ArrayList<>();
for(Identifier identifier: collection.getIdentifiers(Form.TALKER_ALIAS))
{
if(identifier.getValue() != null)
{
aliases.add(identifier.getValue().toString());
}
}
return aliases.isEmpty() ? null : String.join(",", aliases);
}
/**
* Content of an event snapshot that is compared to detect a change that needs to be saved and broadcast.
*/
private static String getSignature(EventRecord record)
{
return record.getStartTime() + "|" + record.getEndTime() + "|" + record.getDuration() + "|" +
record.getEventType() + "|" + record.getProtocol() + "|" + record.getSystem() + "|" + record.getSite() +
"|" + record.getChannel() + "|" + record.getChannelDescriptor() + "|" + record.getFrequency() + "|" +
record.getTimeslot() + "|" + record.getToId() + "|" + record.getToForm() + "|" +
record.getToFullyQualified() + "|" + record.getFromId() + "|" + record.getFromForm() + "|" +
record.getFromFullyQualified() + "|" + record.getAliasList() + "|" + record.getPatchedTalkgroups() + "|" +
record.getTalkerAlias() + "|" + record.getDetails();
}
/**
* An event that is checked for changes, with a strong reference to the event while it's active.
*/
private record ActiveEvent(IDecodeEvent event, TrackedEvent tracked) {}
/**
* Tracking state of one event. Must not reference the event (weak map value). Event manager thread only.
*/
private class TrackedEvent
{
private final EventRecord mRecord = new EventRecord();
private final DatabaseManager.RowHandle mHandle;
private String mLastSignature;
private long mLastActivity;
private boolean mActive;
TrackedEvent(long eventKey)
{
mRecord.setEventKey(eventKey);
mHandle = mDatabaseManager != null ? mDatabaseManager.createHandle() : null;
}
/**
* Updates the record from the event and publishes a snapshot when it changed.
* @return true if the event changed
*/
boolean update(IDecodeEvent event)
{
EventManager.this.update(mRecord, event);
String signature = getSignature(mRecord);
if(Objects.equals(signature, mLastSignature))
{
return false;
}
mLastSignature = signature;
EventRecord snapshot = mRecord.copy();
if(mDatabaseManager != null)
{
//The database writer sets the row id on its own copy
mDatabaseManager.save(mHandle, snapshot.copy());
}
mEventBroadcaster.broadcast(snapshot);
return true;
}
}
}

View File

@@ -23,6 +23,8 @@ import io.github.dsheirer.alias.AliasModel;
import io.github.dsheirer.audio.playback.AudioPlaybackManager;
import io.github.dsheirer.call.CallManager;
import io.github.dsheirer.database.DatabaseManager;
import io.github.dsheirer.event.EventManager;
import io.github.dsheirer.gui.events.EventsView;
import io.github.dsheirer.gui.calls.CallsView;
import io.github.dsheirer.eventbus.MyEventBus;
import io.github.dsheirer.gui.audio.AudioView;
@@ -132,6 +134,7 @@ public class MainWindow
private final HBox mSpectralHiddenBar;
private final NowPlayingView mNowPlayingView;
private final CallsView mCallsView;
private final EventsView mEventsView;
private final TabPane mTabPane = new TabPane();
private BroadcastStatusView mBroadcastStatusView;
private StatusBox mStatusBox;
@@ -159,14 +162,15 @@ public class MainWindow
* @param mapService for the map
* @param resourceMonitor for the status bar
* @param diagnosticMonitor for the reports
* @param databaseManager for the Calls tab
* @param databaseManager for the Calls and Events tabs
* @param callManager for live call updates in the Calls tab
* @param eventManager for live event updates in the Events tab
* @param exitHandler that shuts down the application, invoked on a background thread
*/
public MainWindow(UserPreferences userPreferences, PlaylistManager playlistManager, TunerManager tunerManager,
AudioPlaybackManager audioPlaybackManager, AliasModel aliasModel, IconModel iconModel,
MapService mapService, ResourceMonitor resourceMonitor, DiagnosticMonitor diagnosticMonitor,
DatabaseManager databaseManager, CallManager callManager,
DatabaseManager databaseManager, CallManager callManager, EventManager eventManager,
Runnable exitHandler)
{
mUserPreferences = userPreferences;
@@ -190,6 +194,7 @@ public class MainWindow
boolean detailsVisible = mPreferences.getBoolean(PREFERENCE_NOW_PLAYING_DETAILS_VISIBLE, true);
mNowPlayingView = new NowPlayingView(playlistManager, iconModel, userPreferences, detailsVisible);
mCallsView = new CallsView(databaseManager, callManager, audioPlaybackManager, aliasModel);
mEventsView = new EventsView(databaseManager, eventManager, audioPlaybackManager, aliasModel);
createTabs(mapService, iconModel);
URL stylesheet = MainWindow.class.getResource("/main_window.css");
@@ -307,6 +312,7 @@ public class MainWindow
{
mAudioView.dispose();
mCallsView.dispose();
mEventsView.dispose();
if(mBroadcastStatusView != null)
{
@@ -362,6 +368,7 @@ public class MainWindow
Tab nowPlaying = new Tab("Now Playing", mNowPlayingView);
Tab calls = new Tab("Calls", mCallsView);
Tab events = new Tab("Events", mEventsView);
Tab map = new Tab("Map", mapView);
Tab tuners = new Tab("Tuners", new io.github.dsheirer.source.tuner.ui.TunerView(mTunerManager,
mUserPreferences));
@@ -374,7 +381,7 @@ public class MainWindow
playlist.setGraphic(playIcon);
playlist.setContent(new Region());
mTabPane.getTabs().addAll(nowPlaying, calls, map, tuners, playlist);
mTabPane.getTabs().addAll(nowPlaying, calls, events, map, tuners, playlist);
mTabPane.setTabClosingPolicy(TabPane.TabClosingPolicy.UNAVAILABLE);
mTabPane.getStyleClass().add("main-tab-pane");
mTabPane.getSelectionModel().selectedItemProperty().addListener((observable, oldTab, newTab) -> {

View File

@@ -40,6 +40,7 @@ import io.github.dsheirer.module.log.EventLogManager;
import io.github.dsheirer.monitor.DiagnosticMonitor;
import io.github.dsheirer.call.CallManager;
import io.github.dsheirer.database.DatabaseManager;
import io.github.dsheirer.event.EventManager;
import io.github.dsheirer.monitor.ResourceMonitor;
import io.github.dsheirer.playlist.PlaylistManager;
import io.github.dsheirer.preference.UserPreferences;
@@ -75,6 +76,7 @@ public class SDRTrunk implements Listener<TunerEvent>
private CallManager mCallManager;
private DatabaseManager mDatabaseManager;
private EventManager mEventManager;
private AudioStreamingManager mAudioStreamingManager;
private DiagnosticMonitor mDiagnosticMonitor;
private IconModel mIconModel = new IconModel();
@@ -178,6 +180,8 @@ public class SDRTrunk implements Listener<TunerEvent>
mCallManager = new CallManager(mUserPreferences, mDatabaseManager);
mCallManager.start();
mEventManager = new EventManager(mUserPreferences.getTalkgroupFormatPreference(), mDatabaseManager);
mEventManager.start();
mResourceMonitor.setDatabasePreference(mUserPreferences.getDatabasePreference());
mAudioStreamingManager = new AudioStreamingManager(mPlaylistManager.getBroadcastModel(), BroadcastFormat.MP3,
@@ -193,13 +197,14 @@ public class SDRTrunk implements Listener<TunerEvent>
MapService mapService = new MapService(aliasModel, mIconModel);
mPlaylistManager.getChannelProcessingManager().addDecodeEventListener(mapService);
mPlaylistManager.getChannelProcessingManager().addDecodeEventListener(mEventManager);
if(!headless)
{
mResourceMonitor.start();
mMainWindow = FxToolkit.callAndWait(() -> new MainWindow(mUserPreferences, mPlaylistManager,
mTunerManager, audioPlaybackManager, aliasModel, mIconModel, mapService, mResourceMonitor,
mDiagnosticMonitor, mDatabaseManager, mCallManager, this::processShutdown));
mDiagnosticMonitor, mDatabaseManager, mCallManager, mEventManager, this::processShutdown));
}
TunerSpectralDisplayManager tunerSpectralDisplayManager = new TunerSpectralDisplayManager(
@@ -333,6 +338,7 @@ public class SDRTrunk implements Listener<TunerEvent>
mLog.info("Stopping channels ...");
mPlaylistManager.getChannelProcessingManager().shutdown();
mCallManager.stop();
mEventManager.stop();
mLog.info("Closing call database ...");
mDatabaseManager.stop();
mResourceMonitor.stop();

View File

@@ -21,7 +21,7 @@ package io.github.dsheirer.gui.calls;
import io.github.dsheirer.alias.Alias;
import io.github.dsheirer.alias.AliasList;
import io.github.dsheirer.alias.AliasModel;
import io.github.dsheirer.database.call.CallRecord;
import io.github.dsheirer.database.IdentifierRecord;
import io.github.dsheirer.database.call.PatchedTalkgroup;
/**
@@ -41,7 +41,7 @@ public class CallAliasResolver
mAliasModel = aliasModel;
}
private AliasList getAliasList(CallRecord call)
private AliasList getAliasList(IdentifierRecord call)
{
if(mAliasModel == null || call == null || call.getAliasList() == null || call.getAliasList().isEmpty())
{
@@ -54,7 +54,7 @@ public class CallAliasResolver
/**
* Current alias for the TO talkgroup, patch group or radio of the call, or null.
*/
public Alias getToAlias(CallRecord call)
public Alias getToAlias(IdentifierRecord call)
{
AliasList aliasList = getAliasList(call);
@@ -70,7 +70,7 @@ public class CallAliasResolver
return aliasList.getTalkgroupAlias(call.getProtocolValue(), value, call.getToFullyQualified());
}
if(CallRecord.FORM_RADIO.equals(call.getToForm()))
if(IdentifierRecord.FORM_RADIO.equals(call.getToForm()))
{
return aliasList.getRadioAlias(call.getProtocolValue(), value, call.getToFullyQualified());
}
@@ -81,7 +81,7 @@ public class CallAliasResolver
/**
* Current alias for the FROM radio of the call, or null.
*/
public Alias getFromAlias(CallRecord call)
public Alias getFromAlias(IdentifierRecord call)
{
AliasList aliasList = getAliasList(call);
@@ -99,7 +99,7 @@ public class CallAliasResolver
* Talkgroups patched into the TO patch group, with their current alias names, e.g. "1001 Fire Dispatch, 1002",
* or null when the call isn't a patch group call.
*/
public String getPatchedTalkgroups(CallRecord call)
public String getPatchedTalkgroups(IdentifierRecord call)
{
if(call == null || call.getPatchedTalkgroups().isEmpty())
{
@@ -134,7 +134,7 @@ public class CallAliasResolver
/**
* Name of the current TO alias, or null
*/
public String getToAliasName(CallRecord call)
public String getToAliasName(IdentifierRecord call)
{
Alias alias = getToAlias(call);
return alias != null ? alias.getName() : null;
@@ -143,7 +143,7 @@ public class CallAliasResolver
/**
* Name of the current FROM alias, or null
*/
public String getFromAliasName(CallRecord call)
public String getFromAliasName(IdentifierRecord call)
{
Alias alias = getFromAlias(call);
return alias != null ? alias.getName() : null;

View File

@@ -94,8 +94,8 @@ public class CallsView extends VBox
{
private static final String TABLE_KEY = "calls.view.table";
private static final String PREFERENCE_PAGE_SIZE = "calls.view.page.size";
static final List<Integer> PAGE_SIZES = List.of(50, 100, 250, 500);
static final int DEFAULT_PAGE_SIZE = 500;
public static final List<Integer> PAGE_SIZES = List.of(50, 100, 250, 500);
public static final int DEFAULT_PAGE_SIZE = 500;
private final Preferences mPreferences = Preferences.userNodeForPackage(CallsView.class);
private final ComboBox<Integer> mPageSizeComboBox = new ComboBox<>();
private int mPageSize = DEFAULT_PAGE_SIZE;
@@ -352,7 +352,7 @@ public class CallsView extends VBox
/**
* Page size from the stored preference, or the default when the stored value isn't one of the choices.
*/
static int getStoredPageSize(int stored)
public static int getStoredPageSize(int stored)
{
return PAGE_SIZES.contains(stored) ? stored : DEFAULT_PAGE_SIZE;
}
@@ -360,7 +360,7 @@ public class CallsView extends VBox
/**
* Formats a timestamp for display
*/
static String formatTime(long timestamp)
public static String formatTime(long timestamp)
{
return timestamp > 0 ? TIME_FORMAT.get().format(new Date(timestamp)) : "";
}
@@ -796,7 +796,7 @@ public class CallsView extends VBox
/**
* Menu label suffix for the audio channel, empty for a mono output.
*/
static String getChannelLabel(AudioChannel channel, int channelCount)
public static String getChannelLabel(AudioChannel channel, int channelCount)
{
if(channelCount <= 1)
{

View File

@@ -0,0 +1,767 @@
/*
* *****************************************************************************
* Copyright (C) 2014-2026 Dennis Sheirer
*
* 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 3 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/>
* ****************************************************************************
*/
package io.github.dsheirer.gui.events;
import io.github.dsheirer.alias.Alias;
import io.github.dsheirer.alias.AliasModel;
import io.github.dsheirer.audio.playback.AudioChannel;
import io.github.dsheirer.audio.playback.AudioPlaybackManager;
import io.github.dsheirer.audio.playback.TalkgroupHold;
import io.github.dsheirer.database.DatabaseEvent;
import io.github.dsheirer.database.DatabaseManager;
import io.github.dsheirer.database.event.EventCategory;
import io.github.dsheirer.database.event.EventRecord;
import io.github.dsheirer.database.event.EventSearchCriteria;
import io.github.dsheirer.event.EventManager;
import io.github.dsheirer.gui.calls.AliasSearchField;
import io.github.dsheirer.gui.calls.CallAliasResolver;
import io.github.dsheirer.gui.calls.CallsView;
import io.github.dsheirer.gui.table.ColumnConfigurationButton;
import io.github.dsheirer.gui.table.TableColumnMonitor;
import io.github.dsheirer.module.decode.event.DecodeEventType;
import io.github.dsheirer.protocol.Protocol;
import io.github.dsheirer.sample.Listener;
import javafx.application.Platform;
import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.MenuItem;
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.Tooltip;
import javafx.scene.input.Clipboard;
import javafx.scene.input.ClipboardContent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
import jiconfont.icons.font_awesome.FontAwesome;
import jiconfont.javafx.IconNode;
import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.prefs.Preferences;
/**
* Events tab: searchable view of the decode events from all channels, stored in the events table of the database,
* with live updates.
*
* The table shows one page of search results, newest first. On the first page, new events that match the search
* criteria are added at the top as they arrive, and events that change (e.g. the duration of a call) are updated.
*/
public class EventsView extends VBox
{
private static final String TABLE_KEY = "events.view.table";
private static final String PREFERENCE_PAGE_SIZE = "events.view.page.size";
private static final int MAX_EVENT_KEYS = 5000;
private final Preferences mPreferences = Preferences.userNodeForPackage(EventsView.class);
private final ComboBox<Integer> mPageSizeComboBox = new ComboBox<>();
private int mPageSize = CallsView.DEFAULT_PAGE_SIZE;
private final DatabaseManager mDatabaseManager;
private final EventManager mEventManager;
private final AudioPlaybackManager mAudioPlaybackManager;
private final AliasModel mAliasModel;
private final CallAliasResolver mAliasResolver;
private final ObservableList<EventRecord> mEvents = FXCollections.observableArrayList();
private final TableView<EventRecord> mTableView = new TableView<>(mEvents);
private final TableColumnMonitor<EventRecord> mTableColumnMonitor;
/**
* Latest snapshot of recent live events by event key, so that events reloaded from the database continue to
* update. Oldest entries are dropped.
*/
private final Map<Long,EventRecord> mLiveEvents = new LinkedHashMap<>()
{
@Override
protected boolean removeEldestEntry(Map.Entry<Long,EventRecord> eldest)
{
return size() > MAX_EVENT_KEYS;
}
};
/**
* Event key of recent live events by database row id. Oldest entries are dropped.
*/
private final Map<Long,Long> mEventKeyById = new LinkedHashMap<>()
{
@Override
protected boolean removeEldestEntry(Map.Entry<Long,Long> eldest)
{
return size() > MAX_EVENT_KEYS;
}
};
private final Listener<EventRecord> mEventListener = event -> Platform.runLater(() -> receiveLiveEvent(event));
private final Listener<EventRecord> mSavedListener = event -> Platform.runLater(() -> receiveSavedEvent(event));
private final Listener<DatabaseEvent> mDatabaseListener = event -> Platform.runLater(() -> receive(event));
private final ListChangeListener<Alias> mAliasChangeListener = change -> scheduleAliasRefresh();
private boolean mAliasRefreshPending;
private final AliasSearchField mTalkgroupField;
private final AliasSearchField mRadioField;
private final ComboBox<EventCategory> mCategoryComboBox = new ComboBox<>();
private final ComboBox<DecodeEventType> mEventTypeComboBox = new ComboBox<>();
private final ComboBox<Protocol> mProtocolComboBox = new ComboBox<>();
private final TextField mSystemField = new TextField();
private final TextField mDetailsField = new TextField();
private final DatePicker mFromDatePicker = new DatePicker();
private final DatePicker mToDatePicker = new DatePicker();
private final Label mStatusLabel = new Label();
private final Button mPreviousButton = new Button("Newer");
private final Button mNextButton = new Button("Older");
private EventSearchCriteria mCriteria = EventSearchCriteria.all();
private int mOffset = 0;
private long mTotal = 0;
private long mSearchSequence = 0;
/**
* Constructs an instance
* @param databaseManager for searching the events
* @param eventManager for live event updates
* @param audioPlaybackManager for holding a talkgroup on an audio channel
* @param aliasModel for the current TO and FROM aliases of the events, and the alias search lists
*/
public EventsView(DatabaseManager databaseManager, EventManager eventManager,
AudioPlaybackManager audioPlaybackManager, AliasModel aliasModel)
{
mDatabaseManager = databaseManager;
mEventManager = eventManager;
mAudioPlaybackManager = audioPlaybackManager;
mAliasModel = aliasModel;
mAliasResolver = new CallAliasResolver(aliasModel);
mTalkgroupField = new AliasSearchField(AliasSearchField.Kind.TALKGROUP, aliasModel, "Talkgroup",
this::applySearch);
mRadioField = new AliasSearchField(AliasSearchField.Kind.RADIO, aliasModel, "Radio", this::applySearch);
setSpacing(6);
setPadding(new Insets(6));
getStyleClass().addAll("calls-view", "events-view");
createColumns();
mTableColumnMonitor = new TableColumnMonitor<>(mTableView, TABLE_KEY);
mTableView.setPlaceholder(new Label("No events"));
mTableView.setRowFactory(table -> createRow());
VBox.setVgrow(mTableView, Priority.ALWAYS);
getChildren().addAll(createSearchBar(), mTableView, createPagingBar());
if(mEventManager != null)
{
mEventManager.addEventListener(mEventListener);
}
if(mDatabaseManager != null)
{
mDatabaseManager.addEventSavedListener(mSavedListener);
mDatabaseManager.addListener(mDatabaseListener);
}
if(mAliasModel != null)
{
mAliasModel.aliasList().addListener(mAliasChangeListener);
}
search();
}
/**
* Deregisters listeners
*/
public void dispose()
{
if(mEventManager != null)
{
mEventManager.removeEventListener(mEventListener);
}
if(mDatabaseManager != null)
{
mDatabaseManager.removeEventSavedListener(mSavedListener);
mDatabaseManager.removeListener(mDatabaseListener);
}
if(mAliasModel != null)
{
mAliasModel.aliasList().removeListener(mAliasChangeListener);
}
}
/**
* Refreshes the alias columns once after a burst of alias changes (e.g. loading or editing the playlist).
*/
private void scheduleAliasRefresh()
{
if(!mAliasRefreshPending)
{
mAliasRefreshPending = true;
Platform.runLater(() -> {
mAliasRefreshPending = false;
mTableView.refresh();
});
}
}
private static final DecimalFormat DURATION_FORMAT = new DecimalFormat("0.0");
/**
* Formats an event duration in seconds with one decimal, like the Now Playing events table.
*/
static String formatDuration(long milliseconds)
{
return milliseconds > 0 ? DURATION_FORMAT.format(milliseconds / 1e3d) : "";
}
/**
* Event types for the event type filter: all types, or the types of the category, sorted by label.
*/
static List<DecodeEventType> getEventTypes(EventCategory category)
{
return Arrays.stream(DecodeEventType.values())
.filter(type -> category == null || category.contains(type))
.sorted(Comparator.comparing(DecodeEventType::getLabel, String.CASE_INSENSITIVE_ORDER))
.toList();
}
private HBox createSearchBar()
{
mTalkgroupField.setPrefColumnCount(11);
mRadioField.setPrefColumnCount(11);
mCategoryComboBox.getItems().add(null);
mCategoryComboBox.getItems().addAll(EventCategory.values());
mCategoryComboBox.setButtonCell(createCell("Any Category"));
mCategoryComboBox.setCellFactory(list -> createCell("Any Category"));
mCategoryComboBox.setPromptText("Any Category");
mCategoryComboBox.setTooltip(new Tooltip("Event category: the same groupings as the Now Playing event filters"));
mCategoryComboBox.setOnAction(event -> {
//Limit the event type choices to the category
DecodeEventType selected = mEventTypeComboBox.getValue();
List<DecodeEventType> types = getEventTypes(mCategoryComboBox.getValue());
mEventTypeComboBox.getItems().setAll(types);
mEventTypeComboBox.getItems().addFirst(null);
mEventTypeComboBox.setValue(types.contains(selected) ? selected : null);
applySearch();
});
mEventTypeComboBox.getItems().add(null);
mEventTypeComboBox.getItems().addAll(getEventTypes(null));
mEventTypeComboBox.setButtonCell(createCell("Any Event"));
mEventTypeComboBox.setCellFactory(list -> createCell("Any Event"));
mEventTypeComboBox.setPromptText("Any Event");
mEventTypeComboBox.setVisibleRowCount(20);
mEventTypeComboBox.setOnAction(event -> applySearch());
mProtocolComboBox.getItems().add(null);
mProtocolComboBox.getItems().addAll(Arrays.stream(Protocol.values())
.filter(protocol -> protocol != Protocol.UNKNOWN)
.sorted(Comparator.comparing(Protocol::toString, String.CASE_INSENSITIVE_ORDER)).toList());
mProtocolComboBox.setButtonCell(createCell("Any Protocol"));
mProtocolComboBox.setCellFactory(list -> createCell("Any Protocol"));
mProtocolComboBox.setPromptText("Any Protocol");
mProtocolComboBox.setVisibleRowCount(20);
mProtocolComboBox.setOnAction(event -> applySearch());
mSystemField.setPromptText("System");
mSystemField.setPrefColumnCount(8);
mDetailsField.setPromptText("Details");
mDetailsField.setPrefColumnCount(10);
mDetailsField.setTooltip(new Tooltip("Matches part of the event details (e.g. a text message or GPS)"));
mFromDatePicker.setPromptText("From date");
mFromDatePicker.setPrefWidth(130);
mToDatePicker.setPromptText("To date");
mToDatePicker.setPrefWidth(130);
Button searchButton = new Button("Search");
IconNode searchIcon = new IconNode(FontAwesome.SEARCH);
searchIcon.setIconSize(12);
searchIcon.fillProperty().bind(searchButton.textFillProperty());
searchButton.setGraphic(searchIcon);
searchButton.setOnAction(event -> applySearch());
Button clearButton = new Button("Clear");
clearButton.setOnAction(event -> {
mTalkgroupField.reset();
mRadioField.reset();
mCategoryComboBox.setValue(null);
mEventTypeComboBox.getItems().setAll(getEventTypes(null));
mEventTypeComboBox.getItems().addFirst(null);
mEventTypeComboBox.setValue(null);
mProtocolComboBox.setValue(null);
mSystemField.clear();
mDetailsField.clear();
mFromDatePicker.setValue(null);
mToDatePicker.setValue(null);
applySearch();
});
for(TextField field: new TextField[]{mSystemField, mDetailsField})
{
field.setOnAction(event -> applySearch());
}
Region spacer = new Region();
HBox.setHgrow(spacer, Priority.ALWAYS);
HBox bar = new HBox(6, mTalkgroupField, mRadioField, mCategoryComboBox, mEventTypeComboBox,
mProtocolComboBox, mSystemField, mDetailsField, mFromDatePicker, mToDatePicker, searchButton, clearButton,
spacer, new ColumnConfigurationButton(mTableColumnMonitor, "Events Columns"));
bar.setAlignment(Pos.CENTER_LEFT);
return bar;
}
private static <T> ListCell<T> createCell(String nullLabel)
{
return new ListCell<>()
{
@Override
protected void updateItem(T item, boolean empty)
{
super.updateItem(item, empty);
setText(empty ? null : (item == null ? nullLabel : item.toString()));
}
};
}
private HBox createPagingBar()
{
mPreviousButton.setOnAction(event -> {
mOffset = Math.max(0, mOffset - mPageSize);
search();
});
mNextButton.setOnAction(event -> {
mOffset += mPageSize;
search();
});
Button refreshButton = new Button("Refresh");
refreshButton.setOnAction(event -> search());
mPageSize = CallsView.getStoredPageSize(mPreferences.getInt(PREFERENCE_PAGE_SIZE, CallsView.DEFAULT_PAGE_SIZE));
mPageSizeComboBox.getItems().addAll(CallsView.PAGE_SIZES);
mPageSizeComboBox.setValue(mPageSize);
mPageSizeComboBox.setTooltip(new Tooltip("Number of events per page"));
mPageSizeComboBox.valueProperty().addListener((observable, oldValue, newValue) -> {
if(newValue != null && newValue != mPageSize)
{
//Keep the first displayed event on the new page
mOffset = (mOffset / newValue) * newValue;
mPageSize = newValue;
mPreferences.putInt(PREFERENCE_PAGE_SIZE, newValue);
search();
}
});
Label pageSizeLabel = new Label("Page size:");
pageSizeLabel.setLabelFor(mPageSizeComboBox);
Region spacer = new Region();
HBox.setHgrow(spacer, Priority.ALWAYS);
HBox bar = new HBox(6, mStatusLabel, spacer, pageSizeLabel, mPageSizeComboBox, refreshButton,
mPreviousButton, mNextButton);
bar.setAlignment(Pos.CENTER_LEFT);
return bar;
}
private void createColumns()
{
mTableView.getColumns().add(column("start", "Time", 140, event -> CallsView.formatTime(event.getStartTime())));
mTableView.getColumns().add(column("duration", "Duration", 65, event -> formatDuration(event.getDuration())));
mTableView.getColumns().add(column("event", "Event", 150, event -> event.getEventTypeValue().getLabel()));
TableColumn<EventRecord,String> category = column("category", "Category", 100,
event -> event.getCategory().toString());
category.setVisible(false);
mTableView.getColumns().add(category);
mTableView.getColumns().add(column("to", "To", 90, EventRecord::getToId));
mTableView.getColumns().add(column("toAlias", "To Alias", 150, mAliasResolver::getToAliasName));
mTableView.getColumns().add(column("patched", "Patched", 140, mAliasResolver::getPatchedTalkgroups));
mTableView.getColumns().add(column("from", "From", 90, EventRecord::getFromId));
mTableView.getColumns().add(column("fromAlias", "From Alias", 150, mAliasResolver::getFromAliasName));
mTableView.getColumns().add(column("talkerAlias", "Talker Alias", 120, EventRecord::getTalkerAlias));
mTableView.getColumns().add(column("channelNumber", "Channel Number", 110, EventsView::getChannelNumber));
mTableView.getColumns().add(column("frequency", "Frequency", 90, event -> event.getFrequency() > 0 ?
String.format("%.5f", event.getFrequency() / 1E6) : ""));
mTableView.getColumns().add(column("details", "Details", 300, EventRecord::getDetails));
//Protocol, system, site and channel at the end by default, like the Calls tab
mTableView.getColumns().add(column("protocol", "Protocol", 80, event -> event.getProtocolValue() ==
Protocol.UNKNOWN ? "" : event.getProtocolValue().toString()));
mTableView.getColumns().add(column("system", "System", 110, EventRecord::getSystem));
mTableView.getColumns().add(column("site", "Site", 90, EventRecord::getSite));
mTableView.getColumns().add(column("channel", "Channel", 110, EventRecord::getChannel));
for(TableColumn<EventRecord,?> column: mTableView.getColumns())
{
column.setSortable(false);
}
}
/**
* Channel descriptor and timeslot of the event, like the Now Playing events table.
*/
static String getChannelNumber(EventRecord event)
{
String descriptor = event.getChannelDescriptor();
boolean timeslot = event.getTimeslot() >= 0;
if(descriptor != null)
{
return timeslot && !descriptor.contains("TS") ? descriptor + " TS" + event.getTimeslot() : descriptor;
}
return timeslot ? "TS" + event.getTimeslot() : null;
}
private static TableColumn<EventRecord,String> column(String id, String title, double width,
Function<EventRecord,String> value)
{
TableColumn<EventRecord,String> column = new TableColumn<>(title);
column.setId(id);
column.setPrefWidth(width);
column.setCellValueFactory(cdf -> new ReadOnlyObjectWrapper<>(cdf.getValue() != null ?
value.apply(cdf.getValue()) : null));
return column;
}
private TableRow<EventRecord> createRow()
{
TableRow<EventRecord> row = new TableRow<>();
row.setOnContextMenuRequested(event -> {
if(!row.isEmpty())
{
mTableView.getSelectionModel().select(row.getIndex());
createContextMenu(row.getItem()).show(row, event.getScreenX(), event.getScreenY());
event.consume();
}
});
return row;
}
/**
* Context menu for an event: hold the TO talkgroup on an audio channel, copy the details.
*/
private ContextMenu createContextMenu(EventRecord event)
{
ContextMenu menu = new ContextMenu();
TalkgroupHold hold = TalkgroupHold.from(event, mAliasResolver.getToAliasName(event));
if(mAudioPlaybackManager != null)
{
List<AudioChannel> channels = mAudioPlaybackManager.getAudioChannels();
for(int x = 0; x < channels.size(); x++)
{
final int channelIndex = x;
String channelName = CallsView.getChannelLabel(channels.get(x), channels.size());
TalkgroupHold current = mAudioPlaybackManager.getHold(x);
MenuItem holdItem = new MenuItem(hold != null ? "Hold Talkgroup " + event.getToId() + channelName :
"Hold Talkgroup" + channelName);
holdItem.setDisable(hold == null || hold.isSameTalkgroup(current));
holdItem.setOnAction(e -> mAudioPlaybackManager.setHold(channelIndex, hold));
menu.getItems().add(holdItem);
if(current != null)
{
MenuItem release = new MenuItem("Release Hold " + current + channelName);
release.setOnAction(e -> mAudioPlaybackManager.setHold(channelIndex, null));
menu.getItems().add(release);
}
}
if(!channels.isEmpty())
{
menu.getItems().add(new SeparatorMenuItem());
}
}
MenuItem copy = new MenuItem("Copy Details");
copy.setDisable(event.getDetails() == null);
copy.setOnAction(e -> {
ClipboardContent content = new ClipboardContent();
content.putString(event.getDetails());
Clipboard.getSystemClipboard().setContent(content);
});
menu.getItems().add(copy);
return menu;
}
private void applySearch()
{
long start = toMillis(mFromDatePicker.getValue(), false);
long end = toMillis(mToDatePicker.getValue(), true);
mCriteria = new EventSearchCriteria(mTalkgroupField.getFilter(), mRadioField.getFilter(),
mCategoryComboBox.getValue(), mEventTypeComboBox.getValue(), mProtocolComboBox.getValue(),
mSystemField.getText(), mDetailsField.getText(), start, end);
mOffset = 0;
search();
}
private static long toMillis(LocalDate date, boolean endOfDay)
{
if(date == null)
{
return 0;
}
LocalDate day = endOfDay ? date.plusDays(1) : date;
long millis = day.atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli();
return endOfDay ? millis - 1 : millis;
}
/**
* Search criteria of the view (for testing)
*/
EventSearchCriteria getCriteria()
{
return mCriteria;
}
/**
* Rows of the view (for testing)
*/
List<EventRecord> getEvents()
{
return mEvents;
}
/**
* Runs the current search and replaces the table contents with the results.
*/
private void search()
{
if(mDatabaseManager == null)
{
mStatusLabel.setText("Call database is not available");
return;
}
long sequence = ++mSearchSequence;
EventSearchCriteria criteria = mCriteria;
int offset = mOffset;
mStatusLabel.setText("Searching ...");
mDatabaseManager.searchEvents(criteria, offset, mPageSize).whenComplete((result, error) ->
Platform.runLater(() -> {
if(sequence != mSearchSequence)
{
return;
}
if(error != null)
{
Throwable cause = error.getCause() != null ? error.getCause() : error;
mStatusLabel.setText("Search failed: " + cause.getMessage());
mEvents.clear();
mTotal = 0;
updatePaging();
return;
}
EventRecord selected = mTableView.getSelectionModel().getSelectedItem();
List<EventRecord> events = new ArrayList<>();
if(offset == 0)
{
//Live events that haven't been written to the database yet
List<EventRecord> unsaved = new ArrayList<>();
for(EventRecord live: mLiveEvents.values())
{
if(live.getId() == 0 && criteria.matches(live))
{
unsaved.add(live);
}
}
unsaved.sort((a, b) -> Long.compare(b.getStartTime(), a.getStartTime()));
events.addAll(unsaved);
}
for(EventRecord stored: result.events())
{
//Use the newest live snapshot of a recent event, so that it continues to update
Long eventKey = mEventKeyById.get(stored.getId());
EventRecord live = eventKey != null ? mLiveEvents.get(eventKey) : null;
events.add(live != null ? live : stored);
}
mEvents.setAll(events);
mTotal = result.total();
updatePaging();
if(selected != null)
{
int index = indexOf(selected);
if(index >= 0)
{
mTableView.getSelectionModel().select(index);
}
}
}));
}
private void updatePaging()
{
mPreviousButton.setDisable(mOffset == 0);
mNextButton.setDisable(mOffset + mPageSize >= mTotal);
if(mTotal == 0)
{
mStatusLabel.setText(mCriteria.isEmpty() ? "No events" : "No matching events");
}
else
{
mStatusLabel.setText("Events " + (mOffset + 1) + " - " + Math.min(mTotal, mOffset + mPageSize) + " of " +
mTotal + (mCriteria.isEmpty() ? "" : " matching"));
}
}
private int indexOf(EventRecord event)
{
for(int x = 0; x < mEvents.size(); x++)
{
if(isSameEvent(mEvents.get(x), event))
{
return x;
}
}
return -1;
}
private static boolean isSameEvent(EventRecord a, EventRecord b)
{
if(a.getEventKey() != 0 && a.getEventKey() == b.getEventKey())
{
return true;
}
return a.getId() != 0 && a.getId() == b.getId();
}
/**
* Live event snapshot from the event manager. FX thread.
*/
void receiveLiveEvent(EventRecord event)
{
//Keep the database id that this view already learned for the event
EventRecord previous = mLiveEvents.get(event.getEventKey());
if(previous != null && previous.getId() != 0 && event.getId() == 0)
{
event = event.copy();
event.setId(previous.getId());
}
mLiveEvents.put(event.getEventKey(), event);
int index = indexOf(event);
if(index >= 0)
{
mEvents.set(index, event);
}
else if(mOffset == 0 && mCriteria.matches(event))
{
//Insert in start time order
int insert = 0;
while(insert < mEvents.size() && mEvents.get(insert).getStartTime() > event.getStartTime())
{
insert++;
}
if(insert < mPageSize)
{
mEvents.add(insert, event);
mTotal++;
if(mEvents.size() > mPageSize)
{
mEvents.removeLast();
}
updatePaging();
}
}
}
/**
* Event written to the database, with its row id. FX thread.
*/
void receiveSavedEvent(EventRecord saved)
{
if(saved.getEventKey() == 0)
{
return;
}
mEventKeyById.put(saved.getId(), saved.getEventKey());
EventRecord live = mLiveEvents.get(saved.getEventKey());
if(live != null && live.getId() != saved.getId())
{
EventRecord updated = live.copy();
updated.setId(saved.getId());
mLiveEvents.put(saved.getEventKey(), updated);
int index = indexOf(updated);
if(index >= 0)
{
mEvents.set(index, updated);
}
}
}
private void receive(DatabaseEvent event)
{
if(event.type() == DatabaseEvent.Type.CONNECTED || event.type() == DatabaseEvent.Type.EVENTS_CHANGED)
{
if(event.type() == DatabaseEvent.Type.CONNECTED)
{
mEventKeyById.clear();
}
search();
}
else if(event.type() == DatabaseEvent.Type.ERROR)
{
mStatusLabel.setText("Call database is not available: " + event.errorMessage());
}
}
}

View File

@@ -100,6 +100,8 @@ public class DatabasePreferenceEditor extends VBox
private final Spinner<Integer> mSizeGBSpinner = new Spinner<>(1, 100_000, 10);
private final ComboBox<AgeOffAction> mSizeActionComboBox = new ComboBox<>();
private final ComboBox<RetentionPeriod> mRetentionComboBox = new ComboBox<>();
private final CheckBox mEventRuleCheckBox = new CheckBox("Remove events older than");
private final Spinner<Integer> mEventDaysSpinner = new Spinner<>(1, 36500, 30);
private final Button mRunAgeOffButton = new Button("Run Age-Off Now");
private final Label mAgeOffResultLabel = new Label();
@@ -147,8 +149,8 @@ public class DatabasePreferenceEditor extends VBox
{
Label title = new Label("Call Database");
title.setStyle("-fx-font-weight: bold;");
Label description = new Label("sdrtrunk stores a record of each call in this database. The built-in " +
"database needs no setup. You can use a MariaDB or PostgreSQL server instead; sdrtrunk creates the " +
Label description = new Label("sdrtrunk stores a record of each call and decode event in this database. " +
"The built-in database needs no setup. You can use a MariaDB or PostgreSQL server instead; sdrtrunk creates the " +
"schema and tables automatically. The user needs permission to create schemas and tables.");
description.setWrapText(true);
mStatusLabel.setWrapText(true);
@@ -220,10 +222,10 @@ public class DatabasePreferenceEditor extends VBox
private VBox createAgeOffPane()
{
Label title = new Label("Call Audio Age-Off");
Label title = new Label("Age-Off");
title.setStyle("-fx-font-weight: bold;");
Label description = new Label("Age-off rules remove calls and their recording files automatically " +
"(a minute after startup and then every 5 minutes). Calls in progress are never removed. Only " +
Label description = new Label("Age-off rules remove calls and their recording files, and decode events, " +
"automatically (a minute after startup and then every 5 minutes). Calls in progress are never removed. Only " +
"recordings listed in the call database count toward the size limit. When the size rule is enabled, " +
"the status bar recordings meter shows usage against the size limit.");
description.setWrapText(true);
@@ -234,6 +236,20 @@ public class DatabasePreferenceEditor extends VBox
mAgeDaysSpinner.setPrefWidth(100);
mSizeGBSpinner.setEditable(true);
mSizeGBSpinner.setPrefWidth(100);
mEventDaysSpinner.setEditable(true);
mEventDaysSpinner.setPrefWidth(100);
mEventRuleCheckBox.setSelected(mPreference.isEventRuleEnabled());
mEventDaysSpinner.getValueFactory().setValue(mPreference.getEventRuleDays());
mEventRuleCheckBox.selectedProperty().addListener((obs, old, selected) -> {
mPreference.setEventRuleEnabled(selected);
updateAgeOffControls();
});
mEventDaysSpinner.valueProperty().addListener((obs, old, value) -> {
if(value != null)
{
mPreference.setEventRuleDays(value);
}
});
mAgeRuleCheckBox.setSelected(mPreference.isAgeRuleEnabled());
mAgeDaysSpinner.getValueFactory().setValue(mPreference.getAgeRuleDays());
@@ -304,6 +320,9 @@ public class DatabasePreferenceEditor extends VBox
grid.add(mSizeRuleCheckBox, 0, 3);
grid.add(new HBox(6, mSizeGBSpinner, new Label("GB")), 1, 3);
grid.add(mSizeActionComboBox, 2, 3);
grid.add(mEventRuleCheckBox, 0, 4);
grid.add(new HBox(6, mEventDaysSpinner, new Label("days")), 1, 4);
grid.add(new Label("Decode events (Events tab)"), 2, 4);
mRunAgeOffButton.setOnAction(event -> runAgeOff());
mAgeOffResultLabel.setWrapText(true);
@@ -318,9 +337,10 @@ public class DatabasePreferenceEditor extends VBox
mAgeActionComboBox.setDisable(!mAgeRuleCheckBox.isSelected());
mSizeGBSpinner.setDisable(!mSizeRuleCheckBox.isSelected());
mSizeActionComboBox.setDisable(!mSizeRuleCheckBox.isSelected());
mEventDaysSpinner.setDisable(!mEventRuleCheckBox.isSelected());
RetentionPeriod retention = mRetentionComboBox.getValue();
mRunAgeOffButton.setDisable(mDatabaseManager == null || ((retention == null || !retention.isEnabled()) &&
!mAgeRuleCheckBox.isSelected() && !mSizeRuleCheckBox.isSelected()));
!mAgeRuleCheckBox.isSelected() && !mSizeRuleCheckBox.isSelected() && !mEventRuleCheckBox.isSelected()));
}
private void runAgeOff()
@@ -331,7 +351,7 @@ public class DatabasePreferenceEditor extends VBox
}
if(!confirm("Run Age-Off", "Apply the enabled age-off rules now?",
"Calls and recording files that the rules select are deleted permanently."))
"Calls, recording files and events that the rules select are deleted permanently."))
{
return;
}
@@ -347,8 +367,9 @@ public class DatabasePreferenceEditor extends VBox
}
else
{
mAgeOffResultLabel.setText("Removed " + result.calls() + " calls and " + result.recordings() +
" recordings (" + (result.bytes() / AgeOffManager.BYTES_PER_MB) + " MB)");
mAgeOffResultLabel.setText("Removed " + result.calls() + " calls, " + result.recordings() +
" recordings (" + (result.bytes() / AgeOffManager.BYTES_PER_MB) + " MB) and " + result.events() +
" events");
}
}));
}
@@ -511,7 +532,7 @@ public class DatabasePreferenceEditor extends VBox
case MISSING -> "Schema [" + configuration.schema() + "] doesn't exist and will be created.";
case EMPTY -> "Schema [" + configuration.schema() + "] exists and the sdrtrunk tables will be created in it.";
case SDRTRUNK -> "Schema [" + configuration.schema() + "] has sdrtrunk tables with " + result.callCount() +
" calls.";
" calls and " + result.eventCount() + " events.";
};
}
@@ -578,9 +599,10 @@ public class DatabasePreferenceEditor extends VBox
alert.initModality(Modality.WINDOW_MODAL);
alert.setTitle("Existing sdrtrunk Schema");
alert.setHeaderText("Schema [" + target.schema() + "] already has sdrtrunk tables with " + result.callCount() +
" calls");
alert.setContentText("Append: keep the existing calls and add new calls to them.\n\n" +
"Clear: delete the existing calls and start with an empty schema. Recording files are not deleted.\n\n" +
" calls and " + result.eventCount() + " events");
alert.setContentText("Append: keep the existing calls and events and add new ones to them.\n\n" +
"Clear: delete the existing calls and events and start with an empty schema. Recording files are not " +
"deleted.\n\n" +
"Create Separate Schema: leave this schema as it is and use a new schema with a different name.");
alert.getDialogPane().setMinWidth(560);
@@ -597,8 +619,8 @@ public class DatabasePreferenceEditor extends VBox
}
else if(choice.get() == clear)
{
if(confirm("Clear Schema", "Delete all " + result.callCount() + " calls in schema [" + target.schema() +
"]?", "This can't be undone."))
if(confirm("Clear Schema", "Delete all " + result.callCount() + " calls and " + result.eventCount() +
" events in schema [" + target.schema() + "]?", "This can't be undone."))
{
chooseTransfer(target, DatabaseManager.ExistingSchemaOption.CLEAR);
}
@@ -666,25 +688,27 @@ public class DatabasePreferenceEditor extends VBox
}
/**
* Asks whether to transfer the calls from the current database, then switches.
* Asks whether to transfer the calls and events from the current database, then switches.
*/
private void chooseTransfer(DatabaseConfiguration target, DatabaseManager.ExistingSchemaOption option)
{
CompletableFuture<Long> countFuture = CompletableFuture.supplyAsync(() -> {
CompletableFuture<long[]> countFuture = CompletableFuture.supplyAsync(() -> {
try
{
return mDatabaseManager.isConnected() ? mDatabaseManager.getDatabase().getCallCount() : 0L;
return mDatabaseManager.isConnected() ? new long[]{mDatabaseManager.getDatabase().getCallCount(),
mDatabaseManager.getDatabase().getEventCount()} : new long[]{0, 0};
}
catch(Exception e)
{
return 0L;
return new long[]{0, 0};
}
});
countFuture.whenComplete((count, error) -> Platform.runLater(() -> {
long calls = count != null ? count : 0;
long calls = count != null ? count[0] : 0;
long events = count != null ? count[1] : 0;
if(calls <= 0)
if(calls <= 0 && events <= 0)
{
startSwitch(target, option, false);
return;
@@ -696,12 +720,13 @@ public class DatabasePreferenceEditor extends VBox
Alert alert = new Alert(Alert.AlertType.CONFIRMATION, "", transfer, skip, cancel);
alert.initOwner(getWindow());
alert.initModality(Modality.WINDOW_MODAL);
alert.setTitle("Transfer Calls");
alert.setHeaderText("Transfer your existing calls to the new database?");
alert.setContentText("Copy the " + calls + " calls in " + mDatabaseManager.getConfiguration()
.getDescription() + " to " + target.getDescription() + ".\n\nThe calls are copied; the current " +
"database isn't changed." + (option == DatabaseManager.ExistingSchemaOption.APPEND ?
" Calls that already exist in the new database are skipped." : ""));
alert.setTitle("Transfer Calls and Events");
alert.setHeaderText("Transfer your existing calls and events to the new database?");
alert.setContentText("Copy the " + calls + " calls and " + events + " events in " +
mDatabaseManager.getConfiguration().getDescription() + " to " + target.getDescription() +
".\n\nThe calls and events are copied; the current database isn't changed." +
(option == DatabaseManager.ExistingSchemaOption.APPEND ?
" Calls and events that already exist in the new database are skipped." : ""));
alert.getDialogPane().setMinWidth(560);
Optional<ButtonType> choice = alert.showAndWait();
@@ -738,8 +763,10 @@ public class DatabasePreferenceEditor extends VBox
{
loadConfiguration(target);
showResult("Now using " + target.getDescription() + (transfer ? ". Transferred " +
result.transferred() + " calls" + (result.skipped() > 0 ? ", skipped " + result.skipped() +
" calls that already existed or were in progress" : "") + "." : "."), false);
result.transferred() + " calls" + (result.skipped() > 0 ? " (skipped " + result.skipped() +
" calls that already existed or were in progress)" : "") + " and " +
result.eventsTransferred() + " events" + (result.eventsSkipped() > 0 ? " (skipped " +
result.eventsSkipped() + " events that already existed)" : "") + "." : "."), false);
}
updateStatus();

View File

@@ -49,9 +49,12 @@ public class DatabasePreference extends Preference
private static final String KEY_SIZE_RULE_LIMIT_MB = "ageoff.size.limit.mb";
private static final String KEY_SIZE_RULE_ACTION = "ageoff.size.action";
private static final String KEY_DUPLICATE_DNM_RETENTION = "ageoff.duplicate.dnm.retention";
private static final String KEY_EVENT_RULE_ENABLED = "ageoff.events.enabled";
private static final String KEY_EVENT_RULE_DAYS = "ageoff.events.days";
public static final String DEFAULT_POSTGRES_DATABASE = "postgres";
public static final int DEFAULT_AGE_DAYS = 30;
public static final int DEFAULT_EVENT_AGE_DAYS = 30;
public static final int DEFAULT_SIZE_LIMIT_MB = 10 * 1024;
private final Preferences mPreferences = Preferences.userNodeForPackage(DatabasePreference.class);
@@ -131,6 +134,35 @@ public class DatabasePreference extends Preference
mPreferences.get(KEY_USER, ""), mPreferences.get(KEY_PASSWORD, ""));
}
/**
* Indicates if the event age rule (delete decode events older than a number of days) is enabled. Enabled by
* default, because a busy control channel produces many events.
*/
public boolean isEventRuleEnabled()
{
return mPreferences.getBoolean(KEY_EVENT_RULE_ENABLED, true);
}
public void setEventRuleEnabled(boolean enabled)
{
mPreferences.putBoolean(KEY_EVENT_RULE_ENABLED, enabled);
notifyPreferenceUpdated();
}
/**
* Maximum decode event age in days for the event age rule
*/
public int getEventRuleDays()
{
return Math.max(1, mPreferences.getInt(KEY_EVENT_RULE_DAYS, DEFAULT_EVENT_AGE_DAYS));
}
public void setEventRuleDays(int days)
{
mPreferences.putInt(KEY_EVENT_RULE_DAYS, Math.max(1, days));
notifyPreferenceUpdated();
}
public boolean isAgeRuleEnabled()
{
return mPreferences.getBoolean(KEY_AGE_RULE_ENABLED, false);

View File

@@ -0,0 +1,267 @@
/*
* *****************************************************************************
* Copyright (C) 2014-2026 Dennis Sheirer
*
* 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 3 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/>
* ****************************************************************************
*/
package io.github.dsheirer.database;
import io.github.dsheirer.alias.Alias;
import io.github.dsheirer.alias.id.radio.Radio;
import io.github.dsheirer.alias.id.talkgroup.Talkgroup;
import io.github.dsheirer.database.call.IdentifierFilter;
import io.github.dsheirer.database.call.PatchedTalkgroup;
import io.github.dsheirer.database.event.EventCategory;
import io.github.dsheirer.database.event.EventRecord;
import io.github.dsheirer.database.event.EventSearchCriteria;
import io.github.dsheirer.database.event.IEventRepository;
import io.github.dsheirer.module.decode.event.DecodeEventType;
import io.github.dsheirer.protocol.Protocol;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.nio.file.Path;
import java.sql.Connection;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Events table: schema upgrade, insert/update, search filters (and agreement of the in-memory filter with the SQL
* filter), transfer keys and the event age-off delete, against the built-in H2 database.
*
* The same checks run against network MariaDB and PostgreSQL servers when the SDRTRUNK_TEST_MARIADB and
* SDRTRUNK_TEST_POSTGRES environment variables are set (see {@link CallDatabaseTest}). The tests use (and drop) the
* schema 'sdrtrunk_event_test'.
*/
public class EventDatabaseTest
{
private static final String TEST_SCHEMA = "sdrtrunk_event_test";
@TempDir
Path mTempDir;
@Test
public void h2() throws Exception
{
exercise(new DatabaseConfiguration(DatabaseType.H2, "", 0, "", TEST_SCHEMA, "", ""));
}
@Test
public void mariadb() throws Exception
{
DatabaseConfiguration configuration = fromEnvironment("SDRTRUNK_TEST_MARIADB", DatabaseType.MARIADB);
Assumptions.assumeTrue(configuration != null, "SDRTRUNK_TEST_MARIADB not set");
exercise(configuration);
}
@Test
public void postgres() throws Exception
{
DatabaseConfiguration configuration = fromEnvironment("SDRTRUNK_TEST_POSTGRES", DatabaseType.POSTGRESQL);
Assumptions.assumeTrue(configuration != null, "SDRTRUNK_TEST_POSTGRES not set");
exercise(configuration);
}
private static DatabaseConfiguration fromEnvironment(String variable, DatabaseType type)
{
String value = System.getenv(variable);
if(value == null || value.isBlank())
{
return null;
}
String[] parts = value.split(":");
String database = parts.length > 4 ? parts[4] : "postgres";
return new DatabaseConfiguration(type, parts[0], Integer.parseInt(parts[1]), database, TEST_SCHEMA, parts[2],
parts[3]);
}
private static EventRecord event(long start, DecodeEventType type, String to, Long toValue, String toForm,
String from, Long fromValue, String details)
{
EventRecord event = new EventRecord();
event.setStartTime(start);
event.setEventType(type.name());
event.setProtocol(Protocol.APCO25.name());
event.setSystem("Metro System");
event.setSite("Site 1");
event.setChannel("Control");
event.setChannelDescriptor("1-0234");
event.setFrequency(851_012_500);
event.setTimeslot(-1);
event.setToId(to);
event.setToValue(toValue);
event.setToForm(toForm);
event.setFromId(from);
event.setFromValue(fromValue);
event.setFromForm(from != null ? IdentifierRecord.FORM_RADIO : null);
event.setAliasList("Metro");
event.setDetails(details);
return event;
}
private void exercise(DatabaseConfiguration configuration) throws Exception
{
try(CallDatabase database = new CallDatabase(configuration, mTempDir))
{
try(Connection connection = database.getConnection(); Statement statement = connection.createStatement())
{
statement.execute(configuration.type().getDialect().getDropSchemaStatement(TEST_SCHEMA));
}
database.install();
assertEquals(0, database.getEventCount());
//Upgrade from schema version 2 (before the events tables) creates the events tables
try(Connection connection = database.getConnection(); Statement statement = connection.createStatement())
{
statement.execute("DROP TABLE " + TEST_SCHEMA + ".event_patched_talkgroups");
statement.execute("DROP TABLE " + TEST_SCHEMA + ".events");
statement.executeUpdate("UPDATE " + TEST_SCHEMA + ".schema_info SET version = 2");
}
assertEquals(0, database.getEventCount(), "no events table at version 2");
database.install();
IEventRepository repository = database.getEventRepository();
try(Connection connection = database.getConnection())
{
EventRecord call = event(1000, DecodeEventType.CALL_GROUP, "1234", 1234L,
IdentifierRecord.FORM_TALKGROUP, "5551212", 5551212L, null);
long callId = repository.insert(connection, call);
assertTrue(callId > 0);
call.setEndTime(4000);
call.setDuration(3000);
call.setDetails("Emergency 100%");
assertTrue(repository.update(connection, call));
EventRecord patch = event(2000, DecodeEventType.CALL_PATCH_GROUP, "P:500", 500L,
IdentifierRecord.FORM_PATCH_GROUP, "777", 777L, null);
patch.setPatchedTalkgroups(List.of(new PatchedTalkgroup(2000, null), new PatchedTalkgroup(2001,
null)));
repository.insert(connection, patch);
EventRecord page = event(3000, DecodeEventType.PAGE, "888", 888L, IdentifierRecord.FORM_RADIO,
"777", 777L, "Page radio");
repository.insert(connection, page);
EventRecord registration = event(4000, DecodeEventType.REGISTER, null, null, null, "999", 999L,
"D".repeat(5000));
registration.setProtocol(Protocol.DMR.name());
repository.insert(connection, registration);
assertEquals(4, repository.count(connection));
assertEquals(4, database.getEventCount());
List<EventRecord> all = repository.search(connection, EventSearchCriteria.all(), 0, 100);
assertEquals(4000, all.getFirst().getStartTime(), "newest first");
assertEquals(2048, all.getFirst().getDetails().length(), "long details truncated");
EventRecord loadedCall = all.getLast();
assertEquals(3000, loadedCall.getDuration());
assertEquals(4000, loadedCall.getEndTime());
assertEquals(DecodeEventType.CALL_GROUP, loadedCall.getEventTypeValue());
assertEquals(EventCategory.VOICE_CALL, loadedCall.getCategory());
assertEquals("1-0234", loadedCall.getChannelDescriptor());
assertEquals(-1, loadedCall.getTimeslot());
assertEquals(List.of(new PatchedTalkgroup(2000, null), new PatchedTalkgroup(2001, null)),
all.get(2).getPatchedTalkgroups(), "patched talkgroups loaded");
List<EventRecord> loaded = new ArrayList<>(all);
Alias patchedAlias = new Alias("Tac 2");
patchedAlias.setAliasListName("Metro");
patchedAlias.addAliasID(new Talkgroup(Protocol.APCO25, 2001));
Alias radioAlias = new Alias("Engine 7");
radioAlias.setAliasListName("Metro");
radioAlias.addAliasID(new Radio(Protocol.APCO25, 888));
check(repository, connection, loaded, criteria(IdentifierFilter.text("1234"), null), 1);
check(repository, connection, loaded, criteria(IdentifierFilter.text("2001"), null), 1);
check(repository, connection, loaded, criteria(IdentifierFilter.talkgroupAlias(patchedAlias), null),
1);
check(repository, connection, loaded, criteria(null, IdentifierFilter.text("777")), 2);
check(repository, connection, loaded, criteria(null, IdentifierFilter.text("888")), 1);
check(repository, connection, loaded, criteria(null, IdentifierFilter.radioAlias(radioAlias)), 1);
check(repository, connection, loaded, new EventSearchCriteria(null, null, EventCategory.VOICE_CALL,
null, null, null, null, 0, 0), 2);
check(repository, connection, loaded, new EventSearchCriteria(null, null, null,
DecodeEventType.PAGE, null, null, null, 0, 0), 1);
check(repository, connection, loaded, new EventSearchCriteria(null, null, EventCategory.REGISTRATION,
null, Protocol.DMR, "metro", null, 0, 0), 1);
check(repository, connection, loaded, new EventSearchCriteria(null, null, null, null, null, null,
"emergency 100%", 0, 0), 1);
check(repository, connection, loaded, new EventSearchCriteria(null, null, null, null, null, null,
"emergency%", 0, 0), 0);
check(repository, connection, loaded, new EventSearchCriteria(null, null, null, null, null, null,
null, 1500, 3500), 2);
assertEquals(2, repository.search(connection, EventSearchCriteria.all(), 1, 2).size(), "paging");
Set<String> keys = repository.getTransferKeys(connection);
assertEquals(4, keys.size());
assertTrue(keys.contains(call.getTransferKey()));
//Transfer: a batch insert keeps the patched talkgroups
EventRecord copy = all.get(2).copy();
repository.insertBatch(connection, List.of(copy, all.get(0).copy()));
assertEquals(6, repository.count(connection));
assertEquals(2, repository.count(connection, criteria(IdentifierFilter.text("2001"), null)));
//Age-off deletes the old events and their patched talkgroups
assertEquals(3, repository.deleteStartedBefore(connection, 3000));
assertEquals(3, repository.count(connection));
assertEquals(0, repository.count(connection, criteria(IdentifierFilter.text("2001"), null)));
try(Statement statement = connection.createStatement();
var rs = statement.executeQuery("SELECT COUNT(*) FROM " + TEST_SCHEMA +
".event_patched_talkgroups"))
{
assertTrue(rs.next());
assertEquals(0, rs.getLong(1), "patched rows deleted with their event");
}
assertFalse(repository.update(connection, loadedCall), "deleted row isn't updated");
}
try(Connection connection = database.getConnection(); Statement statement = connection.createStatement())
{
statement.execute(configuration.type().getDialect().getDropSchemaStatement(TEST_SCHEMA));
}
}
}
private static EventSearchCriteria criteria(IdentifierFilter talkgroup, IdentifierFilter radio)
{
return new EventSearchCriteria(talkgroup, radio, null, null, null, null, null, 0, 0);
}
/**
* Checks the database count for the criteria and that the in-memory filter selects the same events.
*/
private static void check(IEventRepository repository, Connection connection, List<EventRecord> events,
EventSearchCriteria criteria, long expected) throws Exception
{
assertEquals(expected, repository.count(connection, criteria), "database count for " + criteria);
assertEquals(expected, events.stream().filter(criteria::matches).count(), "in-memory count for " + criteria);
}
}

View File

@@ -0,0 +1,146 @@
/*
* *****************************************************************************
* Copyright (C) 2014-2026 Dennis Sheirer
*
* 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 3 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/>
* ****************************************************************************
*/
package io.github.dsheirer.event;
import io.github.dsheirer.database.IdentifierRecord;
import io.github.dsheirer.database.call.PatchedTalkgroup;
import io.github.dsheirer.database.event.EventRecord;
import io.github.dsheirer.identifier.Identifier;
import io.github.dsheirer.identifier.IdentifierCollection;
import io.github.dsheirer.identifier.configuration.AliasListConfigurationIdentifier;
import io.github.dsheirer.identifier.configuration.SystemConfigurationIdentifier;
import io.github.dsheirer.identifier.patch.PatchGroup;
import io.github.dsheirer.module.decode.event.DecodeEvent;
import io.github.dsheirer.module.decode.event.DecodeEventType;
import io.github.dsheirer.module.decode.p25.identifier.patch.APCO25PatchGroup;
import io.github.dsheirer.module.decode.p25.identifier.radio.APCO25RadioIdentifier;
import io.github.dsheirer.module.decode.p25.identifier.talkgroup.APCO25Talkgroup;
import io.github.dsheirer.preference.UserPreferences;
import io.github.dsheirer.protocol.Protocol;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Event manager: one event key per decode event, snapshots when the event changes (with or without a re-broadcast),
* and the record fields extracted from the event.
*/
public class EventManagerTest
{
private EventManager mEventManager;
private final List<EventRecord> mSnapshots = new ArrayList<>();
@BeforeEach
public void setup()
{
mEventManager = new EventManager(new UserPreferences().getTalkgroupFormatPreference(), null);
mEventManager.addEventListener(mSnapshots::add);
}
@AfterEach
public void teardown()
{
mEventManager.stop();
}
private static DecodeEvent groupCall(long start, int talkgroup, int radio)
{
List<Identifier> identifiers = new ArrayList<>();
identifiers.add(APCO25Talkgroup.create(talkgroup));
identifiers.add(APCO25RadioIdentifier.createFrom(radio));
identifiers.add(new AliasListConfigurationIdentifier("Metro"));
identifiers.add(new SystemConfigurationIdentifier("Metro System"));
return DecodeEvent.builder(DecodeEventType.CALL_GROUP, start)
.identifiers(new IdentifierCollection(identifiers))
.protocol(Protocol.APCO25)
.details("Phase 1")
.build();
}
@Test
public void eventChangesAreSnapshotsOfOneEvent()
{
DecodeEvent event = groupCall(1000, 100, 5551212);
mEventManager.receive(event);
mEventManager.process();
assertEquals(1, mSnapshots.size());
EventRecord first = mSnapshots.getFirst();
assertEquals("CALL_GROUP", first.getEventType());
assertEquals("APCO25", first.getProtocol());
assertEquals(100L, first.getToValue());
assertEquals(IdentifierRecord.FORM_TALKGROUP, first.getToForm());
assertEquals(5551212L, first.getFromValue());
assertTrue(first.isFromRadio());
assertEquals("Metro", first.getAliasList());
assertEquals("Metro System", first.getSystem());
assertEquals("Phase 1", first.getDetails());
assertEquals(-1, first.getTimeslot());
//No change, no snapshot
mEventManager.process();
assertEquals(1, mSnapshots.size());
//The decoder updates the event without broadcasting it again
event.end(4000);
mEventManager.process();
assertEquals(2, mSnapshots.size());
assertEquals(3000, mSnapshots.get(1).getDuration());
assertEquals(first.getEventKey(), mSnapshots.get(1).getEventKey(), "same event key");
//A re-broadcast of the same event doesn't create a new event
mEventManager.receive(event);
mEventManager.process();
assertEquals(2, mSnapshots.size());
assertEquals(1, mEventManager.getActiveEventCount());
mEventManager.receive(groupCall(5000, 200, 777));
mEventManager.process();
assertEquals(3, mSnapshots.size());
assertNotEquals(first.getEventKey(), mSnapshots.get(2).getEventKey(), "different event, different key");
}
@Test
public void patchGroupMembersAreCaptured()
{
PatchGroup patchGroup = new PatchGroup(APCO25Talkgroup.create(500));
patchGroup.addPatchedTalkgroup(APCO25Talkgroup.create(100));
patchGroup.addPatchedTalkgroup(APCO25Talkgroup.create(101));
DecodeEvent event = DecodeEvent.builder(DecodeEventType.CALL_PATCH_GROUP, 1000)
.identifiers(new IdentifierCollection(List.of(APCO25PatchGroup.create(patchGroup))))
.protocol(Protocol.APCO25)
.build();
mEventManager.receive(event);
mEventManager.process();
EventRecord record = mSnapshots.getFirst();
assertEquals(IdentifierRecord.FORM_PATCH_GROUP, record.getToForm());
assertEquals(500L, record.getToValue());
assertEquals(List.of(new PatchedTalkgroup(100, null), new PatchedTalkgroup(101, null)),
record.getPatchedTalkgroups());
}
}