<commit_before>#!/usr/bin/perl

=pod
GADS - Globally Accessible Data Store
Copyright (C) 2014 Ctrl O Ltd

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
=cut

use FindBin;
use lib "$FindBin::Bin/../lib";

use GADS::Schema;
use GADS::Alert;
use GADS::Email;
use Dancer2 ':script';
use Dancer2::Plugin::DBIC qw(schema resultset rset);
use Dancer2::Plugin::LogReport mode => 'NORMAL';

my $url = config->{gads}->{url};
$url =~ s!/$!!; # Remove trailing slash

sub _send
{   my ($email, @notifications) = @_;

    @notifications or return;

    my $text = "This email contains details of any changes in views that you have asked to be alerted to.\n\n";
    my $html = "<p>This email contains details of any changes in views that you have asked to be alerted to.</p>";
    $text .= join "\n", map { $_->{text} } @notifications;
    $html .= join "\n", map { $_->{html} } @notifications;
    my $email_send = GADS::Email->new(config => config);

    $email_send->send({
        emails  => [$email],
        subject => "Changes in your views",
        text    => $text,
        html    => $html,
    });
}

sub _id_link
{   qq(<a href="$url/record/$_[0]">record $_[0]</a>)
}

sub _do_columns
{   my ($current_id, @columns) = @_;
    @columns or return;
    my $cols = join ', ', @columns;
    my $notification = "* The following columns have changed in record $current_id: $cols";
    my $link = _id_link($current_id);
    my $notification_html = qq(<li>The following columns have changed in $link: $cols</li>);
    {
        text => $notification,
        html => $notification_html,
    };
}

my @rows = rset('AlertSend')->search({}, {
    join     => { alert => 'user' },
    order_by => [qw/ user.id alert_id current_id /],
})->all;

my ($last_current_id, $last_alert_id, $last_user);
my @notifications; my @columns;
foreach my $row (@rows)
{
    my $current_id = $row->current_id;

    if (defined $last_user && $last_user->id != $row->alert->user_id)
    {
        push @notifications, _do_columns($last_current_id, @columns);
        _send $last_user->email, @notifications;
        @notifications = ();
    }

    if (!defined $last_alert_id || $last_alert_id != $row->alert_id)
    {
        push @notifications, _do_columns($last_current_id, @columns);
        push @notifications, { text => '', html => '</ul><p></p>' } if @notifications; # blank line separater
        my $view_name = $row->alert->view->name;
        push @notifications, {
            text => qq(The following are changes in the view "$view_name":),
            html => qq(<p>The following are changes in the view "$view_name":</p><ul>),
        };
    }

    if (defined $last_current_id && $last_current_id != $row->current_id && @columns)
    {
        my $current_id = $row->current_id;
        push @notifications, _do_columns($last_current_id, @columns);
        @columns = ();
    }

    if ($row->status eq 'changed')
    {
        push @columns, $row->layout->name;
    }<commit_msg>Fix previous alert columns being added to next alert<commit_after>#!/usr/bin/perl

=pod
GADS - Globally Accessible Data Store
Copyright (C) 2014 Ctrl O Ltd

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
=cut

use FindBin;
use lib "$FindBin::Bin/../lib";

use GADS::Schema;
use GADS::Alert;
use GADS::Email;
use Dancer2 ':script';
use Dancer2::Plugin::DBIC qw(schema resultset rset);
use Dancer2::Plugin::LogReport mode => 'NORMAL';

my $url = config->{gads}->{url};
$url =~ s!/$!!; # Remove trailing slash

sub _send
{   my ($email, @notifications) = @_;

    @notifications or return;

    my $text = "This email contains details of any changes in views that you have asked to be alerted to.\n\n";
    my $html = "<p>This email contains details of any changes in views that you have asked to be alerted to.</p>";
    $text .= join "\n", map { $_->{text} } @notifications;
    $html .= join "\n", map { $_->{html} } @notifications;
    my $email_send = GADS::Email->new(config => config);

    $email_send->send({
        emails  => [$email],
        subject => "Changes in your views",
        text    => $text,
        html    => $html,
    });
}

sub _id_link
{   qq(<a href="$url/record/$_[0]">record $_[0]</a>)
}

sub _do_columns
{   my ($current_id, @columns) = @_;
    @columns or return;
    my $cols = join ', ', @columns;
    my $notification = "* The following columns have changed in record $current_id: $cols";
    my $link = _id_link($current_id);
    my $notification_html = qq(<li>The following columns have changed in $link: $cols</li>);
    {
        text => $notification,
        html => $notification_html,
    };
}

my @rows = rset('AlertSend')->search({}, {
    join     => { alert => 'user' },
    order_by => [qw/ user.id alert_id current_id /],
})->all;

my ($last_current_id, $last_alert_id, $last_user);
my @notifications; my @columns;
foreach my $row (@rows)
{
    my $current_id = $row->current_id;

    if (defined $last_user && $last_user->id != $row->alert->user_id)
    {
        push @notifications, _do_columns($last_current_id, @columns);
        @columns = ();
        _send $last_user->email, @notifications;
        @notifications = ();
    }

    if (!defined $last_alert_id || $last_alert_id != $row->alert_id)
    {
        push @notifications, _do_columns($last_current_id, @columns);
        @columns = ();
        push @notifications, { text => '', html => '</ul><p></p>' } if @notifications; # blank line separater
        my $view_name = $row->alert->view->name;
        push @notifications, {
            text => qq(The following are changes in the view "$view_name":),
            html => qq(<p>The following are changes in the view "$view_name":</p><ul>),
        };
    }

    if (defined $last_current_id && $last_current_id != $row->current_id && @columns)
    {
        my $current_id = $row->current_id;
        push @notifications, _do_columns($last_current_id, @columns);
        @columns = ();
    }

    if ($row->status eq 'changed')
    {
        push @columns, $row->layout->name;
    }
{'fraction_non_alphanumeric': 0.14331431751774037, 'fraction_numerical': 0.002782802281897871, 'mean_word_length': 3.210896309314587, 'pattern_counts': {'":': 4, '<': 29, '<?xml version=': 0, '>': 103, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3991874', 'n_tokens_mistral': 2303, 'n_tokens_neox': 2241, 'n_words': 748}