I would like to compute a remain-to-do count per month, in foundry-function, from an objects-set having topicOpenTs
and topicCloseTs
properties (Both Timestamp type). Remain-to-do is a simple difference inflow value - outflow value
for each month cumulated month after month (thank to stackoverflow topic 72883635).
I'm blocked because both aggregations don't have the same buckets : Inflow has 126 buckets (aka months) and outflow has 123 buckets (aka months).
A JS wait to proceed could be to create an object where Key is the key object of inflow and outflow aggregations and Value is the difference of inflow/outflow values for this month or 0 if it doesn't exist.
But TypeSCript complexify my way because I not used to manipulate strict Types and I get errors again and again. I tried during 5 hours and my code below is just too poor... I'm humbly asking for help.
My try lead me to this code but it doesn't work:
export class MyFunctions {
@Function()
public async cumulativeTopicsByMonth(topics: ObjectSet<MeFalWbTcardsTopics>): Promise<TwoDimensionalAggregation<IRange<Timestamp>, Double>> {
const bucketedInflow = await topics
.groupBy(topic => topic.topicOpenTs.byMonth())
.count();
//outflow
const bucketedOutflow = await topics
.groupBy(topic => topic.topicCloseTs.byMonth())
.count();
//************and what I would like to do but that doesn't work:
const allKeys = Array.from(new Set([...bucketedInflow.buckets.map(b => b.key), ...bucketedOutflow.buckets.map(b => b.key)]))
const allBuckets = allKeys.map(key => {
let inflow = bucketedInflow.buckets.find(inBucket => inBucket.key == key)
let outflow = bucketedOutflow.buckets.find(outBucket => outBucket.key == key)
return {key, value: inflow-outflow}
}
const remainToDo = {buckets: allBuckets}
********//end of the durty part with lot of Type Errors
//final steps similaire to [stackoverflow topic 72883635]
const sortedBucketedRtd = sortBuckets(remainToDo );
const cumulativeSortedBucketedRtd = cumulativeSum2D(sortedBucketedRtd );
return cumulativeSortedBucketedRtd
return {buckets: allBuckets}
}