#!/usr/bin/perl

=encoding UTF-8
=cut

=head1 DESCRIPTION

=cut

# common modules
use strict;
use warnings FATAL => 'all';
use feature 'say';
use utf8;
use open qw(:std :utf8);

use Carp;
use HTTP::Tiny;
use JSON::PP;
use Moment;
use File::Slurp;

# main
sub main {

    my $url = 'http://127.0.0.1:29167/api/3/betas/';

    my $response = HTTP::Tiny->new()->get($url);
    if ($response->{success}) {
        my $data = decode_json $response->{content};

        my $betas_count = grep { $_->{status} ne 'free' } @{$data};

        my $json_coder = JSON::PP
            ->new
            ->canonical
            ;

        my $now = Moment->now();

        my $log_line = $json_coder->encode({
            timestamp => $now->get_iso_string(),
            betas_count => $betas_count,
        }) . "\n";

        write_file(
            sprintf('/local/creator/log/betas_count.%s.jsonl', $now->get_d()),
            {
                append => 1,
            },
            $log_line,
        );
    }
}
main();
__END__
