juspay / hyperswitch

An open source payments switch written in Rust to make payments fast, reliable and affordable
https://hyperswitch.io/
Apache License 2.0
12.64k stars 1.36k forks source link

feat(analytics): add `first_attempt` as a filter for PaymentFilters #6604

Closed tsdk02 closed 1 day ago

tsdk02 commented 6 days ago

Type of Change

Description

Added first_attempt as a filter in PaymentFilters.

This is required for the new Analytics v2 Smart Retries Metrics, specifically Smart Retries Successful Distribution and Smart Retries Failure Distribution, for calculations only involving smart retries (ignoring first attempts).

Additional Changes

Motivation and Context

Helps in calculating certain metrics seamlessly.

How did you test it?

Hit the curl:

curl --location 'http://localhost:8080/analytics/v1/org/metrics/payments' \
--header 'Accept: */*' \
--header 'Accept-Language: en-US,en;q=0.9' \
--header 'Connection: keep-alive' \
--header 'Content-Type: application/json' \
--header 'Origin: http://localhost:9000' \
--header 'Referer: http://localhost:9000/' \
--header 'Sec-Fetch-Dest: empty' \
--header 'Sec-Fetch-Mode: cors' \
--header 'Sec-Fetch-Site: same-site' \
--header 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36' \
--header 'api-key: test_admin' \
--header 'authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiNTQ5ZTNkMmItMTY5Yi00NzUzLWJmNTQtZDcxMTM2YjRiN2JkIiwibWVyY2hhbnRfaWQiOiJtZXJjaGFudF8xNzI2MDQ2MzI4Iiwicm9sZV9pZCI6Im9yZ19hZG1pbiIsImV4cCI6MTczMjYwODUzNCwib3JnX2lkIjoib3JnX1ZwU0hPanNZZkR2YWJWWUpnQ0FKIiwicHJvZmlsZV9pZCI6InByb192NXNGb0hlODBPZWlVbElvbm9jTSIsInRlbmFudF9pZCI6InB1YmxpYyJ9.tQYvWyhWQInhAymh0c4itE1CWVwhOG3L4_yHMY4RLkQ' \
--header 'sec-ch-ua: "Chromium";v="128", "Not;A=Brand";v="24", "Google Chrome";v="128"' \
--header 'sec-ch-ua-mobile: ?0' \
--header 'sec-ch-ua-platform: "macOS"' \
--data '[
    {
        "timeRange": {
            "startTime": "2024-11-19T00:30:00Z",
            "endTime": "2024-11-19T23:30:00Z"
        },
        "groupByNames": [
            "connector"
        ],
        "filters": {
            "first_attempt": [false]
        },
        "source": "BATCH",
        "metrics": [
            "payments_distribution"
        ],
        "delta": true
    }
]'

Response body:

{
    "queryData": [
        {
            "payment_success_rate": null,
            "payment_count": null,
            "payment_success_count": null,
            "payment_processed_amount": 0,
            "payment_processed_amount_in_usd": null,
            "payment_processed_count": null,
            "payment_processed_amount_without_smart_retries": 0,
            "payment_processed_amount_without_smart_retries_usd": null,
            "payment_processed_count_without_smart_retries": null,
            "avg_ticket_size": null,
            "payment_error_message": null,
            "retries_count": null,
            "retries_amount_processed": 0,
            "connector_success_rate": null,
            "payments_success_rate_distribution": 100.0,
            "payments_success_rate_distribution_without_smart_retries": null,
            "payments_success_rate_distribution_with_only_retries": 100.0,
            "payments_failure_rate_distribution": 0.0,
            "payments_failure_rate_distribution_without_smart_retries": null,
            "payments_failure_rate_distribution_with_only_retries": 0.0,
            "failure_reason_count": 0,
            "failure_reason_count_without_smart_retries": 0,
            "currency": null,
            "status": null,
            "connector": "stripe_test",
            "authentication_type": null,
            "payment_method": null,
            "payment_method_type": null,
            "client_source": null,
            "client_version": null,
            "profile_id": null,
            "card_network": null,
            "merchant_id": null,
            "card_last_4": null,
            "card_issuer": null,
            "error_reason": null,
            "time_range": {
                "start_time": "2024-11-19T00:30:00.000Z",
                "end_time": "2024-11-19T23:30:00.000Z"
            },
            "time_bucket": "2024-11-19 00:30:00"
        }
    ],
    "metaData": [
        {
            "total_payment_processed_amount": 0,
            "total_payment_processed_amount_in_usd": 0,
            "total_payment_processed_amount_without_smart_retries": 0,
            "total_payment_processed_amount_without_smart_retries_usd": 0,
            "total_payment_processed_count": 0,
            "total_payment_processed_count_without_smart_retries": 0,
            "total_failure_reasons_count": 0,
            "total_failure_reasons_count_without_smart_retries": 0
        }
    ]
}

Now this response will only have data related to the attempts which are not the first attempt, and are only subsequent smart rertries. Fields that can be observed in the queryData:

"payments_success_rate_distribution_with_only_retries": 100.0,
"payments_failure_rate_distribution_with_only_retries": 0.0,

Checklist

semanticdiff-com[bot] commented 6 days ago

Review changes with  SemanticDiff

Changed Files
| File | Status | | :--- | :--- | | [](https://app.semanticdiff.com/gh/juspay/hyperswitch/pull/6604/changes#crates/analytics/src/payments/filters.rs)  [crates/analytics/src/payments/filters\.rs](https://app.semanticdiff.com/gh/juspay/hyperswitch/pull/6604/changes#crates/analytics/src/payments/filters.rs) | [](https://app.semanticdiff.com/gh/juspay/hyperswitch/pull/6604/changes#crates/analytics/src/payments/filters.rs)  0% smaller | | [](https://app.semanticdiff.com/gh/juspay/hyperswitch/pull/6604/changes#crates/analytics/src/payments/types.rs)  [crates/analytics/src/payments/types\.rs](https://app.semanticdiff.com/gh/juspay/hyperswitch/pull/6604/changes#crates/analytics/src/payments/types.rs) | [](https://app.semanticdiff.com/gh/juspay/hyperswitch/pull/6604/changes#crates/analytics/src/payments/types.rs)  0% smaller | | [](https://app.semanticdiff.com/gh/juspay/hyperswitch/pull/6604/changes#crates/analytics/src/query.rs)  [crates/analytics/src/query\.rs](https://app.semanticdiff.com/gh/juspay/hyperswitch/pull/6604/changes#crates/analytics/src/query.rs) | [](https://app.semanticdiff.com/gh/juspay/hyperswitch/pull/6604/changes#crates/analytics/src/query.rs)  0% smaller | | [](https://app.semanticdiff.com/gh/juspay/hyperswitch/pull/6604/changes#crates/analytics/src/sqlx.rs)  [crates/analytics/src/sqlx\.rs](https://app.semanticdiff.com/gh/juspay/hyperswitch/pull/6604/changes#crates/analytics/src/sqlx.rs) | [](https://app.semanticdiff.com/gh/juspay/hyperswitch/pull/6604/changes#crates/analytics/src/sqlx.rs)  0% smaller | | [](https://app.semanticdiff.com/gh/juspay/hyperswitch/pull/6604/changes#crates/api_models/src/analytics/payments.rs)  [crates/api\_models/src/analytics/payments\.rs](https://app.semanticdiff.com/gh/juspay/hyperswitch/pull/6604/changes#crates/api_models/src/analytics/payments.rs) | [](https://app.semanticdiff.com/gh/juspay/hyperswitch/pull/6604/changes#crates/api_models/src/analytics/payments.rs)  0% smaller |