<commit_before>package Muninicious::Munin::Graph;

use strict;
use warnings;

use Mojo::Base -base;


use constant {
  START => {
    day   => '-2000m',
    week  => '-12000m',
    month => '-48000m',
    year  => '-400d'
  },
  FONTS => [
    'DejaVuSans',
    'DejaVu Sans',
    'DejaVu LGC Sans',
    'Bitstream Vera Sans'
  ],
  FIXED_FONTS => [
    'DejaVuSansMono',
    'DejaVu Sans Mono',
    'DejaVu LGC Sans Mono',
    'Bitstream Vera Sans Mono',
    'monospace'
  ],
  COLOURS => {
    'back'   => 'F0F0F0',
    'frame'  => 'F0F0F0',
    'font'   => '666666',
    'canvas' => 'FFFFFF',
    'axis'   => 'CFD6F8',
    'arrow'  => 'CFD6F8'
  },
  PALETTE => [qw(
    00CC00 0066B3 FF8000 FFCC00 330099 990099 CCFF00 FF0000 808080
    008F00 00487D B35A00 B38F00         6B006B 8FB300 B30000 BEBEBE
    80FF80 80C9FF FFC080 FFE680 AA80FF EE00CC FF8080
    666600 FFBFFF 00FFCC CC6699 999900
  )],
};

has service       => undef;
has type          => 'day';
has name          => undef;
has filename      => '-';
has palette_index => 0;
has stack         => 0;
has graph_colours => sub { return {} };


sub _get_applicable_fields {
  my ($self) = @_;
  my @list = ();
  foreach my $field (@{$self->service->fields}) {
    push(@list, $field);
  }
  return \@list;
}


sub _get_negative_names {
  my ($self) = @_;
  my @list = ();
  foreach my $field (@{$self->_get_applicable_fields}) {
    push(@list, $field->metadata('negative'))
      if (defined $field->metadata('negative'));
  }
  return \@list;
}

sub _push_defs {
  my ($self, $args, $field) = @_;

  push(@$args, 'DEF:a'.$field->get_rrd_name.'='.$field->get_rrd_file.':42:AVERAGE');
  push(@$args, 'DEF:l'.$field->get_rrd_name.'='.$field->get_rrd_file.':42:MIN');
  push(@$args, 'DEF:h'.$field->get_rrd_name.'='.$field->get_rrd_file.':42:MAX');
  push(@$args, 'CDEF:n'.$field->get_rrd_name.'=a'.$field->get_rrd_name);
  if (defined $field->metadata('warning')) {
    foreach my $limit (split(/\:/, $field->metadata('warning'))) {
      push(@$args, 'HRULE:'.$limit.'#0066B3') if ($limit ne '');
    }
  }
  if (defined $field->metadata('critical')) {
    foreach my $limit (split(/\:/, $field->metadata('critical'))) {
      push(@$args, 'HRULE:'.$limit.'#FF0000') if ($limit ne '');
    }
  }
  return;
}

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

  my $max_label_length = 0;
  foreach my $field (@{$self->_get_applicable_fields}) {
    if (length($field->metadata('label') || '') > $max_label_length) {
      $max_label_length = length($field->metadata('label'));
    }
  }
  return $max_label_length + 1;
}

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

  my $index = $self->palette_index();
  $index = 0 if ($index >= scalar @{&PALETTE});

  my $colour = &PALETTE->[$index++];

  $self->palette_index($index);

  return $colour;
}

