<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::Graph::Data;

use Scalar::Util qw(looks_like_number);
use JSON qw(decode_json encode_json);

use Moo;

extends 'GADS::Graph';

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

has xlabels => (
    is      => 'rw',
    lazy    => 1,
    builder => sub { $_[0]->_data->{xlabels} },
);

has labels => (
    is      => 'rw',
    lazy    => 1,
    builder => sub { $_[0]->_data->{labels} },
);

has points => (
    is      => 'rw',
    lazy    => 1,
    builder => sub { $_[0]->_data->{points} },
);

has showlegend => (
    is      => 'rw',
    lazy    => 1,
    builder => sub { $_[0]->_data->{showlegend} },
);

# Function to fill out the series of data that will be plotted on a graph
has _data => (
    is      => 'rw',
    lazy    => 1,
    builder => sub { $_[0]->_build_data },
);

sub _build_data
{   my $self = shift;

    # XXX A lot of this would probably be better done by the database...

    my $layout   = $self->records->layout;
    my $x_axis   = $layout->column($self->x_axis);
    my $y_axis   = $layout->column($self->y_axis);
    my $group_by = $self->group_by && $layout->column($self->group_by);

    # $y_group_index used to count y_group unique values
    my $y_group_index = 0;

    my @colors = ('#FF6961', '#77DD77', '#FFB347', '#AEC6CF', '#FDFD96');

    # Go through each record, and count how many unique values
    # there are for the field in question. Then define the key
    # of the xy_values hash using the index count
    my %xy_values; my %y_group_values;
    my ($datemin, $datemax);
    foreach my $record (@{$self->records->results})
    {
        my $xval = $record->fields->{$x_axis->id};
        if ($x_axis->return_type && $x_axis->return_type eq 'date')
        {
            $xval = _group_date($xval->value, $self->x_axis_grouping);
        }
        next unless $xval;

        my $gval = $group_by && $record->fields->{$group_by->id};

        if (!defined $xy_values{$xval})
        {
            $xy_values{"$xval"} = 1;
        }<commit_msg>Do not error for missing x values<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::Graph::Data;

use Scalar::Util qw(looks_like_number);
use JSON qw(decode_json encode_json);

use Moo;

extends 'GADS::Graph';

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

has xlabels => (
    is      => 'rw',
    lazy    => 1,
    builder => sub { $_[0]->_data->{xlabels} },
);

has labels => (
    is      => 'rw',
    lazy    => 1,
    builder => sub { $_[0]->_data->{labels} },
);

has points => (
    is      => 'rw',
    lazy    => 1,
    builder => sub { $_[0]->_data->{points} },
);

has showlegend => (
    is      => 'rw',
    lazy    => 1,
    builder => sub { $_[0]->_data->{showlegend} },
);

# Function to fill out the series of data that will be plotted on a graph
has _data => (
    is      => 'rw',
    lazy    => 1,
    builder => sub { $_[0]->_build_data },
);

sub _build_data
{   my $self = shift;

    # XXX A lot of this would probably be better done by the database...

    my $layout   = $self->records->layout;
    my $x_axis   = $layout->column($self->x_axis);
    my $y_axis   = $layout->column($self->y_axis);
    my $group_by = $self->group_by && $layout->column($self->group_by);

    # $y_group_index used to count y_group unique values
    my $y_group_index = 0;

    my @colors = ('#FF6961', '#77DD77', '#FFB347', '#AEC6CF', '#FDFD96');

    # Go through each record, and count how many unique values
    # there are for the field in question. Then define the key
    # of the xy_values hash using the index count
    my %xy_values; my %y_group_values;
    my ($datemin, $datemax);
    foreach my $record (@{$self->records->results})
    {
        my $xval = $record->fields->{$x_axis->id};
        if ($xval && $x_axis->return_type && $x_axis->return_type eq 'date')
        {
            $xval = _group_date($xval->value, $self->x_axis_grouping);
        }
        next unless $xval;

        my $gval = $group_by && $record->fields->{$group_by->id};

        if (!defined $xy_values{$xval})
        {
            $xy_values{"$xval"} = 1;
        }
{'fraction_non_alphanumeric': 0.1378373483064662, 'fraction_numerical': 0.011592102879913059, 'mean_word_length': 2.7899794097460537, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 111, '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': '5734397', 'n_tokens_mistral': 1845, 'n_tokens_neox': 1775, 'n_words': 617}