<commit_before>=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

package GADS::Datum::Rag;

use Log::Report;
use Moo;
use namespace::clean;

extends 'GADS::Datum::Code';

has set_value => (
    is      => 'rw',
    trigger => sub {
        my $self = shift;
        $self->_set_has_value(1);
    },
);

has value => (
    is       => 'rw',
    lazy     => 1,
    clearer  => 1,
    builder  => sub {
        my $self = shift;
        $self->_transform_value($self->set_value);
    },
);

has has_value => (
    is => 'rwp',
);

has dependent_values => (
    is       => 'rw',
    required => 1,
);

has layout => (
    is       => 'rw',
    required => 1,
);

sub _transform_value
{   my ($self, $original) = @_;

    my $column           = $self->column;
    my $layout           = $self->layout;
    my $dependent_values = $self->dependent_values;

    if (exists $original->{value} && !$self->force_update)
    {
        return $original->{value};
    }
    elsif (!$self->column->green && !$self->column->amber && !$self->column->red)
    {
        return $self->_write_rag('a_grey');
    }
    else {
        my $green = $self->column->green;
        my $amber = $self->column->amber;
        my $red   = $self->column->red;

        foreach my $col_id (@{$column->depends_on})
        {
            my $col = $layout->column($col_id);
            $green  = $self->sub_values($col, $green);
            $amber  = $self->sub_values($col, $amber);
            $red    = $self->sub_values($col, $red);
            $green && $amber && $red or return $self->_write_rag('a_grey');
        }

        # Insert current date if required
        $green = $self->sub_date($green, 'CURDATE', DateTime->now->truncate(to => 'day'));
        $amber = $self->sub_date($amber, 'CURDATE', DateTime->now->truncate(to => 'day'));
        $red   = $self->sub_date($red, 'CURDATE', DateTime->now->truncate(to => 'day'));

        # Insert ID if required
        my $current_id = $self->current_id;
        $green =~ s/\[id\]/$current_id/;
        $amber =~ s/\[id\]/$current_id/;
        $red   =~ s/\[id\]/$current_id/;

        my $okaycount = 0;
        foreach my $code ($green, $amber, $red)
        {
            # If there are still square brackets then something is wrong
            if ($code =~ /[\[\]]+/)
            {
                assert "Invalid field names in rag condition. Remaining code: $code";
            }
            else {
                $okaycount++;
            }
        }

        my $ragvalue;
        # XXX Log somewhere if this fails
        if ($okaycount == 3)
        {
            trace "Red code is: $red";
            trace "Amber code is: $red";
            trace "Green code is: $red";
            if ($red && try { $self->safe_eval("($red)") } )
            {
                $ragvalue = 'b_red';
            }
            elsif (!$@ && $amber && try { $self->safe_eval("($amber)") } )
            {
                $ragvalue = 'c_amber';
            }
            elsif (!$@ && $green && try { $self->safe_eval("($green)") } )
            {
                $ragvalue = 'd_green';<commit_msg>Fix uninit warnings when evaluating rags<commit_after>=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

package GADS::Datum::Rag;

use Log::Report;
use Moo;
use namespace::clean;

extends 'GADS::Datum::Code';

has set_value => (
    is      => 'rw',
    trigger => sub {
        my $self = shift;
        $self->_set_has_value(1);
    },
);

has value => (
    is       => 'rw',
    lazy     => 1,
    clearer  => 1,
    builder  => sub {
        my $self = shift;
        $self->_transform_value($self->set_value);
    },
);

has has_value => (
    is => 'rwp',
);

has dependent_values => (
    is       => 'rw',
    required => 1,
);

has layout => (
    is       => 'rw',
    required => 1,
);

sub _transform_value
{   my ($self, $original) = @_;

    my $column           = $self->column;
    my $layout           = $self->layout;
    my $dependent_values = $self->dependent_values;

    if (exists $original->{value} && !$self->force_update)
    {
        return $original->{value};
    }
    elsif (!$self->column->green && !$self->column->amber && !$self->column->red)
    {
        return $self->_write_rag('a_grey');
    }
    else {
        my $green = $self->column->green;
        my $amber = $self->column->amber;
        my $red   = $self->column->red;

        foreach my $col_id (@{$column->depends_on})
        {
            my $col = $layout->column($col_id);
            $green  = $self->sub_values($col, $green);
            $amber  = $self->sub_values($col, $amber);
            $red    = $self->sub_values($col, $red);
            $green && $amber && $red or return $self->_write_rag('a_grey');
        }

        # Insert current date if required
        $green = $self->sub_date($green, 'CURDATE', DateTime->now->truncate(to => 'day'));
        $amber = $self->sub_date($amber, 'CURDATE', DateTime->now->truncate(to => 'day'));
        $red   = $self->sub_date($red, 'CURDATE', DateTime->now->truncate(to => 'day'));

        # Insert ID if required
        my $current_id = $self->current_id;
        $green =~ s/\[id\]/$current_id/ if $green;
        $amber =~ s/\[id\]/$current_id/ if $amber;
        $red   =~ s/\[id\]/$current_id/ if $red;

        my $okaycount = 0;
        foreach my $code ($green, $amber, $red)
        {
            # If there are still square brackets then something is wrong
            if ($code && $code =~ /[\[\]]+/)
            {
                assert "Invalid field names in rag condition. Remaining code: $code";
            }
            else {
                $okaycount++;
            }
        }

        my $ragvalue;
        # XXX Log somewhere if this fails
        if ($okaycount == 3)
        {
            trace __x"Red code is: {red}", red => $red;
            trace __x"Amber code is: {amber}", amber => $amber;
            trace __x"Green code is: {green}", green => $green;
            if ($red && try { $self->safe_eval("($red)") } )
            {
                $ragvalue = 'b_red';
            }
            elsif (!$@ && $amber && try { $self->safe_eval("($amber)") } )
            {
                $ragvalue = 'c_amber';
            }
            elsif (!$@ && $green && try { $self->safe_eval("($green)") } )
            {
                $ragvalue = 'd_green';
{'fraction_non_alphanumeric': 0.1530758226037196, 'fraction_numerical': 0.0031213421771361686, 'mean_word_length': 2.0145041160329282, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 128, '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': '1739249', 'n_tokens_mistral': 2415, 'n_tokens_neox': 2341, 'n_words': 683}