Repository navigation
fix: router output ordering for tiktok_ads - #4364
maheshkutty wants to merge 5 commits into
Conversation
|
Important Review skippedReview was skipped due to path filters ⛔ Files ignored due to path filters (1)
CodeRabbit blocks several paths by default. You can override this behavior by explicitly including those paths in the path filters. For example, including You can disable this status message by setting the Summary by CodeRabbit
Summary by CodeRabbit
WalkthroughThis change introduces a new utility function, Changes
Sequence Diagram(s)sequenceDiagram
participant Caller
participant TikTokTransform
participant Util
Caller->>TikTokTransform: processRouterDest(...)
TikTokTransform->>Util: sortBatchesByMinJobId([batched, track, error])
Util-->>TikTokTransform: Sorted array by min jobId
TikTokTransform-->>Caller: Sorted combined response
Possibly related PRs
Suggested reviewers
Suggestions for improvement:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
Documentation and Community
|
There was a problem hiding this comment.
Actionable comments posted: 2
📜 Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📥 Commits
Reviewing files that changed from the base of the PR and between 43c1056 and ddae63500ca6464ca26c55367a464f105d685d63.
⛔ Files ignored due to path filters (5)
test/integrations/component.test.tsis excluded by!**/test/**test/integrations/destinations/tiktok_ads/common.tsis excluded by!**/test/**test/integrations/destinations/tiktok_ads/dataDelivery/data.tsis excluded by!**/test/**test/integrations/destinations/tiktok_ads/processor/data.tsis excluded by!**/test/**test/integrations/destinations/tiktok_ads/router/data.tsis excluded by!**/test/**
📒 Files selected for processing (4)
src/v0/destinations/tiktok_ads/transform.js(2 hunks)src/v0/destinations/tiktok_ads/transformV2.js(2 hunks)src/v0/util/index.js(3 hunks)src/v0/util/index.test.js(2 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
`**/*.js`: Focus on ESLint errors (max 3) and warnings (max 5).
**/*.js: Focus on ESLint errors (max 3) and warnings (max 5).
src/v0/util/index.test.jssrc/v0/destinations/tiktok_ads/transformV2.jssrc/v0/destinations/tiktok_ads/transform.jssrc/v0/util/index.js
🧬 Code Graph Analysis (2)
src/v0/destinations/tiktok_ads/transformV2.js (1)
src/v0/util/index.js (1)
sortBatchesByMinJobId(2236-2241)
src/v0/destinations/tiktok_ads/transform.js (2)
src/v0/util/index.js (1)
sortBatchesByMinJobId(2236-2241)src/v0/destinations/tiktok_ads/transformV2.js (3)
batchedResponseList(751-751)trackResponseList(723-723)errorRespList(725-725)
⏰ Context from checks skipped due to timeout of 90000ms (2)
- GitHub Check: Analyze (go)
- GitHub Check: Analyze (javascript-typescript)
🔇 Additional comments (6)
src/v0/util/index.js (2)
2291-2291: Good refactoring to use the centralized utility.The replacement of manual sorting logic with the new utility function improves code maintainability and follows the DRY principle.
2549-2549: Export is correctly added.The function is properly exported and maintains alphabetical ordering in the exports list.
src/v0/destinations/tiktok_ads/transformV2.js (2)
18-18: Import statement is correctly added.The import of
sortBatchesByMinJobIdutility function is properly included.
761-763: Excellent use of the sorting utility with clear documentation.The replacement of manual array concatenation with sorted results addresses the router output ordering issue effectively. The comments clearly explain the purpose and the specific case where
testEventCodeproperties can cause out-of-order events.src/v0/destinations/tiktok_ads/transform.js (2)
23-23: Import statement is correctly added.The import of
sortBatchesByMinJobIdutility function maintains consistency with the transformV2.js implementation.
286-286: Consistent implementation across transform versions.The usage of
sortBatchesByMinJobIdensures the same ordering behavior as transformV2.js, maintaining consistency across both TikTok Ads transform implementations. This addresses the router output ordering issue mentioned in the PR objectives.
There was a problem hiding this comment.
💡 Verification agent
🧩 Analysis chain
Comprehensive test coverage with one logical inconsistency to address.
The test suite provides excellent coverage for the sortEventsBasedMetadataJobId function, including edge cases like empty arrays, negative jobIds, and mixed data types. However, there's a logical inconsistency in the non-numeric jobIds test case.
Issue with non-numeric jobIds test logic:
it('should handle batches with non-numeric jobIds gracefully (NaN)', () => {
const input = [
{ metadata: [{ jobId: 'a' }] },
{ metadata: [{ jobId: 2 }] },
{ metadata: [{ jobId: 1 }] },
];
- // Math.min('a') is NaN, so those batches will be sorted to the end
+ // Math.min('a') is NaN, so those batches will be sorted to the end
const expected = [
- { metadata: [{ jobId: 'a' }] },
{ metadata: [{ jobId: 1 }] },
{ metadata: [{ jobId: 2 }] },
+ { metadata: [{ jobId: 'a' }] },
];
expect(sortEventsBasedMetadataJobId(input)).toEqual(expected);
});The comment correctly states that NaN values should be sorted to the end, but the expected output places the non-numeric jobId first. This contradicts standard sorting behavior where NaN values typically sort last.
Please verify the actual behavior of the sortEventsBasedMetadataJobId function with this test case:
🏁 Script executed:
#!/bin/bash
# Description: Verify the sorting behavior with non-numeric jobIds
# Expected: Non-numeric jobIds (NaN) should sort to the end, not the beginning
# Search for the actual implementation of sortEventsBasedMetadataJobId
rg -A 20 "sortEventsBasedMetadataJobId.*=" --type jsLength of output: 1375
To confirm how non-numeric jobId values are treated in the actual comparator, let’s inspect the implementation of sortEventsBasedMetadataJobId:
🏁 Script executed:
#!/bin/bash
# Locate the implementation of sortEventsBasedMetadataJobId
rg -A 20 'sortEventsBasedMetadataJobId' --type jsLength of output: 8892
Let’s inspect the actual implementation to see how it handles NaN from Math.min:
🏁 Script executed:
#!/bin/bash
# Locate implementation in the utility module
rg -A20 'sortEventsBasedMetadataJobId' -g 'src/v0/util/index.js'Length of output: 62
To locate the actual implementation and see how it handles NaN, let’s first list all files in the util folder and then search within the index file(s):
🏁 Script executed:
#!/bin/bash
# 1. List all files in src/v0/util to identify the index file extension
find src/v0/util -maxdepth 1 -type f
# 2. Search for the function definition in any index.* file
rg -A20 'sortEventsBasedMetadataJobId' src/v0/util/index.*Length of output: 9198
Let’s pinpoint where and how sortEventsBasedMetadataJobId is defined and exported in src/v0/util/index.js:
🏁 Script executed:
#!/bin/bash
# 1. Show the exports block to confirm it's being exported
grep -n -C3 "module\\.exports" src/v0/util/index.js
# 2. Search for the function definition anywhere in util
rg -n "sortEventsBasedMetadataJobId" src/v0/utilLength of output: 1283
Incorrect assumption in non-numeric case—update comment or implementation
The current test for non-numeric jobId values actually reflects the implementation’s behavior: since Math.min('a') yields NaN, comparisons against numbers also yield NaN and V8’s stable sort leaves that batch in its original position (first). You have two options:
• If you intend to keep this behavior, update the inline comment to say “batches with non-numeric jobId will remain in original order (so ‘a’ stays first),” instead of “sorted to the end.”
• If you want truly to push non-numeric IDs to the end, change the comparator in sortEventsBasedMetadataJobId to treat NaN as Infinity (e.g. const min = Number.isNaN(rawMin) ? Infinity : rawMin;).
Either way, the test’s expected array (['a', 1, 2]) is correct given the code; only the comment (or the comparator) needs adjustment.
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| describe('sortEventsBasedMetadataJobId', () => { | |
| it('should sort batches by the minimum jobId in each batch (ascending)', () => { | |
| const input = [ | |
| { metadata: [{ jobId: 3 }, { jobId: 4 }] }, | |
| { metadata: [{ jobId: 1 }, { jobId: 5 }] }, | |
| { metadata: [{ jobId: 2 }] }, | |
| ]; | |
| const expected = [ | |
| { metadata: [{ jobId: 1 }, { jobId: 5 }] }, | |
| { metadata: [{ jobId: 2 }] }, | |
| { metadata: [{ jobId: 3 }, { jobId: 4 }] }, | |
| ]; | |
| expect(sortEventsBasedMetadataJobId(input)).toEqual(expected); | |
| }); | |
| it('should handle batches with single metadata item', () => { | |
| const input = [ | |
| { metadata: [{ jobId: 10 }] }, | |
| { metadata: [{ jobId: 2 }] }, | |
| { metadata: [{ jobId: 5 }] }, | |
| ]; | |
| const expected = [ | |
| { metadata: [{ jobId: 2 }] }, | |
| { metadata: [{ jobId: 5 }] }, | |
| { metadata: [{ jobId: 10 }] }, | |
| ]; | |
| expect(sortEventsBasedMetadataJobId(input)).toEqual(expected); | |
| }); | |
| it('should handle batches with multiple metadata items and unordered jobIds', () => { | |
| const input = [ | |
| { metadata: [{ jobId: 8 }, { jobId: 3 }] }, | |
| { metadata: [{ jobId: 2 }, { jobId: 7 }] }, | |
| { metadata: [{ jobId: 5 }, { jobId: 9 }] }, | |
| ]; | |
| const expected = [ | |
| { metadata: [{ jobId: 2 }, { jobId: 7 }] }, | |
| { metadata: [{ jobId: 5 }, { jobId: 9 }] }, | |
| { metadata: [{ jobId: 8 }, { jobId: 3 }] }, | |
| ]; | |
| // Note: The function sorts by the minimum jobId in each batch, so batch 3's min is 3, batch 1's min is 2, batch 2's min is 5 | |
| // So after sorting: batch 1 (min 2), batch 3 (min 3), batch 2 (min 5) | |
| // But the expected output should be sorted by min jobId: 2, 3, 5 | |
| // Let's fix the expected accordingly: | |
| const sortedExpected = [ | |
| { metadata: [{ jobId: 2 }, { jobId: 7 }] }, | |
| { metadata: [{ jobId: 8 }, { jobId: 3 }] }, | |
| { metadata: [{ jobId: 5 }, { jobId: 9 }] }, | |
| ]; | |
| expect(sortEventsBasedMetadataJobId(input)).toEqual(sortedExpected); | |
| }); | |
| it('should handle empty input array', () => { | |
| expect(sortEventsBasedMetadataJobId([])).toEqual([]); | |
| }); | |
| it('should handle batches with empty metadata arrays', () => { | |
| const input = [{ metadata: [] }, { metadata: [{ jobId: 2 }] }, { metadata: [] }]; | |
| // Math.min(...[]) is Infinity, so batches with empty metadata will be sorted to the end | |
| const expected = [{ metadata: [{ jobId: 2 }] }, { metadata: [] }, { metadata: [] }]; | |
| expect(sortEventsBasedMetadataJobId(input)).toEqual(expected); | |
| }); | |
| it('should not mutate the original array order if already sorted', () => { | |
| const input = [ | |
| { metadata: [{ jobId: 1 }] }, | |
| { metadata: [{ jobId: 2 }] }, | |
| { metadata: [{ jobId: 3 }] }, | |
| ]; | |
| const expected = [ | |
| { metadata: [{ jobId: 1 }] }, | |
| { metadata: [{ jobId: 2 }] }, | |
| { metadata: [{ jobId: 3 }] }, | |
| ]; | |
| expect(sortEventsBasedMetadataJobId(input)).toEqual(expected); | |
| }); | |
| it('should handle negative jobIds', () => { | |
| const input = [ | |
| { metadata: [{ jobId: -5 }, { jobId: 2 }] }, | |
| { metadata: [{ jobId: 0 }] }, | |
| { metadata: [{ jobId: -1 }] }, | |
| ]; | |
| const expected = [ | |
| { metadata: [{ jobId: -5 }, { jobId: 2 }] }, | |
| { metadata: [{ jobId: -1 }] }, | |
| { metadata: [{ jobId: 0 }] }, | |
| ]; | |
| expect(sortEventsBasedMetadataJobId(input)).toEqual(expected); | |
| }); | |
| it('should handle batches with non-numeric jobIds gracefully (NaN)', () => { | |
| const input = [ | |
| { metadata: [{ jobId: 'a' }] }, | |
| { metadata: [{ jobId: 2 }] }, | |
| { metadata: [{ jobId: 1 }] }, | |
| ]; | |
| // Math.min('a') is NaN, so those batches will be sorted to the end | |
| const expected = [ | |
| { metadata: [{ jobId: 'a' }] }, | |
| { metadata: [{ jobId: 1 }] }, | |
| { metadata: [{ jobId: 2 }] }, | |
| ]; | |
| expect(sortEventsBasedMetadataJobId(input)).toEqual(expected); | |
| }); | |
| }); | |
| }); | |
| it('should handle batches with non-numeric jobIds gracefully (NaN)', () => { | |
| const input = [ | |
| { metadata: [{ jobId: 'a' }] }, | |
| { metadata: [{ jobId: 2 }] }, | |
| { metadata: [{ jobId: 1 }] }, | |
| ]; | |
| // Math.min('a') is NaN, so those batches will be sorted to the end | |
| const expected = [ | |
| { metadata: [{ jobId: 1 }] }, | |
| { metadata: [{ jobId: 2 }] }, | |
| { metadata: [{ jobId: 'a' }] }, | |
| ]; | |
| expect(sortEventsBasedMetadataJobId(input)).toEqual(expected); | |
| }); |
🤖 Prompt for AI Agents
In src/v0/util/index.test.js around lines 1410 to 1517, the test case for
batches with non-numeric jobIds has a misleading comment stating that such
batches will be sorted to the end, but the expected output actually keeps them
in their original position. To fix this, update the inline comment to accurately
reflect the current behavior of the function, stating that batches with
non-numeric jobIds remain in their original order rather than being sorted to
the end. Alternatively, if you want to change the behavior to push non-numeric
jobIds to the end, modify the comparator in the sortEventsBasedMetadataJobId
function to treat NaN as Infinity. For now, just correct the comment to match
the existing expected output.
ddae635 to
9487d78
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## develop #4364 +/- ##
========================================
Coverage 91.13% 91.14%
========================================
Files 638 638
Lines 33476 33477 +1
Branches 7904 7904
========================================
+ Hits 30509 30513 +4
+ Misses 2728 2725 -3
Partials 239 239 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Allure Test reports for this run are available at:
|
2 similar comments
|
Allure Test reports for this run are available at:
|
|
Allure Test reports for this run are available at:
|
d70a152 to
3348ace
Compare
|
Allure Test reports for this run are available at:
|
c2d1a32 to
9171692
Compare
|
Allure Test reports for this run are available at:
|
9171692 to
ca76ecf
Compare
|
Allure Test reports for this run are available at:
|
ca76ecf to
f71f756
Compare
|
Allure Test reports for this run are available at:
|
|
Allure Test reports for this run are available at:
|
|



What are the changes introduced in this PR?
This pull request introduces a new utility function,
sortBatchesByMinJobId, to ensure consistent ordering of batched requests based on the minimumjobIdin their metadata. The function is integrated into multiple files, enhancing batch processing logic for TikTok Ads transformations and other utilities. Additionally, comprehensive unit tests have been added to validate its behavior across various scenarios.Enhancements to batch processing:
sortBatchesByMinJobIdinsrc/v0/util/index.jsto sort batches by the minimumjobIdin their metadata, ensuring consistent ordering. This replaces inline sorting logic in multiple places. [1] [2] [3]processRouterDestin bothsrc/v0/destinations/tiktok_ads/transform.jsandsrc/v0/destinations/tiktok_ads/transformV2.jsto usesortBatchesByMinJobIdfor sorting batched responses. [1] [2]Testing improvements:
sortBatchesByMinJobId: Added extensive test cases insrc/v0/util/index.test.jsto validate sorting functionality, including edge cases like empty arrays, single metadata items, and already sorted inputs.Code cleanup:
combineBatchRequestsWithSameJobIdsand replaced it with the new utility function for better reusability and maintainability.Other changes:
test/integrations/component.test.ts, likely as part of broader test cleanup or updates.What is the related Linear task?
Resolves INT-3733
Please explain the objectives of your changes below
Put down any required details on the broader aspect of your changes. If there are any dependent changes, mandatorily mention them here
Any changes to existing capabilities/behaviour, mention the reason & what are the changes ?
N/A
Any new dependencies introduced with this change?
N/A
Any new generic utility introduced or modified. Please explain the changes.
N/A
Any technical or performance related pointers to consider with the change?
N/A
@coderabbitai review
Developer checklist
My code follows the style guidelines of this project
No breaking changes are being introduced.
All related docs linked with the PR?
All changes manually tested?
Any documentation changes needed with this change?
Is the PR limited to 10 file changes?
Is the PR limited to one linear task?
Are relevant unit and component test-cases added in new readability format?
Reviewer checklist
Is the type of change in the PR title appropriate as per the changes?
Verified that there are no credentials or confidential data exposed with the changes.