| 1 | package DBIx::Fast::Output; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 7 | ▶ | use v5.38; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 7 | ▶ | use Object::Pad 0.807; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 7 | our $VERSION = '0.22'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 7 | ▶ | use Carp 'croak'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 7 | ▶ | use Time::HiRes (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 7 | ▶ | use POSIX (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | # Unified output layer for the profiler subsystem. Every report method | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | # (print_stats, print_query, ...) funnels through this role instead of | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | # printing directly: 'text' renders exactly as before, 'json' emits one | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | # JSON line per event, a coderef receives the structured data. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | role DBIx::Fast::Output { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | field $output :param = 'text'; # 'text' | 'json' | CODE | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 7 | field $output_fh :param = undef; # undef => currently selected handle | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 7 | ADJUST { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | croak "Invalid output mode: must be 'text', 'json' or a coderef" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | unless _valid_output($output); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 7 | ▶ | sub _valid_output ($v) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_valid_output: called CC=6 SCAR=18.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 24 | ▶ | return 1 if ref $v eq 'CODE'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 20 | ▶ | return 1 if !ref $v && defined $v && ($v eq 'text' || $v eq 'json'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%6/9
not ref $v and defined $v and $v eq "text" || $v eq "json"
not ref $v and defined $v
$v eq "text" or $v eq "json"
MC/DC50%2/4
not ref $v and defined $v and $v eq "text" || $v eq "json"
not ref $v
defined $v
$v eq "text"
$v eq "json"
Truth table60%3/5
not(A) and B and C or D
A= not ref $vB= defined $vC= $v eq "text"D= $v eq "json"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 2 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | # Combined get/set accessors: the generated :accessor setter cannot | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | # validate, so these are written out. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 18 | ▶ | method output (@v) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
output: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 18 | ▶ | if (@v) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 11 | ▶ | croak "Invalid output mode: must be 'text', 'json' or a coderef" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | unless _valid_output($v[0]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 10 | $output = $v[0]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 17 | return $output; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 7 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 2 | ▶ | method output_fh (@v) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
output_fh: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 2 | ▶ | $output_fh = $v[0] if @v; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 2 | return $output_fh; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 7 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 8 | ▶ | method output_is_text () { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
output_is_text: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 8 | ▶ | return !ref $output && $output eq 'text'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
!ref($output) && $output eq 'text'
MC/DC100%2/2
!ref($output) && $output eq 'text'
!ref($output)
$output eq 'text'
Truth table100%3/3
not(A) && B
A= !ref($output)B= $output eq 'text'
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 7 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | # The single funnel every text renderer writes through. With no explicit | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | # filehandle it uses a bare print (the currently selected handle), so | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | # tests and callers that localize *STDOUT keep working. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 9 | ▶ | method out (@text) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
out: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 9 | ▶ | my $ok = $output_fh ? print {$output_fh} @text : print @text; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 9 | ▶ | warn "DBIx::Fast profiler output failed: $!\n" unless $ok; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 9 | return $ok; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 7 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | ▶ | method outf ($fmt, @args) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
outf: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | return $self->out(sprintf $fmt, @args); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 7 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | # Route a report event. Returns 0 in text mode (the caller renders text | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | # itself via out/outf) and 1 when the event was consumed by the json or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | # callback sink. Never dies: profiling must not kill application queries. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 8 | ▶ | method emit ($event, $data) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
emit: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 8 | ▶ | return 0 if $self->output_is_text; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 7 | my $meta = $self->_output_meta; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 7 | $meta->{ts} = _iso_ts(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 7 | my $ok = eval { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 7 | ▶ | if (ref $output eq 'CODE') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 3 | $output->($event, $data, $meta); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 4 | $self->out(_json_encoder()->encode( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | { %$meta, event => $event, data => $data }) . "\n"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 6 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 7 | ▶ | warn "DBIx::Fast profiler output failed: $@" unless $ok; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 7 | return 1; # consumed either way - never fall through to text mid-stream | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 7 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | # Envelope identity, implemented by each consumer: Profile::Base tags | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | # {source => 'profile', driver => <class tail>}, Profiler {source => 'tracker'}. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 7 | method _output_meta; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 4 | ▶ | sub _json_encoder () { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_json_encoder: called CC=2 SCAR=7.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 4 | state $enc = do { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 1 | my $class = eval { require Cpanel::JSON::XS; 'Cpanel::JSON::XS' } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | ▶ | || do { require JSON::PP; 'JSON::PP' }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
eval {
do {
require Cpanel::JSON::XS;
'Cpanel::JSON::XS'
}
} || do {
require JSON::PP;
'JSON::PP'
}
MC/DC0%0/2
eval {
do {
require Cpanel::JSON::XS;
'Cpanel::JSON::XS'
}
} || do {
require JSON::PP;
'JSON::PP'
}
eval {
do {
require Cpanel::JSON::XS;
'Cpanel::JSON::XS'
}
}
do {
require JSON::PP;
'JSON::PP'
}
Truth table33%1/3
A || B
A= eval {
do {
require Cpanel::JSON::XS;
'Cpanel::JSON::XS'
}
}B= do {
require JSON::PP;
'JSON::PP'
}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 1 | $class->new->canonical(1)->allow_blessed(1)->convert_blessed(1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 4 | return $enc; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 7 | ▶ | sub _iso_ts () { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_iso_ts: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 7 | my $now = Time::HiRes::time(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 7 | my $ms = int(($now - int $now) * 1000); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 7 | return POSIX::strftime('%Y-%m-%dT%H:%M:%S', gmtime int $now) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | . sprintf('.%03dZ', $ms); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 7 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||