-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcedarTestFunction.ts
More file actions
123 lines (114 loc) · 3.95 KB
/
cedarTestFunction.ts
File metadata and controls
123 lines (114 loc) · 3.95 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const audienceOverview = () => {
const result = {
totalActualReach: 0,
totalPotentialReach: 0,
ageGenderSummary: {},
genderSummary: {},
regionSummary: [],
deviceSummary: {}
};
const appregration: any = {
reachQueryResult: [{}],
ageGenderQueryResult: [
{
ageGenderReaches: [{ age: "-" }],
ageReaches: [],
reach: 10,
genderReaches: [{ reach: 10 }],
regionReaches: [
{ reach: 10, region: 0 },
{ reach: 11, region: 1 },
{ reach: 12, region: 2 }
],
deviceReaches: [{ reach: 10 }]
}
]
};
const ageGenderSummary = {};
const genderSummary = {};
const deviceSummary = {};
const regionSummary = new Map();
const reachQueryResult = appregration.reachQueryResult[0];
const ageGenderQueryResult = appregration.ageGenderQueryResult;
let regionTotalReach = 0;
result.totalActualReach = reachQueryResult.actualReach || 0;
result.totalPotentialReach = reachQueryResult.potentialReach || 0;
ageGenderQueryResult.forEach(data => {
// process age-gender reaches
[...data.ageGenderReaches, ...data.ageReaches].forEach(record => {
const ageRanger = record.age.replace("-", "to"); // symbol - is acceptable as a JSON
if (!ageGenderSummary[data.channelNme]) {
ageGenderSummary[data.channelNme] = {};
}
if (!ageGenderSummary[data.channelNme][ageRanger]) {
ageGenderSummary[data.channelNme][ageRanger] = {};
}
if (!ageGenderSummary[data.channelNme][ageRanger][record.gender]) {
ageGenderSummary[data.channelNme][ageRanger][record.gender] = 0;
}
ageGenderSummary[data.channelNme][ageRanger][record.gender] =
ageGenderSummary[data.channelNme][ageRanger][record.gender] +
parseInt(record.reach, 0);
});
// process gender reaches
data.genderReaches.forEach(record => {
if (!genderSummary[record.gender]) {
genderSummary[record.gender] = 0;
}
genderSummary[record.gender] =
genderSummary[record.gender] + parseInt(record.reach, 0);
});
// process region summary
data.regionReaches.forEach(record => {
console.log(`record: ${JSON.stringify(record)}`);
if (!regionSummary.has(record.region)) {
regionSummary.set(record.region, 0);
}
regionTotalReach = regionTotalReach + parseInt(record.reach, 0);
console.log(`set : ${record.region}`);
regionSummary.set(
record.region,
regionSummary.get(record.region) + parseInt(record.reach, 0)
);
console.log(`regionSummary: ${JSON.stringify(regionSummary)}`);
});
// process device summary
data.deviceReaches.forEach(record => {
if (!deviceSummary[record.devicePlatform]) {
deviceSummary[record.devicePlatform] = { total: 0 };
}
if (!deviceSummary[record.devicePlatform][record.impressionDevice]) {
deviceSummary[record.devicePlatform][record.impressionDevice] = 0;
}
deviceSummary[record.devicePlatform].total =
deviceSummary[record.devicePlatform].total + parseInt(record.reach, 0);
deviceSummary[record.devicePlatform][record.impressionDevice] =
deviceSummary[record.devicePlatform][record.impressionDevice] +
parseInt(record.reach, 0);
});
});
// convert MAP to Array for sorting
const regionSummaryList = Array.from(
regionSummary,
(record: [string, number]) => {
return {
name: record[0],
value: record[1],
percentage: (record[1] / regionTotalReach).toFixed(5)
};
}
);
result.ageGenderSummary = ageGenderSummary;
result.genderSummary = genderSummary;
// DESC sorting
debugger;
regionSummaryList.sort((a: any, b: any) => {
console.log("come in ", a, b);
console.log(a.value, b.value);
return b.value - a.value;
});
result.regionSummary = regionSummaryList.slice(0, 6);
result.deviceSummary = deviceSummary;
return result;
};
console.log(audienceOverview());