<commit_before>package GADS;
use Dancer2;
use GADS::Record;
use GADS::User;
use GADS::View;
use GADS::Layout;
use GADS::Email;
use GADS::Graph;
use GADS::Config;
use GADS::Alert;
use GADS::Audit;
use GADS::DB;
use GADS::Util         qw(:all);
use Dancer2::Plugin::Auth::Complete;
use Ouch;
use DateTime;
use HTML::Entities;
use JSON qw(decode_json encode_json);
use Text::CSV;

set serializer => 'JSON';
set behind_proxy => config->{behind_proxy}; # XXX Why doesn't this work in config file

our $VERSION = '0.1';

Dancer2::Plugin::Auth::Complete->user_callback( sub {
    my ($retuser, $user) = @_;
    $retuser->{value} = GADS::Record->person_update_value($user);
    $retuser->{views} = GADS::View->all($user->id);
    $retuser->{lastrecord} = $user->lastrecord ? $user->lastrecord->id : undef;
    $retuser;
});

hook before => sub {

    # Static content
    return if request->uri =~ m!^/(error|js|css|login|images|fonts|resetpw)!;
    return if param 'error';

    # Redirect on no session
    redirect '/login' unless user;

    # Dynamically generate "virtual" columns for each row of data, based on the
    # configured layout
    GADS::DB->setup;

};

hook before_template => sub {
    my $tokens = shift;

    # Log to audit
    my $user = user;
    my $method = request->method;
    my $path   = request->path;
    GADS::Audit->user_action($user->{id}, qq(User $user->{username} made $method request to $path))
        if $user;

    my $base = request->base;
    $tokens->{url}->{css}  = "${base}css";
    $tokens->{url}->{js}   = "${base}js";
    $tokens->{url}->{page} = $base;
    $tokens->{url}->{page} =~ s!.*/!!; # Remove trailing slash
    $tokens->{hostlocal}   = config->{gads}->{hostlocal};

    $tokens->{header} = config->{gads}->{header};

    $tokens->{approve_waiting} = scalar @{GADS::Record->approve}
        if permission 'approver';
    $tokens->{messages} = session('messages');
    $tokens->{user}     = $user;
    $tokens->{config}   = config;
    session 'messages' => [];

};

get '/' => sub {

    template 'index' => {
        instance => GADS::Config->conf,
        page     => 'index'
    };
};

get '/data_calendar/:time' => sub {

    # Time variable is used to prevent caching by browser

    my $view_id = session 'view_id';
    my $from    = param 'from';
    my $to      = param 'to';

    header "Cache-Control" => "max-age=0, must-revalidate, private";
    {
        "success" => 1,
        "result"  => GADS::Record->data_calendar($view_id, user, $from, $to),
    }
};

get '/search' => sub {

    my $search = param 'search';
    my @results = GADS::Record->search($search, user);
    template 'search' => {
        results => \@results,
        search  => $search,
        page    => 'search',
    };
};<commit_msg>Add basic ping functionality to check website is alive<commit_after>package GADS;
use Dancer2;
use GADS::Record;
use GADS::User;
use GADS::View;
use GADS::Layout;
use GADS::Email;
use GADS::Graph;
use GADS::Config;
use GADS::Alert;
use GADS::Audit;
use GADS::DB;
use GADS::Util         qw(:all);
use Dancer2::Plugin::Auth::Complete;
use Ouch;
use DateTime;
use HTML::Entities;
use JSON qw(decode_json encode_json);
use Text::CSV;

set serializer => 'JSON';
set behind_proxy => config->{behind_proxy}; # XXX Why doesn't this work in config file

our $VERSION = '0.1';

Dancer2::Plugin::Auth::Complete->user_callback( sub {
    my ($retuser, $user) = @_;
    $retuser->{value} = GADS::Record->person_update_value($user);
    $retuser->{views} = GADS::View->all($user->id);
    $retuser->{lastrecord} = $user->lastrecord ? $user->lastrecord->id : undef;
    $retuser;
});

hook before => sub {

    # Static content
    return if request->uri =~ m!^/(error|js|css|login|images|fonts|resetpw|ping)!;
    return if param 'error';

    # Redirect on no session
    redirect '/login' unless user;

    # Dynamically generate "virtual" columns for each row of data, based on the
    # configured layout
    GADS::DB->setup;

};

hook before_template => sub {
    my $tokens = shift;

    # Log to audit
    my $user = user;
    my $method = request->method;
    my $path   = request->path;
    GADS::Audit->user_action($user->{id}, qq(User $user->{username} made $method request to $path))
        if $user;

    my $base = request->base;
    $tokens->{url}->{css}  = "${base}css";
    $tokens->{url}->{js}   = "${base}js";
    $tokens->{url}->{page} = $base;
    $tokens->{url}->{page} =~ s!.*/!!; # Remove trailing slash
    $tokens->{hostlocal}   = config->{gads}->{hostlocal};

    $tokens->{header} = config->{gads}->{header};

    $tokens->{approve_waiting} = scalar @{GADS::Record->approve}
        if permission 'approver';
    $tokens->{messages} = session('messages');
    $tokens->{user}     = $user;
    $tokens->{config}   = config;
    session 'messages' => [];

};

get '/' => sub {

    template 'index' => {
        instance => GADS::Config->conf,
        page     => 'index'
    };
};

get '/ping' => sub {
    content_type 'text/plain';
    'alive';
};

get '/data_calendar/:time' => sub {

    # Time variable is used to prevent caching by browser

    my $view_id = session 'view_id';
    my $from    = param 'from';
    my $to      = param 'to';

    header "Cache-Control" => "max-age=0, must-revalidate, private";
    {
        "success" => 1,
        "result"  => GADS::Record->data_calendar($view_id, user, $from, $to),
    }
};

get '/search' => sub {

    my $search = param 'search';
    my @results = GADS::Record->search($search, user);
    template 'search' => {
        results => \@results,
        search  => $search,
        page    => 'search',
    };
};
{'fraction_non_alphanumeric': 0.18545390196425413, 'fraction_numerical': 0.002477437621659883, 'mean_word_length': 3.1467351430667643, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 122, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1293677', 'n_tokens_mistral': 2017, 'n_tokens_neox': 1935, 'n_words': 476}