Skip to content

Commit

Permalink
add new endpoint to retrieve archived snapshot list
Browse files Browse the repository at this point in the history
  • Loading branch information
dragonpoo authored and ludomikula committed Oct 17, 2024
1 parent 449f1fd commit 4d8ac38
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.lowcoder.domain.application.repository;

import org.lowcoder.domain.application.model.ApplicationHistorySnapshot;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.repository.Query;
import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
import org.springframework.stereotype.Repository;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.time.Instant;

@Repository
public interface ApplicationHistoryArchivedSnapshotRepository extends ReactiveMongoRepository<ApplicationHistorySnapshot, String> {

@Query(value = "{ 'applicationId': ?0, $and: [" +
"{$or: [ { 'context.operations': { $elemMatch: { 'compName': ?1 } } }, { $expr: { $eq: [?1, null] } } ]}, " +
"{$or: [ { 'dsl.settings.themeId': ?2 }, { $expr: { $eq: [?2, null] } } ] }, " +
"{$or: [ { 'createdAt': { $gte: ?3} }, { $expr: { $eq: [?3, null] } } ] }, " +
"{$or: [ { 'createdAt': { $lte: ?4} }, { $expr: { $eq: [?4, null] } } ] } " +
"]}",
fields = "{applicationId : 1, context: 1, createdBy : 1, createdAt : 1}")
Flux<ApplicationHistorySnapshot> findAllByApplicationId(String applicationId, String compName, String theme, Instant createdAtFrom, Instant createdAtTo, Pageable pageable);

Mono<Long> countByApplicationId(String applicationId);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.lowcoder.domain.application.service;

import org.lowcoder.domain.application.model.ApplicationHistorySnapshot;
import org.lowcoder.domain.application.model.ApplicationHistorySnapshotTS;
import org.springframework.data.domain.PageRequest;
import reactor.core.publisher.Mono;
Expand All @@ -13,8 +14,11 @@ public interface ApplicationHistorySnapshotService {
Mono<Boolean> createHistorySnapshot(String applicationId, Map<String, Object> dsl, Map<String, Object> context, String userId);

Mono<List<ApplicationHistorySnapshotTS>> listAllHistorySnapshotBriefInfo(String applicationId, String compName, String theme, Instant from, Instant to, PageRequest pageRequest);
Mono<List<ApplicationHistorySnapshot>> listAllHistorySnapshotBriefInfoArchived(String applicationId, String compName, String theme, Instant from, Instant to, PageRequest pageRequest);

Mono<Long> countByApplicationId(String applicationId);

Mono<ApplicationHistorySnapshotTS> getHistorySnapshotDetail(String historySnapshotId);

Mono<ApplicationHistorySnapshot> getHistorySnapshotDetailArchived(String historySnapshotId);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.lowcoder.domain.application.service.impl;

import lombok.RequiredArgsConstructor;
import org.lowcoder.domain.application.model.ApplicationHistorySnapshot;
import org.lowcoder.domain.application.model.ApplicationHistorySnapshotTS;
import org.lowcoder.domain.application.repository.ApplicationHistoryArchivedSnapshotRepository;
import org.lowcoder.domain.application.repository.ApplicationHistorySnapshotRepository;
import org.lowcoder.domain.application.service.ApplicationHistorySnapshotService;
import org.lowcoder.sdk.exception.BizError;
Expand All @@ -23,6 +25,7 @@
public class ApplicationHistorySnapshotServiceImpl implements ApplicationHistorySnapshotService {

private final ApplicationHistorySnapshotRepository repository;
private final ApplicationHistoryArchivedSnapshotRepository repositoryArchived;

@Override
public Mono<Boolean> createHistorySnapshot(String applicationId, Map<String, Object> dsl, Map<String, Object> context, String userId) {
Expand All @@ -42,6 +45,13 @@ public Mono<List<ApplicationHistorySnapshotTS>> listAllHistorySnapshotBriefInfo(
.onErrorMap(Exception.class, e -> ofException(BizError.FETCH_HISTORY_SNAPSHOT_FAILURE, "FETCH_HISTORY_SNAPSHOT_FAILURE"));
}

@Override
public Mono<List<ApplicationHistorySnapshot>> listAllHistorySnapshotBriefInfoArchived(String applicationId, String compName, String theme, Instant from, Instant to, PageRequest pageRequest) {
return repositoryArchived.findAllByApplicationId(applicationId, compName, theme, from, to, pageRequest.withSort(Direction.DESC, "id"))
.collectList()
.onErrorMap(Exception.class, e -> ofException(BizError.FETCH_HISTORY_SNAPSHOT_FAILURE, "FETCH_HISTORY_SNAPSHOT_FAILURE"));
}

@Override
public Mono<Long> countByApplicationId(String applicationId) {
return repository.countByApplicationId(applicationId)
Expand All @@ -55,4 +65,11 @@ public Mono<ApplicationHistorySnapshotTS> getHistorySnapshotDetail(String histor
return repository.findById(historySnapshotId)
.switchIfEmpty(deferredError(INVALID_HISTORY_SNAPSHOT, "INVALID_HISTORY_SNAPSHOT", historySnapshotId));
}


@Override
public Mono<ApplicationHistorySnapshot> getHistorySnapshotDetailArchived(String historySnapshotId) {
return repositoryArchived.findById(historySnapshotId)
.switchIfEmpty(deferredError(INVALID_HISTORY_SNAPSHOT, "INVALID_HISTORY_SNAPSHOT", historySnapshotId));
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
package org.lowcoder.api.application;

import static org.lowcoder.api.util.ViewBuilder.multiBuild;

import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.google.common.collect.ImmutableMap;
import lombok.RequiredArgsConstructor;
import org.lowcoder.api.application.view.HistorySnapshotDslView;
import org.lowcoder.api.framework.view.ResponseView;
import org.lowcoder.api.home.SessionUserService;
import org.lowcoder.api.util.Pagination;
import org.lowcoder.domain.application.model.Application;
import org.lowcoder.domain.application.model.ApplicationHistorySnapshot;
import org.lowcoder.domain.application.model.ApplicationHistorySnapshotTS;
import org.lowcoder.domain.application.service.ApplicationHistorySnapshotService;
import org.lowcoder.domain.application.service.ApplicationService;
Expand All @@ -22,11 +18,14 @@
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

import com.google.common.collect.ImmutableMap;
import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import lombok.RequiredArgsConstructor;
import reactor.core.publisher.Mono;
import static org.lowcoder.api.util.ViewBuilder.multiBuild;

@RequiredArgsConstructor
@RestController
Expand Down Expand Up @@ -90,6 +89,43 @@ public Mono<ResponseView<Map<String, Object>>> listAllHistorySnapshotBriefInfo(@
.map(ResponseView::success);
}

@Override
public Mono<ResponseView<Map<String, Object>>> listAllHistorySnapshotBriefInfoArchived(@PathVariable String applicationId,
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "10") int size,
@RequestParam String compName,
@RequestParam String theme,
@RequestParam Instant from,
@RequestParam Instant to) {

Pagination pagination = Pagination.of(page, size).check();

return sessionUserService.getVisitorId()
.delayUntil(visitor -> resourcePermissionService.checkResourcePermissionWithError(visitor, applicationId,
ResourceAction.EDIT_APPLICATIONS))
.flatMap(__ -> applicationHistorySnapshotService.listAllHistorySnapshotBriefInfoArchived(applicationId, compName, theme, from, to, pagination.toPageRequest()))
.flatMap(snapshotList -> {
Mono<List<ApplicationHistorySnapshotBriefInfo>> snapshotBriefInfoList = multiBuild(snapshotList,
ApplicationHistorySnapshot::getCreatedBy,
userService::getByIds,
(applicationHistorySnapshot, user) -> new ApplicationHistorySnapshotBriefInfo(
applicationHistorySnapshot.getId(),
applicationHistorySnapshot.getContext(),
applicationHistorySnapshot.getCreatedBy(),
user.getName(),
user.getAvatarUrl(),
applicationHistorySnapshot.getCreatedAt().toEpochMilli()
)
);

Mono<Long> applicationHistorySnapshotCount = applicationHistorySnapshotService.countByApplicationId(applicationId);

return Mono.zip(snapshotBriefInfoList, applicationHistorySnapshotCount)
.map(tuple -> ImmutableMap.of("list", tuple.getT1(), "count", tuple.getT2()));
})
.map(ResponseView::success);
}

@Override
public Mono<ResponseView<HistorySnapshotDslView>> getHistorySnapshotDsl(@PathVariable String applicationId,
@PathVariable String snapshotId) {
Expand All @@ -98,7 +134,29 @@ public Mono<ResponseView<HistorySnapshotDslView>> getHistorySnapshotDsl(@PathVar
ResourceAction.EDIT_APPLICATIONS))
.flatMap(__ -> applicationHistorySnapshotService.getHistorySnapshotDetail(snapshotId))
.map(ApplicationHistorySnapshotTS::getDsl)
.zipWhen(dsl -> applicationService.getAllDependentModulesFromDsl(dsl))
.zipWhen(applicationService::getAllDependentModulesFromDsl)
.map(tuple -> {
Map<String, Object> applicationDsl = tuple.getT1();
List<Application> dependentModules = tuple.getT2();
Map<String, Map<String, Object>> dependentModuleDsl = dependentModules.stream()
.collect(Collectors.toMap(Application::getId, Application::getLiveApplicationDsl, (a, b) -> b));
return HistorySnapshotDslView.builder()
.applicationsDsl(applicationDsl)
.moduleDSL(dependentModuleDsl)
.build();
})
.map(ResponseView::success);
}

@Override
public Mono<ResponseView<HistorySnapshotDslView>> getHistorySnapshotDslArchived(@PathVariable String applicationId,
@PathVariable String snapshotId) {
return sessionUserService.getVisitorId()
.delayUntil(visitor -> resourcePermissionService.checkResourcePermissionWithError(visitor, applicationId,
ResourceAction.EDIT_APPLICATIONS))
.flatMap(__ -> applicationHistorySnapshotService.getHistorySnapshotDetailArchived(snapshotId))
.map(ApplicationHistorySnapshot::getDsl)
.zipWhen(applicationService::getAllDependentModulesFromDsl)
.map(tuple -> {
Map<String, Object> applicationDsl = tuple.getT1();
List<Application> dependentModules = tuple.getT2();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ public Mono<ResponseView<Map<String, Object>>> listAllHistorySnapshotBriefInfo(@
@RequestParam(required = false ) @Nullable Instant from,
@RequestParam(required = false ) @Nullable Instant to);

@Operation(
tags = TAG_APPLICATION_HISTORY_MANAGEMENT,
operationId = "listApplicationSnapshotsArchived",
summary = "List Archived Application Snapshots",
description = "Retrieve a list of Archived Snapshots associated with a specific Application within Lowcoder."
)
@GetMapping("/archive/{applicationId}")
public Mono<ResponseView<Map<String, Object>>> listAllHistorySnapshotBriefInfoArchived(@PathVariable String applicationId,
@RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "10") int size,
@RequestParam(required = false ) @Nullable String compName, @RequestParam(required = false ) @Nullable String theme,
@RequestParam(required = false ) @Nullable Instant from,
@RequestParam(required = false ) @Nullable Instant to);

@Operation(
tags = TAG_APPLICATION_HISTORY_MANAGEMENT,
operationId = "getApplicationSnapshot",
Expand All @@ -56,6 +69,16 @@ public Mono<ResponseView<Map<String, Object>>> listAllHistorySnapshotBriefInfo(@
public Mono<ResponseView<HistorySnapshotDslView>> getHistorySnapshotDsl(@PathVariable String applicationId,
@PathVariable String snapshotId);

@Operation(
tags = TAG_APPLICATION_HISTORY_MANAGEMENT,
operationId = "getApplicationSnapshotArchived",
summary = "Retrieve Archived Application Snapshot",
description = "Retrieve a specific Archived Application Snapshot within Lowcoder using the Application and Snapshot IDs."
)
@GetMapping("/archive/{applicationId}/{snapshotId}")
public Mono<ResponseView<HistorySnapshotDslView>> getHistorySnapshotDslArchived(@PathVariable String applicationId,
@PathVariable String snapshotId);

public record ApplicationHistorySnapshotBriefInfo(String snapshotId, Map<String, Object> context,
String userId, String userName,
String userAvatar, long createTime) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ common:
cookie:
max-age-in-hours: ${LOWCODER_COOKIE_MAX_AGE:24}
query:
app-snapshot-keep-duration: ${LOWCODER_QUERY_SNAPSHOT_ACTIVE_DURATION:30}
app-snapshot-keep-duration: ${LOWCODER_APP_SNAPSHOT_RETENTIONTIME:30}

debug: true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ common:
cookie:
max-age-in-hours: ${LOWCODER_COOKIE_MAX_AGE:24}
query:
app-snapshot-keep-duration: ${LOWCODER_QUERY_SNAPSHOT_ACTIVE_DURATION:30}
app-snapshot-keep-duration: ${LOWCODER_APP_SNAPSHOT_RETENTIONTIME:30}

material:
mongodb-grid-fs:
Expand Down

0 comments on commit 4d8ac38

Please sign in to comment.