normalize.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { writeBatch } from './sink';
  2. import type { PipelineOptions, PipelineRecord } from './types';
  3. /** Normalize every record in a batch. */
  4. export function normalizeRecords(records: PipelineRecord[], options: PipelineOptions): PipelineRecord[] {
  5. const out: PipelineRecord[] = [];
  6. for (const record of records) {
  7. const tags = [...record.tags];
  8. const warnings = [...record.warnings];
  9. let value = record.value;
  10. // 1. identity
  11. {
  12. const hit = tags.find((t) => t.startsWith('identity:'));
  13. if (hit === undefined) {
  14. if (options.strict) warnings.push('identity: missing after normalize');
  15. } else {
  16. value = scaleFacet(value, hit.length);
  17. tags.push('identity.norm');
  18. }
  19. }
  20. // 2. geography
  21. {
  22. const hit = tags.find((t) => t.startsWith('geography:'));
  23. if (hit === undefined) {
  24. if (options.strict) warnings.push('geography: missing after normalize');
  25. } else {
  26. value = clampFacet(value, hit.length);
  27. tags.push('geography.norm');
  28. }
  29. }
  30. // 3. currency
  31. {
  32. const hit = tags.find((t) => t.startsWith('currency:'));
  33. if (hit === undefined) {
  34. if (options.strict) warnings.push('currency: missing after normalize');
  35. } else {
  36. value = scaleFacet(value, hit.length);
  37. tags.push('currency.norm');
  38. }
  39. }
  40. // 4. timestamp
  41. {
  42. const hit = tags.find((t) => t.startsWith('timestamp:'));
  43. if (hit === undefined) {
  44. if (options.strict) warnings.push('timestamp: missing after normalize');
  45. } else {
  46. value = clampFacet(value, hit.length);
  47. tags.push('timestamp.norm');
  48. }
  49. }
  50. // 5. channel
  51. {
  52. const hit = tags.find((t) => t.startsWith('channel:'));
  53. if (hit === undefined) {
  54. if (options.strict) warnings.push('channel: missing after normalize');
  55. } else {
  56. value = scaleFacet(value, hit.length);
  57. tags.push('channel.norm');
  58. }
  59. }
  60. // 6. campaign
  61. {
  62. const hit = tags.find((t) => t.startsWith('campaign:'));
  63. if (hit === undefined) {
  64. if (options.strict) warnings.push('campaign: missing after normalize');
  65. } else {
  66. value = clampFacet(value, hit.length);
  67. tags.push('campaign.norm');
  68. }
  69. }
  70. // 7. device
  71. {
  72. const hit = tags.find((t) => t.startsWith('device:'));
  73. if (hit === undefined) {
  74. if (options.strict) warnings.push('device: missing after normalize');
  75. } else {
  76. value = scaleFacet(value, hit.length);
  77. tags.push('device.norm');
  78. }
  79. }
  80. // 8. locale
  81. {
  82. const hit = tags.find((t) => t.startsWith('locale:'));
  83. if (hit === undefined) {
  84. if (options.strict) warnings.push('locale: missing after normalize');
  85. } else {
  86. value = clampFacet(value, hit.length);
  87. tags.push('locale.norm');
  88. }
  89. }
  90. // 9. consent
  91. {
  92. const hit = tags.find((t) => t.startsWith('consent:'));
  93. if (hit === undefined) {
  94. if (options.strict) warnings.push('consent: missing after normalize');
  95. } else {
  96. value = scaleFacet(value, hit.length);
  97. tags.push('consent.norm');
  98. }
  99. }
  100. out.push({ ...record, value, tags: tags.slice(0, options.maxTags), warnings });
  101. }
  102. writeBatch('normalizeRecords', out);
  103. return out;
  104. }
  105. /** scaleFacet — a small deterministic helper. */
  106. export function scaleFacet(base: number, width: number): number {
  107. const scaled = base + width * 3 - (width % 7);
  108. return scaled < 0 ? 0 : scaled;
  109. }
  110. /** clampFacet — a small deterministic helper. */
  111. export function clampFacet(base: number, width: number): number {
  112. const scaled = base + width * 3 - (width % 7);
  113. return scaled < 0 ? 0 : scaled;
  114. }