|
@@ -4,7 +4,7 @@ import { ConfigService } from '@nestjs/config';
|
|
|
import { HttpService } from '@nestjs/axios';
|
|
import { HttpService } from '@nestjs/axios';
|
|
|
import { RedisService } from '@box/db/redis/redis.service';
|
|
import { RedisService } from '@box/db/redis/redis.service';
|
|
|
import { MongoPrismaService } from '@box/db/prisma/mongo-prisma.service';
|
|
import { MongoPrismaService } from '@box/db/prisma/mongo-prisma.service';
|
|
|
-import { CacheKeys } from '@box/common/cache/cache-keys';
|
|
|
|
|
|
|
+import { tsCacheKeys } from '@box/common/cache/ts-cache-key.provider';
|
|
|
import { AdDto } from './dto/ad.dto';
|
|
import { AdDto } from './dto/ad.dto';
|
|
|
import {
|
|
import {
|
|
|
AdListResponseDto,
|
|
AdListResponseDto,
|
|
@@ -26,7 +26,6 @@ import {
|
|
|
import { AdsClickEventPayload } from '@box/common/events/ads-click-event.dto';
|
|
import { AdsClickEventPayload } from '@box/common/events/ads-click-event.dto';
|
|
|
import { randomUUID } from 'crypto';
|
|
import { randomUUID } from 'crypto';
|
|
|
import { nowEpochMsBigInt } from '@box/common/time/time.util';
|
|
import { nowEpochMsBigInt } from '@box/common/time/time.util';
|
|
|
-import { readAdPoolEntriesWithLegacySupport } from './ad-pool-cache-compat.util';
|
|
|
|
|
|
|
|
|
|
// This should match what mgnt-side rebuildSingleAdCache stores.
|
|
// This should match what mgnt-side rebuildSingleAdCache stores.
|
|
|
// We only care about a subset for now.
|
|
// We only care about a subset for now.
|
|
@@ -86,7 +85,7 @@ export class AdService {
|
|
|
const { scene, slot, adType } = params;
|
|
const { scene, slot, adType } = params;
|
|
|
const maxTries = params.maxTries ?? 3;
|
|
const maxTries = params.maxTries ?? 3;
|
|
|
|
|
|
|
|
- const poolKey = CacheKeys.appAdPoolByType(adType);
|
|
|
|
|
|
|
+ const poolKey = tsCacheKeys.ad.poolByType(adType);
|
|
|
const pool = await this.readPoolWithDiagnostics(poolKey, {
|
|
const pool = await this.readPoolWithDiagnostics(poolKey, {
|
|
|
scene,
|
|
scene,
|
|
|
slot,
|
|
slot,
|
|
@@ -110,7 +109,7 @@ export class AdService {
|
|
|
usedIndexes.add(idx);
|
|
usedIndexes.add(idx);
|
|
|
|
|
|
|
|
const entry = pool[idx];
|
|
const entry = pool[idx];
|
|
|
- const adKey = CacheKeys.appAdById(entry.id);
|
|
|
|
|
|
|
+ const adKey = tsCacheKeys.ad.byId(entry.id);
|
|
|
|
|
|
|
|
const cachedAd =
|
|
const cachedAd =
|
|
|
(await this.redis.getJson<CachedAd | null>(adKey)) ?? null;
|
|
(await this.redis.getJson<CachedAd | null>(adKey)) ?? null;
|
|
@@ -144,12 +143,7 @@ export class AdService {
|
|
|
placement: Pick<GetAdForPlacementParams, 'scene' | 'slot' | 'adType'>,
|
|
placement: Pick<GetAdForPlacementParams, 'scene' | 'slot' | 'adType'>,
|
|
|
): Promise<AdPoolEntry[] | null> {
|
|
): Promise<AdPoolEntry[] | null> {
|
|
|
try {
|
|
try {
|
|
|
- const pool = await readAdPoolEntriesWithLegacySupport({
|
|
|
|
|
- adType: placement.adType,
|
|
|
|
|
- redis: this.redis,
|
|
|
|
|
- mongoPrisma: this.mongoPrisma,
|
|
|
|
|
- logger: this.logger,
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ const pool = await this.redis.getJson<AdPoolEntry[]>(poolKey);
|
|
|
|
|
|
|
|
if (!pool) {
|
|
if (!pool) {
|
|
|
this.logger.warn(
|
|
this.logger.warn(
|
|
@@ -252,7 +246,7 @@ export class AdService {
|
|
|
const adsList: AdsByTypeDto[] = [];
|
|
const adsList: AdsByTypeDto[] = [];
|
|
|
|
|
|
|
|
for (const adTypeInfo of adTypes) {
|
|
for (const adTypeInfo of adTypes) {
|
|
|
- const poolKey = CacheKeys.appAdPoolByType(adTypeInfo.adType);
|
|
|
|
|
|
|
+ const poolKey = tsCacheKeys.ad.poolByType(adTypeInfo.adType);
|
|
|
|
|
|
|
|
const poolEntries = await this.readPoolWithDiagnostics(poolKey, {
|
|
const poolEntries = await this.readPoolWithDiagnostics(poolKey, {
|
|
|
scene: 'mgmt',
|
|
scene: 'mgmt',
|
|
@@ -284,13 +278,13 @@ export class AdService {
|
|
|
|
|
|
|
|
// Query MongoDB for full ad details
|
|
// Query MongoDB for full ad details
|
|
|
try {
|
|
try {
|
|
|
- const now = BigInt(Date.now());
|
|
|
|
|
|
|
+ const now = BigInt(Math.floor(Date.now() / 1000));
|
|
|
const ads = await this.mongoPrisma.ads.findMany({
|
|
const ads = await this.mongoPrisma.ads.findMany({
|
|
|
where: {
|
|
where: {
|
|
|
id: { in: adIds },
|
|
id: { in: adIds },
|
|
|
status: 1,
|
|
status: 1,
|
|
|
- startDt: { lte: now },
|
|
|
|
|
- OR: [{ expiryDt: BigInt(0) }, { expiryDt: { gte: now } }],
|
|
|
|
|
|
|
+ // startDt: { lte: now },
|
|
|
|
|
+ // OR: [{ expiryDt: BigInt(0) }, { expiryDt: { gte: now } }],
|
|
|
},
|
|
},
|
|
|
});
|
|
});
|
|
|
|
|
|
|
@@ -361,7 +355,7 @@ export class AdService {
|
|
|
page: number,
|
|
page: number,
|
|
|
size: number,
|
|
size: number,
|
|
|
): Promise<AdListResponseDto> {
|
|
): Promise<AdListResponseDto> {
|
|
|
- const poolKey = CacheKeys.appAdPoolByType(adType);
|
|
|
|
|
|
|
+ const poolKey = tsCacheKeys.ad.poolByType(adType);
|
|
|
|
|
|
|
|
const poolEntries = await this.readPoolWithDiagnostics(poolKey, {
|
|
const poolEntries = await this.readPoolWithDiagnostics(poolKey, {
|
|
|
scene: 'mgmt',
|
|
scene: 'mgmt',
|
|
@@ -409,13 +403,13 @@ export class AdService {
|
|
|
// Step 4: Query MongoDB for full ad details
|
|
// Step 4: Query MongoDB for full ad details
|
|
|
let ads: Awaited<ReturnType<typeof this.mongoPrisma.ads.findMany>>;
|
|
let ads: Awaited<ReturnType<typeof this.mongoPrisma.ads.findMany>>;
|
|
|
try {
|
|
try {
|
|
|
- const now = BigInt(Date.now());
|
|
|
|
|
|
|
+ const now = BigInt(Math.floor(Date.now() / 1000));
|
|
|
ads = await this.mongoPrisma.ads.findMany({
|
|
ads = await this.mongoPrisma.ads.findMany({
|
|
|
where: {
|
|
where: {
|
|
|
id: { in: adIds },
|
|
id: { in: adIds },
|
|
|
status: 1,
|
|
status: 1,
|
|
|
- startDt: { lte: now },
|
|
|
|
|
- OR: [{ expiryDt: BigInt(0) }, { expiryDt: { gte: now } }],
|
|
|
|
|
|
|
+ // startDt: { lte: now },
|
|
|
|
|
+ // OR: [{ expiryDt: BigInt(0) }, { expiryDt: { gte: now } }],
|
|
|
},
|
|
},
|
|
|
});
|
|
});
|
|
|
} catch (err) {
|
|
} catch (err) {
|
|
@@ -480,7 +474,7 @@ export class AdService {
|
|
|
advertiser: string;
|
|
advertiser: string;
|
|
|
title: string;
|
|
title: string;
|
|
|
} | null> {
|
|
} | null> {
|
|
|
- const adKey = CacheKeys.appAdById(adsId);
|
|
|
|
|
|
|
+ const adKey = tsCacheKeys.ad.byId(adsId);
|
|
|
|
|
|
|
|
try {
|
|
try {
|
|
|
// Try Redis cache first
|
|
// Try Redis cache first
|
|
@@ -502,7 +496,7 @@ export class AdService {
|
|
|
`Ad cache miss: adsId=${adsId}, key=${adKey}, falling back to MongoDB`,
|
|
`Ad cache miss: adsId=${adsId}, key=${adKey}, falling back to MongoDB`,
|
|
|
);
|
|
);
|
|
|
|
|
|
|
|
- const now = BigInt(Date.now());
|
|
|
|
|
|
|
+ const now = BigInt(Math.floor(Date.now() / 1000));
|
|
|
const ad = await this.mongoPrisma.ads.findUnique({
|
|
const ad = await this.mongoPrisma.ads.findUnique({
|
|
|
where: { id: adsId },
|
|
where: { id: adsId },
|
|
|
select: {
|
|
select: {
|