Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
0296eef
cache templates data
AnaghHegde May 14, 2024
c955b77
fix method parameters
AnaghHegde May 14, 2024
c240600
Merge branch 'release' of https://github.com/appsmithorg/appsmith int…
AnaghHegde May 21, 2024
53d1291
chore: add evict cache flow
AnaghHegde May 22, 2024
ad56029
evit cache flow
AnaghHegde May 22, 2024
c0ae652
review comments
AnaghHegde May 23, 2024
0b6ce52
Merge branch 'release' of https://github.com/appsmithorg/appsmith int…
AnaghHegde May 23, 2024
c4ecc4b
fix reference issue
AnaghHegde May 24, 2024
68326a5
change data type to string
AnaghHegde May 24, 2024
1da7601
add stop watch
AnaghHegde May 8, 2024
a911225
fix issue wioth component static method with Cache annotation
AnaghHegde May 28, 2024
4d86f9f
update cache name
AnaghHegde May 28, 2024
afaedeb
add review comments and fix comments
AnaghHegde May 28, 2024
6308d28
use in memory instead of release
AnaghHegde May 29, 2024
00afdca
review comments
AnaghHegde May 29, 2024
4cc1652
review comments
AnaghHegde May 29, 2024
5c7f3d9
review comments
AnaghHegde May 29, 2024
ba35c3c
Merge branch 'release' of https://github.com/appsmithorg/appsmith int…
AnaghHegde May 29, 2024
1c7f7e3
fix cache logic
AnaghHegde May 29, 2024
d930c9d
add tests
AnaghHegde May 30, 2024
3deb046
Merge branch 'release' of https://github.com/appsmithorg/appsmith int…
AnaghHegde May 31, 2024
2070521
reviw changes
AnaghHegde May 31, 2024
86d38bd
reviw changes
AnaghHegde May 31, 2024
799f449
update review comment
AnaghHegde May 31, 2024
01a3c42
review comment
AnaghHegde May 31, 2024
7b43c43
Merge branch 'release' of https://github.com/appsmithorg/appsmith int…
AnaghHegde Jun 3, 2024
5cbcbd3
deep copy of POJOs to fix source not modified
AnaghHegde Jun 4, 2024
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 @@ -5,11 +5,12 @@
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.util.Set;

