BYRD - Administrator’s and Developer’s Guide

Statistics

Statistics allow you to collect commulated information about your data. An example for an easy statistic that is already delivered by BYRD itself is the compliance statistic in your dashboard.

To create your own statistics an statistics script needs to be registered in the pim.xml of your Datamodel:

<pim>
    <statistics>/my-statistics-script.rb</statistics>
</pim>

The basic idea behind statistics in BYRD are as followed:

  • when saving an item the statistics script is called.
  • the script has access to the new version of the item (new_item) as well as to the item state before (old_item).
  • based on the information of those two item versions you can choose to change values of the stored statistic you are implementing
  • the information provided by statistics can be either used by custom dashlets to be displayed in frontend or via our REST-API

Example

The following snippet shows an example statistics script, that counts all items, that are heavier then 1000kg or lighter then 1kg

PIM::Statistics.calculate_statistics do

  statistic 'extremeWeights' do
     if old_item
      decr('over_1000') if old_item['weight_kg'].to_i > 1000
      decr('below_1') if old_item['weight_kg'].to_i < 1
    end
    if new_item
      incr('over_1000') if new_item['weight_kg'].to_i > 1000
      incr('below_1') if new_item['weight_kg'].to_i < 1
    end
  end

end

As you can see, we define two dimensions here: "over_1000= and "below_1".

To understand whats going on here, consider we are storing an item that previously had a weight of 0,5 kg and was now set to 2000kg. When accessing the first if-statement the 'below_1' dimensions is decremented via the decr function (as the prior value was bellow 1). Accessing the second if-statement then increases the 'over_1000' dimension: Both dimensions are now in a correct state!

To access the data collected by your statistics you can use our REST-API. Doing an HTTP-GET on the following address:

http://app.syncmanager.com/api/v1/statistics/dynamic/extremeWeights

will result in a JSON Result with this format:

[
    {
        "name": "extremeWeights",
        "key": "below_1",
        "dimension": null,
        "count": 1,
        "version": 1,
        "nonShardedId": "extremeWeights:below_1:1"
    },
    {
        "name": "extremeWeights",
        "key": "over_1000",
        "dimension": null,
        "count": 1,
        "version": 1,
        "nonShardedId": "extremeWeights:over_1000:1"
    }
]