Statistics

View the JS Documentation

View the YARD Documentation

View the Sample App: Server1Server2Client

Statistics are useful and common. Consider a datagrid listing posts - we might want a “Total Posts” count displayed above the grid without firing an additional request. Notably, that statistic should take into account filtering, but should not take into account pagination.

You can whitelist stats in your Resource:

allow_stat total: [:count]

And request them like so:

/posts?stats[total]=count

They will be returned in the meta section of the response:

{
  # ...
  meta: {
    stats: {
      total: {
        count: 100
      }
    }
  }
}

You can run stats over specific attributes rather than total:

allow_stat rating: [:average]

Adapters support the following statistics out-of-the-box: count, average, sum, maximum, and minimum. You can also define custom statistics:

allow_stat rating: [:average] do
  standard_deviation do |scope, attr|
    # your standard deviation code here
  end
end