Skip to content

Commit

Permalink
Merge pull request #145 from segment-services-eng/dev-test
Browse files Browse the repository at this point in the history
updates
  • Loading branch information
AubreySine0 authored Feb 12, 2025
2 parents 494f870 + 2bd99c4 commit bb4a57d
Showing 1 changed file with 105 additions and 18 deletions.
123 changes: 105 additions & 18 deletions scripts/save-tracking-plan.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,83 @@
// const fs = require('fs');
// const path = require('path');
// const axios = require('axios');

// // Get environment variables
// const planDir = process.env.PLAN_DIR;
// const workspace = process.env.SEGMENT_WORKSPACE;
// const trackingPlanId = process.env.SEGMENT_TRACKING_PLAN_ID;
// const apiUrl = `https://api.segmentapis.com/tracking-plans/${trackingPlanId}/rules`;
// const apiKey = process.env.SEGMENT_API_KEY;
// const paginationCount = 100; // Fixed pagination count

// console.log('API key:', apiKey);
// console.log('API URL:', apiUrl);
// console.log('Workspace:', workspace);
// console.log('Tracking Plan ID:', trackingPlanId);
// console.log('Pagination Count:', paginationCount);

// // Fetch the updated tracking plan rules with pagination
// async function fetchUpdatedTrackingPlanRules(cursor = null, accumulatedRules = []) {
// try {
// // Construct the API URL with query parameters for pagination
// let requestUrl = `${apiUrl}?count=${paginationCount}`;
// if (cursor) {
// requestUrl += `&cursor=${cursor}`;
// }

// const response = await axios.get(
// requestUrl,
// {
// headers: {
// 'Authorization': `Bearer ${apiKey}`,
// 'Content-Type': 'application/json'
// },
// }
// );

// console.log('Fetched page successfully:', response.data);

// // Accumulate the rules from this page
// const rules = response.data.data.rules || [];
// accumulatedRules = accumulatedRules.concat(rules);
// console.log('Accumulated rules so far:', accumulatedRules.length);

// // Handle pagination if there's more data
// if (response.data.data.pagination && response.data.data.pagination.next) {
// console.log('Fetching next page...');
// return await fetchUpdatedTrackingPlanRules(response.data.data.pagination.next, accumulatedRules);
// } else {
// console.log('All pages fetched.');
// return accumulatedRules;
// }
// } catch (error) {
// console.error('Error fetching updated tracking plan rules:', error.response ? error.response.data : error.message);
// return accumulatedRules;
// }
// }

// // Main function to fetch and save all rules
// async function main() {
// const allRules = await fetchUpdatedTrackingPlanRules();

// console.log('Total rules fetched:', allRules.length);

// // File path for saving the rules
// const filePath = path.join(planDir, 'current-rules.json');
// console.log('Final file path:', filePath);

// // Save all accumulated rules to a single JSON file
// try {
// fs.writeFileSync(filePath, JSON.stringify({ rules: allRules }, null, 2));
// console.log('Saved all rules to:', filePath);
// } catch (error) {
// console.error('Error writing file:', error.message);
// }
// }

// // Run the main function
// main();

const fs = require('fs');
const path = require('path');
const axios = require('axios');
Expand All @@ -16,42 +96,46 @@ console.log('Workspace:', workspace);
console.log('Tracking Plan ID:', trackingPlanId);
console.log('Pagination Count:', paginationCount);

// Ensure the directory exists before writing the file
function ensureDirectoryExists(directoryPath) {
if (!fs.existsSync(directoryPath)) {
fs.mkdirSync(directoryPath, { recursive: true });
console.log(`✅ Created directory: ${directoryPath}`);
}
}

// Fetch the updated tracking plan rules with pagination
async function fetchUpdatedTrackingPlanRules(cursor = null, accumulatedRules = []) {
try {
// Construct the API URL with query parameters for pagination
let requestUrl = `${apiUrl}?count=${paginationCount}`;
if (cursor) {
requestUrl += `&cursor=${cursor}`;
}

const response = await axios.get(
requestUrl,
{
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json'
},
}
);
const response = await axios.get(requestUrl, {
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json'
},
});

console.log('Fetched page successfully:', response.data);

// Accumulate the rules from this page
const rules = response.data.data.rules || [];
accumulatedRules = accumulatedRules.concat(rules);
console.log('Accumulated rules so far:', accumulatedRules.length);
console.log(`Accumulated rules so far: ${accumulatedRules.length}`);

// Handle pagination if there's more data
if (response.data.data.pagination && response.data.data.pagination.next) {
console.log('Fetching next page...');
return await fetchUpdatedTrackingPlanRules(response.data.data.pagination.next, accumulatedRules);
} else {
console.log('All pages fetched.');
console.log('All pages fetched.');
return accumulatedRules;
}
} catch (error) {
console.error('Error fetching updated tracking plan rules:', error.response ? error.response.data : error.message);
console.error('Error fetching updated tracking plan rules:', error.response ? error.response.data : error.message);
return accumulatedRules;
}
}
Expand All @@ -60,20 +144,23 @@ async function fetchUpdatedTrackingPlanRules(cursor = null, accumulatedRules = [
async function main() {
const allRules = await fetchUpdatedTrackingPlanRules();

console.log('Total rules fetched:', allRules.length);
console.log('📌 Total rules fetched:', allRules.length);

// Ensure the directory exists before writing the file
ensureDirectoryExists(planDir);

// File path for saving the rules
const filePath = path.join(planDir, 'current-rules.json');
console.log('Final file path:', filePath);
console.log('📂 Final file path:', filePath);

// Save all accumulated rules to a single JSON file
try {
fs.writeFileSync(filePath, JSON.stringify({ rules: allRules }, null, 2));
console.log('Saved all rules to:', filePath);
console.log(`✅ Successfully saved all rules to: ${filePath}`);
} catch (error) {
console.error('Error writing file:', error.message);
console.error('Error writing file:', error.message);
}
}

// Run the main function
main();
main();

0 comments on commit bb4a57d

Please sign in to comment.