File Coverage

blib/lib/App/Environ/ClickHouse/Proxy.pm
Criterion Covered Total %
statement 29 47 61.7
branch 0 10 0.0
condition 0 6 0.0
subroutine 10 12 83.3
pod 0 2 0.0
total 39 77 50.6


line stmt bran cond sub pod time code
1             package App::Environ::ClickHouse::Proxy;
2              
3             our $VERSION = '0.2';
4              
5 1     1   526 use strict;
  1         3  
  1         22  
6 1     1   5 use warnings;
  1         1  
  1         21  
7 1     1   9 use v5.10;
  1         3  
8 1     1   3 use utf8;
  1         12  
  1         7  
9              
10 1     1   261 use App::Environ;
  1         525  
  1         23  
11 1     1   230 use App::Environ::Config;
  1         6988  
  1         28  
12 1     1   6 use Cpanel::JSON::XS;
  1         2  
  1         45  
13 1     1   387 use IO::Socket;
  1         13629  
  1         5  
14              
15             my $INSTANCE;
16              
17             my $JSON = Cpanel::JSON::XS->new->utf8;
18              
19             App::Environ::Config->register(qw(clickhouse_proxy.yml));
20              
21             sub instance {
22 0     0 0   my $class = shift;
23              
24 0 0         unless ($INSTANCE) {
25 0           my $config = App::Environ::Config->instance;
26              
27             my $sock = IO::Socket::INET->new(
28             Proto => 'udp',
29             PeerAddr => $config->{clickhouse_proxy}{host},
30             PeerPort => $config->{clickhouse_proxy}{port},
31 0 0         ) or die "Could not create socket: $!\n";
32              
33 0           $INSTANCE = bless { sock => $sock }, $class;
34             }
35              
36 0           return $INSTANCE;
37             }
38              
39             sub send {
40 0     0 0   my __PACKAGE__ $self = shift;
41 0           my $query = shift;
42              
43 1     1   508 no warnings 'numeric';
  1         3  
  1         128  
44              
45 0           my @types;
46 0           foreach (@_) {
47 0 0 0       if ( length( ( my $dummy = '' ) & $_ ) && 0 + $_ eq $_ && $_ * 0 == 0 ) {
      0        
48 0 0         if (/^[+-]?\d+\z/) {
49 0           push @types, 'int';
50             }
51             else {
52 0           push @types, 'float';
53             }
54             }
55             else {
56 0           push @types, 'string';
57             }
58             }
59              
60 1     1   6 use warnings 'numeric';
  1         3  
  1         120  
61              
62 0           my %val = (
63             query => $query,
64             data => \@_,
65             types => \@types,
66             version => 1,
67             );
68              
69 0 0         $self->{sock}->send( $JSON->encode( \%val ) ) or warn "Send error: $!\n";
70              
71 0           return;
72             }
73              
74             1;
75              
76             __END__