@Getter
@Setter
public class PageNameIdDTO {
public class PageNameIdDTO implements Serializable {
Comment thread
AnaghHegde marked this conversation as resolved.
Outdated
@JsonView(Views.Public.class)
String id;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.appsmith.server.helpers;

import com.appsmith.caching.annotations.Cache;
import com.appsmith.external.converters.ISOStringToInstantConverter;
import com.appsmith.server.dtos.ApplicationJson;
import com.appsmith.server.dtos.ApplicationTemplate;
import com.appsmith.server.exceptions.AppsmithError;
import com.appsmith.server.exceptions.AppsmithException;
import com.appsmith.server.services.ce.ApplicationTemplateServiceCEImpl;
import com.appsmith.util.WebClientUtils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.web.reactive.function.client.ExchangeStrategies;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.lang.reflect.Type;
import java.time.Instant;
import java.util.List;

@AllArgsConstructor
public class CacheableTemplateHelper {
@Cache(cacheName = "templateMetadata", key = "{#releaseVersion}")
Comment thread
AnaghHegde marked this conversation as resolved.
Outdated
Comment thread
AnaghHegde marked this conversation as resolved.
Outdated
public static Mono<List<ApplicationTemplate>> getTemplates(String releaseVersion, String baseUrl) {
UriComponentsBuilder uriComponentsBuilder =
UriComponentsBuilder.newInstance().queryParam("version", releaseVersion);

// uriComponents will build url in format: version=version&id=id1&id=id2&id=id3
UriComponents uriComponents = uriComponentsBuilder.build();

return WebClientUtils.create(baseUrl + "/api/v1/app-templates?" + uriComponents.getQuery())
.get()
.exchangeToFlux(clientResponse -> {
if (clientResponse.statusCode().equals(HttpStatus.OK)) {
return clientResponse.bodyToFlux(ApplicationTemplate.class);
} else if (clientResponse.statusCode().isError()) {
return Flux.error(
new AppsmithException(AppsmithError.CLOUD_SERVICES_ERROR, clientResponse.statusCode()));
Comment thread
AnaghHegde marked this conversation as resolved.
} else {
return clientResponse.createException().flatMapMany(Flux::error);
}
})
.collectList();
}

@Cache(cacheName = "TemplateApplicationData", key = "{#templateId}")
public static Mono<ApplicationJson> getApplicationByTemplateId(String templateId, String baseUrl) {
final String templateUrl = baseUrl + "/api/v1/app-templates/" + templateId + "/application";
/*
* using a custom url builder factory because default builder always encodes
* URL.
* It's expected that the appDataUrl is already encoded, so we don't need to
* encode that again.
* Encoding an encoded URL will not work and end up resulting a 404 error
*/
final int size = 4 * 1024 * 1024; // 4 MB
final ExchangeStrategies strategies = ExchangeStrategies.builder()
.codecs(codecs -> codecs.defaultCodecs().maxInMemorySize(size))
.build();

WebClient webClient = WebClientUtils.builder()
.uriBuilderFactory(new ApplicationTemplateServiceCEImpl.NoEncodingUriBuilderFactory(templateUrl))
.exchangeStrategies(strategies)
.build();

return webClient
.get()
.retrieve()
.bodyToMono(String.class)
.map(jsonString -> {
Gson gson = new GsonBuilder()
.registerTypeAdapter(Instant.class, new ISOStringToInstantConverter())
.create();
Type fileType = new TypeToken<ApplicationJson>() {}.getType();

ApplicationJson jsonFile = gson.fromJson(jsonString, fileType);
return jsonFile;
})
.switchIfEmpty(
Mono.error(new AppsmithException(AppsmithError.NO_RESOURCE_FOUND, "template", templateId)));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.appsmith.server.services.ce;

import com.appsmith.external.constants.AnalyticsEvents;
import com.appsmith.external.converters.ISOStringToInstantConverter;
import com.appsmith.server.applications.base.ApplicationService;
import com.appsmith.server.configurations.CloudServicesConfig;
import com.appsmith.server.constants.ArtifactType;
Expand All @@ -28,29 +27,25 @@
import com.appsmith.util.WebClientUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import org.jetbrains.annotations.NotNull;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.MultiValueMap;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.ExchangeStrategies;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.util.DefaultUriBuilderFactory;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.lang.reflect.Type;
import java.time.Instant;
import java.util.List;
import java.util.Map;

import static com.appsmith.server.helpers.CacheableTemplateHelper.getApplicationByTemplateId;
import static com.appsmith.server.helpers.CacheableTemplateHelper.getTemplates;

@Service
public class ApplicationTemplateServiceCEImpl implements ApplicationTemplateServiceCE {
private final CloudServicesConfig cloudServicesConfig;
Expand Down Expand Up @@ -114,31 +109,7 @@ public Flux<ApplicationTemplate> getSimilarTemplates(String templateId, MultiVal

@Override
public Mono<List<ApplicationTemplate>> getActiveTemplates(List<String> templateIds) {
final String baseUrl = cloudServicesConfig.getBaseUrl();

UriComponentsBuilder uriComponentsBuilder =
UriComponentsBuilder.newInstance().queryParam("version", releaseNotesService.getRunningVersion());

if (!CollectionUtils.isEmpty(templateIds)) {
uriComponentsBuilder.queryParam("id", templateIds);
}

// uriComponents will build url in format: version=version&id=id1&id=id2&id=id3
UriComponents uriComponents = uriComponentsBuilder.build();

return WebClientUtils.create(baseUrl + "/api/v1/app-templates?" + uriComponents.getQuery())
.get()
.exchangeToFlux(clientResponse -> {
if (clientResponse.statusCode().equals(HttpStatus.OK)) {
return clientResponse.bodyToFlux(ApplicationTemplate.class);
} else if (clientResponse.statusCode().isError()) {
return Flux.error(
new AppsmithException(AppsmithError.CLOUD_SERVICES_ERROR, clientResponse.statusCode()));
} else {
return clientResponse.createException().flatMapMany(Flux::error);
}
})
.collectList();
return getTemplates(releaseNotesService.getRunningVersion(), cloudServicesConfig.getBaseUrl());
}

@Override
Expand All @@ -162,39 +133,7 @@ public Mono<ApplicationTemplate> getTemplateDetails(String templateId) {
@Override
public Mono<ApplicationJson> getApplicationJsonFromTemplate(String templateId) {
final String baseUrl = cloudServicesConfig.getBaseUrl();
final String templateUrl = baseUrl + "/api/v1/app-templates/" + templateId + "/application";
/*
* using a custom url builder factory because default builder always encodes
* URL.
* It's expected that the appDataUrl is already encoded, so we don't need to
* encode that again.
* Encoding an encoded URL will not work and end up resulting a 404 error
*/
final int size = 4 * 1024 * 1024; // 4 MB
final ExchangeStrategies strategies = ExchangeStrategies.builder()
.codecs(codecs -> codecs.defaultCodecs().maxInMemorySize(size))
.build();

WebClient webClient = WebClientUtils.builder()
.uriBuilderFactory(new NoEncodingUriBuilderFactory(templateUrl))
.exchangeStrategies(strategies)
.build();

return webClient
.get()
.retrieve()
.bodyToMono(String.class)
.map(jsonString -> {
Gson gson = new GsonBuilder()
.registerTypeAdapter(Instant.class, new ISOStringToInstantConverter())
.create();
Type fileType = new TypeToken<ApplicationJson>() {}.getType();

ApplicationJson jsonFile = gson.fromJson(jsonString, fileType);
return jsonFile;
})
.switchIfEmpty(
Mono.error(new AppsmithException(AppsmithError.NO_RESOURCE_FOUND, "template", templateId)));
return getApplicationByTemplateId(templateId, baseUrl);
}

@Override
Expand Down