chore: move metadata calculation to datasource storage (#39657)

## Description
> [!TIP]  
> _Add a TL;DR when the description is longer than 500 words or
extremely technical (helps the content, marketing, and DevRel team)._
>
> _Please also include relevant motivation and context. List any
dependencies that are required for this change. Add links to Notion,
Figma or any other documents that might be relevant to the PR._


Fixes #`Issue Number`  
_or_  
Fixes `Issue URL`
> [!WARNING]  
> _If no issue exists, please create an issue first, and check with the
maintainers if the issue is valid._

## Automation

/ok-to-test tags="@tag.Sanity"

### 🔍 Cypress test results
<!-- This is an auto-generated comment: Cypress test results  -->
> [!TIP]
> 🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉
> Workflow run:
<https://github.com/appsmithorg/appsmith/actions/runs/13782216026>
> Commit: b8355265f5d5054f8e6ecb5a2c61bcb9b789fbc2
> <a
href="https://internal.appsmith.com/app/cypress-dashboard/rundetails-65890b3c81d7400d08fa9ee5?branch=master&workflowId=13782216026&attempt=1"
target="_blank">Cypress dashboard</a>.
> Tags: `@tag.Sanity`
> Spec:
> <hr>Tue, 11 Mar 2025 07:40:11 UTC
<!-- end of auto-generated comment: Cypress test results  -->


## Communication
Should the DevRel and Marketing teams inform users about this change?
- [ ] Yes
- [ ] No


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **Refactor**
- Streamlined and optimized the data source configuration process by
simplifying how associated metadata is handled.
- Improved the underlying service interactions to enhance system
performance and maintainability.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

Co-authored-by: Nilesh Sarupriya <20905988+nsarupr@users.noreply.github.com>
This commit is contained in:
Nilesh Sarupriya 2025-03-11 14:46:14 +05:30 committed by GitHub
parent 1c63e5b154
commit 4c61d6b774
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 79 additions and 62 deletions

View File

@ -28,10 +28,8 @@ import com.appsmith.server.ratelimiting.RateLimitService;
import com.appsmith.server.repositories.DatasourceRepository;
import com.appsmith.server.repositories.NewActionRepository;
import com.appsmith.server.services.AnalyticsService;
import com.appsmith.server.services.ConfigService;
import com.appsmith.server.services.DatasourceContextService;
import com.appsmith.server.services.FeatureFlagService;
import com.appsmith.server.services.OrganizationService;
import com.appsmith.server.services.SequenceService;
import com.appsmith.server.services.SessionUserService;
import com.appsmith.server.services.WorkspaceService;
@ -66,8 +64,6 @@ import java.util.UUID;
import static com.appsmith.external.constants.spans.DatasourceSpan.FETCH_ALL_DATASOURCES_WITH_STORAGES;
import static com.appsmith.external.constants.spans.DatasourceSpan.FETCH_ALL_PLUGINS_IN_WORKSPACE;
import static com.appsmith.external.helpers.AppsmithBeanUtils.copyNestedNonNullProperties;
import static com.appsmith.server.constants.ce.FieldNameCE.INSTANCE_ID;
import static com.appsmith.server.constants.ce.FieldNameCE.TENANT_ID;
import static com.appsmith.server.dtos.DBOpsType.SAVE;
import static com.appsmith.server.helpers.CollectionUtils.isNullOrEmpty;
import static com.appsmith.server.helpers.DatasourceAnalyticsUtils.getAnalyticsProperties;
@ -97,9 +93,6 @@ public class DatasourceServiceCEImpl implements DatasourceServiceCE {
private final RateLimitService rateLimitService;
private final FeatureFlagService featureFlagService;
private final ObservationRegistry observationRegistry;
private final OrganizationService organizationService;
private final ConfigService configService;
// Defines blocking duration for test as well as connection created for query execution
// This will block the creation of datasource connection for 5 minutes, in case of more than 3 failed connection
// attempts
@ -125,9 +118,7 @@ public class DatasourceServiceCEImpl implements DatasourceServiceCE {
EnvironmentPermission environmentPermission,
RateLimitService rateLimitService,
FeatureFlagService featureFlagService,
ObservationRegistry observationRegistry,
OrganizationService organizationService,
ConfigService configService) {
ObservationRegistry observationRegistry) {
this.workspaceService = workspaceService;
this.sessionUserService = sessionUserService;
@ -146,8 +137,6 @@ public class DatasourceServiceCEImpl implements DatasourceServiceCE {
this.rateLimitService = rateLimitService;
this.featureFlagService = featureFlagService;
this.observationRegistry = observationRegistry;
this.organizationService = organizationService;
this.configService = configService;
}
@Override
@ -235,28 +224,27 @@ public class DatasourceServiceCEImpl implements DatasourceServiceCE {
}
return datasourceMono.flatMap(savedDatasource -> this.organiseDatasourceStorages(savedDatasource)
.flatMap(datasourceStorageX -> setAdditionalMetadataInDatasourceStorage(datasourceStorageX)
.flatMap(datasourceStorage -> {
// Make sure that we are creating entries only if the id is not already populated
if (hasText(datasourceStorage.getId())) {
return Mono.just(datasourceStorage);
}
.flatMap(datasourceStorage -> {
// Make sure that we are creating entries only if the id is not already populated
if (hasText(datasourceStorage.getId())) {
return Mono.just(datasourceStorage);
}
return datasourceStorageService
.create(datasourceStorage, isDryOps)
.map(datasourceStorage1 -> {
if (datasourceStorageDryRunQueries != null && isDryOps) {
List<DatasourceStorage> datasourceStorages =
datasourceStorageDryRunQueries.get(SAVE);
if (datasourceStorages == null) {
datasourceStorages = new ArrayList<>();
}
datasourceStorages.add(datasourceStorage1);
datasourceStorageDryRunQueries.put(SAVE, datasourceStorages);
}
return datasourceStorage1;
});
}))
return datasourceStorageService
.create(datasourceStorage, isDryOps)
.map(datasourceStorage1 -> {
if (datasourceStorageDryRunQueries != null && isDryOps) {
List<DatasourceStorage> datasourceStorages =
datasourceStorageDryRunQueries.get(SAVE);
if (datasourceStorages == null) {
datasourceStorages = new ArrayList<>();
}
datasourceStorages.add(datasourceStorage1);
datasourceStorageDryRunQueries.put(SAVE, datasourceStorages);
}
return datasourceStorage1;
});
})
.map(datasourceStorageService::createDatasourceStorageDTOFromDatasourceStorage)
.collectMap(DatasourceStorageDTO::getEnvironmentId)
.map(savedStorages -> {
@ -265,20 +253,6 @@ public class DatasourceServiceCEImpl implements DatasourceServiceCE {
}));
}
private Mono<DatasourceStorage> setAdditionalMetadataInDatasourceStorage(DatasourceStorage datasourceStorage) {
Mono<String> organizationIdMono = organizationService.getCurrentUserOrganizationId();
Mono<String> instanceIdMono = configService.getInstanceId();
Map<String, Object> metadata = new HashMap<>();
return organizationIdMono.zipWith(instanceIdMono).map(tuple -> {
metadata.put(TENANT_ID, tuple.getT1());
metadata.put(INSTANCE_ID, tuple.getT2());
datasourceStorage.setMetadata(metadata);
return datasourceStorage;
});
}
// this requires an EE override multiple environments
protected Flux<DatasourceStorage> organiseDatasourceStorages(@NotNull Datasource savedDatasource) {
Map<String, DatasourceStorageDTO> storages = savedDatasource.getDatasourceStorages();

View File

@ -8,10 +8,8 @@ import com.appsmith.server.ratelimiting.RateLimitService;
import com.appsmith.server.repositories.DatasourceRepository;
import com.appsmith.server.repositories.NewActionRepository;
import com.appsmith.server.services.AnalyticsService;
import com.appsmith.server.services.ConfigService;
import com.appsmith.server.services.DatasourceContextService;
import com.appsmith.server.services.FeatureFlagService;
import com.appsmith.server.services.OrganizationService;
import com.appsmith.server.services.SequenceService;
import com.appsmith.server.services.SessionUserService;
import com.appsmith.server.services.WorkspaceService;
@ -43,9 +41,7 @@ public class DatasourceServiceImpl extends DatasourceServiceCEImpl implements Da
EnvironmentPermission environmentPermission,
RateLimitService rateLimitService,
FeatureFlagService featureFlagService,
ObservationRegistry observationRegistry,
OrganizationService organizationService,
ConfigService configService) {
ObservationRegistry observationRegistry) {
super(
repository,
@ -64,8 +60,6 @@ public class DatasourceServiceImpl extends DatasourceServiceCEImpl implements Da
environmentPermission,
rateLimitService,
featureFlagService,
observationRegistry,
organizationService,
configService);
observationRegistry);
}
}

View File

@ -4,6 +4,8 @@ import com.appsmith.server.helpers.PluginExecutorHelper;
import com.appsmith.server.plugins.base.PluginService;
import com.appsmith.server.repositories.DatasourceStorageRepository;
import com.appsmith.server.services.AnalyticsService;
import com.appsmith.server.services.ConfigService;
import com.appsmith.server.services.OrganizationService;
import com.appsmith.server.solutions.DatasourcePermission;
import org.springframework.stereotype.Service;
@ -16,7 +18,16 @@ public class DatasourceStorageServiceCECompatibleImpl extends DatasourceStorageS
DatasourcePermission datasourcePermission,
PluginService pluginService,
PluginExecutorHelper pluginExecutorHelper,
AnalyticsService analyticsService) {
super(repository, datasourcePermission, pluginService, pluginExecutorHelper, analyticsService);
AnalyticsService analyticsService,
ConfigService configService,
OrganizationService organizationService) {
super(
repository,
datasourcePermission,
pluginService,
pluginExecutorHelper,
analyticsService,
configService,
organizationService);
}
}

