Skip to content

Commit

Permalink
Merge pull request #1507 from dermotduffy/engine-max-requests
Browse files Browse the repository at this point in the history
Add `max_simultaneous_engine_requests` parameter
  • Loading branch information
dermotduffy committed Aug 27, 2024
2 parents 5844a03 + 76a750f commit 5ad957f
Show file tree
Hide file tree
Showing 14 changed files with 62 additions and 8 deletions.
10 changes: 6 additions & 4 deletions docs/configuration/performance.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ performance:
# [...]
```

| Option | Default | Description |
| ----------------------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `animated_progress_indicator` | `true` | Will show the animated progress indicator 'spinner' when `true` or a simple loading icon when `false`. |
| `media_chunk_size` | `50` | How many media items to fetch and render at a time (e.g. thumbnails under a live view, or number of snapshots to load in the media viewer). This may only make partial sense in some contexts (e.g. the 'infinite gallery' is still infinite, it just loads thumbnails this many items at a time) or not at all (e.g. the timeline will show the number of events dictated by the time span the user navigates to). |
| Option | Default | Description |
| ---------------------------------- | ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `animated_progress_indicator` | `true` | Will show the animated progress indicator 'spinner' when `true` or a simple loading icon when `false`. |
| `media_chunk_size` | `50` | How many media items to fetch and render at a time (e.g. thumbnails under a live view, or number of snapshots to load in the media viewer). This may only make partial sense in some contexts (e.g. the 'infinite gallery' is still infinite, it just loads thumbnails this many items at a time) or not at all (e.g. the timeline will show the number of events dictated by the time span the user navigates to). |
| `max_simultaneous_engine_requests` | _Infinity_ | How many camera engine requests to allow occur in parallel. Setting lower values will slow the card down since more requests will run in sequence, but it will increase the chances of positive cache hit rates and reduce the chances of overwhelming the backend. |

### `style`

Expand Down Expand Up @@ -59,6 +60,7 @@ performance:
features:
animated_progress_indicator: true
media_chunk_size: 50
max_simultaneous_engine_requests: 100
style:
border_radius: true
box_shadow: true
Expand Down
23 changes: 19 additions & 4 deletions src/camera-manager/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ export class CameraManager {
protected _engineFactory: CameraManagerEngineFactory;
protected _store: CameraManagerStore;
protected _initializationLimit = new PQueue({ concurrency: 1 });
protected _requestLimit = new PQueue();

constructor(
api: CardCameraAPI,
Expand All @@ -135,6 +136,9 @@ export class CameraManager {
return false;
}

this._requestLimit.concurrency =
config.performance.features.max_simultaneous_engine_requests ?? Infinity;

// For each camera merge the config (which has no defaults) into the camera
// global config (which does have defaults). The merging must happen in this
// order, to ensure that the defaults in the cameras global config do not
Expand Down Expand Up @@ -535,7 +539,10 @@ export class CameraManager {
}

const queryStartTime = new Date();
await engine.favoriteMedia(hass, cameraConfig, media, favorite);

await this._requestLimit.add(() =>
engine.favoriteMedia(hass, cameraConfig, media, favorite),
);

log(
this._api.getConfigManager().getCardWideConfig(),
Expand Down Expand Up @@ -590,7 +597,11 @@ export class CameraManager {
return null;
}

return await engine.getMediaSeekTime(hass, this._store, media, target);
return (
(await this._requestLimit.add(() =>
engine.getMediaSeekTime(hass, this._store, media, target),
)) ?? null
);
}

protected async _handleQuery<QT extends DataQuery>(
Expand Down Expand Up @@ -653,7 +664,9 @@ export class CameraManager {
}
await Promise.all(
Array.from(engines.keys()).map((engine) =>
processEngineQuery(engine, { ...query, cameraIDs: engines.get(engine) }),
this._requestLimit.add(() =>
processEngineQuery(engine, { ...query, cameraIDs: engines.get(engine) }),
),
),
);
};
Expand Down Expand Up @@ -789,6 +802,8 @@ export class CameraManager {
if (!engine || !hass) {
return;
}
return await engine.executePTZAction(hass, cameraConfig, action, options);
return await this._requestLimit.add(() =>
engine.executePTZAction(hass, cameraConfig, action, options),
);
}
}
4 changes: 4 additions & 0 deletions src/config/profiles/low-performance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
CONF_MENU_BUTTONS_TIMELINE,
CONF_MENU_STYLE,
CONF_PERFORMANCE_FEATURES_ANIMATED_PROGRESS_INDICATOR,
CONF_PERFORMANCE_FEATURES_MAX_SIMULTANEOUS_ENGINE_REQUESTS,
CONF_PERFORMANCE_FEATURES_MEDIA_CHUNK_SIZE,
CONF_PERFORMANCE_STYLE_BORDER_RADIUS,
CONF_PERFORMANCE_STYLE_BOX_SHADOW,
Expand Down Expand Up @@ -137,4 +138,7 @@ export const LOW_PERFORMANCE_PROFILE = {

// No trigger actions.
[CONF_VIEW_TRIGGERS_ACTIONS_TRIGGER]: 'none',

// One engine request at a time.
[CONF_PERFORMANCE_FEATURES_MAX_SIMULTANEOUS_ENGINE_REQUESTS]: 1,
};
1 change: 1 addition & 0 deletions src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1913,6 +1913,7 @@ export const performanceConfigSchema = z
.min(0)
.max(MEDIA_CHUNK_SIZE_MAX)
.default(performanceConfigDefault.features.media_chunk_size),
max_simultaneous_engine_requests: z.number().min(1).optional(),
})
.default(performanceConfigDefault.features),
style: z
Expand Down
1 change: 1 addition & 0 deletions src/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ export const CONF_OVERRIDES = 'overrides' as const;
const CONF_PERFORMANCE = 'performance' as const;
export const CONF_PERFORMANCE_FEATURES_ANIMATED_PROGRESS_INDICATOR = `${CONF_PERFORMANCE}.features.animated_progress_indicator`;
export const CONF_PERFORMANCE_FEATURES_MEDIA_CHUNK_SIZE = `${CONF_PERFORMANCE}.features.media_chunk_size`;
export const CONF_PERFORMANCE_FEATURES_MAX_SIMULTANEOUS_ENGINE_REQUESTS = `${CONF_PERFORMANCE}.features.max_simultaneous_engine_requests`;
export const CONF_PERFORMANCE_PROFILE = `${CONF_PERFORMANCE}.profile`;
export const CONF_PERFORMANCE_STYLE_BOX_SHADOW = `${CONF_PERFORMANCE}.style.box_shadow`;
export const CONF_PERFORMANCE_STYLE_BORDER_RADIUS = `${CONF_PERFORMANCE}.style.border_radius`;
Expand Down
7 changes: 7 additions & 0 deletions src/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ import {
CONF_MENU_POSITION,
CONF_MENU_STYLE,
CONF_PERFORMANCE_FEATURES_ANIMATED_PROGRESS_INDICATOR,
CONF_PERFORMANCE_FEATURES_MAX_SIMULTANEOUS_ENGINE_REQUESTS,
CONF_PERFORMANCE_FEATURES_MEDIA_CHUNK_SIZE,
CONF_PERFORMANCE_PROFILE,
CONF_PERFORMANCE_STYLE_BORDER_RADIUS,
Expand Down Expand Up @@ -2790,6 +2791,12 @@ export class FrigateCardEditor extends LitElement implements LovelaceCardEditor
${this._renderNumberInput(CONF_PERFORMANCE_FEATURES_MEDIA_CHUNK_SIZE, {
max: MEDIA_CHUNK_SIZE_MAX,
})}
${this._renderNumberInput(
CONF_PERFORMANCE_FEATURES_MAX_SIMULTANEOUS_ENGINE_REQUESTS,
{
min: 1,
},
)}
`,
)}
${this._putInSubmenu(
Expand Down
1 change: 1 addition & 0 deletions src/localize/languages/ca.json
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@
"features": {
"animated_progress_indicator": "Indicador animat del progrés",
"editor_label": "Opcions de característiques",
"max_simultaneous_engine_requests": "",
"media_chunk_size": "Mida del fragment multimèdia"
},
"style": {
Expand Down
1 change: 1 addition & 0 deletions src/localize/languages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@
"features": {
"animated_progress_indicator": "Animated Progress Indicator",
"editor_label": "Feature Options",
"max_simultaneous_engine_requests": "Max simultaneous camera engine requests",
"media_chunk_size": "Media chunk size"
},
"style": {
Expand Down
1 change: 1 addition & 0 deletions src/localize/languages/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@
"features": {
"animated_progress_indicator": "Indicateur de progression animé",
"editor_label": "Options de fonctionnalités",
"max_simultaneous_engine_requests": "",
"media_chunk_size": "Taille du morceau de média"
},
"style": {
Expand Down
1 change: 1 addition & 0 deletions src/localize/languages/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@
"features": {
"animated_progress_indicator": "Indicatore di avanzamento animato",
"editor_label": "Opzioni funzionalità",
"max_simultaneous_engine_requests": "",
"media_chunk_size": "Dimensione del blocco multimediale"
},
"style": {
Expand Down
1 change: 1 addition & 0 deletions src/localize/languages/pt-BR.json
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@
"features": {
"animated_progress_indicator": "Indicador de Carregamento Animado",
"editor_label": "Opções de recursos",
"max_simultaneous_engine_requests": "",
"media_chunk_size": "Tamanho do bloco de mídia"
},
"style": {
Expand Down
1 change: 1 addition & 0 deletions src/localize/languages/pt-PT.json
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@
"features": {
"animated_progress_indicator": "Animação na barra de progresso",
"editor_label": "Editor de etiquetas",
"max_simultaneous_engine_requests": "",
"media_chunk_size": "Tamanho do ficheiro"
},
"style": {
Expand Down
17 changes: 17 additions & 0 deletions tests/camera-manager/manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1271,5 +1271,22 @@ describe('CameraManager', async () => {
middleTime,
);
});

it('handles null return value', async () => {
const api = createCardAPI();
const engine = mock<CameraManagerEngine>();
vi.mocked(api.getHASSManager().getHASS).mockReturnValue(createHASS());
const manager = createCameraManager(api, engine);

expect(await manager.initializeCamerasFromConfig()).toBeTruthy();
engine.getMediaSeekTime.mockResolvedValue(null);

const media = new TestViewMedia({
cameraID: 'id',
startTime: startTime,
endTime: endTime,
});
expect(await manager.getMediaSeekTime(media, middleTime)).toBeNull();
});
});
});
1 change: 1 addition & 0 deletions tests/config/profiles/low-performance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ it('low performance profile', () => {
'menu.buttons.timeline.enabled': false,
'menu.style': 'outside',
'performance.features.animated_progress_indicator': false,
'performance.features.max_simultaneous_engine_requests': 1,
'performance.features.media_chunk_size': 10,
'performance.style.border_radius': false,
'performance.style.box_shadow': false,
Expand Down

0 comments on commit 5ad957f

Please sign in to comment.