feat: Add support for Frigate reviews / detections [initial PR] (#2315)
- Add support for Frigate reviews / detections. - Add support for GenAI metadata. - Significant internal refactor to more flexible "UnifiedQuery" to allow mixing cameras with simple metadata and review metadata (e.g. a timeline view of a Frigate camera with reviews, and a Reolink camera with simple metadata). - Add support for folder media as camera media. There are a few more PRs to commit prior to this going live, but commiting this for now due to the scale of the change. BREAKING CHANGE: `media_type` and `events_type` are retired under `live`, `viewer` and `timeline` configuration sections, instead media type is associated (once) with the camera under `media`.
This commit is contained in:
@@ -14,7 +14,6 @@ import {
|
||||
} from 'vis-timeline';
|
||||
import { CameraManager } from '../../camera-manager/manager';
|
||||
import { rangesOverlap } from '../../camera-manager/range';
|
||||
import { MediaQuery } from '../../camera-manager/types';
|
||||
import { convertRangeToCacheFriendlyTimes } from '../../camera-manager/utils/range-to-cache-friendly';
|
||||
import { FoldersManager } from '../../card-controller/folders/manager';
|
||||
import { ViewItemManager } from '../../card-controller/view/item-manager';
|
||||
@@ -41,22 +40,12 @@ import { findBestMediaTimeIndex } from '../../utils/find-best-media-time-index';
|
||||
import { fireAdvancedCameraCardEvent } from '../../utils/fire-advanced-camera-card-event';
|
||||
import { ViewMedia } from '../../view/item';
|
||||
import { ViewItemClassifier } from '../../view/item-classifier';
|
||||
import {
|
||||
EventMediaQuery,
|
||||
FolderViewQuery,
|
||||
Query,
|
||||
RecordingMediaQuery,
|
||||
} from '../../view/query';
|
||||
import { QueryClassifier, QueryType } from '../../view/query-classifier';
|
||||
import { QueryResults } from '../../view/query-results';
|
||||
import { UnifiedQuery } from '../../view/unified-query';
|
||||
import { UnifiedQueryTransformer } from '../../view/unified-query-transformer';
|
||||
import { mergeViewContext } from '../../view/view';
|
||||
import { AdvancedCameraCardTimelineItem, TimelineDataSource } from './source';
|
||||
import {
|
||||
ExtendedTimeline,
|
||||
TimelineItemClickAction,
|
||||
TimelineKeys,
|
||||
TimelineRangeChange,
|
||||
} from './types';
|
||||
import { ExtendedTimeline, TimelineItemClickAction, TimelineRangeChange } from './types';
|
||||
|
||||
// An event used to fetch data required for thumbnail rendering. See special
|
||||
// note below on why this is necessary.
|
||||
@@ -81,7 +70,7 @@ interface TimelineControllerOptions {
|
||||
timelineConfig?: TimelineCoreConfig;
|
||||
mini?: boolean;
|
||||
thumbnailConfig?: ThumbnailsControlBaseConfig;
|
||||
keys?: TimelineKeys;
|
||||
query?: UnifiedQuery;
|
||||
}
|
||||
|
||||
const TIMELINE_TARGET_BAR_ID = 'target_bar';
|
||||
@@ -94,10 +83,14 @@ export class TimelineController {
|
||||
private _timeline: ExtendedTimeline | null = null;
|
||||
|
||||
private _hass: HomeAssistant | null = null;
|
||||
|
||||
private _cameraManager: CameraManager | null = null;
|
||||
private _foldersManager: FoldersManager | null = null;
|
||||
|
||||
private _viewItemManager: ViewItemManager | null = null;
|
||||
private _viewManagerEpoch: ViewManagerEpoch | null = null;
|
||||
private _timelineConfig: TimelineCoreConfig | null = null;
|
||||
|
||||
private _mini = false;
|
||||
|
||||
private _panMode: TimelinePanMode | null = null;
|
||||
@@ -136,26 +129,56 @@ export class TimelineController {
|
||||
this._pointerHeld = null;
|
||||
}
|
||||
|
||||
public setOptions(options: TimelineControllerOptions): void {
|
||||
this.destroyTimeline();
|
||||
/**
|
||||
* Extract the "shape" of a query - a clone without time ranges.
|
||||
* Shape determines timeline structure (groups).
|
||||
*/
|
||||
private _getQueryShape(query: UnifiedQuery): UnifiedQuery {
|
||||
return UnifiedQueryTransformer.stripTimeRange(query);
|
||||
}
|
||||
|
||||
if (
|
||||
options.keys &&
|
||||
options.cameraManager &&
|
||||
options.foldersManager &&
|
||||
options.conditionStateManager &&
|
||||
options.timelineConfig
|
||||
) {
|
||||
this._source = new TimelineDataSource(
|
||||
options.cameraManager,
|
||||
options.foldersManager,
|
||||
options.conditionStateManager,
|
||||
options.keys,
|
||||
options.timelineConfig.events_media_type,
|
||||
options.timelineConfig.show_recordings,
|
||||
);
|
||||
} else {
|
||||
this._source = null;
|
||||
private _hasSameShape(a?: UnifiedQuery | null, b?: UnifiedQuery | null): boolean {
|
||||
if (!a && !b) {
|
||||
return true;
|
||||
}
|
||||
if (!a || !b) {
|
||||
return false;
|
||||
}
|
||||
return a.isEqual(b);
|
||||
}
|
||||
|
||||
public setOptions(options: TimelineControllerOptions): void {
|
||||
// Extract the shape (query without time ranges) for comparison.
|
||||
const newShape = options.query ? this._getQueryShape(options.query) : null;
|
||||
|
||||
// Rebuild source if config, dependencies, or shape changed.
|
||||
const needsRebuild =
|
||||
!this._source ||
|
||||
this._cameraManager !== (options.cameraManager ?? null) ||
|
||||
this._foldersManager !== (options.foldersManager ?? null) ||
|
||||
!isEqual(this._timelineConfig, options.timelineConfig ?? null) ||
|
||||
!this._hasSameShape(this._source?.shape, newShape);
|
||||
|
||||
if (needsRebuild) {
|
||||
this.destroyTimeline();
|
||||
|
||||
if (
|
||||
newShape &&
|
||||
options.cameraManager &&
|
||||
options.foldersManager &&
|
||||
options.conditionStateManager &&
|
||||
options.timelineConfig
|
||||
) {
|
||||
this._source = new TimelineDataSource(
|
||||
options.cameraManager,
|
||||
options.foldersManager,
|
||||
options.conditionStateManager,
|
||||
newShape,
|
||||
options.timelineConfig.show_recordings,
|
||||
);
|
||||
} else {
|
||||
this._source = null;
|
||||
}
|
||||
}
|
||||
|
||||
if (this._thumbnailConfig !== (options.thumbnailConfig ?? null)) {
|
||||
@@ -187,6 +210,7 @@ export class TimelineController {
|
||||
|
||||
this._thumbnailConfig = options?.thumbnailConfig ?? null;
|
||||
this._cameraManager = options?.cameraManager ?? null;
|
||||
this._foldersManager = options?.foldersManager ?? null;
|
||||
this._viewItemManager = options?.viewItemManager ?? null;
|
||||
this._timelineConfig = options?.timelineConfig ?? null;
|
||||
this._mini = options?.mini ?? false;
|
||||
@@ -268,13 +292,13 @@ export class TimelineController {
|
||||
this._timeline = new Timeline(
|
||||
this._timelineElement,
|
||||
this._source.dataset,
|
||||
this._source.groups,
|
||||
options,
|
||||
);
|
||||
} else {
|
||||
this._timeline = new Timeline(
|
||||
this._timelineElement,
|
||||
this._source.dataset,
|
||||
this._source.groups,
|
||||
options,
|
||||
);
|
||||
}
|
||||
@@ -500,16 +524,15 @@ export class TimelineController {
|
||||
}
|
||||
|
||||
const view = this._viewManagerEpoch?.manager.getView();
|
||||
const id = String(properties.item);
|
||||
const item = this._source?.dataset.get(id) ?? null;
|
||||
const id = properties.item ? String(properties.item) : null;
|
||||
const item = id ? this._source?.dataset.get(id) ?? null : null;
|
||||
|
||||
if (
|
||||
this._ignoreClick ||
|
||||
!view ||
|
||||
!this._viewManagerEpoch ||
|
||||
!this._source ||
|
||||
!properties.what ||
|
||||
!item
|
||||
!properties.what
|
||||
) {
|
||||
return;
|
||||
}
|
||||
@@ -519,9 +542,15 @@ export class TimelineController {
|
||||
if (
|
||||
this._timelineConfig?.show_recordings &&
|
||||
properties.time &&
|
||||
['background', 'axis'].includes(properties.what)
|
||||
['background', 'axis'].includes(properties.what) &&
|
||||
this._source &&
|
||||
this._timeline
|
||||
) {
|
||||
const query = this._createQuery('recording');
|
||||
const query = this._source.buildRecordingsWindowedQuery(
|
||||
convertRangeToCacheFriendlyTimes(
|
||||
this._getPrefetchWindow(this._timeline.getWindow()),
|
||||
),
|
||||
);
|
||||
if (query) {
|
||||
await this._viewManagerEpoch?.manager.setViewByParametersWithExistingQuery({
|
||||
baseView: view,
|
||||
@@ -533,9 +562,14 @@ export class TimelineController {
|
||||
},
|
||||
},
|
||||
},
|
||||
modifiers: [
|
||||
new MergeContextViewModifier({
|
||||
mediaViewer: { seek: properties.time },
|
||||
}),
|
||||
],
|
||||
});
|
||||
}
|
||||
} else if (properties.item && properties.what === 'item') {
|
||||
} else if (item && properties.what === 'item') {
|
||||
const cameraID = String(properties.group);
|
||||
|
||||
const criteria = {
|
||||
@@ -566,49 +600,16 @@ export class TimelineController {
|
||||
// - If a folder media was loaded into the timeline from a prior folder
|
||||
// query other than the one stored in the view (e.g. user navigated to
|
||||
// a different folder in the thumbnails carousel).
|
||||
if (item.query) {
|
||||
// Item has a reference query (e.g. folders), use that.
|
||||
const media = this._source.dataset
|
||||
.get({
|
||||
filter: (timelineItem) => item.query === timelineItem.query,
|
||||
})
|
||||
.map((timelineItem) => timelineItem.media)
|
||||
.filter(isTruthy);
|
||||
const selectedIndex = media.findIndex((m) => m.getID() === id);
|
||||
|
||||
if (selectedIndex >= 0) {
|
||||
const queryResults = new QueryResults({ results: media, selectedIndex });
|
||||
this._viewManagerEpoch?.manager.setViewByParameters({
|
||||
params: {
|
||||
view: 'media',
|
||||
query: item.query,
|
||||
queryResults,
|
||||
},
|
||||
modifiers: [new MergeContextViewModifier(context)],
|
||||
});
|
||||
}
|
||||
} else {
|
||||
const currentQueryType =
|
||||
QueryClassifier.getQueryType(view.query) ??
|
||||
this._source.getKeyType() === 'camera'
|
||||
? 'event'
|
||||
: this._source.getKeyType() === 'folder'
|
||||
? 'folder'
|
||||
: null;
|
||||
|
||||
const query = currentQueryType ? this._createQuery(currentQueryType) : null;
|
||||
if (query) {
|
||||
await this._viewManagerEpoch?.manager.setViewByParametersWithExistingQuery({
|
||||
params: { view: 'media', query: query },
|
||||
queryExecutorOptions: {
|
||||
selectResult: {
|
||||
id,
|
||||
},
|
||||
rejectResults: (results) => !results.hasResults(),
|
||||
},
|
||||
modifiers: [new MergeContextViewModifier(context)],
|
||||
});
|
||||
}
|
||||
const queryResults = this._buildQueryResultsFromExistingItem(item);
|
||||
if (item && item.query && queryResults) {
|
||||
this._viewManagerEpoch?.manager.setViewByParameters({
|
||||
params: {
|
||||
view: 'media',
|
||||
query: item.query,
|
||||
queryResults,
|
||||
},
|
||||
modifiers: [new MergeContextViewModifier(context)],
|
||||
});
|
||||
}
|
||||
} else {
|
||||
this._viewManagerEpoch.manager.setViewByParameters({
|
||||
@@ -630,6 +631,25 @@ export class TimelineController {
|
||||
this._ignoreClick = false;
|
||||
}
|
||||
|
||||
private _buildQueryResultsFromExistingItem(
|
||||
item: AdvancedCameraCardTimelineItem,
|
||||
): QueryResults | null {
|
||||
const query = item.query;
|
||||
if (!query || !this._source) {
|
||||
return null;
|
||||
}
|
||||
const media = this._source.dataset
|
||||
.get({
|
||||
filter: (timelineItem) => !!timelineItem.query && query === timelineItem.query,
|
||||
})
|
||||
.map((timelineItem) => timelineItem.media)
|
||||
.filter(isTruthy);
|
||||
const selectedIndex = media.findIndex((m) => m.getID() === item.id);
|
||||
return selectedIndex >= 0
|
||||
? new QueryResults({ results: media, selectedIndex })
|
||||
: null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a broader prefetch window from a start and end basis.
|
||||
* @param window The window to broaden.
|
||||
@@ -643,31 +663,19 @@ export class TimelineController {
|
||||
};
|
||||
}
|
||||
|
||||
private _createQuery(
|
||||
type: QueryType,
|
||||
options?: {
|
||||
window?: TimelineWindow;
|
||||
},
|
||||
): Query | null {
|
||||
if (!this._timeline || !this._source) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const cacheFriendlyWindow = convertRangeToCacheFriendlyTimes(
|
||||
this._getPrefetchWindow(options?.window ?? this._timeline.getWindow()),
|
||||
);
|
||||
|
||||
if (type === 'event') {
|
||||
const queries = this._source.getTimelineEventQueries(cacheFriendlyWindow);
|
||||
return queries ? new EventMediaQuery(queries) : null;
|
||||
} else if (type === 'recording') {
|
||||
const queries = this._source.getTimelineRecordingQueries(cacheFriendlyWindow);
|
||||
return queries ? new RecordingMediaQuery(queries) : null;
|
||||
} else if (type === 'folder') {
|
||||
const queries = this._source.getTimelineFolderQuery();
|
||||
return queries ? new FolderViewQuery(queries) : null;
|
||||
}
|
||||
return null;
|
||||
/**
|
||||
* Apply a cache-friendly prefetch window to all media queries.
|
||||
*/
|
||||
private _applyWindowToQuery(
|
||||
query: UnifiedQuery,
|
||||
window: TimelineWindow,
|
||||
): UnifiedQuery {
|
||||
const prefetchWindow = this._getPrefetchWindow(window);
|
||||
const cacheFriendlyWindow = convertRangeToCacheFriendlyTimes(prefetchWindow);
|
||||
return UnifiedQueryTransformer.rebuildQuery(query, {
|
||||
start: cacheFriendlyWindow.start,
|
||||
end: cacheFriendlyWindow.end,
|
||||
});
|
||||
}
|
||||
|
||||
private _timelineRangeChangedHandler = async (properties: {
|
||||
@@ -690,16 +698,14 @@ export class TimelineController {
|
||||
return;
|
||||
}
|
||||
|
||||
await this._source?.refresh(this._getPrefetchWindow(properties), {
|
||||
view,
|
||||
});
|
||||
await this._source?.refresh(this._getPrefetchWindow(properties));
|
||||
|
||||
const queryType = QueryClassifier.getQueryType(view.query);
|
||||
if (!queryType) {
|
||||
if (!view.query) {
|
||||
return;
|
||||
}
|
||||
const query = this._createQuery(queryType);
|
||||
if (!query || this._alreadyHasAcceptableMediaQuery(query)) {
|
||||
const query = this._applyWindowToQuery(view.query, properties);
|
||||
|
||||
if (this._alreadyHasAcceptableMediaQuery(query)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -720,27 +726,22 @@ export class TimelineController {
|
||||
});
|
||||
};
|
||||
|
||||
private _alreadyHasAcceptableMediaQuery(freshQuery: Query): boolean {
|
||||
private _alreadyHasAcceptableMediaQuery(freshQuery: UnifiedQuery): boolean {
|
||||
const view = this._viewManagerEpoch?.manager.getView();
|
||||
const query = view?.query;
|
||||
|
||||
if (!this._cameraManager || !query) {
|
||||
if (!this._source || !query) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const currentResultTimestamp = view?.queryResults?.getResultsTimestamp();
|
||||
if (!currentResultTimestamp) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (
|
||||
!!query?.getQuery() &&
|
||||
!!currentResultTimestamp &&
|
||||
((QueryClassifier.isFolderQuery(query) && query.isEqual(freshQuery)) ||
|
||||
(QueryClassifier.isMediaQuery(query) &&
|
||||
QueryClassifier.isMediaQuery(freshQuery) &&
|
||||
query.isSupersetOf(freshQuery) &&
|
||||
this._cameraManager.areMediaQueriesResultsFresh<MediaQuery>(
|
||||
currentResultTimestamp,
|
||||
query.getQuery(),
|
||||
)))
|
||||
query.isSupersetOf(freshQuery) &&
|
||||
this._source.areResultsFresh(currentResultTimestamp, query)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -787,16 +788,14 @@ export class TimelineController {
|
||||
}
|
||||
const prefetchedWindow = this._getPrefetchWindow(desiredWindow);
|
||||
|
||||
if (!this._pointerHeld) {
|
||||
if (!this._pointerHeld && view.query) {
|
||||
// Don't fetch any data or touch the timeline in any way if the user is
|
||||
// currently interacting with it. Without this the subsequent data fetches
|
||||
// (via fetchIfNecessary) may update the timeline contents which causes
|
||||
// the visjs timeline to stop dragging/panning operations which is very
|
||||
// disruptive to the user.
|
||||
await this._source?.refresh(prefetchedWindow, {
|
||||
view,
|
||||
});
|
||||
this._source.addEventMediaToDataset(view.queryResults?.getResults(), view.query);
|
||||
await this._source?.refresh(prefetchedWindow);
|
||||
this._source.addMediaToDataset(view.query, view.queryResults?.getResults());
|
||||
}
|
||||
|
||||
const currentSelection = this._timeline.getSelection();
|
||||
@@ -845,14 +844,11 @@ export class TimelineController {
|
||||
//
|
||||
// Also don't generate thumbnails in mini-timelines (they will already have
|
||||
// been generated).
|
||||
const queryType = QueryClassifier.getQueryType(view.query);
|
||||
if (!queryType) {
|
||||
if (!view.query) {
|
||||
return;
|
||||
}
|
||||
|
||||
const freshMediaQuery = this._createQuery(queryType, {
|
||||
window: desiredWindow,
|
||||
});
|
||||
const freshMediaQuery = this._applyWindowToQuery(view.query, desiredWindow);
|
||||
|
||||
if (
|
||||
!this._mini &&
|
||||
|
||||
@@ -1,31 +1,25 @@
|
||||
import { add, sub } from 'date-fns';
|
||||
import { DataSet } from 'vis-data';
|
||||
import { IdType, TimelineItem, TimelineWindow } from 'vis-timeline/esnext';
|
||||
import { EqualityCache } from '../../cache/equality-cache';
|
||||
import { CameraManager } from '../../camera-manager/manager';
|
||||
import {
|
||||
compressRanges,
|
||||
ExpiringMemoryRangeSet,
|
||||
MemoryRangeSet,
|
||||
} from '../../camera-manager/range';
|
||||
import {
|
||||
EventQuery,
|
||||
RecordingQuery,
|
||||
RecordingSegment,
|
||||
} from '../../camera-manager/types';
|
||||
import { RecordingSegment } from '../../camera-manager/types';
|
||||
import { capEndDate } from '../../camera-manager/utils/cap-end-date';
|
||||
import { convertRangeToCacheFriendlyTimes } from '../../camera-manager/utils/range-to-cache-friendly';
|
||||
import { FoldersManager } from '../../card-controller/folders/manager';
|
||||
import { FolderQuery } from '../../card-controller/folders/types';
|
||||
import { ConditionStateManagerReadonlyInterface } from '../../conditions/types';
|
||||
import { FolderConfig } from '../../config/schema/folders';
|
||||
import { ClipsOrSnapshotsOrAll } from '../../types';
|
||||
import { errorToConsole, ModifyInterface } from '../../utils/basic.js';
|
||||
import { errorToConsole } from '../../utils/basic.js';
|
||||
import { ViewItem, ViewMedia } from '../../view/item';
|
||||
import { ViewItemClassifier } from '../../view/item-classifier';
|
||||
import { FolderViewQuery, Query } from '../../view/query';
|
||||
import { View } from '../../view/view';
|
||||
import { TimelineKeys } from './types';
|
||||
import { UnifiedQuery } from '../../view/unified-query';
|
||||
import { UnifiedQueryBuilder } from '../../view/unified-query-builder';
|
||||
import { UnifiedQueryRunner } from '../../view/unified-query-runner';
|
||||
import { UnifiedQueryTransformer } from '../../view/unified-query-transformer';
|
||||
|
||||
// Allow timeline freshness to be at least this number of seconds out of date
|
||||
// (caching times in the data-engine may increase the effective delay).
|
||||
@@ -37,9 +31,7 @@ const TIMELINE_FRESHNESS_TOLERANCE_SECONDS = 30;
|
||||
// instead of clean recording blocks.
|
||||
const TIMELINE_RECORDING_SEGMENT_CONSECUTIVE_TOLERANCE_SECONDS = 60;
|
||||
|
||||
type TimelineViewQuery = Query;
|
||||
|
||||
export interface AdvancedCameraCardTimelineItem extends TimelineItem {
|
||||
export type AdvancedCameraCardTimelineItem = TimelineItem & {
|
||||
// Use numbers to avoid significant volumes of Date object construction (for
|
||||
// high-quantity recording segments).
|
||||
start: number;
|
||||
@@ -48,25 +40,33 @@ export interface AdvancedCameraCardTimelineItem extends TimelineItem {
|
||||
// DataSet requires string (not HTMLElement) content.
|
||||
content: string;
|
||||
|
||||
media?: ViewMedia;
|
||||
|
||||
// View query object from which this timeline item is associated with.
|
||||
query?: TimelineViewQuery;
|
||||
}
|
||||
// Severity is duplicated here (also available via media.getSeverity())
|
||||
// because vis-timeline's dataAttributes option requires properties to exist
|
||||
// directly on the item object to render them as data-* HTML attributes for
|
||||
// CSS styling.
|
||||
severity?: string;
|
||||
} & ( // Ensure that if there's a media item there is a query it is associated with.
|
||||
| {
|
||||
media: ViewMedia;
|
||||
query: UnifiedQuery;
|
||||
}
|
||||
| {
|
||||
media?: never;
|
||||
query?: never;
|
||||
}
|
||||
);
|
||||
|
||||
interface AdvancedCameraCardGroup {
|
||||
id: string;
|
||||
content: string;
|
||||
}
|
||||
|
||||
interface RefreshOptions {
|
||||
view?: View;
|
||||
}
|
||||
|
||||
export class TimelineDataSource {
|
||||
private _cameraManager: CameraManager;
|
||||
private _foldersManager: FoldersManager;
|
||||
private _conditionStateManager: ConditionStateManagerReadonlyInterface;
|
||||
|
||||
private _builder: UnifiedQueryBuilder;
|
||||
private _runner: UnifiedQueryRunner;
|
||||
|
||||
private _dataset: DataSet<AdvancedCameraCardTimelineItem> = new DataSet();
|
||||
private _groups: DataSet<AdvancedCameraCardGroup>;
|
||||
|
||||
@@ -76,33 +76,34 @@ export class TimelineDataSource {
|
||||
// high-N segments into a smaller number of consecutive recording blocks).
|
||||
private _recordingRanges = new MemoryRangeSet();
|
||||
|
||||
// Cache event ranges since re-adding the same events is a timeline
|
||||
// performance killer (even if the request results are cached).
|
||||
private _eventRanges = new ExpiringMemoryRangeSet();
|
||||
private _folderCache = new EqualityCache<FolderQuery, Date>();
|
||||
// Cache for all query results in this source instance.
|
||||
// Uses a single range set since shape determines content type.
|
||||
private _cache = new ExpiringMemoryRangeSet();
|
||||
|
||||
private _eventsMediaType: ClipsOrSnapshotsOrAll;
|
||||
private _showRecordings: boolean;
|
||||
|
||||
private _keys: TimelineKeys;
|
||||
// The "shape" of the query, a UnifiedQuery without time ranges. Determines
|
||||
// the groups/structure of the timeline.
|
||||
private _shape: UnifiedQuery;
|
||||
|
||||
constructor(
|
||||
cameraManager: CameraManager,
|
||||
foldersManager: FoldersManager,
|
||||
conditionStateManager: ConditionStateManagerReadonlyInterface,
|
||||
keys: TimelineKeys,
|
||||
eventsMediaType: ClipsOrSnapshotsOrAll,
|
||||
shape: UnifiedQuery,
|
||||
showRecordings: boolean,
|
||||
) {
|
||||
this._cameraManager = cameraManager;
|
||||
this._foldersManager = foldersManager;
|
||||
this._conditionStateManager = conditionStateManager;
|
||||
this._keys = keys;
|
||||
|
||||
this._groups = this._generateGroups(keys);
|
||||
|
||||
this._eventsMediaType = eventsMediaType;
|
||||
this._builder = new UnifiedQueryBuilder(cameraManager, foldersManager);
|
||||
this._runner = new UnifiedQueryRunner(
|
||||
cameraManager,
|
||||
foldersManager,
|
||||
conditionStateManager,
|
||||
);
|
||||
this._shape = shape;
|
||||
this._showRecordings = showRecordings;
|
||||
|
||||
this._groups = this._generateGroups();
|
||||
}
|
||||
|
||||
get dataset(): DataSet<AdvancedCameraCardTimelineItem> {
|
||||
@@ -113,8 +114,12 @@ export class TimelineDataSource {
|
||||
return this._groups;
|
||||
}
|
||||
|
||||
public getKeyType(): 'camera' | 'folder' {
|
||||
return this._keys.type;
|
||||
get shape(): UnifiedQuery {
|
||||
return this._shape;
|
||||
}
|
||||
|
||||
public areResultsFresh(resultsTimestamp: Date, query: UnifiedQuery): boolean {
|
||||
return this._runner.areResultsFresh(resultsTimestamp, query);
|
||||
}
|
||||
|
||||
private _getGroupIDForCamera(cameraID: string): string {
|
||||
@@ -122,30 +127,32 @@ export class TimelineDataSource {
|
||||
}
|
||||
|
||||
private _getGroupIDForFolder(folderConfig: FolderConfig): string {
|
||||
return folderConfig.id;
|
||||
return `folder/${folderConfig.id}`;
|
||||
}
|
||||
|
||||
private _generateGroups(keys: TimelineKeys): DataSet<AdvancedCameraCardGroup> {
|
||||
private _generateGroups(): DataSet<AdvancedCameraCardGroup> {
|
||||
const groups: AdvancedCameraCardGroup[] = [];
|
||||
|
||||
/* istanbul ignore else: the else path cannot be reached -- @preserve */
|
||||
if (keys.type === 'camera') {
|
||||
keys.cameraIDs?.forEach((cameraID) => {
|
||||
const cameraMetadata = this._cameraManager.getCameraMetadata(cameraID);
|
||||
|
||||
groups.push({
|
||||
id: this._getGroupIDForCamera(cameraID),
|
||||
content: cameraMetadata?.title ?? cameraID,
|
||||
});
|
||||
});
|
||||
} else if (keys.type === 'folder') {
|
||||
const folderID = this._getGroupIDForFolder(keys.folder);
|
||||
// Add folder-based groups
|
||||
const folderQueries = this._shape.getFolderQueries();
|
||||
for (const folderQuery of folderQueries) {
|
||||
const folderID = this._getGroupIDForFolder(folderQuery.folder);
|
||||
groups.push({
|
||||
id: folderID,
|
||||
content: keys.folder?.title ?? folderID,
|
||||
content: folderQuery.folder.title ?? folderID,
|
||||
});
|
||||
}
|
||||
|
||||
// Add camera-based groups
|
||||
const cameraIDs = this._shape.getAllCameraIDs();
|
||||
cameraIDs.forEach((cameraID) => {
|
||||
const cameraMetadata = this._cameraManager.getCameraMetadata(cameraID);
|
||||
groups.push({
|
||||
id: this._getGroupIDForCamera(cameraID),
|
||||
content: cameraMetadata?.title ?? cameraID,
|
||||
});
|
||||
});
|
||||
|
||||
return new DataSet(groups);
|
||||
}
|
||||
|
||||
@@ -164,14 +171,11 @@ export class TimelineDataSource {
|
||||
}
|
||||
}
|
||||
|
||||
public addEventMediaToDataset(
|
||||
mediaArray?: ViewItem[] | null,
|
||||
query?: TimelineViewQuery | null,
|
||||
): void {
|
||||
public addMediaToDataset(query: UnifiedQuery, mediaArray?: ViewItem[] | null): void {
|
||||
const data: AdvancedCameraCardTimelineItem[] = [];
|
||||
|
||||
for (const media of mediaArray ?? []) {
|
||||
if (!ViewItemClassifier.isEvent(media)) {
|
||||
if (!ViewItemClassifier.isEvent(media) && !ViewItemClassifier.isReview(media)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -193,7 +197,10 @@ export class TimelineDataSource {
|
||||
start: startTime.getTime(),
|
||||
type: 'range',
|
||||
end: media.getUsableEndTime()?.getTime(),
|
||||
...(query && { query }),
|
||||
...(ViewItemClassifier.isReview(media) && {
|
||||
severity: media.getSeverity() ?? undefined,
|
||||
}),
|
||||
query,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -201,134 +208,60 @@ export class TimelineDataSource {
|
||||
this._dataset.update(data);
|
||||
}
|
||||
|
||||
private async _refreshEvents(
|
||||
window: TimelineWindow,
|
||||
options?: RefreshOptions,
|
||||
): Promise<void> {
|
||||
await this._refreshEventsFromCamera(window, options);
|
||||
await this._refreshEventsFromFolder();
|
||||
public buildRecordingsWindowedQuery(window: TimelineWindow): UnifiedQuery | null {
|
||||
return this._builder.buildRecordingsQuery(this._shape.getAllCameraIDs(), {
|
||||
start: window.start,
|
||||
end: window.end,
|
||||
});
|
||||
}
|
||||
|
||||
private async _refreshEventsFromCamera(
|
||||
window: TimelineWindow,
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
_options?: RefreshOptions,
|
||||
): Promise<void> {
|
||||
if (this._keys.type !== 'camera') {
|
||||
return;
|
||||
}
|
||||
private async _refreshQuery(window: TimelineWindow): Promise<void> {
|
||||
const cacheFriendlyWindow = convertRangeToCacheFriendlyTimes(window);
|
||||
|
||||
if (
|
||||
this._eventRanges.hasCoverage({
|
||||
start: window.start,
|
||||
end: sub(capEndDate(window.end), {
|
||||
this._cache.hasCoverage({
|
||||
start: cacheFriendlyWindow.start,
|
||||
end: sub(capEndDate(cacheFriendlyWindow.end), {
|
||||
seconds: TIMELINE_FRESHNESS_TOLERANCE_SECONDS,
|
||||
}),
|
||||
})
|
||||
) {
|
||||
return;
|
||||
}
|
||||
const cacheFriendlyWindow = convertRangeToCacheFriendlyTimes(window);
|
||||
const eventQueries = this.getTimelineEventQueries(cacheFriendlyWindow);
|
||||
if (!eventQueries) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.addEventMediaToDataset(
|
||||
await this._cameraManager.executeMediaQueries(eventQueries),
|
||||
);
|
||||
const query = UnifiedQueryTransformer.rebuildQuery(this._shape, {
|
||||
start: cacheFriendlyWindow.start,
|
||||
end: cacheFriendlyWindow.end,
|
||||
});
|
||||
|
||||
this._eventRanges.add({
|
||||
this.addMediaToDataset(query, await this._runner.execute(query));
|
||||
this._cache.add({
|
||||
...cacheFriendlyWindow,
|
||||
expires: add(new Date(), { seconds: TIMELINE_FRESHNESS_TOLERANCE_SECONDS }),
|
||||
});
|
||||
}
|
||||
|
||||
private async _refreshEventsFromFolder(): Promise<void> {
|
||||
if (this._keys.type !== 'folder') {
|
||||
return;
|
||||
}
|
||||
|
||||
const folderQuery = this.getTimelineFolderQuery();
|
||||
if (!folderQuery) {
|
||||
return;
|
||||
}
|
||||
|
||||
const lastDate = this._folderCache.get(folderQuery);
|
||||
const now = new Date();
|
||||
|
||||
if (
|
||||
lastDate &&
|
||||
lastDate >= sub(now, { seconds: TIMELINE_FRESHNESS_TOLERANCE_SECONDS })
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.addEventMediaToDataset(
|
||||
await this._foldersManager.expandFolder(
|
||||
folderQuery,
|
||||
this._conditionStateManager.getState(),
|
||||
),
|
||||
new FolderViewQuery(folderQuery),
|
||||
);
|
||||
this._folderCache.set(folderQuery, now);
|
||||
}
|
||||
|
||||
public async refresh(window: TimelineWindow, options?: RefreshOptions): Promise<void> {
|
||||
public async refresh(window: TimelineWindow): Promise<void> {
|
||||
try {
|
||||
await Promise.all([
|
||||
this._refreshEvents(window, options),
|
||||
this._refreshQuery(window),
|
||||
...(this._showRecordings ? [this._refreshRecordings(window)] : []),
|
||||
]);
|
||||
} catch (e) {
|
||||
errorToConsole(e as Error);
|
||||
|
||||
// Intentionally ignore errors here, since it is likely the user will
|
||||
// change the range again and a subsequent call may work. To do otherwise
|
||||
// would be jarring to the timeline experience in the case of transient
|
||||
// errors from the backend.
|
||||
}
|
||||
}
|
||||
|
||||
public getTimelineEventQueries(window: TimelineWindow): EventQuery[] | null {
|
||||
if (this._keys.type !== 'camera' || !this._keys.cameraIDs.size) {
|
||||
return null;
|
||||
}
|
||||
return this._cameraManager.generateDefaultEventQueries(this._keys.cameraIDs, {
|
||||
start: window.start,
|
||||
end: window.end,
|
||||
...(this._eventsMediaType === 'clips' && { hasClip: true }),
|
||||
...(this._eventsMediaType === 'snapshots' && { hasSnapshot: true }),
|
||||
});
|
||||
}
|
||||
|
||||
public getTimelineRecordingQueries(window: TimelineWindow): RecordingQuery[] | null {
|
||||
if (this._keys.type !== 'camera' || !this._keys.cameraIDs.size) {
|
||||
return null;
|
||||
}
|
||||
return this._cameraManager.generateDefaultRecordingQueries(this._keys.cameraIDs, {
|
||||
start: window.start,
|
||||
end: window.end,
|
||||
});
|
||||
}
|
||||
|
||||
public getTimelineFolderQuery(): FolderQuery | null {
|
||||
if (this._keys.type !== 'folder') {
|
||||
return null;
|
||||
}
|
||||
return this._foldersManager.generateDefaultFolderQuery(this._keys.folder);
|
||||
}
|
||||
|
||||
private async _refreshRecordings(window: TimelineWindow): Promise<void> {
|
||||
const cameraIDs = this._keys.type === 'camera' ? this._keys.cameraIDs : null;
|
||||
// Recordings only apply to camera-based shapes
|
||||
const cameraIDs = this._shape.getAllCameraIDs();
|
||||
if (!cameraIDs?.size) {
|
||||
return;
|
||||
}
|
||||
|
||||
type AdvancedCameraCardTimelineItemWithEnd = ModifyInterface<
|
||||
AdvancedCameraCardTimelineItem,
|
||||
{ end: number }
|
||||
>;
|
||||
type AdvancedCameraCardTimelineItemWithEnd = AdvancedCameraCardTimelineItem & {
|
||||
end: number;
|
||||
};
|
||||
|
||||
const convertSegmentToRecording = (
|
||||
cameraID: string,
|
||||
|
||||
@@ -3,7 +3,6 @@ import { CameraManager } from '../../camera-manager/manager';
|
||||
import { ViewItemManager } from '../../card-controller/view/item-manager';
|
||||
import { ViewManagerEpoch } from '../../card-controller/view/types';
|
||||
import { CameraConfig } from '../../config/schema/cameras';
|
||||
import { FolderConfig } from '../../config/schema/folders';
|
||||
import { HomeAssistant } from '../../ha/types';
|
||||
import { ViewMedia } from '../../view/item';
|
||||
|
||||
@@ -21,16 +20,6 @@ export interface ThumbnailDataRequest {
|
||||
|
||||
export class ThumbnailDataRequestEvent extends CustomEvent<ThumbnailDataRequest> {}
|
||||
|
||||
export type TimelineKeys =
|
||||
| {
|
||||
type: 'camera';
|
||||
cameraIDs: Set<string>;
|
||||
}
|
||||
| {
|
||||
type: 'folder';
|
||||
folder: FolderConfig;
|
||||
};
|
||||
|
||||
export interface ExtendedTimeline extends Timeline {
|
||||
// setCustomTimeMarker currently missing from Timeline types.
|
||||
setCustomTimeMarker?(time: DateType, id?: IdType): void;
|
||||
|
||||
Reference in New Issue
Block a user