View File

@ -17,6 +17,8 @@ import com.appsmith.server.helpers.PluginExecutorHelper;
import com.appsmith.server.plugins.base.PluginService;
import com.appsmith.server.repositories.DatasourceStorageRepository;
import com.appsmith.server.services.AnalyticsService;
import com.appsmith.server.services.ConfigService;
import com.appsmith.server.services.OrganizationService;
import com.appsmith.server.solutions.DatasourcePermission;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
@ -30,6 +32,8 @@ import java.util.Map;
import java.util.Set;
import static com.appsmith.external.helpers.AppsmithBeanUtils.copyNestedNonNullProperties;
import static com.appsmith.server.constants.FieldName.INSTANCE_ID;
import static com.appsmith.server.constants.FieldName.ORGANIZATION_ID;
import static java.lang.Boolean.FALSE;
import static java.lang.Boolean.TRUE;
@ -41,18 +45,24 @@ public class DatasourceStorageServiceCEImpl implements DatasourceStorageServiceC
private final PluginService pluginService;
private final PluginExecutorHelper pluginExecutorHelper;
private final AnalyticsService analyticsService;
private final ConfigService configService;
private final OrganizationService organizationService;
public DatasourceStorageServiceCEImpl(
DatasourceStorageRepository repository,
DatasourcePermission datasourcePermission,
PluginService pluginService,
PluginExecutorHelper pluginExecutorHelper,
AnalyticsService analyticsService) {
AnalyticsService analyticsService,
ConfigService configService,
OrganizationService organizationService) {
this.repository = repository;
this.datasourcePermission = datasourcePermission;
this.pluginService = pluginService;
this.pluginExecutorHelper = pluginExecutorHelper;
this.analyticsService = analyticsService;
this.configService = configService;
this.organizationService = organizationService;
}
@Override
@ -254,8 +264,10 @@ public class DatasourceStorageServiceCEImpl implements DatasourceStorageServiceC
}
return repository
.save(unsavedDatasourceStorage)
.then(this.executePostSaveActions(unsavedDatasourceStorage))
.thenReturn(unsavedDatasourceStorage);
.flatMap(savedDatasourceStorage -> setAdditionalMetadataInDatasourceStorage(
savedDatasourceStorage)
.flatMap(this::executePostSaveActions)
.thenReturn(savedDatasourceStorage));
});
}
@ -265,6 +277,21 @@ public class DatasourceStorageServiceCEImpl implements DatasourceStorageServiceC
return Mono.just(datasourceStorage);
}
private Mono<DatasourceStorage> setAdditionalMetadataInDatasourceStorage(DatasourceStorage datasourceStorage) {
Mono<String> organizationIdMono = organizationService.getCurrentUserOrganizationId();
Mono<String> instanceIdMono = configService.getInstanceId();
Map<String, Object> metadata = new HashMap<>();
return organizationIdMono.zipWith(instanceIdMono).map(tuple -> {
// Change this to ORGANIZATION_ID once we have the organizationId field in the datasource storage
metadata.put(ORGANIZATION_ID, tuple.getT1());
metadata.put(INSTANCE_ID, tuple.getT2());
datasourceStorage.setMetadata(metadata);
return datasourceStorage;
});
}
private DatasourceStorage sanitizeDatasourceStorage(DatasourceStorage datasourceStorage) {
if (datasourceStorage.getDatasourceConfiguration() != null
&& !CollectionUtils.isEmpty(

View File

@ -4,6 +4,8 @@ import com.appsmith.server.helpers.PluginExecutorHelper;
import com.appsmith.server.plugins.base.PluginService;
import com.appsmith.server.repositories.DatasourceStorageRepository;
import com.appsmith.server.services.AnalyticsService;
import com.appsmith.server.services.ConfigService;
import com.appsmith.server.services.OrganizationService;
import com.appsmith.server.solutions.DatasourcePermission;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@ -17,7 +19,16 @@ public class DatasourceStorageServiceImpl extends DatasourceStorageServiceCEComp
DatasourcePermission datasourcePermission,
PluginService pluginService,
PluginExecutorHelper pluginExecutorHelper,
AnalyticsService analyticsService) {
super(repository, datasourcePermission, pluginService, pluginExecutorHelper, analyticsService);
AnalyticsService analyticsService,
ConfigService configService,
OrganizationService organizationService) {
super(
repository,
datasourcePermission,
pluginService,
pluginExecutorHelper,
analyticsService,
configService,
organizationService);
}
}