sub _push_labels {
  my ($self, $args, $field, $is_negative) = @_;

  return 0 if (defined $field->metadata('graph') && $field->metadata('graph') eq 'no' && !$is_negative);

  my $max_label_length = $self->_get_max_label_length();
  my $colour = $field->metadata('colour') || $field->metadata('color');

  $colour = $self->graph_colours->{$field->name};
  $colour = $self->_get_palette_colour if (!defined $colour);
<commit_msg>Fix user specified colours.<commit_after>package Muninicious::Munin::Graph;

use strict;
use warnings;

use Mojo::Base -base;


use constant {
  START => {
    day   => '-2000m',
    week  => '-12000m',
    month => '-48000m',
    year  => '-400d'
  },
  FONTS => [
    'DejaVuSans',
    'DejaVu Sans',
    'DejaVu LGC Sans',
    'Bitstream Vera Sans'
  ],
  FIXED_FONTS => [
    'DejaVuSansMono',
    'DejaVu Sans Mono',
    'DejaVu LGC Sans Mono',
    'Bitstream Vera Sans Mono',
    'monospace'
  ],
  COLOURS => {
    'back'   => 'F0F0F0',
    'frame'  => 'F0F0F0',
    'font'   => '666666',
    'canvas' => 'FFFFFF',
    'axis'   => 'CFD6F8',
    'arrow'  => 'CFD6F8'
  },
  PALETTE => [qw(
    00CC00 0066B3 FF8000 FFCC00 330099 990099 CCFF00 FF0000 808080
    008F00 00487D B35A00 B38F00         6B006B 8FB300 B30000 BEBEBE
    80FF80 80C9FF FFC080 FFE680 AA80FF EE00CC FF8080
    666600 FFBFFF 00FFCC CC6699 999900
  )],
};

has service       => undef;
has type          => 'day';
has name          => undef;
has filename      => '-';
has palette_index => 0;
has stack         => 0;
has graph_colours => sub { return {} };


sub _get_applicable_fields {
  my ($self) = @_;
  my @list = ();
  foreach my $field (@{$self->service->fields}) {
    push(@list, $field);
  }
  return \@list;
}


sub _get_negative_names {
  my ($self) = @_;
  my @list = ();
  foreach my $field (@{$self->_get_applicable_fields}) {
    push(@list, $field->metadata('negative'))
      if (defined $field->metadata('negative'));
  }
  return \@list;
}

sub _push_defs {
  my ($self, $args, $field) = @_;

  push(@$args, 'DEF:a'.$field->get_rrd_name.'='.$field->get_rrd_file.':42:AVERAGE');
  push(@$args, 'DEF:l'.$field->get_rrd_name.'='.$field->get_rrd_file.':42:MIN');
  push(@$args, 'DEF:h'.$field->get_rrd_name.'='.$field->get_rrd_file.':42:MAX');
  push(@$args, 'CDEF:n'.$field->get_rrd_name.'=a'.$field->get_rrd_name);
  if (defined $field->metadata('warning')) {
    foreach my $limit (split(/\:/, $field->metadata('warning'))) {
      push(@$args, 'HRULE:'.$limit.'#0066B3') if ($limit ne '');
    }
  }
  if (defined $field->metadata('critical')) {
    foreach my $limit (split(/\:/, $field->metadata('critical'))) {
      push(@$args, 'HRULE:'.$limit.'#FF0000') if ($limit ne '');
    }
  }
  return;
}

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

  my $max_label_length = 0;
  foreach my $field (@{$self->_get_applicable_fields}) {
    if (length($field->metadata('label') || '') > $max_label_length) {
      $max_label_length = length($field->metadata('label'));
    }
  }
  return $max_label_length + 1;
}

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

  my $index = $self->palette_index();
  $index = 0 if ($index >= scalar @{&PALETTE});

  my $colour = &PALETTE->[$index++];

  $self->palette_index($index);

  return $colour;
}

sub _push_labels {
  my ($self, $args, $field, $is_negative) = @_;

  return 0 if (defined $field->metadata('graph') && $field->metadata('graph') eq 'no' && !$is_negative);

  my $max_label_length = $self->_get_max_label_length();
  my $colour = $field->metadata('colour') || $field->metadata('color');

  $colour = $self->graph_colours->{$field->name} if (!defined $colour);
  $colour = $self->_get_palette_colour if (!defined $colour);

{'fraction_non_alphanumeric': 0.20064575645756458, 'fraction_numerical': 0.0501230012300123, 'mean_word_length': 3.4071815718157183, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 115, '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': '902012', 'n_tokens_mistral': 2801, 'n_tokens_neox': 2519, 'n_words': 504}