Skip to content

Commit f839e34

Browse files
Merge pull request #954 from exadel-inc/EFRS-1334_optimize_models_retrieval
EFRS-1334: Optimized models retrieval
2 parents c3a82ac + 03be452 commit f839e34

File tree

7 files changed

+44
-23
lines changed

7 files changed

+44
-23
lines changed

java/admin/src/main/java/com/exadel/frs/dto/ui/ModelResponseDto.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,17 @@
1616

1717
package com.exadel.frs.dto.ui;
1818

19+
import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL;
1920
import com.exadel.frs.commonservice.enums.ModelType;
21+
import com.fasterxml.jackson.annotation.JsonInclude;
2022
import lombok.AllArgsConstructor;
2123
import lombok.Builder;
2224
import lombok.Data;
2325

2426
import java.time.LocalDateTime;
2527
import lombok.NoArgsConstructor;
2628

29+
@JsonInclude(NON_NULL)
2730
@Data
2831
@Builder
2932
@AllArgsConstructor
@@ -37,4 +40,4 @@ public class ModelResponseDto {
3740
private Long subjectCount;
3841
private Long imageCount;
3942
private LocalDateTime createdDate;
40-
}
43+
}

java/admin/src/main/java/com/exadel/frs/mapper/MlModelMapper.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616

1717
package com.exadel.frs.mapper;
1818

19-
import com.exadel.frs.dto.ui.ModelResponseDto;
2019
import com.exadel.frs.commonservice.entity.Model;
20+
import com.exadel.frs.commonservice.entity.ModelProjection;
21+
import com.exadel.frs.dto.ui.ModelResponseDto;
2122
import java.util.List;
2223
import org.mapstruct.Context;
2324
import org.mapstruct.Mapper;
@@ -29,5 +30,8 @@ public interface MlModelMapper {
2930
@Mapping(source = "guid", target = "id")
3031
ModelResponseDto toResponseDto(Model model, @Context String appGuid);
3132

33+
@Mapping(source = "guid", target = "id")
34+
ModelResponseDto toResponseDto(ModelProjection model, @Context String appGuid);
35+
3236
List<ModelResponseDto> toResponseDto(List<Model> model, @Context String appGuid);
33-
}
37+
}

java/admin/src/main/java/com/exadel/frs/service/ModelService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.exadel.frs.commonservice.annotation.CollectStatistics;
2323
import com.exadel.frs.commonservice.entity.App;
2424
import com.exadel.frs.commonservice.entity.Model;
25+
import com.exadel.frs.commonservice.entity.ModelProjection;
2526
import com.exadel.frs.commonservice.entity.ModelStatisticProjection;
2627
import com.exadel.frs.commonservice.entity.Subject;
2728
import com.exadel.frs.commonservice.entity.User;
@@ -132,7 +133,7 @@ public List<ModelResponseDto> getModels(final String appGuid, final Long userId)
132133

133134
return modelRepository.findAllByAppId(app.getId())
134135
.stream()
135-
.map(model -> getModelResponseDto(model.getApiKey(), model)).collect(Collectors.toList());
136+
.map(model -> modelMapper.toResponseDto(model, model.apiKey())).collect(Collectors.toList());
136137
}
137138

