Beekeeper

 view release on metacpan or  search on metacpan

examples/dashboard/lib/Beekeeper/Service/Dashboard/Worker.pm  view on Meta::CPAN


    unless ($config && $config->{users} && %{$config->{users}}) {
        log_warn "No users defined into config file 'dashboard.config.json'";
    }

    $self->accept_remote_calls(
        'bkpr.dashboard.login'    => 'login',
        'bkpr.dashboard.services' => 'service_stats',
        'bkpr.dashboard.logs'     => 'log_tail',
    );

    $self->_init_collector;

    log_info "Ready";
}

sub on_shutdown {
    my ($self) = @_;

    $self->_save_state;

    log_info "Stopped";
}

sub stop_working {
    my ($self) = @_;

    delete $self->{collect_tmr};
    delete $self->{consolidate_5s_tmr};
    delete $self->{consolidate_1m_tmr};

    $self->SUPER::stop_working;
}

sub login {
    my ($self, $params) = @_;

    my $username = $params->{username};
    my $password = $params->{password};

    AUTH: {

        last unless defined $username;
        last unless defined $password;

        my $users_cfg = $self->{config}->{users};
        my $pwd_hash  = sha256_hex('Dashboard'.$username.$password);

        last unless $users_cfg;
        last unless $users_cfg->{$username};
        last unless $users_cfg->{$username}->{'password'};
        last unless $users_cfg->{$username}->{'password'} eq $pwd_hash;

        # Set authentication data and assign an address to the user
        $self->set_authentication_data( $username );
        $self->bind_remote_session( address => "frontend.dashboard-$username" );

        return 1;
    }

    Beekeeper::JSONRPC::Error->request_not_authenticated;
}

sub service_stats {
    my ($self, $params, $req) = @_;

    my $resol = $params->{'resolution'} || '1s';
    my $count = $params->{'count'}      || 1;
    my $class = $params->{'class'};
    my $after = $params->{'after'};

    $req->deflate_response;

    my $stats = $self->{"services_$resol"} or die "Invalid resolution";

    my $idx = @$stats - $count;
    $idx = 0 if $idx < 0;

    if ($after) {
        my $min = $idx;
        my $new_data;
        for (my $i = @$stats - 1; $i >= 0; $i--) {
            last if $stats->[$i]->[TSTAMP] <= $after;
            last if $i < $min;
            $new_data = $i;
        }
        return [] unless $new_data;
        $idx = $new_data;
    }

    unless ($class) {
        return [ @$stats[$idx..(@$stats - 1)] ];
    }

    my @svc_stats;

    foreach my $st (@$stats[$idx..(@$stats - 1)]) {
        next unless exists $st->[STATS]->{$class};
        push @svc_stats, [ $st->[TSTAMP], $st->[STATS]->{$class} ];
    }

    return \@svc_stats;
}

sub log_tail {
    my ($self, $params, $req) = @_;

    my %filters;

    foreach my $filter (qw'service count level after host pool message') {
        next unless $params->{$filter};
        $filters{$filter} = $params->{$filter};
    }

    $req->async_response;
    $req->deflate_response;

    Beekeeper::Service::LogTail->tail_async(
        %filters,
        on_success => sub {
            my ($resp) = @_;



( run in 1.015 second using v1.01-cache-2.11-cpan-99c4e6809bf )