Author Topic: Calculated metrics on the gateway  (Read 146 times)

Uncle Buzz

  • Full Member
  • ***
  • Posts: 144
  • Country: fr
Calculated metrics on the gateway
« on: February 03, 2021, 04:10:23 AM »
Hi,
I want to make gauge pressure measurements, but using absolute pressure sensors (for cost, size, and availability reasons)... I could add an absolute barometric pressure sensor on each node and send the calculated gauge pressure between my absolute sensor and the barometric sensor, but it costs 1  barometric pressure sensor on each node to measure the same barometric pressure as the other nodes...

How can I add a calculated metric for each node from the absolute pressure they send and barometric pressure received from a single node?

I would like when I get the pressure from node X, add the pressure as a metric and calculate the difference between this new value and the last known value of another metric (barometric pressure) from node Y which has the barometric pressure sensor to fill in another "virtual" metric for the node X

Should I use events? Is there another way to achieve this?

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6534
  • Country: us
    • LowPowerLab
Re: Calculated metrics on the gateway
« Reply #1 on: February 03, 2021, 10:16:35 AM »
To manipulate the received metric you could use the valuation function, when that is defined, the value goes through it.
You'd have to use events though to generate auxiliary data. When you enable those events they run whenever some data is received from that node, you then have the option to do whatever you want. I would probably define the "fake" metrics somewhere and just pass your calculated data through processSerialData() as if this was real incoming data.
The key function to process your data would be the serverExecute() function.

Here's a template for generating fake min/max metrics for a node, you'd have to populate the '...' section with your own code, see other events for such examples:

Code: [Select]
exports.events = {
  MinMax: {
    label:'Min/Max Generated Metrics',
    icon:'fa-heartbeat', //tons of awesome icons at https://fontawesome.com/
    descr:'Generates minimum/maximum load metrics based on incoming data from multiple metrics',
    serverExecute:function(node) {
      db.findOne({ _id : node._id }, function (err, nodeNow) {
        if (nodeNow)
        {
          //get the previous min/max values for comparison, if any exist
          var oldMin=undefined;
          var oldMax=undefined;
          if (nodeNow.metrics && nodeNow.metrics['MIN'])
            oldMin = nodeNow.metrics['MIN'].valueNumeric;
          if (nodeNow.metrics && nodeNow.metrics['MAX'])
            oldMax = nodeNow.metrics['MAX'].valueNumeric;

          //find all existing values, then determine new min/max values
          //... implementation specific .. query db for other metrics, other nodes/node groups etc.
          //    & determine if oldMin/oldMax need to be updated
         
          //generake new fake MIN and MAX metrics
          var fakeSerialMsg = '['+nodeNow._id+']';
              //if (newMin != oldMin)
                fakeSerialMsg += ' MIN:'+newMin;
              //if (newMax != oldMax)
                fakeSerialMsg += ' MAX:'+newMax;

          //pass to data processor
          processSerialData(fakeSerialMsg, true);
        }
      });
    }
  },
}

and an example for the MAX metric:

Code: [Select]
  Max : {
    name:'MAX',
    regexp:/MAX\:(-?\d{1,3})/i,
    duplicateInterval:3600,
    value:'',
    unit:'',
    pin:0,
    graph:0,
    graphOptions: {
      legendLbl:'',
      lines: { lineWidth:1 },
      yaxis: { min:-100, max:200, autoscaleBottom:false, autoscaleTop:true }
    },
  },

Uncle Buzz

  • Full Member
  • ***
  • Posts: 144
  • Country: fr
Re: Calculated metrics on the gateway
« Reply #2 on: February 03, 2021, 11:40:55 AM »
Thank you for the detailed answer!

Is there some "documentation" somewhere or only some examples to find which function or which variables are available?
Is "db.findOne" documented ?
How could I retrieve the last value from a specific node (if any)  from an event on another node?

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6534
  • Country: us
    • LowPowerLab
Re: Calculated metrics on the gateway
« Reply #3 on: February 03, 2021, 06:05:20 PM »
The code base itself is the "documentation", not much else sorry. Anything db related is documented here: https://github.com/louischatriot/nedb
Then you have the various metrics and examples of functions used in the core.js and other files.