Start on step 17 next
This commit is contained in:
@@ -67,6 +67,8 @@ class TagAnalysisService {
|
||||
analyzeProductTags(products) {
|
||||
const tagCounts = new Map();
|
||||
const tagPrices = new Map();
|
||||
const tagVariantCounts = new Map();
|
||||
const tagTotalValues = new Map();
|
||||
const totalProducts = products.length;
|
||||
|
||||
// Count tags and collect price data
|
||||
@@ -77,18 +79,22 @@ class TagAnalysisService {
|
||||
// Count occurrences
|
||||
tagCounts.set(tag, (tagCounts.get(tag) || 0) + 1);
|
||||
|
||||
// Collect price data
|
||||
// Initialize collections if not exists
|
||||
if (!tagPrices.has(tag)) {
|
||||
tagPrices.set(tag, []);
|
||||
tagVariantCounts.set(tag, 0);
|
||||
tagTotalValues.set(tag, 0);
|
||||
}
|
||||
|
||||
// Get prices from variants
|
||||
// Get prices from variants and calculate statistics
|
||||
if (product.variants && Array.isArray(product.variants)) {
|
||||
product.variants.forEach((variant) => {
|
||||
if (variant.price) {
|
||||
const price = parseFloat(variant.price);
|
||||
if (!isNaN(price)) {
|
||||
tagPrices.get(tag).push(price);
|
||||
tagVariantCounts.set(tag, tagVariantCounts.get(tag) + 1);
|
||||
tagTotalValues.set(tag, tagTotalValues.get(tag) + price);
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -96,26 +102,34 @@ class TagAnalysisService {
|
||||
});
|
||||
});
|
||||
|
||||
// Convert to sorted arrays
|
||||
// Convert to sorted arrays with enhanced statistics
|
||||
const tagCountsArray = Array.from(tagCounts.entries())
|
||||
.map(([tag, count]) => ({
|
||||
tag,
|
||||
count,
|
||||
percentage: (count / totalProducts) * 100,
|
||||
variantCount: tagVariantCounts.get(tag) || 0,
|
||||
totalValue: tagTotalValues.get(tag) || 0,
|
||||
}))
|
||||
.sort((a, b) => b.count - a.count);
|
||||
|
||||
// Calculate price ranges
|
||||
// Calculate price ranges with enhanced statistics
|
||||
const priceRanges = {};
|
||||
tagPrices.forEach((prices, tag) => {
|
||||
if (prices.length > 0) {
|
||||
const sortedPrices = prices.sort((a, b) => a - b);
|
||||
const totalValue = tagTotalValues.get(tag) || 0;
|
||||
const variantCount = tagVariantCounts.get(tag) || 0;
|
||||
|
||||
priceRanges[tag] = {
|
||||
min: sortedPrices[0],
|
||||
max: sortedPrices[sortedPrices.length - 1],
|
||||
average:
|
||||
prices.reduce((sum, price) => sum + price, 0) / prices.length,
|
||||
count: prices.length,
|
||||
variantCount: variantCount,
|
||||
totalValue: totalValue,
|
||||
median: this.calculateMedian(sortedPrices),
|
||||
};
|
||||
}
|
||||
});
|
||||
@@ -385,6 +399,23 @@ class TagAnalysisService {
|
||||
: null,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate median value from sorted array
|
||||
* @param {Array} sortedArray - Sorted array of numbers
|
||||
* @returns {number} Median value
|
||||
*/
|
||||
calculateMedian(sortedArray) {
|
||||
if (sortedArray.length === 0) return 0;
|
||||
|
||||
const mid = Math.floor(sortedArray.length / 2);
|
||||
|
||||
if (sortedArray.length % 2 === 0) {
|
||||
return (sortedArray[mid - 1] + sortedArray[mid]) / 2;
|
||||
} else {
|
||||
return sortedArray[mid];
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Calculate impact score for a tag
|
||||
* @param {Object} tagInfo - Tag information
|
||||
|
||||
Reference in New Issue
Block a user