Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Add updated vault storage insights page to web app and add new API call (#2413)
Leendert de Borst283d0363b329
diff --git a/apps/server/AliasVault.Api/Controllers/V2/VaultController.cs b/apps/server/AliasVault.Api/Controllers/V2/VaultController.csindex 266b1f800..0873c42f6 100644--- a/apps/server/AliasVault.Api/Controllers/V2/VaultController.cs+++ b/apps/server/AliasVault.Api/Controllers/V2/VaultController.cs@@ -232,6 +232,55 @@ public class VaultController(return Ok(manifest);}+ /// <summary>+ /// Exact encrypted storage per accessible manifest: the current manifest, buckets and referenced blobs.+ /// </summary>+ /// <returns>Storage statistics DTO.</returns>+ [HttpGet("storage")]+ public async Task<IActionResult> GetStorageStatistics()+ {+ await using var context = await dbContextFactory.CreateDbContextAsync();+ var user = await GetCurrentUserAsync();+ if (user == null)+ {+ return Unauthorized();+ }++ var accessScope = await ManifestAccessHelper.ResolveScopeAsync(context, user.Id, user.PersonalGroupId);+ var manifests = await AccessibleManifests(context, accessScope)+ .Select(m => new { m.ManifestId, m.OwnerGroupId, ManifestBytes = m.ManifestBlob != null ? (long)m.ManifestBlob.Length : 0 })+ .ToListAsync();+ var manifestIds = manifests.Select(m => m.ManifestId).ToList();++ var bucketBytes = await context.VaultDataBuckets+ .Where(b => manifestIds.Contains(b.ManifestId))+ .GroupBy(b => b.ManifestId)+ .Select(g => new { ManifestId = g.Key, Bytes = g.Sum(b => (long)b.EncryptedData.Length) })+ .ToDictionaryAsync(x => x.ManifestId, x => x.Bytes);++ // Blobs referenced by the current revision of each manifest.+ var currentBlobs = await context.VaultBlobReferences+ .Where(r => manifestIds.Contains(r.ManifestId) && context.VaultManifests.Any(m => m.ManifestId == r.ManifestId && m.RevisionNumber == r.RevisionNumber))+ .Join(context.VaultBlobObjects, r => new { r.ManifestId, Hash = r.BlobHash }, b => new { b.ManifestId, b.Hash }, (r, b) => new { b.ManifestId, b.Category, b.SizeBytes })+ .GroupBy(x => new { x.ManifestId, x.Category })+ .Select(g => new { g.Key.ManifestId, g.Key.Category, Count = g.Count(), Bytes = g.Sum(x => (long)x.SizeBytes) })+ .ToListAsync();++ var response = new StorageStatisticsResponse+ {+ Manifests = manifests.Select(m => new ManifestStorageStatistics+ {+ ManifestId = m.ManifestId,+ IsPersonal = m.OwnerGroupId == user.PersonalGroupId,+ ManifestBytes = m.ManifestBytes,+ BucketBytes = bucketBytes.GetValueOrDefault(m.ManifestId),+ Blobs = currentBlobs.Where(b => b.ManifestId == m.ManifestId).Select(b => new BlobCategoryStatistics { Category = b.Category, Count = b.Count, Bytes = b.Bytes }).ToList(),+ }).ToList(),+ };++ return Ok(response);+ }+/// <summary>/// Unified atomic write. Applies any number of changed manifests (personal and/or shared) and changed data buckets in a single/// all-or-nothing DB transaction. Blobs are uploaded beforehand through POST v2/Vault/blobs.diff --git a/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/BlobCategoryStatistics.cs b/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/BlobCategoryStatistics.csnew file mode 100644index 000000000..9696abe81--- /dev/null+++ b/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/BlobCategoryStatistics.cs@@ -0,0 +1,23 @@+//-----------------------------------------------------------------------+// <copyright file="BlobCategoryStatistics.cs" company="aliasvault">+// Copyright (c) aliasvault. All rights reserved.+// Licensed under the AGPLv3 license. See LICENSE.md file in the project root for full license information.+// </copyright>+//-----------------------------------------------------------------------++namespace AliasVault.Shared.Models.WebApi.V2.Vault;++/// <summary>+/// Count and encrypted size of the blobs in one category.+/// </summary>+public class BlobCategoryStatistics+{+ /// <summary>Gets or sets the blob category (e.g. "favicon" or "attachment").</summary>+ public required string Category { get; set; }++ /// <summary>Gets or sets the number of blobs.</summary>+ public int Count { get; set; }++ /// <summary>Gets or sets the total encrypted size in bytes.</summary>+ public long Bytes { get; set; }+}diff --git a/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/ManifestStorageStatistics.cs b/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/ManifestStorageStatistics.csnew file mode 100644index 000000000..1dada670b--- /dev/null+++ b/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/ManifestStorageStatistics.cs@@ -0,0 +1,29 @@+//-----------------------------------------------------------------------+// <copyright file="ManifestStorageStatistics.cs" company="aliasvault">+// Copyright (c) aliasvault. All rights reserved.+// Licensed under the AGPLv3 license. See LICENSE.md file in the project root for full license information.+// </copyright>+//-----------------------------------------------------------------------++namespace AliasVault.Shared.Models.WebApi.V2.Vault;++/// <summary>+/// Encrypted bytes the server stores for one manifest.+/// </summary>+public class ManifestStorageStatistics+{+ /// <summary>Gets or sets the manifest id.</summary>+ public Guid ManifestId { get; set; }++ /// <summary>Gets or sets a value indicating whether this is the caller's personal manifest.</summary>+ public bool IsPersonal { get; set; }++ /// <summary>Gets or sets the size of the current encrypted manifest.</summary>+ public long ManifestBytes { get; set; }++ /// <summary>Gets or sets the size of the current settings and stats buckets.</summary>+ public long BucketBytes { get; set; }++ /// <summary>Gets or sets the blobs the current revision references, per category.</summary>+ public List<BlobCategoryStatistics> Blobs { get; set; } = [];+}diff --git a/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/StorageStatisticsResponse.cs b/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/StorageStatisticsResponse.csnew file mode 100644index 000000000..50ef6957c--- /dev/null+++ b/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/StorageStatisticsResponse.cs@@ -0,0 +1,17 @@+//-----------------------------------------------------------------------+// <copyright file="StorageStatisticsResponse.cs" company="aliasvault">+// Copyright (c) aliasvault. All rights reserved.+// Licensed under the AGPLv3 license. See LICENSE.md file in the project root for full license information.+// </copyright>+//-----------------------------------------------------------------------++namespace AliasVault.Shared.Models.WebApi.V2.Vault;++/// <summary>+/// Exact server-side storage of every manifest the caller can access.+/// </summary>+public class StorageStatisticsResponse+{+ /// <summary>Gets or sets the storage per accessible manifest.</summary>+ public List<ManifestStorageStatistics> Manifests { get; set; } = [];+}diff --git a/apps/web/src/App.tsx b/apps/web/src/App.tsxindex b9b8d3e42..edf345c45 100644--- a/apps/web/src/App.tsx+++ b/apps/web/src/App.tsx@@ -25,6 +25,7 @@ import DeleteAccount from '@/pages/settings/security/DeleteAccount';import Disable2Fa from '@/pages/settings/security/Disable2Fa';import Enable2Fa from '@/pages/settings/security/Enable2Fa';import SecuritySettings from '@/pages/settings/security/Security';+import StorageInsights from '@/pages/settings/StorageInsights';import Sync from '@/pages/sync/Sync';/**@@ -64,6 +65,7 @@ const App: React.FC = () => (<Route path="/settings/security/enable-2fa" element={<Enable2Fa />} /><Route path="/settings/security/disable-2fa" element={<Disable2Fa />} /><Route path="/settings/security/delete-account" element={<DeleteAccount />} />+ <Route path="/settings/storage-insights" element={<StorageInsights />} /><Route path="/settings/import-export" element={<ImportExport />} /><Route path="/settings/import-export/reset-vault" element={<ResetVault />} /><Route path="/settings/apps" element={<AppsSettings />} />diff --git a/apps/web/src/pages/settings/StorageInsights.tsx b/apps/web/src/pages/settings/StorageInsights.tsxnew file mode 100644index 000000000..be099b78b--- /dev/null+++ b/apps/web/src/pages/settings/StorageInsights.tsx@@ -0,0 +1,379 @@+import { BulkFaviconService } from '@aliasvault/client/items/BulkFaviconService';+import { fromStandardFormat } from '@aliasvault/client/utilities/DateFormatter';+import { LogoKinds } from '@aliasvault/models/vault';+import React, { useCallback, useEffect, useMemo, useState } from 'react';+import { useTranslation } from 'react-i18next';+import { useNavigate } from 'react-router-dom';++import Button from '@/components/shared/Button';+import Card from '@/components/shared/Card';+import PageContent from '@/components/shared/PageContent';+import PageHeader from '@/components/shared/PageHeader';+import SortableTable, { SortableTableColumn, SortableTableRow, type TableColumn } from '@/components/shared/SortableTable';+import { useConfirmModal } from '@/context/ConfirmModalContext';+import { useDb } from '@/context/DbContext';+import { useNotifications } from '@/context/NotificationContext';+import { useWebApi } from '@/context/WebApiContext';+import { usePageTitle } from '@/hooks/usePageTitle';+import { useVaultMutate } from '@/hooks/useVaultMutate';+import { formatBytes } from '@/utils/FormatBytes';+import { itemRoute } from '@/utils/ItemRoute';++import type { AttachmentSizeRow, LogoSizeRow, StorageCounts } from '@aliasvault/client/database/repositories/StorageInsightsRepository';+import type { StorageStatisticsResponse } from '@aliasvault/models/webapi';++const tk = 'pages.main.settings.storageInsights';++/** Blob categories as the server stores them. */+const ATTACHMENT_CATEGORY = 'attachment';+const LOGO_CATEGORY = 'favicon';++/** Rows in each "largest" table. */+const TOP_ROWS = 10;++type Segment = {+ label: string;+ bytes: number;+ colorClass: string;+};++type LocalStats = {+ counts: StorageCounts;+ attachments: AttachmentSizeRow[];+ logos: LogoSizeRow[];+};++/**+ * One count tile of the overview card.+ */+const CountTile: React.FC<{ label: string; value: number }> = ({ label, value }) => (+ <div className="p-4 border border-gray-200 rounded-lg dark:border-gray-700">+ <p className="text-sm text-gray-500 dark:text-gray-400">{label}</p>+ <p className="mt-1 text-2xl font-semibold text-gray-900 dark:text-white">{value}</p>+ </div>+);++/**+ * Card heading with an optional description.+ */+const CardHeading: React.FC<{ title: string; description?: string }> = ({ title, description }) => (+ <>+ <h3 className={`${description ? 'mb-2' : 'mb-4'} text-lg font-medium text-gray-900 dark:text-white`}>{title}</h3>+ {description && <p className="mb-4 text-sm text-gray-500 dark:text-gray-400">{description}</p>}+ </>+);++/**+ * Share of a total as a percentage, 0 when the total is 0.+ * @param part - the part+ * @param total - the total+ */+const percent = (part: number, total: number): number => (total === 0 ? 0 : (part / total) * 100);++/**+ * A stored UTC timestamp as a local yyyy-mm-dd date.+ * @param value - the stored timestamp+ */+const formatDate = (value: string): string => {+ const date = fromStandardFormat(value);+ return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`;+};++/**+ * Vault storage insights page.+ */+const StorageInsights: React.FC = () => {+ const { t } = useTranslation();+ const navigate = useNavigate();+ const dbContext = useDb();+ const webApi = useWebApi();+ const notifications = useNotifications();+ const { showConfirmation } = useConfirmModal();+ const { executeVaultMutationAsync } = useVaultMutate();+ usePageTitle(t(`${tk}.PageTitle`));++ const [localStats, setLocalStats] = useState<LocalStats | null>(null);+ const [serverStats, setServerStats] = useState<StorageStatisticsResponse | null>(null);+ const [serverStatsFailed, setServerStatsFailed] = useState(false);+ const [isDeletingLogos, setIsDeletingLogos] = useState(false);+ const [redownloadProgress, setRedownloadProgress] = useState<{ processed: number; total: number } | null>(null);++ /**+ * Read the counts and the largest attachments and logos from the local vault.+ */+ const loadLocalStats = useCallback((): void => {+ const client = dbContext.sqliteClient;+ if (!client) {+ return;+ }+ setLocalStats({+ counts: client.storageInsights.getCounts(),+ attachments: client.storageInsights.getLargestAttachments(TOP_ROWS),+ logos: client.storageInsights.getLargestLogos(TOP_ROWS),+ });+ }, [dbContext.sqliteClient]);++ /**+ * Fetch the exact stored size from the server.+ */+ const loadServerStats = useCallback((): void => {+ webApi.get<StorageStatisticsResponse>('Vault/storage')+ .then((stats) => {+ setServerStats(stats);+ setServerStatsFailed(false);+ })+ .catch((error: unknown) => {+ console.error('Failed to load the vault storage statistics:', error);+ setServerStatsFailed(true);+ });+ }, [webApi]);++ useEffect(() => {+ loadLocalStats();+ }, [loadLocalStats]);++ useEffect(() => {+ loadServerStats();+ }, [loadServerStats]);++ /**+ * Open the item a table row belongs to.+ * @param id - the item id+ * @param manifestId - the item's manifest+ */+ const openItem = (id: string, manifestId: string): void => {+ void navigate(itemRoute({ Id: id, ManifestId: manifestId }));+ };++ /**+ * Reload both sources after a logo action changed the vault.+ */+ const reload = (): void => {+ loadLocalStats();+ loadServerStats();+ };++ /**+ * The server size per category, summed over every vault the user can open.+ */+ const segments = useMemo((): Segment[] => {+ if (!serverStats) {+ return [];+ }+ let credentials = 0;+ let attachments = 0;+ let logos = 0;+ for (const manifest of serverStats.manifests) {+ credentials += manifest.manifestBytes + manifest.bucketBytes;+ for (const blob of manifest.blobs) {+ if (blob.category === ATTACHMENT_CATEGORY) {+ attachments += blob.bytes;+ } else if (blob.category === LOGO_CATEGORY) {+ logos += blob.bytes;+ } else {+ credentials += blob.bytes;+ }+ }+ }+ return [+ { label: t(`${tk}.BreakdownCredentialsLabel`), bytes: credentials, colorClass: 'bg-blue-500' },+ { label: t(`${tk}.BreakdownAttachmentsLabel`), bytes: attachments, colorClass: 'bg-amber-500' },+ { label: t(`${tk}.BreakdownLogosLabel`), bytes: logos, colorClass: 'bg-emerald-500' },+ ];+ }, [serverStats, t]);++ const totalBytes = segments.reduce((sum, segment) => sum + segment.bytes, 0);+ const isLogoActionRunning = isDeletingLogos || redownloadProgress !== null;++ const attachmentColumns: TableColumn[] = [+ { title: t(`${tk}.ColumnFilename`) },+ { title: t(`${tk}.ColumnSize`) },+ { title: t(`${tk}.ColumnItem`) },+ { title: t(`${tk}.ColumnCreated`) },+ ];+ const logoColumns: TableColumn[] = [+ { title: t(`${tk}.ColumnWebsiteURL`) },+ { title: t(`${tk}.ColumnSize`) },+ { title: t(`${tk}.ColumnItemCount`) },+ ];++ /**+ * Remove every favicon from the personal vault after confirmation.+ */+ const deleteAllLogos = async (): Promise<void> => {+ const client = dbContext.sqliteClient;+ if (!client || isLogoActionRunning) {+ return;+ }+ const confirmed = await showConfirmation(t(`${tk}.DeleteAllLogosConfirmTitle`), t(`${tk}.DeleteAllLogosConfirmMessage`), t('sharedResources.Confirm'), t('sharedResources.Cancel'));+ if (!confirmed) {+ return;+ }+ setIsDeletingLogos(true);+ try {+ await executeVaultMutationAsync(async () => {+ await client.logos.deleteAllFavicons();+ });+ notifications.addSuccessMessage(t('sharedResources.Success'));+ } catch (error) {+ console.error('Failed to delete the logos:', error);+ notifications.addErrorMessage(t('sharedResources.ErrorGeneric'), true);+ } finally {+ setIsDeletingLogos(false);+ reload();+ }+ };++ /**+ * Fetch the favicon of every item in the personal vault again after confirmation.+ */+ const redownloadAllLogos = async (): Promise<void> => {+ const client = dbContext.sqliteClient;+ if (!client || isLogoActionRunning) {+ return;+ }+ const confirmed = await showConfirmation(t(`${tk}.RedownloadAllLogosConfirmTitle`), t(`${tk}.RedownloadAllLogosConfirmMessage`), t('sharedResources.Confirm'), t('sharedResources.Cancel'));+ if (!confirmed) {+ return;+ }+ setRedownloadProgress({ processed: 0, total: 0 });+ try {+ let rateLimited = false;+ await executeVaultMutationAsync(async () => {+ const result = await BulkFaviconService.refreshVaultFavicons(client, webApi, (processed, total) => setRedownloadProgress({ processed, total }));+ rateLimited = result.status === 'rateLimited';+ });+ if (rateLimited) {+ notifications.addErrorMessage(t('sharedResources.ErrorGeneric'), true);+ } else {+ notifications.addSuccessMessage(t('sharedResources.Success'));+ }+ } catch (error) {+ console.error('Failed to re-download the logos:', error);+ notifications.addErrorMessage(t('sharedResources.ErrorGeneric'), true);+ } finally {+ setRedownloadProgress(null);+ reload();+ }+ };++ return (+ <>+ <PageHeader breadcrumbItems={[{ displayName: t(`${tk}.BreadcrumbTitle`) }]} title={t(`${tk}.PageTitle`)} description={t(`${tk}.PageDescription`)} />++ <PageContent>+ <Card>+ <h3 className="mb-2 text-lg font-medium text-gray-900 dark:text-white">{t(`${tk}.ServerTotalTitle`)}</h3>+ {serverStatsFailed ? (+ <p className="text-sm text-gray-500 dark:text-gray-400">{t('sharedResources.ErrorGeneric')}</p>+ ) : serverStats ? (+ <>+ <p className="text-4xl font-semibold text-gray-900 dark:text-white">{formatBytes(totalBytes)}</p>+ <p className="mt-2 text-sm text-gray-500 dark:text-gray-400">{t(`${tk}.ServerTotalDescription`)}</p>+ </>+ ) : (+ <p className="text-gray-600 dark:text-gray-400">{t('sharedResources.Loading')}</p>+ )}+ </Card>++ <Card>+ <CardHeading title={t(`${tk}.CountsTitle`)} />+ {localStats ? (+ <div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-5">+ <CountTile label={t(`${tk}.ItemCountLabel`)} value={localStats.counts.ItemCount} />+ <CountTile label={t(`${tk}.ItemsWithAttachmentsLabel`)} value={localStats.counts.ItemsWithAttachments} />+ <CountTile label={t(`${tk}.ItemsWithLogosLabel`)} value={localStats.counts.ItemsWithLogos} />+ <CountTile label={t(`${tk}.BreakdownAttachmentsLabel`)} value={localStats.counts.AttachmentCount} />+ <CountTile label={t(`${tk}.BreakdownLogosLabel`)} value={localStats.counts.LogoCount} />+ </div>+ ) : (+ <p className="text-gray-600 dark:text-gray-400">{t('sharedResources.Loading')}</p>+ )}+ </Card>++ {totalBytes > 0 && (+ <Card>+ <CardHeading title={t(`${tk}.BreakdownTitle`)} />+ <div className="flex w-full h-4 overflow-hidden rounded-full bg-gray-200 dark:bg-gray-700">+ {segments.map(segment => (+ <div key={segment.label} className={segment.colorClass} style={{ width: `${percent(segment.bytes, totalBytes).toFixed(2)}%` }} title={segment.label}></div>+ ))}+ </div>+ <div className="mt-4 grid gap-2 sm:grid-cols-3 text-sm">+ {segments.map(segment => (+ <div key={segment.label} className="flex items-center gap-2">+ <span className={`inline-block w-3 h-3 rounded-sm ${segment.colorClass}`}></span>+ <span className="text-gray-700 dark:text-gray-300">{segment.label}:</span>+ <span className="font-medium text-gray-900 dark:text-white">{formatBytes(segment.bytes)} ({percent(segment.bytes, totalBytes).toFixed(1)}%)</span>+ </div>+ ))}+ </div>+ </Card>+ )}++ {localStats && (+ <>+ <Card>+ <CardHeading title={t(`${tk}.TopAttachmentsTitle`)} description={t(`${tk}.TopAttachmentsDescription`)} />+ {localStats.attachments.length === 0 ? (+ <p className="text-sm text-gray-500 dark:text-gray-400">-</p>+ ) : (+ <SortableTable columns={attachmentColumns} sortColumn="" sortDirection="desc" onSortChanged={() => undefined}>+ {localStats.attachments.map(attachment => (+ <SortableTableRow key={`${attachment.ManifestId}:${attachment.Id}`} onClick={() => openItem(attachment.ItemId, attachment.ManifestId)}>+ <SortableTableColumn isPrimary>{attachment.Filename}</SortableTableColumn>+ <SortableTableColumn>{formatBytes(attachment.SizeBytes)}</SortableTableColumn>+ <SortableTableColumn>{attachment.ItemName?.trim() ? attachment.ItemName : '-'}</SortableTableColumn>+ <SortableTableColumn>{formatDate(attachment.CreatedAt)}</SortableTableColumn>+ </SortableTableRow>+ ))}+ </SortableTable>+ )}+ </Card>++ <Card>+ <CardHeading title={t(`${tk}.TopLogosTitle`)} description={t(`${tk}.TopLogosDescription`)} />+ {localStats.logos.length === 0 ? (+ <p className="text-sm text-gray-500 dark:text-gray-400">-</p>+ ) : (+ <SortableTable columns={logoColumns} sortColumn="" sortDirection="desc" onSortChanged={() => undefined}>+ {localStats.logos.map(logo => {+ const firstItemId = logo.FirstItemId;+ return (+ <SortableTableRow key={`${logo.ManifestId}:${logo.Id}`} onClick={firstItemId ? (): void => openItem(firstItemId, logo.ManifestId) : undefined}>+ <SortableTableColumn isPrimary>{logo.Kind === LogoKinds.Favicon ? logo.Source : (logo.Name ?? '-')}</SortableTableColumn>+ <SortableTableColumn>{formatBytes(logo.SizeBytes)}</SortableTableColumn>+ <SortableTableColumn>{logo.ItemCount}</SortableTableColumn>+ </SortableTableRow>+ );+ })}+ </SortableTable>+ )}+ </Card>+ </>+ )}++ <Card>+ <CardHeading title={t(`${tk}.LogoManagementTitle`)} description={t(`${tk}.LogoManagementDescription`)} />+ {redownloadProgress && (+ <div className="mb-4">+ <div className="flex justify-between text-sm text-gray-700 dark:text-gray-300 mb-1">+ <span>{t('sharedResources.Loading')}</span>+ <span>{redownloadProgress.processed} / {redownloadProgress.total}</span>+ </div>+ <div className="w-full bg-gray-200 rounded-full h-2.5 dark:bg-gray-700">+ <div className="bg-primary-600 h-2.5 rounded-full transition-all duration-300" style={{ width: `${percent(redownloadProgress.processed, redownloadProgress.total).toFixed(1)}%` }}></div>+ </div>+ </div>+ )}+ <div className="flex flex-col sm:flex-row gap-3">+ <Button color="secondary" isDisabled={isLogoActionRunning} onClick={() => void redownloadAllLogos()}>{t(`${tk}.RedownloadAllLogosButton`)}</Button>+ <Button color="danger" isDisabled={isLogoActionRunning} onClick={() => void deleteAllLogos()}>{t(`${tk}.DeleteAllLogosButton`)}</Button>+ </div>+ </Card>+ </PageContent>+ </>+ );+};++export default StorageInsights;diff --git a/core/client/src/database/SqliteClient.ts b/core/client/src/database/SqliteClient.tsindex 6a1695700..d4ddfd17d 100644--- a/core/client/src/database/SqliteClient.ts+++ b/core/client/src/database/SqliteClient.ts@@ -19,7 +19,8 @@ import {SettingsRepository,EncryptionKeyRepository,LogoRepository,- ImportExportRepository+ ImportExportRepository,+ StorageInsightsRepository} from './index';import type { ISyncDatabaseClient, SqliteBindValue } from './BaseRepository';@@ -62,6 +63,7 @@ export class SqliteClient implements ISyncDatabaseClient {private _encryptionKeys: SyncRepository<EncryptionKeyRepository> | null = null;private _logos: SyncRepository<LogoRepository> | null = null;private _importExport: SyncRepository<ImportExportRepository> | null = null;+ private _storageInsights: SyncRepository<StorageInsightsRepository> | null = null;private _logoRepository: LogoRepository | null = null;/**@@ -152,6 +154,16 @@ export class SqliteClient implements ISyncDatabaseClient {return this._importExport;}+ /**+ * Repository for the storage statistics of the local vault.+ */+ public get storageInsights(): SyncRepository<StorageInsightsRepository> {+ if (!this._storageInsights) {+ this._storageInsights = syncRepository(new StorageInsightsRepository(this), this);+ }+ return this._storageInsights;+ }+/*** The logo repository itself, which the item and import/export repositories call into.*/diff --git a/core/client/src/database/__tests__/LogoScoping.test.ts b/core/client/src/database/__tests__/LogoScoping.test.tsindex c7fed53e4..911ec3f25 100644--- a/core/client/src/database/__tests__/LogoScoping.test.ts+++ b/core/client/src/database/__tests__/LogoScoping.test.ts@@ -46,17 +46,11 @@ describe('logo manifest scoping', () => {expect(rows(db, LogoQueries.GET_BEST_FOR_KEY, ['favicon', 'github.com'])[0].Name).toBe('real');});- it('GET_BY_ID answers for an item whose logo lives in another manifest', async () => {+ it('GET_BY_ID only resolves a logo inside the given manifest', async () => {const db = await makeDb();db.run(`INSERT INTO Logos VALUES ('${PERSONAL}','L-PERSONAL','builtin','shopping',NULL,NULL,NULL,'t','t',0)`);- // Moved into the shared manifest by a restamp, still pointing at the personal logo row.- db.run(`INSERT INTO Items VALUES ('${SHARED}','ITEM-MOVED','L-PERSONAL','FOLDER-SHARED',0)`);-- /*- * The write path prefers the item's own manifest but still finds the logo row the item came with, so the- * built-in logo the user picked is kept instead of being replaced by the domain's favicon.- */- expect(rows(db, LogoQueries.GET_BY_ID, ['L-PERSONAL', SHARED])[0]).toMatchObject({ Kind: 'builtin', Source: 'shopping' });+ expect(rows(db, LogoQueries.GET_BY_ID, ['L-PERSONAL', PERSONAL])[0]).toMatchObject({ Kind: 'builtin', Source: 'shopping' });+ expect(rows(db, LogoQueries.GET_BY_ID, ['L-PERSONAL', SHARED])).toEqual([]);});it('GET_ID_FOR_KEY will not hand one manifest another manifest row', async () => {diff --git a/core/client/src/database/index.ts b/core/client/src/database/index.tsindex 14c775526..304629dcb 100644--- a/core/client/src/database/index.ts+++ b/core/client/src/database/index.ts@@ -24,3 +24,4 @@ export { EncryptionKeyRepository } from './repositories/EncryptionKeyRepository'export { LogoRepository } from './repositories/LogoRepository';export { ItemStatsRepository, type ItemUsageAction } from './repositories/ItemStatsRepository';export { ImportExportRepository, type VaultExportData } from './repositories/ImportExportRepository';+export { StorageInsightsRepository, type StorageCounts, type AttachmentSizeRow, type LogoSizeRow } from './repositories/StorageInsightsRepository';diff --git a/core/client/src/database/queries/LogoQueries.ts b/core/client/src/database/queries/LogoQueries.tsindex fe41f6633..33006d9f2 100644--- a/core/client/src/database/queries/LogoQueries.ts+++ b/core/client/src/database/queries/LogoQueries.ts@@ -29,14 +29,43 @@ export class LogoQueries {LIMIT 1`;/**- * The kind and key of an item's logo. Binds [logoId, item manifest]: the item's own manifest wins, and another- * manifest only answers for an item that was moved and still points at the logo row it came with.+ * The kind and key of an item's logo.*/public static readonly GET_BY_ID = `SELECT Id, Kind, Source, Name FROM Logos- WHERE Id = ? AND IsDeleted = 0- ORDER BY (ManifestId = ?) DESC, ManifestId- LIMIT 1`;+ WHERE Id = ? AND ManifestId = ? AND IsDeleted = 0`;++ /**+ * Get all items with the kind and key of their current logo.+ */+ public static readonly GET_ITEMS_WITH_LOGO_KIND = `+ SELECT i.Id, l.Kind AS LogoKind, l.Source AS LogoSource, i.LogoId+ FROM Items i+ LEFT JOIN Logos l ON l.ManifestId = i.ManifestId AND l.Id = i.LogoId AND l.IsDeleted = 0+ WHERE i.ManifestId = ? AND i.IsDeleted = 0 AND i.DeletedAt IS NULL`;++ /**+ * The values of one field key for every item of one manifest, in display order.+ */+ public static readonly GET_FIELD_VALUES_FOR_MANIFEST = `+ SELECT ItemId, Value FROM FieldValues+ WHERE ManifestId = ? AND FieldKey = ? AND IsDeleted = 0 AND Value IS NOT NULL+ ORDER BY ItemId, Weight, ValueIndex`;++ /**+ * Point an item at another logo, or at none.+ */+ public static readonly SET_ITEM_LOGO = `+ UPDATE Items SET LogoId = ?, UpdatedAt = ?+ WHERE Id = ? AND ManifestId = ?`;++ /**+ * Unlink every item of one manifest from its favicon. The orphaned favicon rows are then pruned before the next push.+ */+ public static readonly UNLINK_FAVICONS = `+ UPDATE Items SET LogoId = NULL, UpdatedAt = ?+ WHERE ManifestId = ? AND IsDeleted = 0+ AND LogoId IN (SELECT Id FROM Logos WHERE ManifestId = ? AND Kind = ?)`;/*** Insert or update a logo.diff --git a/core/client/src/database/queries/StorageInsightsQueries.ts b/core/client/src/database/queries/StorageInsightsQueries.tsnew file mode 100644index 000000000..fda1e862b--- /dev/null+++ b/core/client/src/database/queries/StorageInsightsQueries.ts@@ -0,0 +1,42 @@+/**+ * SQL query constants for the storage insights of the local vault. Trashed items count (they are still stored until+ * purged), tombstoned rows do not.+ */+export class StorageInsightsQueries {+ /**+ * Item, attachment and logo counts.+ */+ public static readonly GET_COUNTS = `+ SELECT+ (SELECT COUNT(*) FROM Items WHERE IsDeleted = 0) AS ItemCount,+ (SELECT COUNT(*) FROM Items i WHERE i.IsDeleted = 0+ AND EXISTS (SELECT 1 FROM Attachments a WHERE a.ItemId = i.Id AND a.ManifestId = i.ManifestId AND a.IsDeleted = 0)) AS ItemsWithAttachments,+ (SELECT COUNT(*) FROM Items i WHERE i.IsDeleted = 0 AND i.LogoId IS NOT NULL) AS ItemsWithLogos,+ (SELECT COUNT(*) FROM Attachments a JOIN Items i ON i.Id = a.ItemId AND i.ManifestId = a.ManifestId+ WHERE a.IsDeleted = 0 AND i.IsDeleted = 0) AS AttachmentCount,+ (SELECT COUNT(*) FROM Logos WHERE IsDeleted = 0 AND (FileData IS NOT NULL OR FileDataHash IS NOT NULL)) AS LogoCount`;++ /**+ * The largest attachments with the item they belong to.+ */+ public static readonly GET_LARGEST_ATTACHMENTS = `+ SELECT a.Id, a.Filename, LENGTH(a.Blob) AS SizeBytes, a.ItemId, a.ManifestId, i.Name AS ItemName, a.CreatedAt+ FROM Attachments a+ JOIN Items i ON i.Id = a.ItemId AND i.ManifestId = a.ManifestId+ WHERE a.IsDeleted = 0 AND i.IsDeleted = 0 AND a.Blob IS NOT NULL+ ORDER BY SizeBytes DESC+ LIMIT ?`;++ /**+ * The largest logos with the number of items showing them.+ */+ public static readonly GET_LARGEST_LOGOS = `+ SELECT l.Id, l.ManifestId, l.Kind, l.Source, l.Name, LENGTH(l.FileData) AS SizeBytes,+ COUNT(i.Id) AS ItemCount, MIN(i.Id) AS FirstItemId+ FROM Logos l+ LEFT JOIN Items i ON i.LogoId = l.Id AND i.ManifestId = l.ManifestId AND i.IsDeleted = 0+ WHERE l.IsDeleted = 0 AND l.FileData IS NOT NULL+ GROUP BY l.Id, l.ManifestId+ ORDER BY SizeBytes DESC+ LIMIT ?`;+}diff --git a/core/client/src/database/repositories/LogoRepository.ts b/core/client/src/database/repositories/LogoRepository.tsindex 2386baf53..a23d4992d 100644--- a/core/client/src/database/repositories/LogoRepository.ts+++ b/core/client/src/database/repositories/LogoRepository.ts@@ -1,4 +1,4 @@-import { LogoKinds } from '@aliasvault/models/vault';+import { FieldKey, LogoKinds } from '@aliasvault/models/vault';import { vaultCodecLogoContentHash, vaultCodecLogoIdFor } from '../../rust/RustCore';import { logExpected } from '../../utilities/Diagnostics';@@ -8,6 +8,15 @@ import { LogoQueries } from '../queries/LogoQueries';import type { DbOp } from '../DbOp';import type { ItemLogo, LogoKind } from '@aliasvault/models/vault';+/**+ * An item whose favicon a bulk refresh may replace (has no favicon yet).+ */+export type FaviconRefreshCandidate = {+ ItemId: string;+ Urls: string[];+ CurrentSource: string | null;+};+/*** Repository for item logo operations.*@@ -130,6 +139,75 @@ export class LogoRepository extends BaseRepository {return this.getOrCreate(manifestId, LogoKinds.Custom, contentHash, fileData, currentDateTime, options);}+ /**+ * The items of the personal vault a bulk favicon refresh applies to, every active item except those showing a+ * built-in or uploaded logo, which the user chose on purpose.+ * @returns The items to refresh+ */+ public *getFaviconRefreshCandidates(): DbOp<FaviconRefreshCandidate[]> {+ const manifestId = yield* this.writeManifestId();+ const items = yield* this.query<{ Id: string; LogoKind: LogoKind | null; LogoSource: string | null; LogoId: string | null }>(LogoQueries.GET_ITEMS_WITH_LOGO_KIND, [manifestId]);+ const urlRows = yield* this.query<{ ItemId: string; Value: string }>(LogoQueries.GET_FIELD_VALUES_FOR_MANIFEST, [manifestId, FieldKey.LoginUrl]);++ const urlsByItem = new Map<string, string[]>();+ for (const row of urlRows) {+ const urls = urlsByItem.get(row.ItemId) ?? [];+ urls.push(row.Value);+ urlsByItem.set(row.ItemId, urls);+ }++ return items+ .filter(item => !item.LogoId || !item.LogoKind || item.LogoKind === LogoKinds.Favicon)+ .map(item => ({ ItemId: item.Id, Urls: urlsByItem.get(item.Id) ?? [], CurrentSource: item.LogoKind ? item.LogoSource : null }));+ }++ /**+ * Point every candidate at the favicon of its current domain: freshly fetched bytes win, otherwise the vault's+ * stored favicon for that domain, otherwise none. Never the favicon of a previous domain.+ * @param assignments - Each candidate with the domain its favicon comes from, or null when it has no usable URL+ * @param favicons - The fetched favicon bytes by domain+ * @returns The number of items whose logo changed+ */+ public async applyFaviconRefresh(assignments: { candidate: FaviconRefreshCandidate; source: string | null }[], favicons: Map<string, Uint8Array>): Promise<number> {+ return this.withTransaction(async () => {+ const manifestId = await this.run(this.writeManifestId());+ const currentDateTime = this.now();+ const logoIdBySource = new Map<string, string | null>();+ let changed = 0;++ for (const { candidate, source } of assignments) {+ let logoId: string | null = null;+ if (source) {+ if (!logoIdBySource.has(source)) {+ const bytes = favicons.get(source);+ logoIdBySource.set(source, bytes+ ? await this.getOrCreate(manifestId, LogoKinds.Favicon, source, bytes, currentDateTime, { mimeType: 'image/x-icon' })+ : await this.ensureInScope(manifestId, LogoKinds.Favicon, source, currentDateTime));+ }+ logoId = logoIdBySource.get(source) ?? null;+ }++ const currentLogoId = candidate.CurrentSource === null ? null : await this.run(this.getIdForKey(manifestId, LogoKinds.Favicon, candidate.CurrentSource));+ if (logoId !== currentLogoId) {+ await this.run(this.execute(LogoQueries.SET_ITEM_LOGO, [logoId, currentDateTime, candidate.ItemId, manifestId]));+ changed++;+ }+ }++ return changed;+ });+ }++ /**+ * Remove the favicon of every item in the personal vault. Built-in and uploaded logos stay; the favicon rows+ * themselves are pruned before the next push, once nothing points at them.+ * @returns The number of items whose favicon was removed+ */+ public async deleteAllFavicons(): Promise<number> {+ const manifestId = await this.run(this.writeManifestId());+ return this.run(this.execute(LogoQueries.UNLINK_FAVICONS, [this.now(), manifestId, manifestId, LogoKinds.Favicon]));+ }+/*** Convert image data from various formats to Uint8Array.* @param image The image data in various possible formatsdiff --git a/core/client/src/database/repositories/StorageInsightsRepository.ts b/core/client/src/database/repositories/StorageInsightsRepository.tsnew file mode 100644index 000000000..cfcb4a833--- /dev/null+++ b/core/client/src/database/repositories/StorageInsightsRepository.ts@@ -0,0 +1,76 @@+import { BaseRepository } from '../BaseRepository';+import { StorageInsightsQueries } from '../queries/StorageInsightsQueries';++import type { DbOp } from '../DbOp';+import type { LogoKind } from '@aliasvault/models/vault';++/**+ * How many items, attachments and logos the local vault holds.+ */+export type StorageCounts = {+ ItemCount: number;+ ItemsWithAttachments: number;+ ItemsWithLogos: number;+ AttachmentCount: number;+ LogoCount: number;+};++/**+ * One attachment in the largest-attachments list.+ */+export type AttachmentSizeRow = {+ Id: string;+ Filename: string;+ SizeBytes: number;+ ItemId: string;+ ManifestId: string;+ ItemName: string | null;+ CreatedAt: string;+};++/**+ * One logo in the largest-logos list.+ */+export type LogoSizeRow = {+ Id: string;+ ManifestId: string;+ Kind: LogoKind;+ Source: string;+ Name: string | null;+ SizeBytes: number;+ ItemCount: number;+ /** An item showing this logo, to link to; null when no item uses it. */+ FirstItemId: string | null;+};++/**+ * Read-only storage statistics of the local vault, for the storage insights screen.+ */+export class StorageInsightsRepository extends BaseRepository {+ /**+ * Item, attachment and logo counts.+ * @returns The counts+ */+ public *getCounts(): DbOp<StorageCounts> {+ const row = (yield* this.query<StorageCounts>(StorageInsightsQueries.GET_COUNTS))[0];+ return row ?? { ItemCount: 0, ItemsWithAttachments: 0, ItemsWithLogos: 0, AttachmentCount: 0, LogoCount: 0 };+ }++ /**+ * The largest attachments, biggest first.+ * @param limit - The number of rows+ * @returns The attachments+ */+ public *getLargestAttachments(limit: number): DbOp<AttachmentSizeRow[]> {+ return yield* this.query<AttachmentSizeRow>(StorageInsightsQueries.GET_LARGEST_ATTACHMENTS, [limit]);+ }++ /**+ * The largest logos, biggest first.+ * @param limit - The number of rows+ * @returns The logos+ */+ public *getLargestLogos(limit: number): DbOp<LogoSizeRow[]> {+ return yield* this.query<LogoSizeRow>(StorageInsightsQueries.GET_LARGEST_LOGOS, [limit]);+ }+}diff --git a/core/client/src/items/BulkFaviconService.ts b/core/client/src/items/BulkFaviconService.tsindex ef015443c..f8b866b56 100644--- a/core/client/src/items/BulkFaviconService.ts+++ b/core/client/src/items/BulkFaviconService.ts@@ -1,7 +1,9 @@import { ApiRequestError } from '../api/errors/ApiRequestError';+import { selectFaviconTarget } from '../rust/RustCore';import { base64ToBytes } from '../utilities/Base64';import type { WebApiService } from '../api/WebApiService';+import type SqliteClient from '../database/SqliteClient';import type { FaviconTarget } from '../rust/RustCore';/**@@ -32,7 +34,16 @@ type FaviconExtractBatchResult = {const BATCH_SIZE = 10;/**- * Bulk favicon extraction for imports. Single-item favicon logic lives in FaviconService.+ * Result of re-downloading every favicon of the personal vault.+ */+export type FaviconRefreshResult = {+ status: BulkExtractStatus;+ /** The number of items whose logo changed. */+ changedItems: number;+};++/**+ * Bulk favicon extraction for imports and vault-wide refreshes. Single-item favicon logic lives in FaviconService.*/export class BulkFaviconService {/**@@ -94,4 +105,36 @@ export class BulkFaviconService {return { favicons, status: 'completed' };}++ /**+ * Re-download the favicon of every item in the personal vault that shows one or none, and point each item at+ * the favicon of its current domain.+ * @param client The vault database+ * @param webApi The WebAPI service for the favicon requests+ * @param onProgress Called with the number of domains processed and the total+ * @param signal Aborts the extraction between batches+ * @returns The extraction outcome and the number of items whose logo changed+ */+ public static async refreshVaultFavicons(client: SqliteClient, webApi: WebApiService, onProgress?: (processed: number, total: number) => Promise<void> | void, signal?: AbortSignal): Promise<FaviconRefreshResult> {+ const candidates = client.logos.getFaviconRefreshCandidates();+ const assignments: { candidate: typeof candidates[number]; source: string | null }[] = [];+ const targetsBySource = new Map<string, FaviconTarget>();+ for (const candidate of candidates) {+ const target = await selectFaviconTarget(candidate.Urls);+ assignments.push({ candidate, source: target?.source ?? null });+ if (target && !targetsBySource.has(target.source)) {+ targetsBySource.set(target.source, target);+ }+ }++ const total = targetsBySource.size;+ await onProgress?.(0, total);+ const result = await BulkFaviconService.extractBulk(webApi, [...targetsBySource.values()], processed => onProgress?.(processed, total), signal);+ if (result.status === 'cancelled') {+ return { status: result.status, changedItems: 0 };+ }++ const changedItems = await client.logos.applyFaviconRefresh(assignments, result.favicons);+ return { status: result.status, changedItems };+ }}diff --git a/core/models/src/webapi/StorageStatistics.ts b/core/models/src/webapi/StorageStatistics.tsnew file mode 100644index 000000000..03e7d2562--- /dev/null+++ b/core/models/src/webapi/StorageStatistics.ts@@ -0,0 +1,27 @@+/**+ * Count and encrypted size of the blobs in one category.+ */+export type BlobCategoryStatistics = {+ /** The blob category, e.g. "favicon" or "attachment". */+ category: string;+ count: number;+ bytes: number;+}++/**+ * Encrypted bytes the server stores for one manifest.+ */+export type ManifestStorageStatistics = {+ manifestId: string;+ isPersonal: boolean;+ manifestBytes: number;+ bucketBytes: number;+ blobs: BlobCategoryStatistics[];+}++/**+ * Exact server-side storage of every manifest the caller can access (GET v2/Vault/storage).+ */+export type StorageStatisticsResponse = {+ manifests: ManifestStorageStatistics[];+}diff --git a/core/models/src/webapi/index.ts b/core/models/src/webapi/index.tsindex 68016b81b..cb1c7a289 100644--- a/core/models/src/webapi/index.ts+++ b/core/models/src/webapi/index.ts@@ -31,3 +31,4 @@ export * from './ManifestKeyType';export * from './VaultKeyAlgorithm';export * from './Groups';export * from './CapabilityKeys';+export * from './StorageStatistics';