I have issue with having percentage of the count of each API compared to total count of APIs. i've had challenges through Mixpanel JQL to reach this purpose, i used reducer also. now i've got that each item, always carries the data of that specific 'api_endpoint', so does this mean that i can never have percentage of each API in a column in front of 'Counts' column?
function main() {
return Events({
from_date: '2024-08-01',
to_date: '2024-08-15'
})
.filter(function(event) {
return event.name === "api_response" && event.properties.api_response_type === "success";
})
.groupBy(['properties.api_endpoint'], mixpanel.reducer.count())
.map(function(item) {
var apiName = item.key[0];
return {
"API Name": apiName,
"Counts": item.value
};
});
}
My expectation is to have this :
API Name | Counts | Percentage |
---|---|---|
api_name_01 | 8 | 20.51% |
api_name_02 | 13 | 33.33% |
api_name_03 | 3 | 7.69% |
api_name_04 | 9 | 23.07% |
api_name_05 | 6 | 15.38% |
I fixed it by using only 'reduce' after 'groupBy' and not 'map'.
function main() {
return Events({
from_date: '2024-08-01',
to_date: '2024-08-15'
})
.filter(function(event) {
return event.name === "api_response";
})
.groupBy(['properties.api_endpoint'], mixpanel.reducer.count())
.reduce(function(acu, events) {
var total = events.reduce(function(acu, item) {
return acu += item.value
}, 0)
var generateApiName = function(endpoint) {
return endpointToName[endpoint] || endpoint
}
var generatePercentage = function(apiCount) {
var percNum = (apiCount * 100) / total
return `${parseFloat(percNum.toFixed(2))}%`;
};
var modifiedEvents = events.map(function(event) {
var endpoint = generateApiName(event.key[0]);
var counts = event.value;
var percentage = generatePercentage(counts)
return {
"API Name": endpoint,
Counts: counts,
Percentage: percentage
}
})
return modifiedEvents
});
}
I've got that in a whole reduce, we have access to filtered and grouped events all at once, therefore we can generate the data in a way we want.