package App::Netdisco::Web::Health;

use Dancer ':syntax';
use Dancer::Plugin::DBIC;

use Try::Tiny;

if (my $path = setting('health_path')) {
  get $path => sub {
    content_type 'application/json';

    # Deflater has no minimum size, and gzipping a body this small makes it
    # bigger. Probes hit this every few seconds.
    request->env->{'psgix.no-compress'} = 1;

    my $db_ok = try {
      schema('netdisco')->storage->dbh->ping;
      1;
    } catch { 0 };

    my @backends = try {
      schema('netdisco')->resultset('DeviceSkip')
        ->search({ device => '255.255.255.255' })->hri->all;
    } catch { () };

    my $num_backends = scalar @backends;
    my $tot_workers  = 0;
    $tot_workers += $_->{deferrals} for @backends;

    my $status = $db_ok ? 'ok' : 'degraded';
    status( $db_ok ? 200 : 503 );

    return to_json {
      status   => $status,
      db       => ($db_ok ? 'ok' : 'error'),
      backends => $num_backends,
      workers  => $tot_workers,
    };
  };
}

true;
