Add new function for count pending (#43)

This commit is contained in:
struga 2024-05-15 16:36:58 +03:00 committed by GitHub
parent 1387eec33b
commit 1be8cd7488
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 50 additions and 1 deletions

View File

@ -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>

View File

@ -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
;

View File

@ -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);

View File

@ -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);

View File

@ -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));

View File

@ -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);

View File

@ -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),

View File

@ -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");

View File

@ -0,0 +1,2 @@
rule: countPending("fingerprint", 1444) > 5
-> decline;