BenchmarkAnything-Storage-Backend-SQL

 view release on metacpan or  search on metacpan

lib/BenchmarkAnything/Storage/Backend/SQL.pm  view on Meta::CPAN


    my ( $or_self, $s_pattern ) = @_;

    my $ar_pattern = defined($s_pattern) ? [$s_pattern] : [];

    my $s_key;
    if ( $or_self->{cache} ) {
        require JSON::XS;
        $s_key = JSON::XS::encode_json($ar_pattern);
        if ( my $ar_search_data = $or_self->{cache}->get("list_additional_keys||$s_key") ) {
            return $ar_search_data;
        }
    }

    my $ar_result = $or_self->{query}
        ->select_additional_keys( @$ar_pattern )
        ->fetchall_arrayref([0]);
    my $ar_key_names = [ map { $_->[0] } @$ar_result ];

    if ( $or_self->{cache} ) {
        $or_self->{cache}->set( "list_additional_keys||$s_key" => $ar_key_names );
    }

    return $ar_key_names;

}

sub get_stats {

    my ( $or_self ) = @_;

    my %h_searchengine_stats  = ();
    my %h_flat_searchengine_stats = ();
    my %stats = ();

    # Not strictly *stats* but useful information.
    if ( $or_self->{searchengine}{elasticsearch}{index} )
    {
        require BenchmarkAnything::Storage::Search::Elasticsearch;
        my ($or_es, $s_index, $s_type) = BenchmarkAnything::Storage::Search::Elasticsearch::get_elasticsearch_client
         (
          {searchengine => $or_self->{searchengine}}
         );

        $stats{count_datapoints} = (map {chomp; $_} split(qr/ +/, $or_es->cat->count))[2];
        %h_searchengine_stats =
            (
             index          => $or_self->{searchengine}{elasticsearch}{index} || 'UNKNOWN',
             type           => $or_self->{searchengine}{elasticsearch}{type}  || 'UNKNOWN',
             enable_query   => $or_self->{searchengine}{elasticsearch}{enable_query} || 0,
             cluster_health => $or_es->cluster->health,
             index_single_added_values_immediately => $or_self->{searchengine}{elasticsearch}{index_single_added_values_immediately} || 0,
            );
        # boolean -> 0/1
        for (values %{$h_searchengine_stats{elasticsearch}{cluster_health}}) {
            $_ = $_ ? 1 : 0 if ref eq 'JSON::XS::Boolean';
        }
        $h_flat_searchengine_stats{"elasticsearch_$_"} = $h_searchengine_stats{$_}
          for qw(index type enable_query index_single_added_values_immediately);
        $h_flat_searchengine_stats{"elasticsearch_cluster_health_$_"} = $h_searchengine_stats{cluster_health}{$_}
          for qw(cluster_name active_shards_percent_as_number active_primary_shards number_of_nodes status);
    }

    $stats{count_datapoints}    ||= 0+$or_self->{query}->select_count_datapoints->fetch->[0];
    $stats{count_datapointkeys}   = 0+$or_self->{query}->select_count_datapointkeys->fetch->[0] if $or_self->{verbose};
    $stats{count_metrics}         = 0+$or_self->{query}->select_count_metrics->fetch->[0]       if $or_self->{verbose};
    $stats{count_keys}            = 0+$or_self->{query}->select_count_keys->fetch->[0]          if $or_self->{verbose};

    %stats = (%stats, %h_flat_searchengine_stats);

    return \%stats;
}

sub get_single_benchmark_point {

    my ( $or_self, $i_bench_value_id ) = @_;

    return {} unless $i_bench_value_id;

    # cache?
    my $s_key;
    if ( $or_self->{cache} ) {
        require JSON::XS;
        $s_key = JSON::XS::encode_json({bench_value_id => $i_bench_value_id});
        if ( my $hr_search_data = $or_self->{cache}->get("get_single_benchmark_point||$s_key") ) {
            return $hr_search_data;
        }
    }

    # fetch all additional key/value fields
    my $ar_query_result = $or_self->{query}
        ->select_complete_benchmark_point( $i_bench_value_id )
        ->fetchall_arrayref({});

    # fetch essentials, like NAME, VALUE, UNIT
    my $hr_essentials = $or_self->{query}
        ->select_benchmark_point_essentials( $i_bench_value_id )
        ->fetchrow_hashref();

    # create complete BenchmarkAnything-like key/value entry
    my $hr_result;
    $hr_result          = { map { ($_->{bench_additional_type} => $_->{bench_additional_value} ) } @$ar_query_result };
    $hr_result->{NAME}  = $hr_essentials->{bench};
    $hr_result->{VALUE} = $hr_essentials->{bench_value};
    $hr_result->{VALUE_ID} = $hr_essentials->{bench_value_id};
    $hr_result->{CREATED} = $hr_essentials->{created_at};
    $hr_result->{UNIT}  = $hr_essentials->{bench_unit} if $hr_essentials->{bench_unit};

    # cache!
    if ( $or_self->{cache} ) {
        $or_self->{cache}->set( "get_single_benchmark_point||$s_key" => $hr_result );
    }

    return $hr_result;
}

sub get_full_benchmark_points {

    my ( $or_self, $i_bench_value_id, $i_count ) = @_;

    return [] unless $i_bench_value_id;



( run in 2.721 seconds using v1.01-cache-2.11-cpan-5b529ec07f3 )