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

Add PG indexes #4230

Merged
merged 4 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import jakarta.transaction.Transactional;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
Expand Down Expand Up @@ -171,18 +170,8 @@ public ResponseEntity<List<Project>> getProjects(
}

projects.forEach(project -> {
final List<AssetType> assetTypes = Arrays.asList(
AssetType.DATASET,
AssetType.MODEL,
AssetType.DOCUMENT,
AssetType.WORKFLOW
);

final RebacProject rebacProject = new RebacProject(project.getId(), reBACService);
final Schema.Permission permission = projectService.checkPermissionCanRead(
currentUserService.get().getId(),
project.getId()
);
projectService.checkPermissionCanRead(currentUserService.get().getId(), project.getId());

// Set the user permission for the project. If we are unable to get the user
// permission, we remove the project.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.Index;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotNull;
import java.io.Serial;
import java.util.UUID;
Expand All @@ -21,6 +23,15 @@
@Accessors(chain = true)
@TSModel
@Entity
@Table(
name = "project_asset",
indexes = {
@Index(name = "idx_asset_id", columnList = "assetId"),
@Index(name = "idx_asset_type", columnList = "assetType"),
@Index(name = "idx_project_id", columnList = "project_id"),
@Index(name = "idx_project_asset_count", columnList = "project_id, assetType, deletedOn")
}
)
public class ProjectAsset extends TerariumAsset {

@Serial
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.Index;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotNull;
import java.io.Serial;
import java.sql.Timestamp;
Expand All @@ -26,6 +28,13 @@
@EqualsAndHashCode(callSuper = true)
@TSModel
@Entity
@Table(
name = "notification_event",
indexes = {
@Index(name = "idx_notification_group_id", columnList = "notification_group_id"),
@Index(name = "idx_acknowledged_on", columnList = "acknowledgedOn")
}
)
public class NotificationEvent extends TerariumEntity {

@Serial
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.Index;
import jakarta.persistence.OneToMany;
import jakarta.persistence.OrderBy;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotNull;
import java.io.Serial;
import java.util.ArrayList;
Expand All @@ -24,6 +26,12 @@
@EqualsAndHashCode(callSuper = true)
@TSModel
@Entity
@Table(
name = "notification_group",
indexes = {
@Index(name = "idx_user_id", columnList = "userId"), @Index(name = "idx_created_on", columnList = "createdOn")
}
)
public class NotificationGroup extends TerariumEntity {

@Serial
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.NoRepositoryBean;
import software.uncharted.terarium.hmiserver.models.dataservice.model.ModelFramework;

@NoRepositoryBean
public interface PSCrudSoftDeleteRepository<T, ID> extends PSCrudRepository<T, ID> {
Expand All @@ -13,4 +14,6 @@ public interface PSCrudSoftDeleteRepository<T, ID> extends PSCrudRepository<T, I
Optional<T> getByIdAndDeletedOnIsNull(final ID id);

Page<T> findAllByPublicAssetIsTrueAndTemporaryIsFalseAndDeletedOnIsNull(final Pageable pageable);

List<ModelFramework> findAllByDeletedOnIsNull();
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
package software.uncharted.terarium.hmiserver.repository.data;

import java.util.List;
import java.util.Optional;
import java.util.UUID;
import org.springframework.stereotype.Repository;
import software.uncharted.terarium.hmiserver.models.dataservice.model.ModelFramework;
import software.uncharted.terarium.hmiserver.repository.PSCrudRepository;
import software.uncharted.terarium.hmiserver.repository.PSCrudSoftDeleteRepository;

@Repository
public interface FrameworkRepository extends PSCrudRepository<ModelFramework, UUID> {
List<ModelFramework> findAllByDeletedOnIsNull();

List<ModelFramework> findAllByIdInAndDeletedOnIsNull(final List<UUID> ids);

Optional<ModelFramework> getByIdAndDeletedOnIsNull(final UUID id);
}
public interface FrameworkRepository extends PSCrudSoftDeleteRepository<ModelFramework, UUID> {}
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,39 @@ public interface ProjectRepository extends PSCrudRepository<Project, UUID>, JpaS
Optional<Project> getByIdAndDeletedOnIsNull(final UUID id);

@Query(
"select " +
" p.id as id, " +
" p.createdOn as createdOn, " +
" p.updatedOn as updatedOn, " +
" p.deletedOn as deletedOn, " +
" p.description as description, " +
" p.fileNames as fileNames, " +
" p.name as name, " +
" p.overviewContent as overviewContent, " +
" p.publicAsset as publicAsset, " +
" p.temporary as temporary, " +
" p.thumbnail as thumbnail, " +
" p.userId as userId, " +
" p2.assetCount as assetCount, " +
" p2.assetType as assetType " +
"from " +
" Project p " +
"left join (" +
"select " +
" pa.project.id as projectId, " +
" pa.assetType as assetType, " +
" count(*) as assetCount " +
"from " +
" ProjectAsset pa " +
"where " +
" pa.deletedOn is null " +
"group by pa.project.id, pa.assetType " +
") as p2 " +
"on p.id = p2.projectId " +
"where " +
" p.id in (:ids) " +
" and p.deletedOn is null"
"""
select
p.id as id,
p.createdOn as createdOn,
p.updatedOn as updatedOn,
p.deletedOn as deletedOn,
p.description as description,
p.fileNames as fileNames,
p.name as name,
p.overviewContent as overviewContent,
p.publicAsset as publicAsset,
p.temporary as temporary,
p.thumbnail as thumbnail,
p.userId as userId,
p2.assetCount as assetCount,
p2.assetType as assetType
from
Project p
left join (
select
pa.project.id as projectId,
pa.assetType as assetType,
count(*) as assetCount
from
ProjectAsset pa
where
pa.deletedOn is null
group by pa.project.id, pa.assetType) as p2
on p.id = p2.projectId
where
p.id in (:ids)
and p.deletedOn is null
"""
)
List<ProjectAndAssetAggregate> findByIdsWithAssets(@Param("ids") final List<UUID> ids);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ public List<Project> getProjects(final List<UUID> ids) {

@Observed(name = "function_profile")
public List<Project> getActiveProjects(final List<UUID> ids) {
Map<UUID, Project> projectMap = new HashMap<>();
List<ProjectAndAssetAggregate> projectAggregates = projectRepository.findByIdsWithAssets(ids);
for (ProjectAndAssetAggregate aggregate : projectAggregates) {
final Map<UUID, Project> projectMap = new HashMap<>();
final List<ProjectAndAssetAggregate> projectAggregates = projectRepository.findByIdsWithAssets(ids);
for (final ProjectAndAssetAggregate aggregate : projectAggregates) {
if (projectMap.containsKey(aggregate.getId())) {
Project project = projectMap.get(aggregate.getId());
final Project project = projectMap.get(aggregate.getId());
addAssetCount(project, aggregate.getAssetType(), aggregate.getAssetCount());
} else {
Project project = new Project();
final Project project = new Project();
project.setId(aggregate.getId());
project.setCreatedOn(aggregate.getCreatedOn());
project.setUpdatedOn(aggregate.getUpdatedOn());
Expand All @@ -76,7 +76,7 @@ public List<Project> getActiveProjects(final List<UUID> ids) {
return new ArrayList<>(projectMap.values());
}

private void addAssetCount(Project project, String assetTypeName, Integer assetCount) {
private void addAssetCount(final Project project, final String assetTypeName, final Integer assetCount) {
if (AssetType.DATASET.name().equals(assetTypeName)) {
project.getMetadata().put("datasets-count", assetCount.toString());
}
Expand Down Expand Up @@ -129,6 +129,7 @@ public boolean delete(final UUID id) {
return true;
}

@Observed(name = "function_profile")
public boolean isProjectPublic(final UUID id) {
final Optional<Boolean> isPublic = projectRepository.findPublicAssetByIdNative(id);
if (isPublic.isEmpty()) {
Expand All @@ -137,6 +138,7 @@ public boolean isProjectPublic(final UUID id) {
return isPublic.get();
}

@Observed(name = "function_profile")
public Schema.Permission checkPermissionCanReadOrNone(final String userId, final UUID projectId)
throws ResponseStatusException {
try {
Expand All @@ -152,6 +154,7 @@ public Schema.Permission checkPermissionCanReadOrNone(final String userId, final
return Schema.Permission.NONE;
}

@Observed(name = "function_profile")
public Schema.Permission checkPermissionCanRead(final String userId, final UUID projectId)
throws ResponseStatusException {
try {
Expand All @@ -167,6 +170,7 @@ public Schema.Permission checkPermissionCanRead(final String userId, final UUID
throw new ResponseStatusException(HttpStatus.FORBIDDEN, messages.get("rebac.unauthorized-update"));
}

@Observed(name = "function_profile")
public Schema.Permission checkPermissionCanWrite(final String userId, final UUID projectId)
throws ResponseStatusException {
try {
Expand All @@ -182,6 +186,7 @@ public Schema.Permission checkPermissionCanWrite(final String userId, final UUID
throw new ResponseStatusException(HttpStatus.FORBIDDEN, messages.get("rebac.unauthorized-update"));
}

@Observed(name = "function_profile")
public Schema.Permission checkPermissionCanAdministrate(final String userId, final UUID projectId)
throws ResponseStatusException {
try {
Expand Down
Loading
Loading