]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiOverview.pm
lei: test SIGPIPE, stop xsearch workers on client abort
[public-inbox.git] / lib / PublicInbox / LeiOverview.pm
1 # Copyright (C) 2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # per-mitem/smsg iterators for search results
5 # "ovv" => "Overview viewer"
6 package PublicInbox::LeiOverview;
7 use strict;
8 use v5.10.1;
9 use POSIX qw(strftime);
10 use File::Spec;
11 use PublicInbox::MID qw($MID_EXTRACT);
12 use PublicInbox::Address qw(pairs);
13 use PublicInbox::Config;
14 use PublicInbox::Search qw(get_pct);
15
16 # cf. https://en.wikipedia.org/wiki/JSON_streaming
17 my $JSONL = 'ldjson|ndjson|jsonl'; # 3 names for the same thing
18
19 sub _iso8601 ($) { strftime('%Y-%m-%dT%H:%M:%SZ', gmtime($_[0])) }
20
21 sub new {
22         my ($class, $lei) = @_;
23         my $opt = $lei->{opt};
24         my $out = $opt->{output} // '-';
25         $out = '/dev/stdout' if $out eq '-';
26
27         my $fmt = $opt->{'format'};
28         $fmt = lc($fmt) if defined $fmt;
29         if ($out =~ s/\A([a-z]+)://is) { # e.g. Maildir:/home/user/Mail/
30                 my $ofmt = lc $1;
31                 $fmt //= $ofmt;
32                 return $lei->fail(<<"") if $fmt ne $ofmt;
33 --format=$fmt and --output=$ofmt conflict
34
35         }
36         $fmt //= 'json' if $out eq '/dev/stdout';
37         $fmt //= 'maildir'; # TODO
38
39         if (index($out, '://') < 0) { # not a URL, so assume path
40                  $out = File::Spec->canonpath($out);
41         } # else URL
42
43         my $self = bless { fmt => $fmt, out => $out }, $class;
44         my $json;
45         if ($fmt =~ /\A($JSONL|(?:concat)?json)\z/) {
46                 $json = $self->{json} = ref(PublicInbox::Config->json);
47         }
48         my ($isatty, $seekable);
49         if ($out eq '/dev/stdout') {
50                 $isatty = -t $lei->{1};
51                 $lei->start_pager if $isatty;
52                 $opt->{pretty} //= $isatty;
53         } elsif ($json) {
54                 return $lei->fail('JSON formats only output to stdout');
55         }
56         $self;
57 }
58
59 # called once by parent
60 sub ovv_begin {
61         my ($self, $lei) = @_;
62         if ($self->{fmt} eq 'json') {
63                 print { $lei->{1} } '[';
64         } # TODO HTML/Atom/...
65 }
66
67 # called once by parent (via PublicInbox::EOFpipe)
68 sub ovv_end {
69         my ($self, $lei) = @_;
70         if ($self->{fmt} eq 'json') {
71                 # JSON doesn't allow trailing commas, and preventing
72                 # trailing commas is a PITA when parallelizing outputs
73                 print { $lei->{1} } "null]\n";
74         } elsif ($self->{fmt} eq 'concatjson') {
75                 print { $lei->{1} } "\n";
76         }
77 }
78
79 sub ovv_atfork_child {
80         my ($self) = @_;
81         # reopen dedupe here
82 }
83
84 # prepares an smsg for JSON
85 sub _unbless_smsg {
86         my ($smsg, $mitem) = @_;
87
88         delete @$smsg{qw(lines bytes num tid)};
89         $smsg->{rcvd} = _iso8601(delete $smsg->{ts}); # JMAP receivedAt
90         $smsg->{dt} = _iso8601(delete $smsg->{ds}); # JMAP UTCDate
91         $smsg->{relevance} = get_pct($mitem) if $mitem;
92
93         if (my $r = delete $smsg->{references}) {
94                 $smsg->{references} = [
95                                 map { "<$_>" } ($r =~ m/$MID_EXTRACT/go) ];
96         }
97         if (my $m = delete($smsg->{mid})) {
98                 $smsg->{'m'} = "<$m>";
99         }
100         for my $f (qw(from to cc)) {
101                 my $v = delete $smsg->{$f} or next;
102                 $smsg->{substr($f, 0, 1)} = pairs($v);
103         }
104         $smsg->{'s'} = delete $smsg->{subject};
105         # can we be bothered to parse From/To/Cc into arrays?
106         scalar { %$smsg }; # unbless
107 }
108
109 sub ovv_atexit_child {
110         my ($self, $lei) = @_;
111         if (my $bref = delete $lei->{ovv_buf}) {
112                 print { $lei->{1} } $$bref;
113         }
114 }
115
116 # JSON module ->pretty output wastes too much vertical white space,
117 # this (IMHO) provides better use of screen real-estate while not
118 # being excessively compact:
119 sub _json_pretty {
120         my ($json, $k, $v) = @_;
121         if (ref $v eq 'ARRAY') {
122                 if (@$v) {
123                         my $sep = ",\n" . (' ' x (length($k) + 7));
124                         if (ref($v->[0])) { # f/t/c
125                                 $v = '[' . join($sep, map {
126                                         my $pair = $json->encode($_);
127                                         $pair =~ s/(null|"),"/$1, "/g;
128                                         $pair;
129                                 } @$v) . ']';
130                         } else { # references
131                                 $v = '[' . join($sep, map {
132                                         substr($json->encode([$_]), 1, -1);
133                                 } @$v) . ']';
134                         }
135                 } else {
136                         $v = '[]';
137                 }
138         }
139         qq{  "$k": }.$v;
140 }
141
142 sub ovv_each_smsg_cb {
143         my ($self, $lei) = @_;
144         $lei->{ovv_buf} = \(my $buf = '');
145         my $json = $self->{json}->new;
146         if ($json) {
147                 $json->utf8->canonical;
148                 $json->ascii(1) if $lei->{opt}->{ascii};
149         }
150         if ($self->{fmt} =~ /\A(concat)?json\z/ && $lei->{opt}->{pretty}) {
151                 my $EOR = ($1//'') eq 'concat' ? "\n}" : "\n},";
152                 sub { # DIY prettiness :P
153                         my ($smsg, $mitem) = @_;
154                         $smsg = _unbless_smsg($smsg, $mitem);
155                         $buf .= "{\n";
156                         $buf .= join(",\n", map {
157                                 my $v = $smsg->{$_};
158                                 if (ref($v)) {
159                                         _json_pretty($json, $_, $v);
160                                 } else {
161                                         $v = $json->encode([$v]);
162                                         qq{  "$_": }.substr($v, 1, -1);
163                                 }
164                         } sort keys %$smsg);
165                         $buf .= $EOR;
166                         if (length($buf) > 65536) {
167                                 print { $lei->{1} } $buf;
168                                 $buf = '';
169                         }
170                 }
171         } elsif ($json) {
172                 my $ORS = $self->{fmt} eq 'json' ? ",\n" : "\n"; # JSONL
173                 sub {
174                         my ($smsg, $mitem) = @_;
175                         delete @$smsg{qw(tid num)};
176                         $buf .= $json->encode(_unbless_smsg(@_)) . $ORS;
177                         if (length($buf) > 65536) {
178                                 print { $lei->{1} } $buf;
179                                 $buf = '';
180                         }
181                 }
182         } elsif ($self->{fmt} eq 'oid') {
183                 sub {
184                         my ($smsg, $mitem) = @_;
185                 }
186         } # else { ...
187 }
188
189 1;