Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public enum AnalyticsEvents {
PAGE_REORDER,
GENERATE_CRUD_PAGE("generate_CRUD_PAGE"),
CREATE_SUPERUSER,
ACTION_RUN_BEHAVIOUR_CHANGED("action_RUN_BEHAVIOUR_CHANGED"),
SUBSCRIBE_MARKETING_EMAILS,
UNSUBSCRIBE_MARKETING_EMAILS,
INSTALLATION_SETUP_COMPLETE("Installation Setup Complete"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,6 @@ public class FieldNameCE {
public static final String NONE = "none";
public static final String ORGANIZATION_ADMINISTRATOR_ROLE = "Organization Administrator Role";
public static final String INSTANCE_VARIABLES = "instanceVariables";

public static final String ACTION_CONFIGURATION_RUN_BEHAVIOUR = "runBehaviour";
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.appsmith.server.onload.internal;

import com.appsmith.external.constants.AnalyticsEvents;
import com.appsmith.external.dtos.DslExecutableDTO;
import com.appsmith.external.dtos.LayoutExecutableUpdateDTO;
import com.appsmith.external.enums.FeatureFlagEnum;
Expand All @@ -19,6 +20,7 @@
import com.appsmith.server.helpers.CollectionUtils;
import com.appsmith.server.helpers.ObservationHelperImpl;
import com.appsmith.server.onload.executables.ExecutableOnLoadService;
import com.appsmith.server.services.AnalyticsService;
import com.appsmith.server.services.AstService;
import com.appsmith.server.services.FeatureFlagService;
import com.fasterxml.jackson.core.type.TypeReference;
Expand Down Expand Up @@ -94,6 +96,7 @@ public class OnLoadExecutablesUtilCEImpl implements OnLoadExecutablesUtilCE {
private final ObservationRegistry observationRegistry;
private final ObservationHelperImpl observationHelper;
private final FeatureFlagService featureFlagService;
private final AnalyticsService analyticsService;

/**
* This function computes the sequenced on page load executables.
Expand Down Expand Up @@ -307,6 +310,7 @@ public Mono<Boolean> updateExecutablesRunBehaviour(
List<String> messagesRef,
CreatorContextType creatorType) {
List<Executable> toUpdateExecutables = new ArrayList<>();
Map<String, RunBehaviourEnum> oldRunBehaviourMap = new HashMap<>();

// Fetch all the actions which exist in this page.
Flux<Executable> creatorContextExecutablesFlux =
Expand Down Expand Up @@ -373,7 +377,6 @@ public Mono<Boolean> updateExecutablesRunBehaviour(
turnedOnExecutableNames.removeAll(existingOnLoadExecutableNames);

for (Executable executable : creatorContextExecutables) {

String executableName = executable.getUserExecutableName();
// If a user has ever set execute on load, this field can not be changed
// automatically.
Expand All @@ -382,6 +385,8 @@ public Mono<Boolean> updateExecutablesRunBehaviour(
// this
// condition is false.
if (FALSE.equals(executable.getUserSetOnLoad())) {
// Store old run behaviour before updating
oldRunBehaviourMap.put(executableName, executable.getRunBehaviour());

// If this executable is no longer an onload executable, turn the execute on
// load to
Expand Down Expand Up @@ -457,13 +462,51 @@ public Mono<Boolean> updateExecutablesRunBehaviour(

// Finally update the actions which require an update
return Flux.fromIterable(toUpdateExecutables)
.flatMap(executable -> this.updateUnpublishedExecutable(
executable.getId(), executable, creatorType))
.flatMap(executable -> {
RunBehaviourEnum oldRunBehaviour = oldRunBehaviourMap.getOrDefault(
executable.getUserExecutableName(), null);
return this.updateUnpublishedExecutable(
executable.getId(), executable, creatorType)
.flatMap(updatedExecutable -> sendRunBehaviourChangedAnalytics(
updatedExecutable, oldRunBehaviour)
.onErrorResume(err -> {
log.warn(
"Analytics publish failed for action {}: {}",
updatedExecutable.getId(),
err.getMessage());
return Mono.just(updatedExecutable);
}));
})
.then(Mono.just(TRUE));
Comment thread
coderabbitai[bot] marked this conversation as resolved.
});
});
}

protected Mono<Executable> sendRunBehaviourChangedAnalytics(
Executable executable, RunBehaviourEnum oldRunBehaviour) {
if (!(executable instanceof ActionDTO actionDTO)) {
return Mono.just(executable);
}
Map<String, Object> data = new HashMap<>();
data.put("id", actionDTO.getId());
data.put("name", actionDTO.getName());
data.put("pageId", actionDTO.getPageId());
data.put("applicationId", actionDTO.getApplicationId());
data.put("pluginId", actionDTO.getPluginId());
data.put("createdAt", actionDTO.getCreatedAt());
data.put("oldRunBehaviour", oldRunBehaviour);
data.put("newRunBehaviour", actionDTO.getRunBehaviour());
data.put("pluginType", actionDTO.getPluginType());
data.put("actionConfiguration", actionDTO.getActionConfiguration());
data.put(
"wasChangedBy",
"system"); // This is a change that automatically happens during update layout, so we set it to "system"

return analyticsService
.sendObjectEvent(AnalyticsEvents.ACTION_RUN_BEHAVIOUR_CHANGED, actionDTO, data)
.thenReturn(executable);
}

@Override
public Mono<Layout> findAndUpdateLayout(
String creatorId, CreatorContextType creatorType, String layoutId, Layout layout) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.appsmith.server.domains.NewPage;
import com.appsmith.server.helpers.ObservationHelperImpl;
import com.appsmith.server.onload.executables.ExecutableOnLoadService;
import com.appsmith.server.services.AnalyticsService;
import com.appsmith.server.services.AstService;
import com.appsmith.server.services.FeatureFlagService;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand All @@ -20,13 +21,15 @@ public OnLoadExecutablesUtilImpl(
ExecutableOnLoadService<NewPage> pageExecutableOnLoadService,
ObservationRegistry observationRegistry,
ObservationHelperImpl observationHelper,
FeatureFlagService featureFlagService) {
FeatureFlagService featureFlagService,
AnalyticsService analyticsService) {
super(
astService,
objectMapper,
pageExecutableOnLoadService,
observationRegistry,
observationHelper,
featureFlagService);
featureFlagService,
analyticsService);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.appsmith.server.services.ce;

import com.appsmith.external.constants.AnalyticsEvents;
import com.appsmith.external.helpers.AppsmithEventContext;
import com.appsmith.external.helpers.AppsmithEventContextType;
import com.appsmith.external.models.ActionDTO;
Expand Down Expand Up @@ -36,7 +37,9 @@
import reactor.core.publisher.Mono;
import reactor.util.function.Tuple2;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.appsmith.external.constants.spans.ActionSpan.GET_ACTION_BY_ID;
import static com.appsmith.external.constants.spans.ActionSpan.UPDATE_ACTION_BASED_ON_CONTEXT;
Expand Down Expand Up @@ -321,19 +324,49 @@ public Mono<ActionDTO> setRunBehaviour(String id, RunBehaviourEnum behaviour) {
.flatMap(newAction -> {
ActionDTO action = newAction.getUnpublishedAction();

RunBehaviourEnum oldRunBehaviour = action.getRunBehaviour();

action.setUserSetOnLoad(true);
action.setRunBehaviour(behaviour);

newAction.setUnpublishedAction(action);

return newActionService.save(newAction).flatMap(savedAction -> updateLayoutService
.updateLayoutByContextTypeAndContextId(action.getContextType(), action.getContextId())
.name(UPDATE_PAGE_LAYOUT_BY_PAGE_ID)
.tap(Micrometer.observation(observationRegistry))
.thenReturn(newActionService.generateActionByViewMode(savedAction, false)));
return newActionService
.save(newAction)
.flatMap(savedAction -> updateLayoutService
.updateLayoutByContextTypeAndContextId(
action.getContextType(), action.getContextId())
.name(UPDATE_PAGE_LAYOUT_BY_PAGE_ID)
.tap(Micrometer.observation(observationRegistry))
.thenReturn(newActionService.generateActionByViewMode(savedAction, false)))
.flatMap(updatedAction -> sendRunBehaviourChangedAnalytics(updatedAction, oldRunBehaviour))
.flatMap(updatedAction -> sendRunBehaviourChangedAnalytics(updatedAction, oldRunBehaviour)
.onErrorResume(e -> {
log.warn("Run behaviour analytics failed, continuing", e);
return Mono.just(updatedAction);
}));
});
Comment thread
sneha122 marked this conversation as resolved.
}

private Mono<ActionDTO> sendRunBehaviourChangedAnalytics(ActionDTO actionDTO, RunBehaviourEnum oldRunBehaviour) {
Map<String, Object> data = new HashMap<>();
data.put("id", actionDTO.getId());
data.put("name", actionDTO.getName());
data.put("pageId", actionDTO.getPageId());
data.put("applicationId", actionDTO.getApplicationId());
data.put("pluginId", actionDTO.getPluginId());
data.put("createdAt", actionDTO.getCreatedAt());
data.put("oldRunBehaviour", oldRunBehaviour);
data.put("newRunBehaviour", actionDTO.getRunBehaviour());
data.put("pluginType", actionDTO.getPluginType());
data.put("actionConfiguration", actionDTO.getActionConfiguration());
data.put("wasChangedBy", "user"); // This is a user-initiated change

return analyticsService
.sendObjectEvent(AnalyticsEvents.ACTION_RUN_BEHAVIOUR_CHANGED, actionDTO, data)
.thenReturn(actionDTO);
}

/**
* - Delete action.
* - Update page layout since a deleted action cannot be marked as on page load.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1233,6 +1233,7 @@ private Mono<ActionExecutionRequest> sendExecuteAnalyticsEvent(
}
data.put(FieldName.ACTION_CONFIGURATION, rawActionConfiguration);
data.put(FieldName.EVENT_DATA, eventData);
data.put(FieldName.ACTION_CONFIGURATION_RUN_BEHAVIOUR, actionDTO.getRunBehaviour());
return analyticsService
Comment thread
sneha122 marked this conversation as resolved.
.sendObjectEvent(AnalyticsEvents.EXECUTE_ACTION, actionDTO, data)
.thenReturn(request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.appsmith.external.models.RunBehaviourEnum;
import com.appsmith.server.helpers.ObservationHelperImpl;
import com.appsmith.server.onload.executables.ExecutableOnLoadService;
import com.appsmith.server.services.AnalyticsService;
import com.appsmith.server.services.AstService;
import com.appsmith.server.services.FeatureFlagService;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand All @@ -30,6 +31,7 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
Expand Down Expand Up @@ -58,23 +60,19 @@ public class OnLoadExecutablesUtilCEImplTest {

private OnLoadExecutablesUtilCEImpl onLoadExecutablesUtilCE;

@Mock
private AnalyticsService analyticsService;

Comment thread
sneha122 marked this conversation as resolved.
@BeforeEach
public void setUp() {
onLoadExecutablesUtilCE = new OnLoadExecutablesUtilCEImpl(
astService,
objectMapper,
executableOnLoadService,
observationRegistry,
observationHelper,
featureFlagService);

onLoadExecutablesUtilCE = spy(new OnLoadExecutablesUtilCEImpl(
astService,
objectMapper,
executableOnLoadService,
observationRegistry,
observationHelper,
featureFlagService));
featureFlagService,
analyticsService));

ObservationRegistry.ObservationConfig mockObservationConfig =
Mockito.mock(ObservationRegistry.ObservationConfig.class);
Expand Down Expand Up @@ -107,6 +105,10 @@ public void testUpdateExecutablesRunBehaviour_WhenFeatureFlagEnabled_SetsRunBeha
when(executableOnLoadService.updateUnpublishedExecutable(anyString(), any(ActionDTO.class)))
.thenAnswer(invocation -> Mono.just(invocation.getArgument(1)));

doAnswer(invocation -> Mono.just(invocation.getArgument(0)))
.when(onLoadExecutablesUtilCE)
.sendRunBehaviourChangedAnalytics(any(Executable.class), any(RunBehaviourEnum.class));

// Execute
Mono<Boolean> result = onLoadExecutablesUtilCE.updateExecutablesRunBehaviour(
onLoadExecutables, creatorId, executableUpdatesRef, messagesRef, creatorType);
Expand Down Expand Up @@ -147,6 +149,10 @@ public void testUpdateExecutablesRunBehaviour_WhenFeatureFlagDisabled_SetsRunBeh
when(executableOnLoadService.updateUnpublishedExecutable(anyString(), any(ActionDTO.class)))
.thenAnswer(invocation -> Mono.just(invocation.getArgument(1)));

doAnswer(invocation -> Mono.just(invocation.getArgument(0)))
.when(onLoadExecutablesUtilCE)
.sendRunBehaviourChangedAnalytics(any(Executable.class), any(RunBehaviourEnum.class));

// Execute
Mono<Boolean> result = onLoadExecutablesUtilCE.updateExecutablesRunBehaviour(
onLoadExecutables, creatorId, executableUpdatesRef, messagesRef, creatorType);
Expand Down Expand Up @@ -190,6 +196,10 @@ public void testUpdateExecutablesRunBehaviour_WhenUserSetOnLoadIsTrue_DoesNotUpd
when(executableOnLoadService.updateUnpublishedExecutable(anyString(), any(ActionDTO.class)))
.thenAnswer(invocation -> Mono.just(invocation.getArgument(1)));

doAnswer(invocation -> Mono.just(invocation.getArgument(0)))
.when(onLoadExecutablesUtilCE)
.sendRunBehaviourChangedAnalytics(any(Executable.class), any(RunBehaviourEnum.class));

// Execute
Mono<Boolean> result = onLoadExecutablesUtilCE.updateExecutablesRunBehaviour(
onLoadExecutables, creatorId, executableUpdatesRef, messagesRef, creatorType);
Expand Down Expand Up @@ -260,6 +270,10 @@ public void whenFeatureFlagOn_andExecutableTurnedOn_shouldShowReactiveMessage()
when(executableOnLoadService.updateUnpublishedExecutable(eq("1"), any()))
.thenReturn(Mono.just(updatedAction));

doAnswer(invocation -> Mono.just(invocation.getArgument(0)))
.when(onLoadExecutablesUtilCE)
.sendRunBehaviourChangedAnalytics(any(Executable.class), any(RunBehaviourEnum.class));

// Execute and verify
StepVerifier.create(onLoadExecutablesUtilCE.updateExecutablesRunBehaviour(
onLoadExecutables, "creatorId", executableUpdates, messages, CreatorContextType.PAGE))
Expand Down Expand Up @@ -296,6 +310,10 @@ public void whenFeatureFlagOff_andExecutableTurnedOn_shouldShowPageLoadMessage()
when(executableOnLoadService.updateUnpublishedExecutable(eq("1"), any()))
.thenReturn(Mono.just(updatedAction));

doAnswer(invocation -> Mono.just(invocation.getArgument(0)))
.when(onLoadExecutablesUtilCE)
.sendRunBehaviourChangedAnalytics(any(Executable.class), any(RunBehaviourEnum.class));

// Execute and verify
StepVerifier.create(onLoadExecutablesUtilCE.updateExecutablesRunBehaviour(
onLoadExecutables, "creatorId", executableUpdates, messages, CreatorContextType.PAGE))
Expand Down Expand Up @@ -332,6 +350,10 @@ public void whenFeatureFlagOn_andExecutableTurnedOff_shouldShowManualMessage() {
when(executableOnLoadService.updateUnpublishedExecutable(eq("1"), any()))
.thenReturn(Mono.just(updatedAction));

doAnswer(invocation -> Mono.just(invocation.getArgument(0)))
.when(onLoadExecutablesUtilCE)
.sendRunBehaviourChangedAnalytics(any(Executable.class), any(RunBehaviourEnum.class));

// Execute and verify
StepVerifier.create(onLoadExecutablesUtilCE.updateExecutablesRunBehaviour(
onLoadExecutables, "creatorId", executableUpdates, messages, CreatorContextType.PAGE))
Expand Down Expand Up @@ -384,6 +406,10 @@ public void whenOnlyOneActionChange_shouldShowOnlyThatEntityInToastMessage() {
when(executableOnLoadService.updateUnpublishedExecutable(eq("2"), any()))
.thenReturn(Mono.just(updatedAction2));

doAnswer(invocation -> Mono.just(invocation.getArgument(0)))
.when(onLoadExecutablesUtilCE)
.sendRunBehaviourChangedAnalytics(any(Executable.class), any(RunBehaviourEnum.class));

// Execute and verify
StepVerifier.create(onLoadExecutablesUtilCE.updateExecutablesRunBehaviour(
onLoadExecutables, "creatorId", executableUpdates, messages, CreatorContextType.PAGE))
Expand Down Expand Up @@ -439,6 +465,10 @@ public void whenMultipleExecutablesChange_shouldShowAllMessages() {
when(executableOnLoadService.updateUnpublishedExecutable(eq("2"), any()))
.thenReturn(Mono.just(updatedAction2));

doAnswer(invocation -> Mono.just(invocation.getArgument(0)))
.when(onLoadExecutablesUtilCE)
.sendRunBehaviourChangedAnalytics(any(Executable.class), any(RunBehaviourEnum.class));

// Execute and verify
StepVerifier.create(onLoadExecutablesUtilCE.updateExecutablesRunBehaviour(
onLoadExecutables, "creatorId", executableUpdates, messages, CreatorContextType.PAGE))
Expand Down