I want to count the number of an item in the received event. What an agent can help me achieve this?
For example:
receive some event:
[
{
"a":"aa"
},
{
"a":"aaa"
},
{
"a":"aa"
}
]
then I want output:
{
"aa":2,
"aaa":1
}
Hi @feilongfl,
I think the only way to do this is to use a JavascriptAgent.
@dsander
Thank you for reply.
I use js agent now,but I have another question now.
How can I keep variable summary.
Agent.receive = function() {
var events = this.incomingEvents();
var summary = {}
for(var i = 0; i < events.length; i++) {
if(typeof(summary[events[i].payload.website]) == "undefined") {
summary[events[i].payload.website] = 1;
} else {
summary[events[i].payload.website] ++;
}
this.createEvent({ items : summary });
}
}
Now this program can only output the results of one event, not all the data together.
I found a solution.
Agent.receive = function () {
var events = this.incomingEvents();
var summary = {}
if (typeof (this.memory('dict')) != "undefined") {
summary = this.memory('dict');
this.log('find dict!')
}
for (var i = 0; i < events.length; i++) {
if (typeof (summary[events[i].payload.website]) == "undefined") {
summary[events[i].payload.website] = 1;
} else {
summary[events[i].payload.website]++;
}
this.createEvent({
items: summary
});
this.memory("dict", summary)
}
}
Most helpful comment
I found a solution.