JD-452 add sorting

This commit is contained in:
ggmaleva 2021-07-20 18:28:04 +03:00
parent ebeefdaad2
commit 8bfdcae93b
No known key found for this signature in database
GPG Key ID: 0E412B78565B108F
10 changed files with 82 additions and 17 deletions

View File

@ -25,7 +25,7 @@
<fraudo.version>0.0.22</fraudo.version>
<wb.list.proto.version>1.33-554d59c</wb.list.proto.version>
<clickhouse-jdbc.version>0.3.1</clickhouse-jdbc.version>
<fraudbusters-proto.version>1.80-46a1cc1</fraudbusters-proto.version>
<fraudbusters-proto.version>1.83-6d01db2</fraudbusters-proto.version>
<machinegun-proto.version>1.18-d814d69</machinegun-proto.version>
<sonar.issue.ignore.multicriteria>e1</sonar.issue.ignore.multicriteria>

View File

@ -0,0 +1,6 @@
package com.rbkmoney.fraudbusters.constant;
public enum SortOrder {
ASC,
DESC
}

View File

@ -2,8 +2,10 @@ package com.rbkmoney.fraudbusters.converter;
import com.rbkmoney.damsel.fraudbusters.Filter;
import com.rbkmoney.damsel.fraudbusters.Page;
import com.rbkmoney.damsel.fraudbusters.Sort;
import com.rbkmoney.fraudbusters.constant.PaymentField;
import com.rbkmoney.fraudbusters.service.dto.FilterDto;
import com.rbkmoney.fraudbusters.service.dto.SortDto;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Component;
@ -13,7 +15,7 @@ import java.util.Map;
@Component
public class FilterConverter {
public FilterDto convert(Filter filter, Page page) {
public FilterDto convert(Filter filter, Page page, Sort sort) {
FilterDto filterDto = new FilterDto();
Map<PaymentField, String> searchPatterns = assembleSearchPatterns(filter);
filterDto.setSearchPatterns(searchPatterns);
@ -23,6 +25,10 @@ public class FilterConverter {
filterDto.setTimeFrom(filter.getInterval().getLowerBound().getBoundTime());
filterDto.setTimeTo(filter.getInterval().getUpperBound().getBoundTime());
}
SortDto sortDto = new SortDto();
sortDto.setField(sort.getField());
sortDto.setOrder(com.rbkmoney.fraudbusters.constant.SortOrder.valueOf(sort.getOrder().name()));
filterDto.setSort(sortDto);
return filterDto;
}

View File

@ -3,6 +3,7 @@ package com.rbkmoney.fraudbusters.repository.impl;
import com.google.common.base.Splitter;
import com.rbkmoney.fraudbusters.constant.EventSource;
import com.rbkmoney.fraudbusters.constant.PaymentField;
import com.rbkmoney.fraudbusters.constant.SortOrder;
import com.rbkmoney.fraudbusters.domain.CheckedPayment;
import com.rbkmoney.fraudbusters.repository.HistoricalDataRepository;
import com.rbkmoney.fraudbusters.repository.mapper.CheckedPaymentMapper;
@ -68,10 +69,15 @@ public class HistoricalDataRepositoryImpl implements HistoricalDataRepository {
params.addValue("id", compositeId.get(0))
.addValue("status", compositeId.get(1));
}
filters.append(" and (id < :id or (status != :status and id = :id)) ");
String paging = " and (id %s :id or (status != :status and id = :id)) ";
if (SortOrder.DESC.equals(filter.getSort().getOrder())) {
filters.append(String.format(paging, "<"));
} else {
filters.append(String.format(paging, ">"));
}
}
String pagination = "ORDER BY id DESC LIMIT :size";
String query = select + filters.toString() + pagination;
String sorting = "ORDER BY (eventTime, id) " + filter.getSort().getOrder().name() + " LIMIT :size";
String query = select + filters.toString() + sorting;
params.addValue("from", filter.getTimeFrom())
.addValue("to", filter.getTimeTo())

View File

@ -22,8 +22,8 @@ public class HistoricalDataHandler implements HistoricalDataServiceSrv.Iface {
private final FilterConverter filterConverter;
@Override
public PaymentInfoResult getPayments(Filter filter, Page page) {
FilterDto filterDto = filterConverter.convert(filter, page);
public PaymentInfoResult getPayments(Filter filter, Page page, Sort sort) {
FilterDto filterDto = filterConverter.convert(filter, page, sort);
HistoricalPaymentsDto historicalPaymentsDto = historicalDataService.getPayments(filterDto);
return buildResult(historicalPaymentsDto);
}

View File

@ -15,5 +15,6 @@ public class FilterDto {
private String timeFrom;
private String timeTo;
private Map<PaymentField, String> searchPatterns;
private SortDto sort;
}

View File

@ -0,0 +1,12 @@
package com.rbkmoney.fraudbusters.service.dto;
import com.rbkmoney.fraudbusters.constant.SortOrder;
import lombok.Data;
@Data
public class SortDto {
private SortOrder order;
private String field;
}

View File

@ -4,6 +4,8 @@ import com.rbkmoney.damsel.base.TimestampInterval;
import com.rbkmoney.damsel.base.TimestampIntervalBound;
import com.rbkmoney.damsel.fraudbusters.Filter;
import com.rbkmoney.damsel.fraudbusters.Page;
import com.rbkmoney.damsel.fraudbusters.Sort;
import com.rbkmoney.damsel.fraudbusters.SortOrder;
import com.rbkmoney.fraudbusters.TestObjectsFactory;
import com.rbkmoney.fraudbusters.constant.PaymentField;
import com.rbkmoney.fraudbusters.service.dto.FilterDto;
@ -55,8 +57,11 @@ class FilterConverterTest {
Long size = TestObjectsFactory.randomLong();
page.setSize(size);
page.setContinuationId(continuationId);
Sort sort = new Sort();
sort.setField(TestObjectsFactory.randomString());
sort.setOrder(SortOrder.DESC);
FilterDto dto = filterConverter.convert(filter, page);
FilterDto dto = filterConverter.convert(filter, page, sort);
assertEquals(page.getSize(), dto.getSize());
assertEquals(page.getContinuationId(), dto.getLastId());
@ -72,6 +77,8 @@ class FilterConverterTest {
assertEquals(filter.getProviderCountry(), searchPatterns.get(PaymentField.BANK_COUNTRY));
assertEquals(filter.getTerminal(), searchPatterns.get(PaymentField.TERMINAL));
assertEquals(filter.getPaymentId(), searchPatterns.get(PaymentField.ID));
assertEquals(sort.getField(), dto.getSort().getField());
assertEquals(sort.getOrder(), SortOrder.valueOf(dto.getSort().getOrder().name()));
}

View File

@ -3,10 +3,12 @@ package com.rbkmoney.fraudbusters.repository.impl;
import com.rbkmoney.clickhouse.initializer.ChInitializer;
import com.rbkmoney.fraudbusters.config.ClickhouseConfig;
import com.rbkmoney.fraudbusters.constant.PaymentField;
import com.rbkmoney.fraudbusters.constant.SortOrder;
import com.rbkmoney.fraudbusters.domain.CheckedPayment;
import com.rbkmoney.fraudbusters.repository.HistoricalDataRepository;
import com.rbkmoney.fraudbusters.repository.mapper.CheckedPaymentMapper;
import com.rbkmoney.fraudbusters.service.dto.FilterDto;
import com.rbkmoney.fraudbusters.service.dto.SortDto;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@ -50,7 +52,12 @@ class HistoricalDataRepositoryImplTest {
FilterDto filter = new FilterDto();
filter.setTimeFrom("2020-05-01T18:04:53");
filter.setTimeTo("2020-10-01T18:04:53");
SortDto sortDto = new SortDto();
sortDto.setOrder(SortOrder.DESC);
filter.setSort(sortDto);
List<CheckedPayment> payments = historicalDataRepository.getPayments(filter);
assertFalse(payments.isEmpty());
assertEquals(6, payments.size());
}
@ -64,7 +71,12 @@ class HistoricalDataRepositoryImplTest {
patterns.put(PaymentField.PARTY_ID, "partyId_2");
patterns.put(PaymentField.SHOP_ID, "2035728");
filter.setSearchPatterns(patterns);
SortDto sortDto = new SortDto();
sortDto.setOrder(SortOrder.DESC);
filter.setSort(sortDto);
List<CheckedPayment> payments = historicalDataRepository.getPayments(filter);
assertFalse(payments.isEmpty());
assertEquals(1, payments.size());
assertEquals("2035728", payments.get(0).getShopId());
@ -77,33 +89,48 @@ class HistoricalDataRepositoryImplTest {
filter.setTimeFrom("2020-05-01T18:04:53");
filter.setTimeTo("2020-10-01T18:04:53");
filter.setSize(3L);
SortDto sortDto = new SortDto();
sortDto.setOrder(SortOrder.DESC);
filter.setSort(sortDto);
List<CheckedPayment> payments = historicalDataRepository.getPayments(filter);
assertFalse(payments.isEmpty());
assertEquals(3, payments.size());
}
@Test
void getPaymentsByTimeSlotAndPage() {
void getPaymentsByTimeSlotAndPageAndSort() {
FilterDto filter = new FilterDto();
filter.setTimeFrom("2020-05-01T18:04:53");
filter.setTimeTo("2020-10-01T18:04:53");
filter.setLastId("1DkraVdGJfs.1-processed");
SortDto sortDto = new SortDto();
sortDto.setOrder(SortOrder.DESC);
filter.setSort(sortDto);
List<CheckedPayment> payments = historicalDataRepository.getPayments(filter);
assertFalse(payments.isEmpty());
assertEquals(3, payments.size());
}
@Test
void getPaymentsByTimeSlotAndPageAndSearchPatterns() {
void getPaymentsByTimeSlotAndPageAndSearchPatternsAndSort() {
FilterDto filter = new FilterDto();
filter.setTimeFrom("2020-05-01T18:04:53");
filter.setTimeTo("2020-10-01T18:04:53");
filter.setSize(3L);
filter.setLastId("1DkraVdGJfs.1-processed");
SortDto sortDto = new SortDto();
sortDto.setOrder(SortOrder.DESC);
filter.setSort(sortDto);
Map<PaymentField, String> patterns = new HashMap<>();
patterns.put(PaymentField.PARTY_ID, "partyId_2");
filter.setSearchPatterns(patterns);
List<CheckedPayment> payments = historicalDataRepository.getPayments(filter);
assertFalse(payments.isEmpty());
assertEquals(1, payments.size());
assertEquals("partyId_2", payments.get(0).getPartyId());

View File

@ -1,9 +1,6 @@
package com.rbkmoney.fraudbusters.resource;
import com.rbkmoney.damsel.fraudbusters.Filter;
import com.rbkmoney.damsel.fraudbusters.Page;
import com.rbkmoney.damsel.fraudbusters.PaymentInfo;
import com.rbkmoney.damsel.fraudbusters.PaymentInfoResult;
import com.rbkmoney.damsel.fraudbusters.*;
import com.rbkmoney.fraudbusters.TestObjectsFactory;
import com.rbkmoney.fraudbusters.converter.CheckedPaymentToPaymentInfoConverter;
import com.rbkmoney.fraudbusters.converter.FilterConverter;
@ -43,13 +40,14 @@ class HistoricalDataHandlerTest {
void getPaymentsWithoutPayments() {
Filter filter = new Filter();
Page page = new Page();
Sort sort = new Sort();
HistoricalPaymentsDto dto = HistoricalPaymentsDto.builder()
.payments(Collections.emptyList())
.lastId(null)
.build();
when(service.getPayments(any(FilterDto.class))).thenReturn(dto);
PaymentInfoResult actualPayments = handler.getPayments(filter, page);
PaymentInfoResult actualPayments = handler.getPayments(filter, page, sort);
assertNull(actualPayments.getContinuationId());
assertTrue(actualPayments.getPayments().isEmpty());
@ -59,6 +57,7 @@ class HistoricalDataHandlerTest {
void getPaymentsWithoutLastId() {
Filter filter = new Filter();
Page page = new Page();
Sort sort = new Sort();
CheckedPayment checkedPayment = TestObjectsFactory.testCheckedPayment();
HistoricalPaymentsDto dto = HistoricalPaymentsDto.builder()
.payments(List.of(checkedPayment))
@ -66,7 +65,7 @@ class HistoricalDataHandlerTest {
.build();
when(service.getPayments(any(FilterDto.class))).thenReturn(dto);
PaymentInfoResult actualPayments = handler.getPayments(filter, page);
PaymentInfoResult actualPayments = handler.getPayments(filter, page, sort);
assertNull(actualPayments.getContinuationId());
assertFalse(actualPayments.getPayments().isEmpty());
@ -97,6 +96,7 @@ class HistoricalDataHandlerTest {
void getPayments() {
Filter filter = new Filter();
Page page = new Page();
Sort sort = new Sort();
String lastId = TestObjectsFactory.randomString();
CheckedPayment checkedPayment = TestObjectsFactory.testCheckedPayment();
List<CheckedPayment> checkedPayments = List.of(checkedPayment);
@ -106,7 +106,7 @@ class HistoricalDataHandlerTest {
.build();
when(service.getPayments(any(FilterDto.class))).thenReturn(dto);
PaymentInfoResult actualPayments = handler.getPayments(filter, page);
PaymentInfoResult actualPayments = handler.getPayments(filter, page, sort);
assertEquals(lastId, actualPayments.getContinuationId());
assertEquals(checkedPayments.size(), actualPayments.getPaymentsSize());