File Coverage

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


line stmt bran cond sub pod time code
1             package App::Environ::ClickHouse::Proxy;
2              
3             our $VERSION = '0.3';
4              
5 1     1   914 use strict;
  1         4  
  1         44  
6 1     1   10 use warnings;
  1         2  
  1         41  
7 1     1   18 use v5.10;
  1         5  
8 1     1   9 use utf8;
  1         3  
  1         9  
9              
10 1     1   469 use App::Environ;
  1         858  
  1         35  
11 1     1   388 use App::Environ::Config;
  1         12985  
  1         48  
12 1     1   14 use Cpanel::JSON::XS;
  1         4  
  1         85  
13 1     1   651 use IO::Socket;
  1         25125  
  1         7  
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   959 no warnings 'numeric';
  1         4  
  1         242  
44              
45 0           my @types;
46 0           foreach (@_) {
47 0 0 0       if ( defined($_)
      0        
      0        
48             && length( ( my $dummy = '' ) & $_ )
49             && 0 + $_ eq $_
50             && $_ * 0 == 0 )
51             {
52 0 0         if (/^[+-]?\d+\z/) {
53 0           push @types, 'int';
54             }
55             else {
56 0           push @types, 'float';
57             }
58             }
59             else {
60 0           push @types, 'string';
61             }
62             }
63              
64 1     1   12 use warnings 'numeric';
  1         4  
  1         224  
65              
66 0           my %val = (
67             query => $query,
68             data => \@_,
69             types => \@types,
70             version => 1,
71             );
72              
73 0 0         $self->{sock}->send( $JSON->encode( \%val ) ) or warn "Send error: $!\n";
74              
75 0           return;
76             }
77              
78             1;
79              
80             __END__