Skip to content

Commit

Permalink
perf: heavily optimize getHangarProjectInfo, remove owner from hangar…
Browse files Browse the repository at this point in the history
… project
  • Loading branch information
MiniDigger committed Apr 24, 2024
1 parent 517f4f9 commit 98733f6
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,15 @@ public interface HangarProjectsDAO {
List<JoinableMember<ProjectRoleTable>> getProjectMembers(long projectId, Long userId, @Define boolean canSeePending);

@RegisterConstructorMapper(HangarProject.HangarProjectInfo.class)
@SqlQuery("SELECT count(DISTINCT pv.id) public_versions," +
" count(DISTINCT pf.id) flag_count," +
" count(DISTINCT ps.user_id) star_count," +
" count(DISTINCT pw.user_id) watcher_count," +
" count(DISTINCT pn.id) note_count" +
" FROM projects p" +
" LEFT JOIN project_versions pv ON p.id = pv.project_id AND pv.visibility = 0" +
" LEFT JOIN project_stars ps ON p.id = ps.project_id" +
" LEFT JOIN project_watchers pw ON p.id = pw.project_id" +
" LEFT JOIN project_flags pf ON p.id = pf.project_id" +
" LEFT JOIN project_notes pn ON p.id = pn.project_id" +
" WHERE p.id = :projectId" +
" GROUP BY p.id")
@SqlQuery("""
SELECT (SELECT count(DISTINCT pv.id) from project_versions pv where p.id = pv.project_id and pv.visibility = 0) public_versions,
(SELECT count(DISTINCT pf.id) from project_flags pf where p.id = pf.project_id) flag_count,
(SELECT count(DISTINCT ps.user_id) from project_stars ps where p.id = ps.project_id) star_count,
(SELECT count(DISTINCT pw.user_id) from project_watchers pw where p.id = pw.project_id) watcher_count,
(SELECT count(DISTINCT pn.id) from project_notes pn where p.id = pn.project_id) note_count
FROM projects p
WHERE p.id = :projectId
""")
HangarProject.HangarProjectInfo getHangarProjectInfo(long projectId);

@RegisterConstructorMapper(HangarChannel.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public long getId() {
return this.id;
}

@Override
public ProjectOwner getOwner() {
return this.owner;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

public interface Joinable<R extends ExtendedRoleTable<?, ?>> {

ProjectOwner getOwner();
default ProjectOwner getOwner2() {return null; }

RoleCategory getRoleCategory();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import io.papermc.hangar.model.api.project.version.PlatformVersionDownload;
import io.papermc.hangar.model.common.NamedPermission;
import io.papermc.hangar.model.common.Platform;
import io.papermc.hangar.model.db.projects.ProjectOwner;
import io.papermc.hangar.model.db.roles.ProjectRoleTable;
import io.papermc.hangar.model.identified.ProjectIdentified;
import io.papermc.hangar.model.internal.Joinable;
Expand All @@ -24,7 +23,6 @@

public class HangarProject extends Project implements Joinable<ProjectRoleTable>, ProjectIdentified {

private final ProjectOwner owner;
private final List<JoinableMember<ProjectRoleTable>> members;
private final String lastVisibilityChangeComment;
private final String lastVisibilityChangeUserName;
Expand All @@ -34,9 +32,8 @@ public class HangarProject extends Project implements Joinable<ProjectRoleTable>
private final List<PinnedVersion> pinnedVersions;
private final Map<Platform, HangarVersion> mainChannelVersions;

public HangarProject(final Project project, final ProjectOwner owner, final List<JoinableMember<ProjectRoleTable>> members, final String lastVisibilityChangeComment, final String lastVisibilityChangeUserName, final HangarProjectInfo info, final Collection<HangarProjectPage> pages, final List<PinnedVersion> pinnedVersions, final Map<Platform, HangarVersion> mainChannelVersions, final ExtendedProjectPage mainPage) {
public HangarProject(final Project project, final List<JoinableMember<ProjectRoleTable>> members, final String lastVisibilityChangeComment, final String lastVisibilityChangeUserName, final HangarProjectInfo info, final Collection<HangarProjectPage> pages, final List<PinnedVersion> pinnedVersions, final Map<Platform, HangarVersion> mainChannelVersions, final ExtendedProjectPage mainPage) {
super(project);
this.owner = owner;
this.members = members;
this.lastVisibilityChangeComment = lastVisibilityChangeComment;
this.lastVisibilityChangeUserName = lastVisibilityChangeUserName;
Expand All @@ -57,11 +54,6 @@ public long getProjectId() {
return this.id;
}

@Override
public ProjectOwner getOwner() {
return this.owner;
}

@Override
public RoleCategory getRoleCategory() {
return RoleCategory.PROJECT;
Expand Down Expand Up @@ -104,7 +96,6 @@ public ExtendedProjectPage getMainPage() {
public String toString() {
return "HangarProject{" +
"id=" + this.id +
", owner=" + this.owner +
", members=" + this.members +
", lastVisibilityChangeComment='" + this.lastVisibilityChangeComment + '\'' +
", lastVisibilityChangeUserName='" + this.lastVisibilityChangeUserName + '\'' +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,7 @@ public String getProjectUrlFromSlug(final String slug) {
}

public HangarProject getHangarProject(final String slug) {
//TODO All of this is dumb and needs to be redone into as little queries as possible
// Not with CompletableFuture
// TODO All of this is dumb and needs to be redone into as little queries as possible
final Long hangarUserId = this.getHangarUserId();
final Project project = this.hangarProjectsDAO.getProject(slug, hangarUserId);
final long projectId = project.getId();
Expand Down Expand Up @@ -166,27 +165,25 @@ public HangarProject getHangarProject(final String slug) {
}
}));

final CompletableFuture<HangarProject.HangarProjectInfo> info = this.supply(() -> this.hangarProjectsDAO.getHangarProjectInfo(projectId));
final CompletableFuture<Map<Long, HangarProjectPage>> pages = this.supply(() -> this.projectPageService.getProjectPages(projectId));
final HangarProject.HangarProjectInfo info = this.hangarProjectsDAO.getHangarProjectInfo(projectId);
final Map<Long, HangarProjectPage> pages = this.projectPageService.getProjectPages(projectId);
final CompletableFuture<List<HangarProject.PinnedVersion>> pinnedVersions = this.supply(() -> this.pinnedVersionService.getPinnedVersions(ownerName, project.getNamespace().getSlug(), projectId));
final CompletableFuture<ExtendedProjectPage> projectPage = this.supply(() -> this.projectPageService.getProjectHomePage(projectId));
final CompletableFuture<String> avatarUrl = this.supply(() -> this.avatarService.getProjectAvatarUrl(project.getId(), project.getNamespace().getOwner()));
final CompletableFuture<ProjectOwner> projectOwner = this.supply(() -> this.getProjectOwner(ownerName));
final ExtendedProjectPage projectPage = this.projectPageService.getProjectHomePage(projectId);
final String avatarUrl = this.avatarService.getProjectAvatarUrl(project.getId(), project.getNamespace().getOwner());

mainChannelFuture.join();
final HangarProject hangarProject = new HangarProject(
project,
projectOwner.join(),
membersFuture.join(),
lastVisibilityChangeComment,
lastVisibilityChangeUserName,
info.join(),
pages.join().values(),
info,
pages.values(),
pinnedVersions.join(),
mainChannelVersions,
projectPage.join()
projectPage
);
hangarProject.setAvatarUrl(avatarUrl.join());
hangarProject.setAvatarUrl(avatarUrl);
return hangarProject;
}

Expand Down
3 changes: 1 addition & 2 deletions frontend/src/components/projects/MemberList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ const props = withDefaults(
organization?: boolean;
author?: string;
slug?: string;
owner: number;
}>(),
{
organization: false,
Expand Down Expand Up @@ -52,7 +51,7 @@ const canLeave = computed<boolean>(() => {
return false;
}
return props.members.some((member) => member.user.id === authStore.user?.id && member.user.id !== props.owner);
return props.members.some((member) => member.user.id === authStore.user?.id && member.user.name !== props.author);
});
const canEdit = computed<boolean>(() => hasPerms(NamedPermission.EditSubjectSettings));
const saving = ref<boolean>(false);
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/projects/ProjectHeader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ function requiresConfirmation(): ConfirmationType {
<p class="sm:ml-1">{{ project.description }}</p>
</div>
<div class="flex flex-col justify-around lt-sm:items-center space-y-2 items-end justify-between flex-shrink-0">
<span v-if="Object.keys(project.mainChannelVersions).length !== 0" class="inline-flex items-center">
<span v-if="project.mainChannelVersions" class="inline-flex items-center">
<Tooltip v-if="requiresConfirmation() !== ConfirmationType.NO" :content="i18n.t(requiresConfirmation())">
<div class="mr-2 text-2xl">
<IconMdiAlert v-if="requiresConfirmation() === ConfirmationType.EXTERNAL_URL" />
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/pages/[user]/[project]/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ function createPinnedVersionUrl(version: PinnedVersion): string {
</Card>
</template>
<MemberList :members="project.members" :author="project.owner.name" :slug="project.name" :owner="project.owner.userId" class="overflow-visible" />
<MemberList v-if="project.members" :members="project.members" :author="project.namespace.owner" :slug="project.name" class="overflow-visible" />
</section>
</div>
</template>
8 changes: 1 addition & 7 deletions frontend/src/pages/[user]/[project]/settings/[...slug].vue
Original file line number Diff line number Diff line change
Expand Up @@ -407,12 +407,6 @@ useHead(useSeo(i18n.t("project.settings.title") + " | " + props.project.name, pr
</template>-->
</Tabs>
</Card>
<MemberList
:members="project.members"
:author="project.owner.name"
:slug="project.name"
:owner="project.owner.userId"
class="basis-full md:basis-3/12 h-max"
/>
<MemberList :members="project.members" :author="project.namespace.owner" :slug="project.name" class="basis-full md:basis-3/12 h-max" />
</div>
</template>
9 changes: 1 addition & 8 deletions frontend/src/pages/[user]/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,7 @@ useHead(useSeo(props.user.name, description, route, props.user.avatarUrl));
</span>
</Card>
</template>
<MemberList
v-else-if="organization"
:members="organization.members"
:roles="orgRoles"
organization
:author="user.name"
:owner="organization.owner.userId"
/>
<MemberList v-else-if="organization" :members="organization.members" :roles="orgRoles" organization :author="user.name" />
</div>
</div>
</div>
Expand Down

0 comments on commit 98733f6

Please sign in to comment.