This repository was archived by the owner on Oct 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
add exrate listener #72
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/main/java/dev/vality/newway/dao/exrate/iface/ExchangeRateDao.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package dev.vality.newway.dao.exrate.iface; | ||
|
|
||
| import dev.vality.dao.DaoException; | ||
| import dev.vality.dao.GenericDao; | ||
| import dev.vality.newway.domain.tables.pojos.ExRate; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public interface ExchangeRateDao extends GenericDao { | ||
| void saveBatch(List<ExRate> exchangeRates) throws DaoException; | ||
| ExRate findBySourceSymbolicCode(String symbolicCode); | ||
| } |
48 changes: 48 additions & 0 deletions
48
src/main/java/dev/vality/newway/dao/exrate/impl/ExchangeRateDaoImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| package dev.vality.newway.dao.exrate.impl; | ||
|
|
||
| import dev.vality.dao.DaoException; | ||
| import dev.vality.dao.impl.AbstractGenericDao; | ||
| import dev.vality.mapper.RecordRowMapper; | ||
| import dev.vality.newway.dao.exrate.iface.ExchangeRateDao; | ||
| import dev.vality.newway.domain.tables.pojos.ExRate; | ||
| import org.jooq.Query; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.beans.factory.annotation.Qualifier; | ||
| import org.springframework.jdbc.core.RowMapper; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| import javax.sql.DataSource; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import static dev.vality.newway.domain.tables.ExRate.EX_RATE; | ||
|
|
||
| @Component | ||
| public class ExchangeRateDaoImpl extends AbstractGenericDao implements ExchangeRateDao { | ||
|
|
||
| private final RowMapper<ExRate> rowMapper; | ||
|
|
||
| @Autowired | ||
| public ExchangeRateDaoImpl(@Qualifier("dataSource") DataSource dataSource) { | ||
| super(dataSource); | ||
| this.rowMapper = new RecordRowMapper<>(EX_RATE, ExRate.class); | ||
| } | ||
|
|
||
| @Override | ||
| public void saveBatch(List<ExRate> exchangeRates) throws DaoException { | ||
| List<Query> queryList = exchangeRates.stream() | ||
| .map(exrate -> getDslContext().newRecord(EX_RATE, exrate)) | ||
| .map(record -> (Query) getDslContext().insertInto(EX_RATE).set(record) | ||
| .onConflict(EX_RATE.EVENT_ID) | ||
| .doNothing()) | ||
| .collect(Collectors.toList()); | ||
| batchExecute(queryList); | ||
| } | ||
|
|
||
| @Override | ||
| public ExRate findBySourceSymbolicCode(String symbolicCode) { | ||
| Query query = getDslContext().selectFrom(EX_RATE) | ||
| .where(EX_RATE.SOURCE_CURRENCY_SYMBOLIC_CODE.eq(symbolicCode)); | ||
| return fetchOne(query, rowMapper); | ||
| } | ||
| } |
47 changes: 47 additions & 0 deletions
47
...n/java/dev/vality/newway/handler/event/stock/impl/exrate/CurrencyExchangeRateHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package dev.vality.newway.handler.event.stock.impl.exrate; | ||
|
|
||
| import dev.vality.exrates.events.CurrencyEvent; | ||
| import dev.vality.exrates.events.CurrencyExchangeRate; | ||
| import dev.vality.geck.common.util.TypeUtil; | ||
| import dev.vality.newway.dao.exrate.iface.ExchangeRateDao; | ||
| import dev.vality.newway.domain.tables.pojos.ExRate; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| import java.util.List; | ||
| import java.util.UUID; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| @Slf4j | ||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class CurrencyExchangeRateHandler implements ExchangeRateHandler { | ||
|
|
||
| private final ExchangeRateDao exchangeRateDao; | ||
|
|
||
| @Override | ||
| public void handle(List<CurrencyEvent> events) { | ||
| List<ExRate> exrates = events.stream().map(currencyEvent -> { | ||
| CurrencyExchangeRate exchangeRate = currencyEvent.getPayload().getExchangeRate(); | ||
| ExRate exrate = new ExRate(); | ||
| exrate.setEventId(UUID.fromString(currencyEvent.getEventId())); | ||
| exrate.setEventCreatedAt(TypeUtil.stringToLocalDateTime(currencyEvent.getEventCreatedAt())); | ||
| exrate.setSourceCurrencySymbolicCode(exchangeRate.getSourceCurrency().getSymbolicCode()); | ||
| exrate.setSourceCurrencyExponent(exchangeRate.getSourceCurrency().getExponent()); | ||
| exrate.setDestinationCurrencySymbolicCode(exchangeRate.getDestinationCurrency().getSymbolicCode()); | ||
| exrate.setDestinationCurrencyExponent(exchangeRate.getDestinationCurrency().getExponent()); | ||
| exrate.setRationalP(exchangeRate.getExchangeRate().getP()); | ||
| exrate.setRationalQ(exchangeRate.getExchangeRate().getQ()); | ||
| exrate.setRateTimestamp(TypeUtil.stringToLocalDateTime(exchangeRate.timestamp)); | ||
| return exrate; | ||
| }).collect(Collectors.toList()); | ||
|
|
||
| exchangeRateDao.saveBatch(exrates); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isHandle(CurrencyEvent currencyEvent) { | ||
| return currencyEvent.payload.isSetExchangeRate(); | ||
| } | ||
| } | ||
12 changes: 12 additions & 0 deletions
12
src/main/java/dev/vality/newway/handler/event/stock/impl/exrate/ExchangeRateHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package dev.vality.newway.handler.event.stock.impl.exrate; | ||
|
|
||
| import dev.vality.exrates.events.CurrencyEvent; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public interface ExchangeRateHandler { | ||
|
|
||
| void handle(List<CurrencyEvent> currencyEvents); | ||
|
|
||
| boolean isHandle(CurrencyEvent currencyEvent); | ||
| } |
36 changes: 36 additions & 0 deletions
36
src/main/java/dev/vality/newway/listener/ExchangeRateListener.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package dev.vality.newway.listener; | ||
|
|
||
| import dev.vality.exrates.events.CurrencyEvent; | ||
| import dev.vality.newway.service.ExchangeRateService; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.apache.kafka.clients.consumer.ConsumerRecord; | ||
| import org.springframework.kafka.annotation.KafkaListener; | ||
| import org.springframework.kafka.support.Acknowledgment; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| @Slf4j | ||
| @RequiredArgsConstructor | ||
| @Service | ||
| public class ExchangeRateListener { | ||
|
|
||
| private final ExchangeRateService exchangeRateService; | ||
|
|
||
| @KafkaListener( | ||
| autoStartup = "${kafka.topics.exrate.enabled}", | ||
| topics = "${kafka.topics.exrate.id}", | ||
| containerFactory = "exchangeRateContainerFactory") | ||
| public void handle(List<ConsumerRecord<String, CurrencyEvent>> messages, Acknowledgment ack) { | ||
| log.info("Got ExchangeRate messages batch with size: {}", messages.size()); | ||
| exchangeRateService.handleEvents( | ||
| messages.stream() | ||
| .map(ConsumerRecord::value) | ||
| .collect(Collectors.toList()) | ||
| ); | ||
| ack.acknowledge(); | ||
| log.info("Batch ExchangeRate has been committed, size={}", messages.size()); | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
src/main/java/dev/vality/newway/serde/CurrencyExchangeRateEventDeserializer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package dev.vality.newway.serde; | ||
|
|
||
| import dev.vality.exrates.events.CurrencyEvent; | ||
| import dev.vality.kafka.common.serialization.AbstractThriftDeserializer; | ||
|
|
||
| public class CurrencyExchangeRateEventDeserializer extends AbstractThriftDeserializer<CurrencyEvent> { | ||
| @Override | ||
| public CurrencyEvent deserialize(String topic, byte[] data) { | ||
| return deserialize(data, new CurrencyEvent()); | ||
| } | ||
| } |
29 changes: 29 additions & 0 deletions
29
src/main/java/dev/vality/newway/service/ExchangeRateService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package dev.vality.newway.service; | ||
|
|
||
| import dev.vality.exrates.events.CurrencyEvent; | ||
| import dev.vality.newway.handler.event.stock.impl.exrate.ExchangeRateHandler; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Propagation; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class ExchangeRateService { | ||
|
|
||
| private final List<ExchangeRateHandler> exchangeRateHandlers; | ||
|
|
||
| @Transactional(propagation = Propagation.REQUIRED) | ||
| public void handleEvents(List<CurrencyEvent> events) { | ||
| events.stream() | ||
| .collect(Collectors.groupingBy( | ||
| currencyEvent -> exchangeRateHandlers.stream() | ||
| .filter(exchangeRateHandler -> exchangeRateHandler.isHandle(currencyEvent)) | ||
| .findAny().orElseThrow()) | ||
| ).forEach(ExchangeRateHandler::handle); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/main/resources/db/migration/V7__add_new_exchange_rates.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| CREATE TABLE dw.ex_rate | ||
| ( | ||
| id BIGSERIAL NOT NULL, | ||
| event_id uuid UNIQUE NOT NULL, | ||
| event_created_at TIMESTAMP WITHOUT TIME ZONE NOT NULL, | ||
| source_currency_symbolic_code CHARACTER VARYING NOT NULL, | ||
| source_currency_exponent SMALLINT NOT NULL, | ||
| destination_currency_symbolic_code CHARACTER VARYING NOT NULL, | ||
| destination_currency_exponent SMALLINT NOT NULL, | ||
| rational_p BIGINT NOT NULL, | ||
| rational_q BIGINT NOT NULL, | ||
| rate_timestamp TIMESTAMP WITHOUT TIME ZONE NOT NULL | ||
| ); | ||
|
|
||
| CREATE INDEX rate_timestamp_idx ON dw.ex_rate (rate_timestamp); | ||
|
|
||
| CREATE INDEX source_currency_sc_destination_currency_sc_timestamp_idx ON dw.ex_rate (source_currency_symbolic_code, | ||
| destination_currency_symbolic_code, | ||
| rate_timestamp); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,7 +28,8 @@ | |
| "kafka.topics.source.enabled=true", | ||
| "kafka.topics.destination.enabled=true", | ||
| "kafka.topics.pm-events-payout.enabled=true", | ||
| "kafka.topics.limit-config.enabled=true"}, | ||
| "kafka.topics.limit-config.enabled=true", | ||
| "kafka.topics.exrate.enabled=true"}, | ||
| topicsKeys = { | ||
| "kafka.topics.invoice.id", | ||
| "kafka.topics.recurrent-payment-tool.id", | ||
|
|
@@ -42,7 +43,10 @@ | |
| "kafka.topics.source.id", | ||
| "kafka.topics.destination.id", | ||
| "kafka.topics.pm-events-payout.id", | ||
| "kafka.topics.limit-config.id"}) | ||
| "kafka.topics.limit-config.id", | ||
| "kafka.topics.limit-config.id", | ||
|
Comment on lines
+46
to
+47
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. дубль |
||
| "kafka.topics.exrate.id"} | ||
| ) | ||
| @DefaultSpringBootTest | ||
| @Import(KafkaProducer.class) | ||
| public @interface KafkaPostgresqlSpringBootITest { | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Опционально, не хочешь вынести в конвертер или отдельный метод внутри handler'а?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nononononono
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
По-хорошему, тут везде надо рефакторить и заводить конвертеры. Ну короч хз, читабельнее щас от этого не станет. Если очень хочешь, тогда сделаю конечно.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Не, у меня были сомнения и я решил тебе о них сказать :)
вдруг бы тоже сомнения породил xD