Skip to content

Audit log before after detail #1705

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

Merged
merged 9 commits into from
May 24, 2025
Prev Previous commit
Next Next commit
Added before/after detail of library query update event
  • Loading branch information
dragonpoo committed May 24, 2025
commit a775b3cb89d5b4f7730f70f39aa94685a1f2bed6
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class LibraryQueryEvent extends AbstractEvent {
private String id;
private String name;
private EventType eventType;
private String oldName;

@Override
public EventType getEventType() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.lowcoder.infra.event;

import lombok.Getter;
import lombok.experimental.SuperBuilder;

@Getter
@SuperBuilder
public class LibraryQueryPublishEvent extends AbstractEvent {

private String id;
private String oldVersion;
private String newVersion;
private EventType eventType;

@Override
public EventType getEventType() {
return eventType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;

import org.lowcoder.api.datasource.UpsertDatasourceRequest;
import org.lowcoder.api.framework.view.PageResponseView;
import org.lowcoder.api.framework.view.ResponseView;
import org.lowcoder.api.query.view.LibraryQueryAggregateView;
Expand All @@ -11,7 +12,10 @@
import org.lowcoder.api.query.view.UpsertLibraryQueryRequest;
import org.lowcoder.api.util.BusinessEventPublisher;
import org.lowcoder.api.util.GidService;
import org.lowcoder.domain.datasource.model.Datasource;
import org.lowcoder.domain.query.model.LibraryQuery;
import org.lowcoder.domain.query.model.LibraryQueryRecord;
import org.lowcoder.domain.query.service.LibraryQueryRecordService;
import org.lowcoder.domain.query.service.LibraryQueryService;
import org.lowcoder.plugin.api.event.LowcoderEvent.EventType;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -22,8 +26,10 @@

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.util.function.Tuple2;

import static org.lowcoder.api.util.Pagination.fluxToPageResponseView;
import static org.lowcoder.plugin.api.event.LowcoderEvent.EventType.DATA_SOURCE_UPDATE;

@RestController
public class LibraryQueryController implements LibraryQueryEndpoints
Expand All @@ -37,6 +43,8 @@ public class LibraryQueryController implements LibraryQueryEndpoints
private BusinessEventPublisher businessEventPublisher;
@Autowired
private GidService gidService;
@Autowired
private LibraryQueryRecordService libraryQueryRecordService;

@Override
public Mono<ResponseView<List<LibraryQueryAggregateView>>> dropDownList(@RequestParam(required = false, defaultValue = "") String name) {
Expand Down Expand Up @@ -64,16 +72,20 @@ public Mono<ResponseView<LibraryQueryView>> create(@RequestBody LibraryQuery lib
return libraryQueryApiService.create(libraryQuery)
.delayUntil(libraryQueryView ->
businessEventPublisher.publishLibraryQueryEvent(libraryQueryView.id(), libraryQueryView.name(),
EventType.LIBRARY_QUERY_CREATE))
EventType.LIBRARY_QUERY_CREATE, null))
.map(ResponseView::success);
}

@Override
public Mono<ResponseView<Boolean>> update(@PathVariable String libraryQueryId,
@RequestBody UpsertLibraryQueryRequest upsertLibraryQueryRequest) {
@RequestBody UpsertLibraryQueryRequest request) {
return gidService.convertLibraryQueryIdToObjectId(libraryQueryId).flatMap(objectId ->
libraryQueryApiService.update(objectId, upsertLibraryQueryRequest)
.map(ResponseView::success));
libraryQueryService.getById(objectId).flatMap(orgLibraryQuery ->
libraryQueryApiService.update(objectId, request)
.zipWith( libraryQueryService.getById(objectId))
.delayUntil(tuple -> businessEventPublisher.publishLibraryQueryEvent(tuple.getT2().getId(), tuple.getT2().getName(), EventType.LIBRARY_QUERY_UPDATE, orgLibraryQuery.getName()))
.map(Tuple2::getT1)
.map(ResponseView::success)));
}

@Override
Expand All @@ -82,18 +94,19 @@ public Mono<ResponseView<Boolean>> delete(@PathVariable String libraryQueryId) {
libraryQueryService.getById(objectId)
.delayUntil(__ -> libraryQueryApiService.delete(objectId))
.delayUntil(libraryQuery -> businessEventPublisher.publishLibraryQueryEvent(libraryQuery.getId(), libraryQuery.getName(),
EventType.LIBRARY_QUERY_DELETE))
EventType.LIBRARY_QUERY_DELETE, libraryQuery.getName()))
.thenReturn(ResponseView.success(true)));
}

