1. Add show/dxstats command
[spider.git] / perl / Spot.pm
1 #
2 # the dx spot handler
3 #
4 # Copyright (c) - 1998 Dirk Koopman G1TLH
5 #
6 # $Id$
7 #
8
9 package Spot;
10
11 use IO::File;
12 use DXVars;
13 use DXDebug;
14 use DXUtil;
15 use DXLog;
16 use Julian;
17 use Prefix;
18 use DXDupe;
19 use Data::Dumper;
20
21 use strict;
22
23 use vars qw($VERSION $BRANCH);
24 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
25 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ ) || 0;
26 $main::build += $VERSION;
27 $main::branch += $BRANCH;
28
29 use vars qw($fp $statp $maxspots $defaultspots $maxdays $dirprefix $duplth $dupage $filterdef);
30
31 $fp = undef;
32 $statp = undef;
33 $maxspots = 50;                                 # maximum spots to return
34 $defaultspots = 10;                             # normal number of spots to return
35 $maxdays = 100;                         # normal maximum no of days to go back
36 $dirprefix = "spots";
37 $duplth = 20;                                   # the length of text to use in the deduping
38 $dupage = 3*3600;               # the length of time to hold spot dups
39 $filterdef = bless ([
40                           # tag, sort, field, priv, special parser 
41                           ['freq', 'r', 0, 0, \&decodefreq],
42                           ['on', 'r', 0, 0, \&decodefreq],
43                           ['call', 'c', 1],
44                           ['info', 't', 3],
45                           ['by', 'c', 4],
46                           ['call_dxcc', 'n', 5],
47                           ['by_dxcc', 'n', 6],
48                           ['origin', 'c', 7, 9],
49                           ['call_itu', 'n', 8],
50                           ['call_zone', 'n', 9],
51                           ['by_itu', 'n', 10],
52                           ['by_zone', 'n', 11],
53                           ['channel', 'n', 12, 9],
54                          ], 'Filter::Cmd');
55
56
57 # create a Spot Object
58 sub new
59 {
60         my $class = shift;
61         my $self = [ @_ ];
62         return bless $self, $class;
63 }
64
65 sub decodefreq
66 {
67         my $dxchan = shift;
68         my $l = shift;
69         my @f = split /,/, $l;
70         my @out;
71         my $f;
72         
73         foreach $f (@f) {
74                 my ($a, $b); 
75                 if (m{^\d+/\d+$}) {
76                         push @out, $f;
77                 } elsif (($a, $b) = $f =~ m{^(\w+)(?:/(\w+))?$}) {
78                         $b = lc $b if $b;
79                         my @fr = Bands::get_freq(lc $a, $b);
80                         if (@fr) {
81                                 while (@fr) {
82                                         $a = shift @fr;
83                                         $b = shift @fr;
84                                         push @out, "$a/$b";  # add them as ranges
85                                 }
86                         } else {
87                                 return ('dfreq', $dxchan->msg('dfreq1', $f));
88                         }
89                 } else {
90                         return ('dfreq', $dxchan->msg('e20', $f));
91                 }
92         }
93         return (0, join(',', @out));                     
94 }
95
96 sub init
97 {
98         mkdir "$dirprefix", 0777 if !-e "$dirprefix";
99         $fp = DXLog::new($dirprefix, "dat", 'd');
100         $statp = DXLog::new($dirprefix, "cys", 'd');
101         system("rm -f $main::data/$dirprefix/2001/*.bys");
102 }
103
104 sub prefix
105 {
106         return $fp->{prefix};
107 }
108
109 # fix up the full spot data from the basic spot data
110 sub prepare
111 {
112         # $freq, $call, $t, $comment, $spotter = @_
113         my @out = @_[0..4];      # just up to the spotter
114
115         # normalise frequency
116         $_[0] = sprintf "%.1f", $_[0];
117   
118         # remove ssids if present on spotter
119         $out[4] =~ s/-\d+$//o;
120
121         # remove leading and trailing spaces
122         $_[3] = unpad($_[3]);
123         
124         # add the 'dxcc' country on the end for both spotted and spotter, then the cluster call
125         my @dxcc = Prefix::extract($out[1]);
126         my $spotted_dxcc = (@dxcc > 0 ) ? $dxcc[1]->dxcc() : 0;
127         my $spotted_itu = (@dxcc > 0 ) ? $dxcc[1]->itu() : 0;
128         my $spotted_cq = (@dxcc > 0 ) ? $dxcc[1]->cq() : 0;
129         push @out, $spotted_dxcc;
130         @dxcc = Prefix::extract($out[4]);
131         my $spotter_dxcc = (@dxcc > 0 ) ? $dxcc[1]->dxcc() : 0;
132         my $spotter_itu = (@dxcc > 0 ) ? $dxcc[1]->itu() : 0;
133         my $spotter_cq = (@dxcc > 0 ) ? $dxcc[1]->cq() : 0;
134         push @out, $spotter_dxcc;
135         push @out, $_[5];
136         return (@out, $spotted_itu, $spotted_cq, $spotter_itu, $spotter_cq);
137 }
138
139 sub add
140 {
141         my $buf = join("\^", @_[0..7]);
142         $fp->writeunix($_[2], $buf);
143 }
144
145 # search the spot database for records based on the field no and an expression
146 # this returns a set of references to the spots
147 #
148 # the expression is a legal perl 'if' statement with the possible fields indicated
149 # by $f<n> where :-
150 #
151 #   $f0 = frequency
152 #   $f1 = call
153 #   $f2 = date in unix format
154 #   $f3 = comment
155 #   $f4 = spotter
156 #   $f5 = spotted dxcc country
157 #   $f6 = spotter dxcc country
158 #   $f7 = origin
159 #
160 #
161 # In addition you can specify a range of days, this means that it will start searching
162 # from <n> days less than today to <m> days less than today
163 #
164 # Also you can select a range of entries so normally you would get the 0th (latest) entry
165 # back to the 5th latest, you can specify a range from the <x>th to the <y>the oldest.
166 #
167 # This routine is designed to be called as Spot::search(..)
168 #
169
170 sub search
171 {
172         my ($expr, $dayfrom, $dayto, $from, $to, $hint) = @_;
173         my $eval;
174         my @out;
175         my $ref;
176         my $i;
177         my $count;
178         my $today = Julian::Day->new(time());
179         my $fromdate;
180         my $todate;
181
182         $dayfrom = 0 if !$dayfrom;
183         $dayto = $maxdays unless $dayto;
184         $dayto = $dayfrom + $maxdays if $dayto < $dayfrom;
185         $fromdate = $today->sub($dayfrom);
186         $todate = $fromdate->sub($dayto);
187         $from = 0 unless $from;
188         $to = $defaultspots unless $to;
189         $hint = $hint ? "next unless $hint" : "";
190         $expr = "1" unless $expr;
191         
192         $to = $from + $maxspots if $to - $from > $maxspots || $to - $from <= 0;
193
194         $expr =~ s/\$f(\d)/\$ref->[$1]/g; # swap the letter n for the correct field name
195         #  $expr =~ s/\$f(\d)/\$spots[$1]/g;               # swap the letter n for the correct field name
196   
197         dbg("hint='$hint', expr='$expr', spotno=$from-$to, day=$dayfrom-$dayto\n") if isdbg('search');
198   
199         # build up eval to execute
200         $eval = qq(
201                            while (<\$fh>) {
202                                    $hint;
203                                    chomp;
204                                    push \@spots, [ split '\\^' ];
205                            }
206                            my \$c;
207                            my \$ref;
208                            for (\$c = \$#spots; \$c >= 0; \$c--) {
209                                         \$ref = \$spots[\$c];
210                                         if ($expr) {
211                                                 \$count++;
212                                                 next if \$count < \$from; # wait until from 
213                                                 push(\@out, \$ref);
214                                                 last if \$count >= \$to; # stop after to
215                                         }
216                                 }
217                           );
218
219         $fp->close;                                     # close any open files
220
221         for ($i = $count = 0; $i < $maxdays; ++$i) {    # look thru $maxdays worth of files only
222                 my $now = $fromdate->sub($i); # but you can pick which $maxdays worth
223                 last if $now->cmp($todate) <= 0;         
224         
225                 my @spots = ();
226                 my $fh = $fp->open($now); # get the next file
227                 if ($fh) {
228                         my $in;
229                         eval $eval;                     # do the search on this file
230                         last if $count >= $to; # stop after to
231                         return ("Spot search error", $@) if $@;
232                 }
233         }
234
235         return @out;
236 }
237
238 # change a freq range->regular expression
239 sub ftor
240 {
241         my ($a, $b) = @_;
242         return undef unless $a < $b;
243         $b--;
244         my $d = $b - $a;
245         my @a = split //, $a;
246         my @b = split //, $b;
247         my $out;
248         while (@b > @a) {
249                 $out .= shift @b;
250         }
251         while (@b) {
252                 my $aa = shift @a;
253                 my $bb = shift @b;
254                 if (@b < (length $d)) {
255                         $out .= '\\d';
256                 } elsif ($aa eq $bb) {
257                         $out .= $aa;
258                 } elsif ($aa < $bb) {
259                         $out .= "[$aa-$bb]";
260                 } else {
261                         $out .= "[0-$bb$aa-9]";
262                 }
263         }
264         return $out;
265 }
266
267 # format a spot for user output in 'broadcast' mode
268 sub formatb
269 {
270         my $wantgrid = shift;
271         my $t = ztime($_[2]);
272         my $ref = DXUser->get_current($_[4]);
273         my $loc = $ref->qra if $ref && $ref->qra && $wantgrid;
274         $loc = ' ' . substr($ref->qra, 0, 4) if $loc;
275         $loc = "" unless $loc;
276         return sprintf "DX de %-7.7s%11.1f  %-12.12s %-30s %s$loc", "$_[4]:", $_[0], $_[1], $_[3], $t ;
277 }
278
279 # format a spot for user output in list mode
280 sub formatl
281 {
282         my $t = ztime($_[2]);
283         my $d = cldate($_[2]);
284         return sprintf "%8.1f  %-11s %s %s  %-28.28s%7s>", $_[0], $_[1], $d, $t, $_[3], "<$_[4]" ;
285 }
286
287 #
288 # return all the spots from a day's file as an array of references
289 # the parameter passed is a julian day
290 sub readfile($)
291 {
292         my @spots;
293         
294         my $fh = $fp->open(shift); 
295         if ($fh) {
296                 my $in;
297                 while (<$fh>) {
298                         chomp;
299                         push @spots, [ split '\^' ];
300                 }
301         }
302         return @spots;
303 }
304
305 # enter the spot for dup checking and return true if it is already a dup
306 sub dup
307 {
308         my ($freq, $call, $d, $text) = @_; 
309
310         # dump if too old
311         return 2 if $d < $main::systime - $dupage;
312  
313         $freq = sprintf "%.1f", $freq;       # normalise frequency
314         chomp $text;
315         $text =~ s/\%([0-9A-F][0-9A-F])/chr(hex($1))/eg;
316         $text = substr($text, 0, $duplth) if length $text > $duplth; 
317         unpad($text);
318         $text = pack("C*", map {$_ & 127} unpack("C*", $text));
319         $text =~ s/[^a-zA-Z0-9]//g;
320         for (0,60,120,180,240,300) {
321                 my $dt = $d - $_;
322                 my $dupkey = "X$freq|$call|$dt|\L$text";
323                 return 1 if DXDupe::find($dupkey);
324         }
325         my $dupkey = "X$freq|$call|$d|\L$text";
326         DXDupe::add($dupkey, $main::systime+$dupage);
327         return 0;
328 }
329
330 sub listdups
331 {
332         return DXDupe::listdups('X', $dupage, @_);
333 }
334
335 sub genstats($)
336 {
337         my $date = shift;
338         my $in = $fp->open($date);
339         my $out = $statp->open($date, 'w');
340         my @freq = (
341                                 [0, Bands::get_freq('160m')],
342                                 [1, Bands::get_freq('80m')],
343                                 [2, Bands::get_freq('40m')],
344                                 [3, Bands::get_freq('30m')],
345                                 [4, Bands::get_freq('20m')],
346                                 [5, Bands::get_freq('17m')],
347                                 [6, Bands::get_freq('15m')],
348                                 [7, Bands::get_freq('12m')],
349                                 [8, Bands::get_freq('10m')],
350                                 [9, Bands::get_freq('6m')],
351                                 [10, Bands::get_freq('4m')],
352                                 [11, Bands::get_freq('2m')],
353                                 [12, Bands::get_freq('220')],
354                                 [13, Bands::get_freq('70cm')],
355                                 [14, Bands::get_freq('23cm')],
356                                 [15, Bands::get_freq('13cm')],
357                                 [16, Bands::get_freq('9cm')],
358                                 [17, Bands::get_freq('6cm')],
359                                 [18, Bands::get_freq('3cm')],
360                                 [19, Bands::get_freq('12mm')],
361                                 [20, Bands::get_freq('6cm')],
362                            );
363         my %list;
364         my @tot;
365         
366         if ($in && $out) {
367                 while (<$in>) {
368                         chomp;
369                         my ($freq, $by, $dxcc) = (split /\^/)[0,4,6];
370                         my $ref = $list{$by} || [0, $dxcc];
371                         for (@freq) {
372                                 if ($freq >= $_->[1] && $freq <= $_->[2]) {
373                                         $$ref[$_->[0]+2]++;
374                                         $tot[$_->[0]+2]++;
375                                         $$ref[0]++;
376                                         $tot[0]++;
377                                         $list{$by} = $ref;
378                                         last;
379                                 }
380                         }
381                 }
382
383                 my $i;
384                 for ($i = 0; $i < @freq+2; $i++) {
385                         $tot[$i] ||= 0;
386                 }
387                 $out->write(join('^', 'TOTALS', @tot) . "\n");
388
389                 for (sort {$list{$b}->[0] <=> $list{$a}->[0]} keys %list) {
390                         my $ref = $list{$_};
391                         my $call = $_;
392                         for ($i = 0; $i < @freq+2; ++$i) {
393                                 $ref->[$i] ||= 0;
394                         }
395                         $out->write(join('^', $call, @$ref) . "\n");
396                 }
397                 $out->close;
398         }
399 }
400
401 # return true if the stat file is newer than than the spot file
402 sub checkstats($)
403 {
404         my $date = shift;
405         my $in = $fp->mtime($date);
406         my $out = $statp->mtime($date);
407         return defined $out && defined $in && $out >= $in;
408 }
409
410 # daily processing
411 sub daily
412 {
413         my $date = Julian::Day->new($main::systime)->sub(1);
414         genstats($date) unless checkstats($date);
415 }
416 1;
417
418
419
420