Skip to content

Commit

Permalink
P2p change (#19)
Browse files Browse the repository at this point in the history
* Generalize interfaces

* Add tests for p2p

* Add currency function

* Added naming lists

* Change logic exception
  • Loading branch information
strug authored Oct 30, 2019
1 parent 90a88fa commit d714889
Show file tree
Hide file tree
Showing 51 changed files with 953 additions and 478 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</parent>

<artifactId>fraudo</artifactId>
<version>0.0.8-SNAPSHOT</version>
<version>0.0.9-SNAPSHOT</version>

<properties>
<hamcrest.junit.version>2.0.0.0</hamcrest.junit.version>
Expand Down
10 changes: 10 additions & 0 deletions src/main/antlr4/com.rbkmoney.fraudo/Fraudo.g4
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ expression
| in_white_list #inWhiteListExpression
| in_black_list #inBlackListExpression
| in_grey_list #inGreyListExpression
| in_list #inListExpression
| like #likeFunctionExpression
| country #countryFunctionExpression
| country_by #countryByFunctionExpression
| amount #amountFunctionExpression
| currency #currencyFunctionExpression
| IDENTIFIER #identifierExpression
| DECIMAL #decimalExpression
| STRING #stringExpression
Expand All @@ -50,6 +52,10 @@ amount
: 'amount' LPAREN RPAREN
;

currency
: 'currency' LPAREN RPAREN
;

count
: 'count' LPAREN STRING time_window (group_by)? RPAREN
;
Expand Down Expand Up @@ -94,6 +100,10 @@ in_grey_list
: 'inGreyList' LPAREN string_list RPAREN
;

in_list
: 'inList' LPAREN STRING DELIMETER string_list RPAREN
;

like
: 'like' LPAREN STRING DELIMETER STRING RPAREN
;
Expand Down
19 changes: 4 additions & 15 deletions src/main/java/com/rbkmoney/fraudo/aggregator/CountAggregator.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,15 @@
package com.rbkmoney.fraudo.aggregator;

import com.rbkmoney.fraudo.constant.CheckedField;
import com.rbkmoney.fraudo.model.FraudModel;
import com.rbkmoney.fraudo.model.TimeWindow;

import java.util.List;

public interface CountAggregator {
public interface CountAggregator<T, U> {

@Deprecated
Integer count(CheckedField checkedField, FraudModel model, Long timeInMinutes);
Integer count(U checkedField, T model, TimeWindow timeWindow, List<U> fields);

Integer count(CheckedField checkedField, FraudModel model, TimeWindow timeWindow, List<CheckedField> fields);
Integer countSuccess(U checkedField, T model, TimeWindow timeWindow, List<U> fields);

@Deprecated
Integer countSuccess(CheckedField checkedField, FraudModel model, Long timeInMinutes);

Integer countSuccess(CheckedField checkedField, FraudModel model, TimeWindow timeWindow, List<CheckedField> fields);

@Deprecated
Integer countError(CheckedField checkedField, FraudModel model, Long timeInMinutes, String errorCode);

Integer countError(CheckedField checkedField, FraudModel model, TimeWindow timeWindow, String errorCode, List<CheckedField> fields);
Integer countError(U checkedField, T model, TimeWindow timeWindow, String errorCode, List<U> fields);

}
19 changes: 4 additions & 15 deletions src/main/java/com/rbkmoney/fraudo/aggregator/SumAggregator.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,15 @@
package com.rbkmoney.fraudo.aggregator;

import com.rbkmoney.fraudo.constant.CheckedField;
import com.rbkmoney.fraudo.model.FraudModel;
import com.rbkmoney.fraudo.model.TimeWindow;

import java.util.List;

public interface SumAggregator {
public interface SumAggregator<T, U> {

@Deprecated
Double sum(CheckedField checkedField, FraudModel model, Long timeInMinutes);
Double sum(U checkedField, T model, TimeWindow timeWindow, List<U> fields);

Double sum(CheckedField checkedField, FraudModel model, TimeWindow timeWindow, List<CheckedField> fields);
Double sumSuccess(U checkedField, T model, TimeWindow timeWindow, List<U> fields);

@Deprecated
Double sumSuccess(CheckedField checkedField, FraudModel model, Long timeInMinutes);

Double sumSuccess(CheckedField checkedField, FraudModel model, TimeWindow timeWindow, List<CheckedField> fields);

@Deprecated
Double sumError(CheckedField checkedField, FraudModel model, Long timeInMinutes, String errorCode);

Double sumError(CheckedField checkedField, FraudModel model, TimeWindow timeWindow, String errorCode, List<CheckedField> fields);
Double sumError(U checkedField, T model, TimeWindow timeWindow, String errorCode, List<U> fields);

}
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
package com.rbkmoney.fraudo.aggregator;

import com.rbkmoney.fraudo.constant.CheckedField;
import com.rbkmoney.fraudo.model.FraudModel;
import com.rbkmoney.fraudo.model.TimeWindow;

import java.util.List;

public interface UniqueValueAggregator {
public interface UniqueValueAggregator<T, U> {

@Deprecated
Integer countUniqueValue(CheckedField countField, FraudModel fraudModel, CheckedField onField, Long time);

Integer countUniqueValue(CheckedField countField, FraudModel fraudModel, CheckedField onField, TimeWindow timeWindow, List<CheckedField> fields);
Integer countUniqueValue(U countField, T payoutModel, U onField, TimeWindow timeWindow, List<U> fields);

}
36 changes: 36 additions & 0 deletions src/main/java/com/rbkmoney/fraudo/constant/P2PCheckedField.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.rbkmoney.fraudo.constant;

import java.util.HashMap;
import java.util.Map;

public enum P2PCheckedField {

EMAIL("email"),
IP("ip"),
FINGERPRINT("fingerprint"),
COUNTRY_BANK("country_bank"),
BIN("bin"),
PAN("pan"),
CURRENCY("currency"),
IDENTITY_ID("identity_id"),
CARD_TOKEN_FROM("card_token_from"),
CARD_TOKEN_TO("card_token_to");

private String value;
private static Map<String, P2PCheckedField> valueMap = new HashMap<>();

static {
for (P2PCheckedField value : P2PCheckedField.values()) {
valueMap.put(value.value, value);
}
}

P2PCheckedField(String value) {
this.value = value;
}

public static P2PCheckedField getByValue(String value) {
return valueMap.get(value);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.util.HashMap;
import java.util.Map;

public enum CheckedField {
public enum PaymentCheckedField {

EMAIL("email"),
IP("ip"),
Expand All @@ -12,24 +12,25 @@ public enum CheckedField {
COUNTRY_IP("country_ip"),
BIN("bin"),
PAN("pan"),
CURRENCY("currency"),
SHOP_ID("shop_id"),
PARTY_ID("party_id"),
CARD_TOKEN("card_token");

private String value;
private static Map<String, CheckedField> valueMap = new HashMap<>();
private static Map<String, PaymentCheckedField> valueMap = new HashMap<>();

static {
for (CheckedField value : CheckedField.values()) {
for (PaymentCheckedField value : PaymentCheckedField.values()) {
valueMap.put(value.value, value);
}
}

CheckedField(String value) {
PaymentCheckedField(String value) {
this.value = value;
}

public static CheckedField getByValue(String value) {
public static PaymentCheckedField getByValue(String value) {
return valueMap.get(value);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.rbkmoney.fraudo.exception;

public class NotValidContextException extends RuntimeException {

private static final String ERROR_MESSAGE = "Context is not valid!";

public NotValidContextException() {
super(ERROR_MESSAGE);
}
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,39 @@
package com.rbkmoney.fraudo.factory;

import com.rbkmoney.fraudo.FraudoVisitor;
import com.rbkmoney.fraudo.aggregator.CountAggregator;
import com.rbkmoney.fraudo.aggregator.SumAggregator;
import com.rbkmoney.fraudo.aggregator.UniqueValueAggregator;
import com.rbkmoney.fraudo.finder.InListFinder;
import com.rbkmoney.fraudo.model.FraudModel;
import com.rbkmoney.fraudo.model.BaseModel;
import com.rbkmoney.fraudo.resolver.CountryResolver;
import com.rbkmoney.fraudo.visitor.*;
import com.rbkmoney.fraudo.resolver.FieldResolver;
import com.rbkmoney.fraudo.resolver.GroupByModelResolver;
import com.rbkmoney.fraudo.visitor.CountVisitor;
import com.rbkmoney.fraudo.visitor.CustomFuncVisitor;
import com.rbkmoney.fraudo.visitor.ListVisitor;
import com.rbkmoney.fraudo.visitor.SumVisitor;
import com.rbkmoney.fraudo.visitor.impl.*;

public class FastFraudVisitorFactory implements FraudVisitorFactory {
public class FastFraudVisitorFactory<T extends BaseModel, U> implements FraudVisitorFactory<T, U> {

@Override
public FraudoVisitor<Object> createVisitor(FraudModel model,
CountAggregator countAggregator,
SumAggregator sumAggregator,
UniqueValueAggregator uniqueValueAggregator,
CountryResolver countryResolver,
InListFinder blackListFinder,
InListFinder whiteListFinder,
InListFinder greyListFinder) {
CountVisitorImpl countVisitor = new CountVisitorImpl(model, countAggregator);
SumVisitorImpl sumVisitor = new SumVisitorImpl(model, sumAggregator);
ListVisitorImpl listVisitor = new ListVisitorImpl(model, blackListFinder, whiteListFinder, greyListFinder);
CustomFuncVisitorImpl customFuncVisitor = new CustomFuncVisitorImpl(model, uniqueValueAggregator, countryResolver);
return new FastFraudVisitorImpl(countVisitor, sumVisitor, listVisitor, customFuncVisitor);
public FastFraudVisitorImpl<T> createVisitor(
CountAggregator<T, U> countAggregator,
SumAggregator<T, U> sumAggregator,
UniqueValueAggregator<T, U> uniqueValueAggregator,
CountryResolver<U> countryResolver,
InListFinder<T, U> listFinder,
FieldResolver<T, U> fieldResolver,
GroupByModelResolver<T, U> groupByModelResolver) {
CountVisitor<T> countVisitor = new CountVisitorImpl<>(countAggregator, fieldResolver, groupByModelResolver);
SumVisitor<T> sumVisitor = new SumVisitorImpl<>(sumAggregator, fieldResolver, groupByModelResolver);
ListVisitor<T> listVisitor = new ListVisitorImpl<>(listFinder, fieldResolver);
CustomFuncVisitor<T> customFuncVisitor = new CustomFuncVisitorImpl<>(
uniqueValueAggregator,
countryResolver,
fieldResolver,
groupByModelResolver);
return new FastFraudVisitorImpl<>(countVisitor, sumVisitor, listVisitor, customFuncVisitor);
}

}
22 changes: 11 additions & 11 deletions src/main/java/com/rbkmoney/fraudo/factory/FraudVisitorFactory.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package com.rbkmoney.fraudo.factory;

import com.rbkmoney.fraudo.FraudoVisitor;
import com.rbkmoney.fraudo.aggregator.CountAggregator;
import com.rbkmoney.fraudo.aggregator.SumAggregator;
import com.rbkmoney.fraudo.aggregator.UniqueValueAggregator;
import com.rbkmoney.fraudo.finder.InListFinder;
import com.rbkmoney.fraudo.model.FraudModel;
import com.rbkmoney.fraudo.resolver.CountryResolver;
import com.rbkmoney.fraudo.resolver.FieldResolver;
import com.rbkmoney.fraudo.resolver.GroupByModelResolver;
import com.rbkmoney.fraudo.visitor.impl.FastFraudVisitorImpl;

public interface FraudVisitorFactory {
public interface FraudVisitorFactory<T, U> {

FraudoVisitor<Object> createVisitor(FraudModel model,
CountAggregator countAggregator,
SumAggregator sumAggregator,
UniqueValueAggregator uniqueValueAggregator,
CountryResolver countryResolver,
InListFinder blackListFinder,
InListFinder whiteListFinder,
InListFinder greyListFinder);
FastFraudVisitorImpl createVisitor(CountAggregator<T, U> countAggregator,
SumAggregator<T, U> sumAggregator,
UniqueValueAggregator<T, U> uniqueValueAggregator,
CountryResolver<U> countryResolver,
InListFinder<T, U> listFinder,
FieldResolver<T, U> fieldPairResolver,
GroupByModelResolver<T, U> groupByModelResolver);

}
13 changes: 9 additions & 4 deletions src/main/java/com/rbkmoney/fraudo/finder/InListFinder.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package com.rbkmoney.fraudo.finder;

import com.rbkmoney.fraudo.constant.CheckedField;

import com.rbkmoney.fraudo.model.Pair;

import java.util.List;

public interface InListFinder {
public interface InListFinder<T, U> {

Boolean findInBlackList(List<Pair<U, String>> fields, T model);

Boolean findInWhiteList(List<Pair<U, String>> fields, T model);

Boolean findInList(String partyId, String shopId, CheckedField field, String value);
Boolean findInGreyList(List<Pair<U, String>> fields, T model);

Boolean findInList(String partyId, String shopId, List<CheckedField> fields, List<String> value);
Boolean findInList(String name, List<Pair<U, String>> fields, T model);

}
14 changes: 14 additions & 0 deletions src/main/java/com/rbkmoney/fraudo/model/BaseModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.rbkmoney.fraudo.model;

import lombok.Data;

@Data
public class BaseModel {

private String ip;
private String email;
private String fingerprint;
private Long amount;
private String currency;

}
15 changes: 0 additions & 15 deletions src/main/java/com/rbkmoney/fraudo/model/GroupByModel.java

This file was deleted.

15 changes: 15 additions & 0 deletions src/main/java/com/rbkmoney/fraudo/model/P2PModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.rbkmoney.fraudo.model;

import lombok.Data;

@Data
public class P2PModel extends BaseModel {

private String bin;
private String pan;
private String country;
private String cardTokenFrom;
private String cardTokenTo;
private String identityId;

}
15 changes: 15 additions & 0 deletions src/main/java/com/rbkmoney/fraudo/model/Pair.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.rbkmoney.fraudo.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Pair<T, U> {

private T first;
private U second;

}
Loading

0 comments on commit d714889

Please sign in to comment.