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>
101 lines
4.5 KiB
Java
101 lines
4.5 KiB
Java
package com.krylov.refound.controller.advertising;
|
|
|
|
import com.krylov.refound.dto.advertising.CampaignCreateRequestDto;
|
|
import com.krylov.refound.dto.advertising.CampaignResponseDto;
|
|
import com.krylov.refound.dto.advertising.CampaignStatusUpdateRequestDto;
|
|
import com.krylov.refound.dto.advertising.CampaignUpdateRequestDto;
|
|
import com.krylov.refound.dto.advertising.CampaignStatsDto;
|
|
import com.krylov.refound.service.advertising.CampaignService;
|
|
import com.krylov.refound.service.advertising.CampaignStatsService;
|
|
import jakarta.persistence.EntityNotFoundException;
|
|
import jakarta.validation.Valid;
|
|
import java.time.LocalDate;
|
|
import java.util.List;
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.format.annotation.DateTimeFormat;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.security.access.prepost.PreAuthorize;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
import org.springframework.web.bind.annotation.PatchMapping;
|
|
import org.springframework.web.bind.annotation.PathVariable;
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
import org.springframework.web.bind.annotation.PutMapping;
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
@Slf4j
|
|
@RestController
|
|
@RequestMapping("/api/v1/admin/campaigns")
|
|
@RequiredArgsConstructor
|
|
public class CampaignAdminController {
|
|
|
|
private final CampaignService campaignService;
|
|
private final CampaignStatsService campaignStatsService;
|
|
|
|
/**
|
|
* Показы, клики и CTR кампании по объявлениям. from/to — дни (yyyy-MM-dd)
|
|
* включительно в часовом поясе рекламы; по умолчанию последние 30 дней.
|
|
*/
|
|
@GetMapping("/{campaignId}/stats")
|
|
@PreAuthorize("hasRole('ADMIN')")
|
|
public ResponseEntity<CampaignStatsDto> getStats(@PathVariable Long campaignId,
|
|
@RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate from,
|
|
@RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate to) {
|
|
return ResponseEntity.ok(campaignStatsService.getStats(campaignId, from, to));
|
|
}
|
|
|
|
@GetMapping
|
|
@PreAuthorize("hasRole('ADMIN')")
|
|
public ResponseEntity<List<CampaignResponseDto>> getAllCampaigns() {
|
|
List<CampaignResponseDto> campaigns = campaignService.getCampaign();
|
|
return ResponseEntity .ok(campaigns);
|
|
}
|
|
|
|
@GetMapping("/{campaignId}")
|
|
@PreAuthorize("hasRole('ADMIN')")
|
|
public ResponseEntity<CampaignResponseDto> getCampaignById(@PathVariable Long campaignId) {
|
|
log.info("getCampaignById {}", campaignId);
|
|
CampaignResponseDto campaign = campaignService.getCampaignById(campaignId)
|
|
.orElseThrow(() -> new EntityNotFoundException("Campaign not found: " + campaignId));
|
|
return ResponseEntity.ok(campaign);
|
|
}
|
|
|
|
@PostMapping
|
|
@PreAuthorize("hasRole('ADMIN')")
|
|
public ResponseEntity<CampaignResponseDto> createCampaign(@Valid @RequestBody CampaignCreateRequestDto request) {
|
|
log.info("createCampaign {}", request);
|
|
if (!request.startDate().isBefore(request.endDate())) {
|
|
return ResponseEntity.badRequest().build();
|
|
}
|
|
if (request.dailyImpressionCap() <= 0) {
|
|
return ResponseEntity.badRequest().build();
|
|
}
|
|
|
|
CampaignResponseDto campaign = campaignService.createCampaign(request);
|
|
return ResponseEntity.status(HttpStatus.CREATED).body(campaign);
|
|
}
|
|
|
|
@PutMapping("/{campaignId}")
|
|
@PreAuthorize("hasRole('ADMIN')")
|
|
public ResponseEntity<CampaignResponseDto> updateCampaign(@PathVariable Long campaignId, @Valid @RequestBody CampaignUpdateRequestDto request) {
|
|
log.info("updateCampaign {}", campaignId);
|
|
CampaignResponseDto campaign = campaignService.updateCampaign(campaignId, request);
|
|
return ResponseEntity.ok(campaign);
|
|
}
|
|
|
|
@PatchMapping("/{campaignId}/status")
|
|
@PreAuthorize("hasRole('ADMIN')")
|
|
@Transactional
|
|
public ResponseEntity<CampaignResponseDto> updateStatus(@PathVariable Long campaignId, @Valid @RequestBody CampaignStatusUpdateRequestDto request) {
|
|
log.info("updateStatus company {}", campaignId);
|
|
CampaignResponseDto campaign = campaignService.updateCampaignStatus(campaignId, request);
|
|
return ResponseEntity.ok(campaign);
|
|
}
|
|
|
|
}
|