seed-ads.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import {
  2. PrismaClient,
  3. AdType,
  4. ImageSource,
  5. Prisma,
  6. } from '@prisma/mongo/client';
  7. import { nowSecBigInt } from '@box/common/time/time.util';
  8. const prisma = new PrismaClient();
  9. type SeedArgs = {
  10. clean: boolean;
  11. perType: number;
  12. };
  13. function parseArgs(argv: string[]): SeedArgs {
  14. const clean =
  15. argv.includes('--clean') ||
  16. argv.includes('-c') ||
  17. process.env.SEED_CLEAN === '1' ||
  18. process.env.SEED_CLEAN === 'true';
  19. const perTypeFromArg = (() => {
  20. const idx = argv.findIndex((a) => a === '--perType');
  21. if (idx >= 0) {
  22. const v = Number(argv[idx + 1]);
  23. if (Number.isFinite(v) && v > 0) return Math.floor(v);
  24. }
  25. const envValue = Number(process.env.SEED_PER_TYPE);
  26. if (Number.isFinite(envValue) && envValue > 0) return Math.floor(envValue);
  27. return 100;
  28. })();
  29. return { clean, perType: perTypeFromArg };
  30. }
  31. function nowEpochSec(): bigint {
  32. return nowSecBigInt();
  33. }
  34. function randInt(min: number, max: number): number {
  35. // inclusive range
  36. return Math.floor(Math.random() * (max - min + 1)) + min;
  37. }
  38. function pick<T>(arr: readonly T[]): T {
  39. return arr[randInt(0, arr.length - 1)];
  40. }
  41. function clampLen(s: string, maxLen: number): string {
  42. return s.length <= maxLen ? s : s.slice(0, maxLen);
  43. }
  44. function makeShortLabel(prefix: string, maxLen = 20): string {
  45. const tail = randInt(1, 9999).toString().padStart(4, '0');
  46. return clampLen(`${prefix}${tail}`, maxLen);
  47. }
  48. function makeCoverKey(adType: AdType, idx: number): string {
  49. const n = String(idx + 1).padStart(3, '0');
  50. return `ads/${adType.toLowerCase()}/img_${n}.png`;
  51. }
  52. function makeAdsUrl(adType: AdType): string {
  53. const id = randInt(100000, 999999);
  54. return `https://example.com/ads/${adType.toLowerCase()}?c=${id}`;
  55. }
  56. function makeContent(adType: AdType): string {
  57. const phrases = [
  58. '限时优惠',
  59. '新人专享',
  60. '立即领取',
  61. '官方推荐',
  62. '热卖爆款',
  63. '今日必看',
  64. '立刻参与',
  65. '福利放送',
  66. '超值精选',
  67. '品质保证',
  68. ] as const;
  69. const base = `${pick(phrases)} · ${adType}`;
  70. const extra = `|${pick(phrases)}|编号${randInt(1000, 9999)}`;
  71. return clampLen(base + extra, 500);
  72. }
  73. function makeTimeWindow(): { startDt: bigint; expiryDt: bigint } {
  74. const now = Number(nowEpochSec());
  75. const startOffsetDays = randInt(-30, 30);
  76. const start = now + startOffsetDays * 86400;
  77. const durationDays = randInt(7, 90);
  78. const expiry = start + durationDays * 86400;
  79. return { startDt: BigInt(start), expiryDt: BigInt(expiry) };
  80. }
  81. async function main() {
  82. const args = parseArgs(process.argv.slice(2));
  83. const tsNow = nowEpochSec();
  84. const adTypes = Object.values(AdType) as AdType[];
  85. if (args.clean) {
  86. await prisma.ads.deleteMany();
  87. console.log('[seed:ads] cleaned ads collection');
  88. }
  89. const advertiserPrefixes = [
  90. '广告商A',
  91. '广告商B',
  92. '广告商C',
  93. '品牌X',
  94. '品牌Y',
  95. '品牌Z',
  96. ] as const;
  97. const titlePrefixes = [
  98. '爆款推荐',
  99. '限时福利',
  100. '新品上线',
  101. '今日热推',
  102. '官方活动',
  103. '专属礼包',
  104. ] as const;
  105. const batch: Prisma.AdsCreateManyInput[] = [];
  106. for (const adType of adTypes) {
  107. for (let idx = 0; idx < args.perType; idx++) {
  108. const { startDt, expiryDt } = makeTimeWindow();
  109. const advertiser = makeShortLabel(pick(advertiserPrefixes), 20);
  110. const title = makeShortLabel(pick(titlePrefixes), 20);
  111. const adsCoverImg =
  112. Math.random() < 0.85 ? makeCoverKey(adType, idx) : null;
  113. const adsUrl = Math.random() < 0.9 ? makeAdsUrl(adType) : null;
  114. const adsContent = Math.random() < 0.8 ? makeContent(adType) : null;
  115. batch.push({
  116. adType,
  117. advertiser,
  118. title,
  119. adsContent,
  120. adsCoverImg,
  121. adsUrl,
  122. imgSource: ImageSource.LOCAL_ONLY,
  123. startDt,
  124. expiryDt,
  125. seq: randInt(0, 9999),
  126. status: Math.random() < 0.9 ? 1 : 0,
  127. createAt: tsNow,
  128. updateAt: tsNow,
  129. });
  130. }
  131. }
  132. const result = await prisma.ads.createMany({ data: batch });
  133. console.log(
  134. `[seed:ads] adTypes=${adTypes.length} perType=${args.perType} inserted=${result.count} clean=${args.clean}`,
  135. );
  136. }
  137. main()
  138. .catch((error) => {
  139. console.error('[seed:ads] failed:', error);
  140. process.exitCode = 1;
  141. })
  142. .finally(async () => {
  143. await prisma.$disconnect();
  144. });