Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable25] fix(tags performance): Cache tag counts on server and load them using initial state #1620

Merged
merged 4 commits into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@
use OCA\Photos\Listener\SabrePluginAuthInitListener;
use OCA\DAV\Connector\Sabre\Principal;
use OCA\Photos\Listener\NodeDeletedListener;
use OCA\Photos\Listener\TagListener;
use OCA\Photos\Listener\GroupUserRemovedListener;
use OCA\Photos\Listener\GroupDeletedListener;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\Files\Events\Node\NodeDeletedEvent;
use OCP\SystemTag\MapperEvent;
use OCP\Group\Events\UserRemovedEvent;
use OCP\Group\Events\GroupDeletedEvent;

Expand Down Expand Up @@ -69,14 +71,17 @@ public function __construct() {
public function register(IRegistrationContext $context): void {
/** Register $principalBackend for the DAV collection */
$context->registerServiceAlias('principalBackend', Principal::class);

$context->registerEventListener(NodeDeletedEvent::class, NodeDeletedListener::class);

$context->registerEventListener(UserRemovedEvent::class, GroupUserRemovedListener::class);

$context->registerEventListener(GroupDeletedEvent::class, GroupDeletedListener::class);

$context->registerEventListener(SabrePluginAuthInitEvent::class, SabrePluginAuthInitListener::class);

$context->registerEventListener(MapperEvent::EVENT_ASSIGN, TagListener::class);
$context->registerEventListener(MapperEvent::EVENT_UNASSIGN, TagListener::class);
}

public function boot(IBootContext $context): void {
Expand Down
44 changes: 35 additions & 9 deletions lib/Controller/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
use OCP\AppFramework\Services\IInitialState;
use OCP\IRequest;
use OCP\IUserSession;
use OCP\SystemTag\ISystemTagManager;
use OCP\SystemTag\ISystemTagObjectMapper;
use OCP\Util;
use Psr\Log\LoggerInterface;

Expand All @@ -62,18 +64,25 @@ class PageController extends Controller {
private IRootFolder $rootFolder;
private ICacheFactory $cacheFactory;
private ICache $nomediaPathsCache;
private ICache $tagCountsCache;
private LoggerInterface $logger;

private ISystemTagObjectMapper $tagObjectMapper;

private ISystemTagManager $tagManager;

public function __construct(
IRequest $request,
IAppManager $appManager,
IEventDispatcher $eventDispatcher,
IRequest $request,
IAppManager $appManager,
IEventDispatcher $eventDispatcher,
UserConfigService $userConfig,
IInitialState $initialState,
IUserSession $userSession,
IRootFolder $rootFolder,
ICacheFactory $cacheFactory,
LoggerInterface $logger
IInitialState $initialState,
IUserSession $userSession,
IRootFolder $rootFolder,
ICacheFactory $cacheFactory,
LoggerInterface $logger,
ISystemTagObjectMapper $tagObjectMapper,
ISystemTagManager $tagManager
) {
parent::__construct(Application::APP_ID, $request);

Expand All @@ -85,7 +94,10 @@ public function __construct(
$this->rootFolder = $rootFolder;
$this->cacheFactory = $cacheFactory;
$this->nomediaPathsCache = $this->cacheFactory->createLocal('photos:nomedia-paths');
$this->tagCountsCache = $this->cacheFactory->createLocal('photos:tag-counts');
$this->logger = $logger;
$this->tagObjectMapper = $tagObjectMapper;
$this->tagManager = $tagManager;
}

/**
Expand Down Expand Up @@ -125,14 +137,28 @@ public function index(): TemplateResponse {
$paths = array_map(function (Node $node) use ($userFolder) {
return substr(dirname($node->getPath()), strlen($userFolder->getPath()));
}, $search);
$this->nomediaPathsCache->set($key, $paths, 60 * 60 * 24 * 28);
$this->nomediaPathsCache->set($key, $paths, 60 * 60 * 24 * 28); // 28 days
}
} catch (InvalidPathException | NotFoundException | NotPermittedException | NoUserException $e) {
$this->logger->error($e->getMessage());
}

$this->initialState->provideInitialState('nomedia-paths', $paths);


$key = $user->getUID();
$tagCounts = $this->tagCountsCache->get($key);
if ($tagCounts === null) {
$tags = $this->tagManager->getAllTags(true);
$tagCounts = [];
foreach ($tags as $tag) {
$search = $userFolder->search(new SearchQuery(new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'systemtag', $tag->getName()), 0, 0, [], $user));
$tagCounts[$tag->getName()] = count($search);
}
$this->tagCountsCache->set($key, $tagCounts, 60 * 60 * 24 * 7); // 7 days
}
$this->initialState->provideInitialState('tag-counts', $tagCounts);

Util::addScript(Application::APP_ID, 'photos-main');

if ($this->appManager->isEnabledForUser('recognize') === true) {
Expand Down
44 changes: 44 additions & 0 deletions lib/Listener/TagListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace OCA\Photos\Listener;

use OCP\EventDispatcher\Event;
use OCP\Files\Config\ICachedMountInfo;
use OCP\Files\Config\IUserMountCache;
use OCP\Files\IRootFolder;
use OCP\ICacheFactory;
use OCP\SystemTag\MapperEvent;

class TagListener implements \OCP\EventDispatcher\IEventListener {
private \OCP\ICache $tagCountsCache;
private IRootFolder $rootFolder;
private IUserMountCache $userMountCache;

public function __construct(ICacheFactory $cacheFactory, IRootFolder $rootFolder, IUserMountCache $userMountCache) {
$this->tagCountsCache = $cacheFactory->createLocal('photos:tag-counts');
$this->rootFolder = $rootFolder;
$this->userMountCache = $userMountCache;
}

/**
* @inheritDoc
*/
public function handle(Event $event): void {
if ($event instanceof MapperEvent) {
if ($event->getObjectType() !== 'files') {
return;
}
$node = current($this->rootFolder->getById((int)$event->getObjectId()));
if (!$node) {
return;
}
$mounts = $this->userMountCache->getMountsForRootId($node->getMountPoint()->getStorageRootId());
$userIds = array_map(static function (ICachedMountInfo $mount) {
return $mount->getUser()->getUID();
}, $mounts);
foreach ($userIds as $userId) {
$this->tagCountsCache->remove($userId);
}
}
}
}
20 changes: 14 additions & 6 deletions src/components/TagCover.vue
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
</h3>
</div>
<div class="tag-cover__details__second-line">
{{ n('photos', '%n photo', '%n photos', tag.files.length,) }}
{{ n('photos', '%n photo', '%n photos', count) }}
</div>
</div>
</router-link>
Expand All @@ -50,6 +50,7 @@ import ImageMultipleIcon from 'vue-material-design-icons/ImageMultiple'
import { generateUrl } from '@nextcloud/router'

import AbortControllerMixin from '../mixins/AbortControllerMixin.js'
import { loadState } from '@nextcloud/initial-state'

export default {
name: 'TagCover',
Expand All @@ -73,6 +74,7 @@ export default {
return {
loadCover: false,
observer: null,
tagCounts: loadState('photos', 'tag-counts'),
}
},

Expand All @@ -92,13 +94,19 @@ export default {
}
return generateUrl(`/core/preview?fileId=${this.tag.files[this.tag.files.length - 1]}&x=${512}&y=${512}&forceIcon=0&a=1`)
},

count() {
return this.tag.files.length || this.tagCounts[this.tag.displayName]
},
},

async created() {
if (this.tag.files.length) {
return
}
this.$store.dispatch('fetchTagFiles', { id: this.tag.id, signal: this.abortController.signal })
watch: {
loadCover() {
if (this.tag.files.length) {
return
}
this.$store.dispatch('fetchTagFiles', { id: this.tag.id, signal: this.abortController.signal })
},
},

mounted() {
Expand Down
27 changes: 9 additions & 18 deletions src/views/Tags.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<!-- Errors handlers-->
<NcEmptyContent v-if="error" :title="t('photos', 'An error occurred')" />

<NcEmptyContent v-if="!loading && !hasTagsWithFiles" :title="t('photos', 'No tags yet')" :description="t('photos', 'Photos with tags will show up here')" />
<NcEmptyContent v-if="!loading && tagsList.length === 0" :title="t('photos', 'No tags yet')" :description="t('photos', 'Photos with tags will show up here')" />

<NcLoadingIcon v-if="loading" class="loader" />

Expand All @@ -51,6 +51,7 @@
import { mapGetters } from 'vuex'

import { NcEmptyContent, NcLoadingIcon } from '@nextcloud/vue'
import { loadState } from '@nextcloud/initial-state'

import TagCover from '../components/TagCover.vue'
import AbortControllerMixin from '../mixins/AbortControllerMixin.js'
Expand All @@ -69,6 +70,7 @@ export default {
error: null,
loading: false,
showTags: false,
tagCounts: loadState('photos', 'tag-counts'),
}
},

Expand All @@ -87,14 +89,11 @@ export default {
},

popularTags() {
return Object.values(this.tags)
.filter(tag => tag.files && tag.files.length > 50)
.sort((a, b) => b.files.length - a.files.length)
return Object.keys(this.tagsNames)
.filter(tagName => (this.tags[this.tagsNames[tagName]].files.length || this.tagCounts[tagName]) > 50)
.sort((a, b) => (this.tags[this.tagsNames[b]].files.length || this.tagCounts[b]) - (this.tags[this.tagsNames[a]].files.length || this.tagCounts[a]))
.slice(0, 9)
},

hasTagsWithFiles() {
return Object.values(this.tags).some(tag => !!tag.files.length)
.map(tagName => this.tags[this.tagsNames[tagName]])
},
},

Expand All @@ -107,25 +106,16 @@ export default {
// close any potential opened viewer
OCA.Viewer.close()

// if we don't already have some cached data let's show a loader
if (!this.tags[this.tagId]) {
this.loading = true
}
this.error = null

try {
// fetch content
if (!this.tagsList.length) {
this.loading = true
await this.$store.dispatch('fetchAllTags', {
signal: this.abortController.signal,
})
}
if (!this.hasTagsWithFiles) {
await Promise.all(this.tagsList.slice(0, 15).map(tag => this.$store.dispatch('fetchTagFiles', {
id: tag.id,
signal: this.abortController.signal,
})))
}
} catch (error) {
console.error(error)
this.error = true
Expand All @@ -135,6 +125,7 @@ export default {
}
},
},

}
</script>

Expand Down
13 changes: 9 additions & 4 deletions tests/psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="4.26.0@6998fabb2bf528b65777bf9941920888d23c03ac">
<files psalm-version="4.29.0@7ec5ffbd5f68ae03782d7fd33fff0c45a69f95b3">
<file src="lib/Controller/AlbumsController.php">
<UndefinedClass occurrences="2">
<code>SharedStorage</code>
<code>\OCA\GroupFolders\Mount\GroupFolderStorage</code>
</UndefinedClass>
</file>
<file src="lib/Controller/PageController.php">
<UndefinedClass occurrences="10">
<InvalidArgument occurrences="1">
<code>new SearchQuery(new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'systemtag', $tag-&gt;getName()), 0, 0, [], $user)</code>
</InvalidArgument>
<UndefinedClass occurrences="12">
<code>$e</code>
<code>LoadSidebar</code>
<code>LoadSidebar</code>
Expand All @@ -17,13 +20,15 @@
<code>SearchBinaryOperator</code>
<code>SearchComparison</code>
<code>SearchComparison</code>
<code>SearchComparison</code>
<code>SearchQuery</code>
<code>SearchQuery</code>
</UndefinedClass>
</file>
<file src="lib/Controller/PublicAlbumController.php">
<UndefinedClass occurrences="10">
<UndefinedClass occurrences="2">
<code>LoadViewer</code>
<code>LoadViewer</code>
</UndefinedClass>
</file>
</files>
</files>