mirror of
https://github.com/valitydev/fraudo.git
synced 2024-11-06 01:45:16 +00:00
Add new function for count pending (#43)
This commit is contained in:
parent
1387eec33b
commit
1be8cd7488
2
pom.xml
2
pom.xml
@ -11,7 +11,7 @@
|
||||
</parent>
|
||||
|
||||
<artifactId>fraudo</artifactId>
|
||||
<version>1.0.4</version>
|
||||
<version>1.0.5</version>
|
||||
|
||||
<name>Fraudo</name>
|
||||
<description>Language for describing antifraud patterns</description>
|
||||
|
@ -50,6 +50,7 @@ unaryExpression
|
||||
|
||||
integerExpression
|
||||
: count
|
||||
| count_pending
|
||||
| count_success
|
||||
| count_error
|
||||
| count_chargeback
|
||||
@ -72,6 +73,10 @@ count_success
|
||||
: 'countSuccess' LPAREN STRING time_window (group_by)? RPAREN
|
||||
;
|
||||
|
||||
count_pending
|
||||
: 'countPending' LPAREN STRING time_window (group_by)? RPAREN
|
||||
;
|
||||
|
||||
count_error
|
||||
: 'countError' LPAREN STRING time_window (DELIMITER STRING)? (group_by)? RPAREN
|
||||
;
|
||||
|
@ -13,6 +13,8 @@ public interface CountPaymentAggregator<T, U> extends CountAggregator<T, U> {
|
||||
|
||||
Integer countError(U checkedField, T model, TimeWindow timeWindow, List<U> fields);
|
||||
|
||||
Integer countPending(U checkedField, T model, TimeWindow timeWindow, List<U> fields);
|
||||
|
||||
Integer countChargeback(U checkedField, T model, TimeWindow timeWindow, List<U> fields);
|
||||
|
||||
Integer countRefund(U checkedField, T model, TimeWindow timeWindow, List<U> fields);
|
||||
|
@ -8,6 +8,8 @@ public interface CountVisitor<T> {
|
||||
|
||||
Integer visitCountSuccess(Count_successContext ctx, T model);
|
||||
|
||||
Integer visitCountPending(Count_pendingContext ctx, T model);
|
||||
|
||||
Integer visitCountError(Count_errorContext ctx, T model);
|
||||
|
||||
Integer visitCountChargeback(Count_chargebackContext ctx, T model);
|
||||
|
@ -39,6 +39,17 @@ public class CountVisitorImpl<T, U> implements CountVisitor<T> {
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer visitCountPending(FraudoPaymentParser.Count_pendingContext ctx, T model) {
|
||||
String countTarget = TextUtil.safeGetText(ctx.STRING());
|
||||
return countPaymentAggregator.countPending(
|
||||
fieldResolver.resolveName(countTarget),
|
||||
model,
|
||||
timeWindowResolver.resolve(ctx.time_window()),
|
||||
groupResolver.resolve(ctx.group_by())
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer visitCountError(FraudoPaymentParser.Count_errorContext ctx, T model) {
|
||||
String countTarget = TextUtil.safeGetText(ctx.STRING(0));
|
||||
|
@ -296,6 +296,15 @@ public class FirstFindVisitorImpl<T extends BaseModel, U> extends FraudoPaymentB
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer visitCount_pending(Count_pendingContext ctx) {
|
||||
String key = CountKeyGenerator.generatePendingKey(ctx, fieldResolver::resolveName);
|
||||
return (Integer) localFuncCache.get().computeIfAbsent(
|
||||
key,
|
||||
s -> countVisitor.visitCountPending(ctx, threadLocalModel.get())
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer visitCount_error(Count_errorContext ctx) {
|
||||
String key = CountKeyGenerator.generateErrorKey(ctx, fieldResolver::resolveName);
|
||||
|
@ -26,6 +26,14 @@ public class CountKeyGenerator {
|
||||
resolve);
|
||||
}
|
||||
|
||||
public static <T> String generatePendingKey(Count_pendingContext ctx, Function<String, T> resolve) {
|
||||
return CommonKeyGenerator.generateKeyGroupedFunction(ctx.STRING(),
|
||||
ctx.children.get(0),
|
||||
ctx.time_window(),
|
||||
ctx.group_by(),
|
||||
resolve);
|
||||
}
|
||||
|
||||
public static <T> String generateErrorKey(Count_errorContext ctx, Function<String, T> resolve) {
|
||||
if (ctx.STRING().size() == 2) {
|
||||
return CommonKeyGenerator.generateKeyGroupedTwoFieldFunction(ctx.STRING(0),
|
||||
|
@ -53,6 +53,16 @@ public class CountTest extends AbstractPaymentTest {
|
||||
assertEquals("0", ResultUtils.findFirstNotNotifyStatus(result).get().getRuleChecked());
|
||||
}
|
||||
|
||||
@Test
|
||||
void countPendingTest() throws Exception {
|
||||
InputStream resourceAsStream = CountTest.class.getResourceAsStream("/rules/countPending.frd");
|
||||
when(countPaymentAggregator.countPending(anyObject(), any(), any(), any())).thenReturn(6);
|
||||
ParseContext parseContext = getParseContext(resourceAsStream);
|
||||
ResultModel result = invokeParse(parseContext);
|
||||
assertEquals(ResultStatus.DECLINE, ResultUtils.findFirstNotNotifyStatus(result).get().getResultStatus());
|
||||
assertEquals("0", ResultUtils.findFirstNotNotifyStatus(result).get().getRuleChecked());
|
||||
}
|
||||
|
||||
@Test
|
||||
void countCargeRefundTest() throws Exception {
|
||||
InputStream resourceAsStream = CountTest.class.getResourceAsStream("/rules/count_chargeback_refund.frd");
|
||||
|
2
src/test/resources/rules/countPending.frd
Normal file
2
src/test/resources/rules/countPending.frd
Normal file
@ -0,0 +1,2 @@
|
||||
rule: countPending("fingerprint", 1444) > 5
|
||||
-> decline;
|
Loading…
Reference in New Issue
Block a user