<commit_before>package ISC::BIND::Stats::Parser;
use common::sense;
use base qw(XML::SAX::Base);
use Data::Dumper;

my $elements              = [];
my $current_view          = q{};
my $current_zone          = q{};
my $cluster_counters      = {};
my $current_query_counter = q{};
my $valid_zone            = 0;
my $current_opcode        = q{};
my $current_nsstat        = q{};
my $current_zonestat      = q{};

my $boot_time;
my $sample_time;

my $zone = {};

my $server = {};
my $isc_version = undef;
my $stats_version = undef;
my $counter_name  = undef;

sub start_element {
  my ( $self, $el ) = @_;

  if($el->{Name} eq 'isc') {
      $isc_version   = $el->{Attributes}->{'{}version'}->{'Value'};
  }
  if($el->{Name} eq 'statistics') {
      $stats_version = $el->{Attributes}->{'{}version'}->{'Value'};
  }

  if($el->{Name} eq 'counter') {
      $counter_name = $el->{Attributes}->{'name'}->{'Value'};
  }

  #print Dumper($self);
  # process element start event
  push @$elements, lc $el->{Name};

}

sub end_element {
  my ( $self, $el ) = @_;

  if ( $el->{Name} eq 'counters' ) {
    $valid_zone = 0;
  }

  pop @$elements;
}

sub characters {
  my ( $self, $data ) = @_;

  # do this with log4perl?
  if($self->{bind9statsdebug}) {
      print sprintf("in item[%u]: %s\n", scalar @{$elements}, join(' ', @{$elements}));
  }

  my $element_name1 = $elements->[-1];
  my $element_name2 = $elements->[-2];
  my $element_name3 = $elements->[-3];
  my $element_name4 = $elements->[-4];

  if ( $element_name1 eq 'name' && $element_name2 eq 'view' ) {
    $current_view = $data->{Data};
    return;
  }

  if ( $element_name1 eq 'name' && $element_name2 eq 'zone' ) {
    $current_zone = $data->{Data};
    $current_zone =~ s|/IN$||;
    return;
  }

  if (    $element_name1 eq 'serial'
       && $data->{Data} > 0 )
  {
    $valid_zone = 1;
    $zone->{$current_zone}->{serial} = $data->{Data};
    return;
  }
  if ( $element_name2 eq 'counters' ) {
    if ($valid_zone) {
      $zone->{$current_zone}->{counters}->{ $element_name1 } = $data->{Data};
      return;
    }<commit_msg>change element stack from list of names to stack of objects<commit_after>package ISC::BIND::Stats::Parser;
use common::sense;
use base qw(XML::SAX::Base);
use Data::Dumper;

my $element_stack         = [];
my $current_view          = q{};
my $current_zone          = q{};
my $cluster_counters      = {};
my $current_query_counter = q{};
my $valid_zone            = 0;
my $current_opcode        = q{};
my $current_nsstat        = q{};
my $current_zonestat      = q{};

my $boot_time;
my $sample_time;

my $zone = {};

my $server = {};
my $isc_version = undef;
my $stats_version = undef;
my $counter_name  = undef;

sub start_element {
  my ( $self, $el ) = @_;

  if($el->{Name} eq 'isc') {
      $isc_version   = $el->{Attributes}->{'{}version'}->{'Value'};
  }
  if($el->{Name} eq 'statistics') {
      $stats_version = $el->{Attributes}->{'{}version'}->{'Value'};
  }

  if($el->{Name} eq 'counter') {
      $counter_name = $el->{Attributes}->{'name'}->{'Value'};
  }

  #print Dumper($self);
  # process element start event
  push @$element_stack, $el;

}

sub end_element {
  my ( $self, $el ) = @_;

  if ( $el->{Name} eq 'counters' ) {
    $valid_zone = 0;
  }

  pop @$element_stack;
}

sub characters {
  my ( $self, $data ) = @_;

  # do this with log4perl?
  if($self->{bind9statsdebug}) {
      print sprintf("in item[%u]: %s\n", scalar @{$element_stack}, join(' ', map { lc $_->{Name} } @{$element_stack}));
  }

  my $element_name1 = lc $element_stack->[-1]->{Name};
  my $element_name2 = lc $element_stack->[-2]->{Name};
  my $element_name3 = lc $element_stack->[-3]->{Name};
  my $element_name4 = lc $element_stack->[-4]->{Name};

  if ( $element_name1 eq 'name' && $element_name2 eq 'view' ) {
    $current_view = $data->{Data};
    return;
  }

  if ( $element_name1 eq 'name' && $element_name2 eq 'zone' ) {
    $current_zone = $data->{Data};
    $current_zone =~ s|/IN$||;
    return;
  }

  if (    $element_name1 eq 'serial'
       && $data->{Data} > 0 )
  {
    $valid_zone = 1;
    $zone->{$current_zone}->{serial} = $data->{Data};
    return;
  }
  if ( $element_name2 eq 'counters' ) {
    if ($valid_zone) {
      $zone->{$current_zone}->{counters}->{ $element_name1 } = $data->{Data};
      return;
    }
{'fraction_non_alphanumeric': 0.20851654709557974, 'fraction_numerical': 0.009719972228650776, 'mean_word_length': 3.054409005628518, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 67, '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': '3302308', 'n_tokens_mistral': 1690, 'n_tokens_neox': 1642, 'n_words': 350}