<commit_before>package App::KSP_CKAN::Status;

use v5.010;
use strict;
use warnings;
use autodie;
use Method::Signatures 20140224;
use Carp qw( croak );
use File::Slurper qw(read_text write_text);
use Try::Tiny;
use JSON;
use File::Path qw(mkpath);
use App::KSP_CKAN::Status::NetKAN;
use Moo;
use MooX::HandlesVia;
use namespace::clean;

# ABSTRACT: NetKAN Status Tracker

# VERSION: Generated by DZP::OurPkg:Version

=head1 SYNOPSIS

  use App::KSP_CKAN::Status;

  my $status = App::KSP_CKAN::Status->new(
    config => $config,
  );

=head1 DESCRIPTION

Status object for tracking status of at least initially, NetKAN 
indexing.

=cut

my $Ref = sub {
  croak("auth isn't a 'App::KSP_CKAN::Tools::Config' object!") unless $_[0]->DOES("App::KSP_CKAN::Tools::Config");
};

has 'config'        => ( is => 'ro', required => 1, isa => $Ref );
has 'status_file'   => ( is => 'ro', default => sub { "netkan.json" } );
has 'status_path'   => ( is => 'ro', lazy => 1, builder => 1 );
has '_status_file'  => ( is => 'ro', lazy => 1, builder => 1 );
has '_data'         => ( is => 'ro', lazy => 1, builder => 1 );
has '_json'         => ( is => 'ro', lazy => 1, builder => 1 );

has 'status' => ( 
  is => 'rw',
  handles_via => 'Hash',
  handles => {
    get_val => 'get',
    set_val => 'set',
    all_keys => 'keys'
  },
  default => sub { { } }, 
);

method _build__json {
  return JSON->new->allow_blessed(1)->convert_blessed(1);
}

method _build_status_path {
  return $self->config->working."/status";
}

method _build__status_file {
  if ( ! -d $self->status_path ) {
    mkpath($self->status_path);
  }

  return $self->status_path."/".$self->status_file;
}

method _build__data {
  my $raw;
  if (! -e $self->_status_file) {
    $raw = "{}";
  } else {
    $raw = read_text($self->_status_file);
  }

  my $data;
  try {
    $data = $self->_json->decode($raw);
  } catch {
    $self->logdie("JSON Status failed to parse");
  };

  return $data;
}

=method get_status

  my $netkan_status = $status->get_status( "BaconLabs" );

Will return a reference to the "BaconLabs" status object. Creating
it if necessary or loading it from the available data if it exists.

=cut

method get_status($name) {
  if ($self->_data->{$name}) {
    $self->set_val($name, App::KSP_CKAN::Status::NetKAN->new($self->_data->{$name}));
  } else {
    $self->set_val($name, App::KSP_CKAN::Status::NetKAN->new(name => $name));
  }
  return $self->get_val($name);
}

=method write_json
  
  $status->write_json

Writes our status file out to disk.

=cut

method write_json {
  my $json = $self->_json->encode($self->status);
  write_text($self->_status_file, $json);
}

1;
<commit_msg>Minor cleanup of status attribute layout<commit_after>package App::KSP_CKAN::Status;

use v5.010;
use strict;
use warnings;
use autodie;
use Method::Signatures 20140224;
use Carp qw( croak );
use File::Slurper qw(read_text write_text);
use Try::Tiny;
use JSON;
use File::Path qw(mkpath);
use App::KSP_CKAN::Status::NetKAN;
use Moo;
use MooX::HandlesVia;
use namespace::clean;

# ABSTRACT: NetKAN Status Tracker

# VERSION: Generated by DZP::OurPkg:Version

=head1 SYNOPSIS

  use App::KSP_CKAN::Status;

  my $status = App::KSP_CKAN::Status->new(
    config => $config,
  );

=head1 DESCRIPTION

Status object for tracking status of at least initially, NetKAN 
indexing.

=cut

my $Ref = sub {
  croak("auth isn't a 'App::KSP_CKAN::Tools::Config' object!") unless $_[0]->DOES("App::KSP_CKAN::Tools::Config");
};

has 'config'        => ( is => 'ro', required => 1, isa => $Ref );
has 'status_file'   => ( is => 'ro', default => sub { "netkan.json" } );
has 'status_path'   => ( is => 'ro', lazy => 1, builder => 1 );
has '_status_file'  => ( is => 'ro', lazy => 1, builder => 1 );
has '_data'         => ( is => 'ro', lazy => 1, builder => 1 );
has '_json'         => ( is => 'ro', lazy => 1, builder => 1 );

has 'status' => ( 
  is            => 'rw',
  handles_via   => 'Hash',
  handles       => {
    get_val   => 'get',
    set_val   => 'set',
    all_keys  => 'keys'
  },
  default       => sub { { } }, 
);

method _build__json {
  return JSON->new->allow_blessed(1)->convert_blessed(1);
}

method _build_status_path {
  return $self->config->working."/status";
}

method _build__status_file {
  if ( ! -d $self->status_path ) {
    mkpath($self->status_path);
  }

  return $self->status_path."/".$self->status_file;
}

method _build__data {
  my $raw;
  if (! -e $self->_status_file) {
    $raw = "{}";
  } else {
    $raw = read_text($self->_status_file);
  }

  my $data;
  try {
    $data = $self->_json->decode($raw);
  } catch {
    $self->logdie("JSON Status failed to parse");
  };

  return $data;
}

=method get_status

  my $netkan_status = $status->get_status( "BaconLabs" );

Will return a reference to the "BaconLabs" status object. Creating
it if necessary or loading it from the available data if it exists.

=cut

method get_status($name) {
  if ($self->_data->{$name}) {
    $self->set_val($name, App::KSP_CKAN::Status::NetKAN->new($self->_data->{$name}));
  } else {
    $self->set_val($name, App::KSP_CKAN::Status::NetKAN->new(name => $name));
  }
  return $self->get_val($name);
}

=method write_json
  
  $status->write_json

Writes our status file out to disk.

=cut

method write_json {
  my $json = $self->_json->encode($self->status);
  write_text($self->_status_file, $json);
}

1;

{'fraction_non_alphanumeric': 0.18330544710912808, 'fraction_numerical': 0.010039040713887339, 'mean_word_length': 3.7110332749562174, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 131, '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': '2613717', 'n_tokens_mistral': 2070, 'n_tokens_neox': 2019, 'n_words': 485}