@Override
public Mono<ResponseView<LibraryQueryRecordMetaView>> publish(@PathVariable String libraryQueryId,
@RequestBody LibraryQueryPublishRequest libraryQueryPublishRequest) {
return gidService.convertLibraryQueryIdToObjectId(libraryQueryId).flatMap(objectId ->
libraryQueryApiService.publish(objectId, libraryQueryPublishRequest)
.delayUntil(__ -> libraryQueryService.getById(objectId)
.flatMap(libraryQuery -> businessEventPublisher.publishLibraryQuery(libraryQuery, EventType.LIBRARY_QUERY_PUBLISH)))
.map(ResponseView::success));
libraryQueryRecordService.getLatestRecordByLibraryQueryId(objectId).map(LibraryQueryRecord::getTag).defaultIfEmpty("").flatMap(oldVersion ->
libraryQueryApiService.publish(objectId, libraryQueryPublishRequest)
.delayUntil(__ -> libraryQueryService.getById(objectId)
.flatMap(libraryQuery -> businessEventPublisher.publishLibraryQueryPublishEvent(libraryQueryId, oldVersion.isEmpty()?null:oldVersion, libraryQueryPublishRequest.tag(), EventType.LIBRARY_QUERY_PUBLISH)))
.map(ResponseView::success)));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@
import org.lowcoder.domain.query.model.LibraryQuery;
import org.lowcoder.domain.user.model.User;
import org.lowcoder.domain.user.service.UserService;
import org.lowcoder.infra.event.ApplicationCommonEvent;
import org.lowcoder.infra.event.FolderCommonEvent;
import org.lowcoder.infra.event.LibraryQueryEvent;
import org.lowcoder.infra.event.QueryExecutionEvent;
import org.lowcoder.infra.event.*;
import org.lowcoder.infra.event.datasource.DatasourceEvent;
import org.lowcoder.infra.event.datasource.DatasourcePermissionEvent;
import org.lowcoder.infra.event.group.GroupCreateEvent;
Expand Down Expand Up @@ -766,11 +763,34 @@ public Mono<Void> publishDatasourcePermissionEvent(String datasourceId,
});
}

public Mono<Void> publishLibraryQuery(LibraryQuery libraryQuery, EventType eventType) {
return publishLibraryQueryEvent(libraryQuery.getId(), libraryQuery.getName(), eventType);
public Mono<Void> publishLibraryQueryPublishEvent(String id, String oldVersion, String newVersion, EventType eventType) {
return sessionUserService.getVisitorOrgMemberCache()
.zipWith(sessionUserService.getVisitorToken())
.flatMap(tuple -> {
LibraryQueryPublishEvent event = LibraryQueryPublishEvent.builder()
.id(id)
.oldVersion(oldVersion)
.newVersion(newVersion)
.eventType(eventType)
.userId(tuple.getT1().getUserId())
.orgId(tuple.getT1().getOrgId())
.isAnonymous(Authentication.isAnonymousUser(tuple.getT1().getUserId()))
.sessionHash(Hashing.sha512().hashString(tuple.getT2(), StandardCharsets.UTF_8).toString())
.build();
return Mono.deferContextual(contextView -> {
event.populateDetails(contextView);
applicationEventPublisher.publishEvent(event);
return Mono.<Void>empty();
});
})
.then()
.onErrorResume(throwable -> {
log.error("publishLibraryQueryPublishEvent error.", throwable);
return Mono.empty();
});
}

public Mono<Void> publishLibraryQueryEvent(String id, String name, EventType eventType) {
public Mono<Void> publishLibraryQueryEvent(String id, String name, EventType eventType, String oldName) {
return sessionUserService.getVisitorOrgMemberCache()
.zipWith(sessionUserService.getVisitorToken())
.flatMap(tuple -> {
Expand All @@ -779,6 +799,7 @@ public Mono<Void> publishLibraryQueryEvent(String id, String name, EventType eve
.orgId(tuple.getT1().getOrgId())
.id(id)
.name(name)
.oldName(oldName)
.eventType(eventType)
.isAnonymous(Authentication.isAnonymousUser(tuple.getT1().getUserId()))
.sessionHash(Hashing.sha512().hashString(tuple.getT2(), StandardCharsets.UTF_8).toString())
Expand Down