138139
private Model createModel(final ModelCreateDto modelCreateDto, final String appGuid, final Long userId) {

java/admin/src/test/java/com/exadel/frs/ModelServiceTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818

1919
import com.exadel.frs.commonservice.entity.App;
2020
import com.exadel.frs.commonservice.entity.Model;
21+
import com.exadel.frs.commonservice.entity.ModelProjection;
2122
import com.exadel.frs.commonservice.entity.User;
23+
import com.exadel.frs.commonservice.enums.ModelType;
2224
import com.exadel.frs.commonservice.repository.ImgRepository;
2325
import com.exadel.frs.commonservice.repository.ModelRepository;
2426
import com.exadel.frs.commonservice.repository.ModelStatisticRepository;
@@ -34,6 +36,7 @@
3436
import com.exadel.frs.service.ModelService;
3537
import com.exadel.frs.service.UserService;
3638
import com.exadel.frs.system.security.AuthorizationManager;
39+
import java.time.LocalDateTime;
3740
import lombok.val;
3841
import org.junit.jupiter.api.Test;
3942
import org.mockito.ArgumentCaptor;
@@ -43,6 +46,8 @@
4346
import java.util.Optional;
4447
import java.util.Random;
4548

49+
import static com.exadel.frs.commonservice.enums.ModelType.RECOGNITION;
50+
import static java.time.LocalDateTime.now;
4651
import static java.util.UUID.randomUUID;
4752
import static org.assertj.core.api.Assertions.assertThatThrownBy;
4853
import static org.hamcrest.MatcherAssert.assertThat;
@@ -140,12 +145,7 @@ void successGetModels() {
140145
.id(APPLICATION_ID)
141146
.build();
142147

143-
val model = Model.builder()
144-
.id(MODEL_ID)
145-
.guid(MODEL_GUID)
146-
.apiKey(MODEL_API_KEY)
147-
.app(app)
148-
.build();
148+
val model = new ModelProjection(MODEL_GUID, "1REC", MODEL_API_KEY, RECOGNITION, now());
149149

150150
val user = User.builder()
151151
.id(USER_ID)

java/admin/src/test/java/com/exadel/frs/controller/ModelControllerTest.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,6 @@ void shouldReturnMessageAndCodeWhenModelNameIsMissingOnUpdate() throws Exception
108108

109109
@Test
110110
void shouldReturnErrorMessageWhenNameIsMissingOnCreateNewModel() throws Exception {
111-
val bodyWithEmptyName = new ModelCreateDto();
112-
bodyWithEmptyName.setName("");
113-
bodyWithEmptyName.setType("RECOGNITION");
114-
115111
val bodyWithNoName = new ModelCreateDto();
116112
bodyWithNoName.setType("RECOGNITION");
117113

@@ -120,10 +116,6 @@ void shouldReturnErrorMessageWhenNameIsMissingOnCreateNewModel() throws Exceptio
120116
.with(user(buildUser()))
121117
.contentType(APPLICATION_JSON);
122118

123-
mockMvc.perform(createNewModelRequest.content(mapper.writeValueAsString(bodyWithEmptyName)))
124-
.andExpect(status().isBadRequest())
125-
.andExpect(content().string("{\"message\":\"Model name size must be between 1 and 50\",\"code\":26}"));
126-
127119
mockMvc.perform(createNewModelRequest.content(mapper.writeValueAsString(bodyWithNoName)))
128120
.andExpect(status().isBadRequest())
129121
.andExpect(content().string("{\"message\":\"Model name cannot be empty\",\"code\":26}"));
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.exadel.frs.commonservice.entity;
2+
3+
import com.exadel.frs.commonservice.enums.ModelType;
4+
import java.time.LocalDateTime;
5+
6+
public record ModelProjection(
7+
String guid,
8+
String name,
9+
String apiKey,
10+
ModelType type,
11+
LocalDateTime createdDate) {
12+
13+
}

java/common/src/main/java/com/exadel/frs/commonservice/repository/ModelRepository.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.exadel.frs.commonservice.repository;
1818

1919
import com.exadel.frs.commonservice.entity.Model;
20+
import com.exadel.frs.commonservice.entity.ModelProjection;
2021
import com.exadel.frs.commonservice.entity.ModelSubjectProjection;
2122
import com.exadel.frs.commonservice.enums.ModelType;
2223
import java.util.Set;
@@ -32,11 +33,6 @@
3233
public interface ModelRepository extends JpaRepository<Model, Long> {
3334
Optional<Model> findByApiKeyAndType(String apiKey, ModelType type);
3435

35-
@Query("select distinct m " +
36-
"from Model m " +
37-
"where m.app.id = :appId")
38-
List<Model> findAllByAppId(Long appId);
39-
4036
Stream<Model> findAllByIdIn(Set<Long> ids);
4137

4238
Optional<Model> findByGuid(String guid);
@@ -60,4 +56,16 @@ public interface ModelRepository extends JpaRepository<Model, Long> {
6056
" GROUP BY " +
6157
" m.guid")
6258
List<ModelSubjectProjection> getModelSubjectsCount();
59+
60+
@Query("""
61+
SELECT DISTINCT
62+
new com.exadel.frs.commonservice.entity.ModelProjection(
63+
m.guid, m.name, m.apiKey, m.type, m.createdDate
64+
)
65+
FROM
66+
Model m LEFT JOIN m.app a
67+
WHERE
68+
a.id = :appId
69+
""")
70+
List<ModelProjection> findAllByAppId(Long appId);
6371
}

0 commit comments

Comments
 (0)