- Implemented setup_ordrestyring_import.sh for initial database setup and data import from API. - Created sync_ordrestyring_data.sh for incremental updates to the local database. - Developed test_ordrestyring_import.js to validate API data import functionality. - Added test_enhanced_quote_demo.js to demonstrate the enhanced quote system using hybrid data sources. - Introduced test_ordrestyring_system.sh for comprehensive testing of the database system and API functionality. - Enhanced logging and error handling across scripts for better traceability. - Included analysis script creation for post-import data analysis.
182 lines
6.2 KiB
JavaScript
Executable File
182 lines
6.2 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
||
/**
|
||
* Quick API Data Count Check
|
||
* Checks how many records are available in each Ordrestyring API endpoint
|
||
*/
|
||
|
||
const axios = require('axios');
|
||
|
||
const CONFIG = {
|
||
baseUrl: 'https://v2.api.ordrestyring.dk', // Correct API v2 base URL
|
||
bearerToken: process.env.ORDRESTYRING_TOKEN || '',
|
||
timeout: 30000
|
||
};
|
||
|
||
async function makeApiRequest(endpoint, params = {}) {
|
||
const url = `${CONFIG.baseUrl}${endpoint}`;
|
||
const config = {
|
||
auth: {
|
||
username: CONFIG.bearerToken,
|
||
password: 'x' // Password can be anything according to docs
|
||
},
|
||
timeout: CONFIG.timeout,
|
||
params
|
||
};
|
||
|
||
try {
|
||
console.log(`📡 API Request: ${url} (attempt 1)`);
|
||
const response = await axios.get(url, config);
|
||
|
||
return response.data;
|
||
} catch (error) {
|
||
console.error(`❌ Error for ${endpoint}:`, error.message);
|
||
return null;
|
||
}
|
||
}
|
||
|
||
async function checkDataCounts() {
|
||
console.log('🔍 Checking Ordrestyring API Data Counts');
|
||
console.log('=' .repeat(45));
|
||
|
||
if (!CONFIG.bearerToken) {
|
||
console.error('❌ ORDRESTYRING_TOKEN environment variable is required');
|
||
console.error('Usage: ORDRESTYRING_TOKEN=your_token node check_api_data_counts.js');
|
||
process.exit(1);
|
||
}
|
||
|
||
const endpoints = [
|
||
'/cases',
|
||
'/case-materials',
|
||
'/hours',
|
||
'/debtors',
|
||
'/debtor-invoices',
|
||
'/users',
|
||
'/departments',
|
||
'/case-types',
|
||
'/case-statuses',
|
||
'/employee-types'
|
||
];
|
||
|
||
const results = {};
|
||
|
||
for (const endpoint of endpoints) {
|
||
console.log(`\n🔍 Analyzing ${endpoint}...`);
|
||
|
||
// Try different page sizes to find optimal and get accurate count
|
||
let totalCount = 0;
|
||
let pageSize = 100; // Start with 100 records per page
|
||
let currentPage = 1;
|
||
let hasMoreData = true;
|
||
|
||
try {
|
||
while (hasMoreData) {
|
||
console.log(` 📄 Fetching page ${currentPage} (${pageSize} records/page)...`);
|
||
|
||
const data = await makeApiRequest(endpoint, {
|
||
page: currentPage,
|
||
pagesize: pageSize // Note: API uses 'pagesize' not 'per_page'
|
||
});
|
||
|
||
if (data && Array.isArray(data)) {
|
||
const recordsOnPage = data.length;
|
||
totalCount += recordsOnPage;
|
||
|
||
console.log(` ✅ Found ${recordsOnPage} records on page ${currentPage}`);
|
||
|
||
// Check if we got fewer records than requested (last page)
|
||
if (recordsOnPage < pageSize) {
|
||
hasMoreData = false;
|
||
console.log(` 🏁 Last page reached (${recordsOnPage} < ${pageSize})`);
|
||
} else {
|
||
currentPage++;
|
||
|
||
// Safety limit to prevent infinite loops
|
||
if (currentPage > 100) {
|
||
console.log(` 🛑 Stopped at page ${currentPage} for safety`);
|
||
totalCount = `${totalCount}+ (stopped at page ${currentPage})`;
|
||
hasMoreData = false;
|
||
}
|
||
}
|
||
|
||
// Rate limiting - be nice to the API
|
||
await new Promise(resolve => setTimeout(resolve, 300));
|
||
|
||
} else if (data && data.length === 0) {
|
||
// Empty response
|
||
hasMoreData = false;
|
||
console.log(` ℹ️ Empty response - no more data`);
|
||
} else {
|
||
// API error or unexpected response
|
||
console.log(` ❌ Unexpected response format:`, typeof data);
|
||
hasMoreData = false;
|
||
}
|
||
}
|
||
|
||
results[endpoint] = {
|
||
available: totalCount > 0,
|
||
totalCount: totalCount,
|
||
pagesChecked: currentPage
|
||
};
|
||
|
||
console.log(` 📊 Total for ${endpoint}: ${totalCount} records (${currentPage} pages)`);
|
||
|
||
} catch (error) {
|
||
console.error(` ❌ Error checking ${endpoint}:`, error.message);
|
||
results[endpoint] = {
|
||
available: false,
|
||
totalCount: 0,
|
||
error: error.message
|
||
};
|
||
}
|
||
}
|
||
|
||
console.log('\n📊 Summary:');
|
||
console.log('=' .repeat(45));
|
||
|
||
let totalRecords = 0;
|
||
let availableEndpoints = 0;
|
||
|
||
Object.entries(results).forEach(([endpoint, info]) => {
|
||
if (info.available) {
|
||
availableEndpoints++;
|
||
if (typeof info.estimatedTotal === 'number') {
|
||
totalRecords += info.estimatedTotal;
|
||
}
|
||
}
|
||
});
|
||
|
||
console.log(`Available endpoints: ${availableEndpoints}/${endpoints.length}`);
|
||
console.log(`Estimated total records: ${totalRecords}${totalRecords > 0 ? '+' : ''}`);
|
||
|
||
// Save results to file
|
||
const summary = {
|
||
timestamp: new Date().toISOString(),
|
||
endpoints: results,
|
||
summary: {
|
||
availableEndpoints,
|
||
totalEndpoints: endpoints.length,
|
||
estimatedTotalRecords: totalRecords
|
||
}
|
||
};
|
||
|
||
require('fs').writeFileSync(
|
||
'/home/alex/git/tilbudgivern/api_data_count.json',
|
||
JSON.stringify(summary, null, 2)
|
||
);
|
||
|
||
console.log('\n💾 Results saved to: api_data_count.json');
|
||
|
||
// Special focus on cases
|
||
if (results['/cases'] && results['/cases'].available) {
|
||
console.log('\n🏗️ Cases Analysis:');
|
||
console.log(`Total cases available: ${results['/cases'].estimatedTotal}`);
|
||
|
||
if (typeof results['/cases'].estimatedTotal === 'number' && results['/cases'].estimatedTotal > 2) {
|
||
console.log(`That's ${results['/cases'].estimatedTotal - 2} more cases than we currently use in JSON file!`);
|
||
console.log('💡 Local database import would significantly improve estimate accuracy');
|
||
}
|
||
}
|
||
}
|
||
|
||
checkDataCounts().catch(console.error);
|