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>
70 lines
3.4 KiB
Java
70 lines
3.4 KiB
Java
package com.krylov.refound.controller.advertising;
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import com.krylov.refound.dto.advertising.AdvertisingResponseDto;
|
|
import com.krylov.refound.dto.advertising.ClickRequestDto;
|
|
import com.krylov.refound.dto.advertising.ImpressionRequestDto;
|
|
import com.krylov.refound.dto.advertising.SelectedAdvertisingDto;
|
|
import com.krylov.refound.service.advertising.AdvertisingMediaService;
|
|
import com.krylov.refound.service.advertising.AdvertisingSelectionService;
|
|
import com.krylov.refound.service.advertising.AdvertisingTrackingService;
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
import java.util.Optional;
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.http.MediaType;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.security.core.Authentication;
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
import org.springframework.web.bind.annotation.RequestHeader;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
@Slf4j
|
|
@RestController
|
|
@RequiredArgsConstructor
|
|
public class AdvertisingController {
|
|
|
|
private final AdvertisingSelectionService advertisingSelectionService;
|
|
private final AdvertisingMediaService advertisingMediaService;
|
|
private final AdvertisingTrackingService advertisingTrackingService;
|
|
private final ObjectMapper objectMapper;
|
|
|
|
@GetMapping("/api/ads/next")
|
|
public ResponseEntity<AdvertisingResponseDto> getNextAdvertising(Authentication authentication, @RequestHeader(value = "X-Session-Id", required = false) String sessionId, HttpServletRequest request) {
|
|
log.debug("getNextAdvertising");
|
|
Optional<SelectedAdvertisingDto> selected = advertisingSelectionService.selectAdvertising(authentication, sessionId, request);
|
|
if (selected.isEmpty()) {
|
|
// Нормальная ситуация (нет активных реклам / кулдаун / кап) — не ошибка
|
|
log.debug("No advertising to show, returning 204");
|
|
return ResponseEntity.noContent().build();
|
|
}
|
|
|
|
SelectedAdvertisingDto advertising = selected.get();
|
|
String mediaUrl = advertisingMediaService.getMediaUrl(advertising.mediaKey());
|
|
|
|
return ResponseEntity.ok(new AdvertisingResponseDto(advertising.advertisingId(), advertising.type(), mediaUrl,
|
|
advertising.targetUrl(), advertising.impressionToken()));
|
|
}
|
|
|
|
@PostMapping("/api/ads/impression")
|
|
public ResponseEntity<Void> registerImpression(@RequestBody String body, Authentication authentication) throws JsonProcessingException {
|
|
log.debug("registerImpression for body {}", body);
|
|
ImpressionRequestDto request = objectMapper.readValue(body, ImpressionRequestDto.class);
|
|
advertisingTrackingService.recordImpression(request.advertisingId(), authentication, request.sessionId(),
|
|
request.token());
|
|
return ResponseEntity.accepted().build();
|
|
}
|
|
|
|
@PostMapping("/api/ads/click")
|
|
public ResponseEntity<Void> registerClick(@RequestBody ClickRequestDto request, Authentication authentication) {
|
|
log.debug("registerClick");
|
|
advertisingTrackingService.recordClick(request.advertisingId(), authentication, request.sessionId(),
|
|
request.token());
|
|
return ResponseEntity.accepted().build();
|
|
}
|
|
|
|
}
|