Rework ad serving: signed impression tokens, per-campaign daily caps, stats
Follows the usual ad-server flow (serve -> impression -> click):
- GET /api/ads/next returns an impressionToken: HMAC-signed (ads.tracking.secret,
defaults to jwt.secret) ad id, campaign id, subject and nonce, valid 1 hour.
/impression and /click accept it, verify the signature, count each nonce once
(Redis SET NX) and take the user/session from the token, so impressions
cannot be forged or replayed. Requests without a token keep working until
ads.tracking.require-token is switched on after the frontend update.
- The frequency cap is per campaign and calendar day (ads.timezone) and grows
on real impressions, not on serving: ad:cap:<subject>:<campaign>:<yyyyMMdd>.
- Selection reads a 30-second in-memory snapshot of eligible ads (projection,
invalidated locally on admin changes), filters capped campaigns with one
MGET, picks by weight and then sets the cooldown atomically. The weighted
sort drew random numbers inside the comparator, breaking its contract.
- Campaigns past endDate are finished every 10 minutes (ShedLock).
- GET /api/v1/admin/campaigns/{id}/stats: impressions, clicks and CTR per ad
for a date range; composite indexes on impression/click (changelog 021).
- Removed unused cascade collections on Campaign/Advertising, the unused
FullscreenAdActivateRequestDto and the /api/ads/impression/beacon rule.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -4,7 +4,9 @@ public record AdvertisingResponseDto(
|
||||
Long advertisingId,
|
||||
String type,
|
||||
String mediaUrl,
|
||||
String targetUrl
|
||||
String targetUrl,
|
||||
/** Подписанный токен показа: вернуть в /api/ads/impression и /api/ads/click. */
|
||||
String impressionToken
|
||||
) {
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.krylov.refound.dto.advertising;
|
||||
|
||||
/** Показы, клики и CTR (в процентах) одного объявления за период отчёта. */
|
||||
public record AdvertisingStatsDto(
|
||||
Long advertisingId,
|
||||
long impressions,
|
||||
long clicks,
|
||||
double ctr
|
||||
) {
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.krylov.refound.dto.advertising;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
|
||||
/** Отчёт по кампании за период [from, to] (оба дня включительно). CTR — в процентах. */
|
||||
public record CampaignStatsDto(
|
||||
Long campaignId,
|
||||
LocalDate from,
|
||||
LocalDate to,
|
||||
long impressions,
|
||||
long clicks,
|
||||
double ctr,
|
||||
List<AdvertisingStatsDto> advertisings
|
||||
) {
|
||||
}
|
||||
@@ -2,7 +2,9 @@ package com.krylov.refound.dto.advertising;
|
||||
|
||||
public record ClickRequestDto(
|
||||
Long advertisingId,
|
||||
String sessionId
|
||||
String sessionId,
|
||||
/** Тот же токен показа, что и для /api/ads/impression. */
|
||||
String token
|
||||
) {
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.krylov.refound.dto.advertising;
|
||||
|
||||
import com.krylov.refound.enums.advertising.AdvertisingType;
|
||||
|
||||
/**
|
||||
* Объявление, которое сейчас можно показывать, вместе с нужными для выбора
|
||||
* полями кампании. Неизменяемый снимок — его можно держать в кэше без сессии JPA.
|
||||
*/
|
||||
public record EligibleAdvertising(
|
||||
Long advertisingId,
|
||||
Long campaignId,
|
||||
AdvertisingType type,
|
||||
String mediaKey,
|
||||
String targetUrl,
|
||||
Integer weight,
|
||||
Integer dailyImpressionCap
|
||||
) {
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
package com.krylov.refound.dto.advertising;
|
||||
|
||||
public record FullscreenAdActivateRequestDto(boolean active) {
|
||||
|
||||
}
|
||||
@@ -2,6 +2,8 @@ package com.krylov.refound.dto.advertising;
|
||||
|
||||
public record ImpressionRequestDto(
|
||||
Long advertisingId,
|
||||
String sessionId
|
||||
String sessionId,
|
||||
/** Токен показа из GET /api/ads/next; без него — устаревший путь без защиты от повторов. */
|
||||
String token
|
||||
) {
|
||||
}
|
||||
|
||||
@@ -4,7 +4,8 @@ public record SelectedAdvertisingDto(
|
||||
Long advertisingId,
|
||||
String type,
|
||||
String mediaKey,
|
||||
String targetUrl
|
||||
String targetUrl,
|
||||
String impressionToken
|
||||
) {
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user