| 1 | package App::Netdisco::Web::API::Statistics; | ||
| 2 | |||
| 3 | use Dancer ':syntax'; | ||
| 4 | use Dancer::Plugin::DBIC; | ||
| 5 | use Dancer::Plugin::Swagger; | ||
| 6 | use Dancer::Plugin::Auth::Extensible; | ||
| 7 | |||
| 8 | use Try::Tiny; | ||
| 9 | |||
| 10 | swagger_path { | ||
| 11 | tags => ['General'], | ||
| 12 | path => (setting('api_base') || '').'/statistics', | ||
| 13 | description => 'Returns the latest row from the statistics table', | ||
| 14 | parameters => [], | ||
| 15 | responses => { default => {} }, | ||
| 16 | |||
| 17 | }, get '/api/v1/statistics' => require_role api => sub { | ||
| 18 | my $stats = try { | ||
| 19 | schema(vars->{'tenant'})->resultset('Statistics') | ||
| 20 | ->search(undef, { order_by => { -desc => 'day' }, rows => 1 })->first | ||
| 21 | } or send_error('No statistics available', 404); | ||
| 22 | |||
| 23 | return to_json $stats->TO_JSON; | ||
| 24 | }; | ||
| 25 | |||
| 26 | true; |