forked from apache/superset
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-comparison.js
More file actions
95 lines (80 loc) · 3.49 KB
/
test-comparison.js
File metadata and controls
95 lines (80 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Simple test to verify our comparison logic works
function testComparisonLogic() {
console.log('🧪 Testing BigNumber Comparison Logic');
// Test case 1: Positive change
const test1 = {
queriesResponse: [{
data: [{ 'Gross Sale': 120, 'Gross Sale__1 day ago': 100 }],
colnames: ['Gross Sale', 'Gross Sale__1 day ago']
}],
formData: { viz_type: 'big_number_total', metric: 'Gross Sale' }
};
// Test case 2: Negative change (from your logs)
const test2 = {
queriesResponse: [{
data: [{ 'Gross Sale': 42324187.71, 'Gross Sale__1 day ago': 65945361.96 }],
colnames: ['Gross Sale', 'Gross Sale__1 day ago']
}],
formData: { viz_type: 'big_number_total', metric: 'Gross Sale' }
};
// Test case 3: Non-BigNumber chart
const test3 = {
queriesResponse: [{
data: [{ 'Value': 100, 'Value__1 day ago': 80 }],
colnames: ['Value', 'Value__1 day ago']
}],
formData: { viz_type: 'table', metric: 'Value' }
};
function extractComparisonData(queriesResponse, formData) {
let bigNumberComparisonData = null;
if (queriesResponse && queriesResponse.length > 0 && formData) {
const { data = [], colnames = [] } = queriesResponse[0];
const vizType = formData?.viz_type;
if (data.length > 0 && vizType && vizType.includes('big_number')) {
const hasTimeOffsetColumns = colnames?.some(
(col) => col.includes('__') && col !== formData.metric
);
if (hasTimeOffsetColumns) {
const metricName = formData.metric || 'value';
const currentValue = data[0][metricName];
let previousPeriodValue = null;
for (const col of colnames) {
if (col.includes('__') && col !== metricName) {
const rawValue = data[0][col];
if (rawValue !== null && rawValue !== undefined) {
previousPeriodValue = typeof rawValue === 'number' ? rawValue : parseFloat(rawValue);
break;
}
}
}
if (currentValue !== null && previousPeriodValue !== null && !isNaN(previousPeriodValue)) {
let percentageChange = 0;
let comparisonIndicator = 'neutral';
if (previousPeriodValue === 0) {
percentageChange = currentValue > 0 ? 1 : currentValue < 0 ? -1 : 0;
comparisonIndicator = currentValue > 0 ? 'positive' : currentValue < 0 ? 'negative' : 'neutral';
} else if (currentValue === 0) {
percentageChange = -1;
comparisonIndicator = 'negative';
} else {
percentageChange = (currentValue - previousPeriodValue) / Math.abs(previousPeriodValue);
comparisonIndicator = percentageChange > 0 ? 'positive' : percentageChange < 0 ? 'negative' : 'neutral';
}
bigNumberComparisonData = {
percentageChange,
comparisonIndicator,
previousPeriodValue,
currentValue,
};
}
}
}
}
return bigNumberComparisonData;
}
console.log('Test 1 (Positive change):', extractComparisonData(test1.queriesResponse, test1.formData));
console.log('Test 2 (Negative change):', extractComparisonData(test2.queriesResponse, test2.formData));
console.log('Test 3 (Non-BigNumber):', extractComparisonData(test3.queriesResponse, test3.formData));
console.log('✅ Test completed');
}
testComparisonLogic();