]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiOverview.pm
8a1f4f822014a9a539ac9ea6a92f2f3789aeea28
[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         my $bref = delete $lei->{ovv_buf} or return;
112         print { $lei->{1} } $$bref;
113 }
114
115 # JSON module ->pretty output wastes too much vertical white space,
116 # this (IMHO) provides better use of screen real-estate while not
117 # being excessively compact:
118 sub _json_pretty {
119         my ($json, $k, $v) = @_;
120         if (ref $v eq 'ARRAY') {
121                 if (@$v) {
122                         my $sep = ",\n" . (' ' x (length($k) + 7));
123                         if (ref($v->[0])) { # f/t/c
124                                 $v = '[' . join($sep, map {
125                                         my $pair = $json->encode($_);
126                                         $pair =~ s/(null|"),"/$1, "/g;
127                                         $pair;
128                                 } @$v) . ']';
129                         } else { # references
130                                 $v = '[' . join($sep, map {
131                                         substr($json->encode([$_]), 1, -1);
132                                 } @$v) . ']';
133                         }
134                 } else {
135                         $v = '[]';
136                 }
137         }
138         qq{  "$k": }.$v;
139 }
140
141 sub ovv_each_smsg_cb {
142         my ($self, $lei) = @_;
143         $lei->{ovv_buf} = \(my $buf = '');
144         my $json = $self->{json}->new;
145         if ($json) {
146                 $json->utf8->canonical;
147                 $json->ascii(1) if $lei->{opt}->{ascii};
148         }
149         if ($self->{fmt} =~ /\A(concat)?json\z/ && $lei->{opt}->{pretty}) {
150                 my $EOR = ($1//'') eq 'concat' ? "\n}" : "\n},";
151                 sub { # DIY prettiness :P
152                         my ($smsg, $mitem) = @_;
153                         $smsg = _unbless_smsg($smsg, $mitem);
154                         $buf .= "{\n";
155                         $buf .= join(",\n", map {
156                                 my $v = $smsg->{$_};
157                                 if (ref($v)) {
158                                         _json_pretty($json, $_, $v);
159                                 } else {
160                                         $v = $json->encode([$v]);
161                                         qq{  "$_": }.substr($v, 1, -1);
162                                 }
163                         } sort keys %$smsg);
164                         $buf .= $EOR;
165                         if (length($buf) > 65536) {
166                                 print { $lei->{1} } $buf;
167                                 $buf = '';
168                         }
169                 }
170         } elsif ($json) {
171                 my $ORS = $self->{fmt} eq 'json' ? ",\n" : "\n"; # JSONL
172                 sub {
173                         my ($smsg, $mitem) = @_;
174                         delete @$smsg{qw(tid num)};
175                         $buf .= $json->encode(_unbless_smsg(@_)) . $ORS;
176                         if (length($buf) > 65536) {
177                                 print { $lei->{1} } $buf;
178                                 $buf = '';
179                         }
180                 }
181         } elsif ($self->{fmt} eq 'oid') {
182                 sub {
183                         my ($smsg, $mitem) = @_;
184                 }
185         } # else { ...
186 }
187
188 1;