Huginn: Is there an agent to count event

Created on 6 May 2018  路  3Comments  路  Source: huginn/huginn

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
}

Most helpful comment

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)
    }
}

All 3 comments

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)
    }
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

johnnyzen picture johnnyzen  路  6Comments

vijayrajah picture vijayrajah  路  5Comments

TheChrisK picture TheChrisK  路  4Comments

Fishwaldo picture Fishwaldo  路  4Comments

ameicler picture ameicler  路  4Comments