fix default spot qrq dupe granularity
[spider.git] / perl / Spot.pm
1 #
2 # the dx spot handler
3 #
4 # Copyright (c) - 1998 Dirk Koopman G1TLH
5 #
6 #
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 use QSL;
21 use DXSql;
22 use Time::HiRes qw(gettimeofday tv_interval);
23 use Math::Round qw(nearest nearest_floor);
24
25 use strict;
26
27 use vars qw($fp $statp $maxspots $defaultspots $maxdays $dirprefix $duplth $dupage $filterdef
28                         $totalspots $hfspots $vhfspots $maxcalllth $can_encode $use_db_for_search);
29
30 $fp = undef;
31 $statp = undef;
32 $maxspots = 100;                                        # maximum spots to return
33 $defaultspots = 10;                             # normal number of spots to return
34 $maxdays = 100;                         # normal maximum no of days to go back
35 $dirprefix = "spots";
36 $duplth = 20;                                   # the length of text to use in the deduping
37 $dupage = 1*3600;               # the length of time to hold spot dups
38 $maxcalllth = 12;                               # the max length of call to take into account for dupes
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                                          ['spotter', 'c', 4],
46                                          ['by', 'c', 4],
47                                          ['dxcc', 'nc', 5],
48                                          ['call_dxcc', 'nc', 5],
49                                          ['by_dxcc', 'nc', 6],
50                                          ['origin', 'c', 7, 9],
51                                          ['call_itu', 'ni', 8],
52                                          ['itu', 'ni', 8],
53                                          ['call_zone', 'nz', 9],
54                                          ['cq', 'nz', 9],
55                                          ['zone', 'nz', 9],
56                                          ['by_itu', 'ni', 10],
57                                          ['byitu', 'ni', 10],
58                                          ['by_zone', 'nz', 11],
59                                          ['byzone', 'nz', 11],
60                                          ['bycq', 'nz', 11],
61                                          ['call_state', 'ns', 12],
62                                          ['state', 'ns', 12],
63                                          ['by_state', 'ns', 13],
64                                          ['bystate', 'ns', 13],
65                                          ['ip', 'c', 14],
66 #                                        ['channel', 'c', 15],
67 #                                        ['rbn', 'a', 4, 0, \&filterrbnspot],
68                                         ], 'Filter::Cmd');
69 $totalspots = $hfspots = $vhfspots = 0;
70 $use_db_for_search = 0;
71
72 our %spotcache;                                 # the cache of data within the last $spotcachedays 0 or 2+ days
73 our $spotcachedays = 2;                 # default 2 days worth
74 our $minselfspotqrg = 1240000;  # minimum freq above which self spotting is allowed
75
76 our $readback = $main::is_win ? 0 : 1; # don't read spot files backwards if it's windows
77 our $qrggranularity = 1000;     # normalise the qrg to this number of hz (default: 100khz), so tough luck if you have a fumble fingers moment
78 our $timegranularity = 600;             # ditto to the nearest 100 seconds 
79 our $oldstyle = 0;                              # revert to traditional dupe key format
80
81
82 if ($readback) {
83         $readback = `which tac`;
84         chomp $readback;
85 }
86
87 # create a Spot Object
88 sub new
89 {
90         my $class = shift;
91         my $self = [ @_ ];
92         return bless $self, $class;
93 }
94
95 sub decodefreq
96 {
97         my $dxchan = shift;
98         my $l = shift;
99         my @f = split /,/, $l;
100         my @out;
101         my $f;
102         
103         foreach $f (@f) {
104                 my ($a, $b); 
105                 if ($f =~ m{^\d+/\d+$}) {
106                         push @out, $f;
107                 } elsif (($a, $b) = $f =~ m{^(\w+)(?:/(\w+))?$}) {
108                         $b = lc $b if $b;
109                         my @fr = Bands::get_freq(lc $a, $b);
110                         if (@fr) {
111                                 while (@fr) {
112                                         $a = shift @fr;
113                                         $b = shift @fr;
114                                         push @out, "$a/$b";  # add them as ranges
115                                 }
116                         } else {
117                                 return ('dfreq', $dxchan->msg('dfreq1', $f));
118                         }
119                 } else {
120                         return ('dfreq', $dxchan->msg('e20', $f));
121                 }
122         }
123         return (0, join(',', @out));                     
124 }
125
126 # filter setup for rbn spot so return the regex to detect it
127 sub filterrbnspot
128 {
129         my $dxchan = shift;
130         return ('-#$');
131 }
132
133 sub init
134 {
135         mkdir "$dirprefix", 0777 if !-e "$dirprefix";
136         $fp = DXLog::new($dirprefix, "dat", 'd');
137         $statp = DXLog::new($dirprefix, "dys", 'd');
138         my $today = Julian::Day->new(time);
139
140         # load up any old spots 
141         if ($main::dbh) {
142                 unless (grep $_ eq 'spot', $main::dbh->show_tables) {
143                         dbg('initialising spot tables');
144                         my $t = time;
145                         my $total;
146                         $main::dbh->spot_create_table;
147                         
148                         my $now = Julian::Day->alloc(1995, 0);
149                         my $sth = $main::dbh->spot_insert_prepare;
150                         while ($now->cmp($today) <= 0) {
151                                 my $fh = $fp->open($now);
152                                 if ($fh) {
153 #                                       $main::dbh->{RaiseError} = 0;
154                                         $main::dbh->begin_work;
155                                         my $count = 0;
156                                         while (<$fh>) {
157                                                 chomp;
158                                                 my @s = split /\^/;
159                                                 if (@s < 14) {
160                                                         my @a = (Prefix::cty_data($s[1]))[1..3];
161                                                         my @b = (Prefix::cty_data($s[4]))[1..3];
162                                                         push @s, $b[1] if @s < 7;
163                                                         push @s, '' if @s < 8;
164                                                         push @s, @a[0,1], @b[0,1] if @s < 12;
165                                                         push @s,  $a[2], $b[2] if @s < 14;
166                                                 } 
167                                                 $main::dbh->spot_insert(\@s, $sth);
168                                                 $count++;
169                                         }
170                                         $main::dbh->commit;
171                                         dbg("inserted $count spots from $now->[0] $now->[1]");
172                                         $fh->close;
173                                         $total += $count;
174                                 }
175                                 $now = $now->add(1);
176                         }
177                         $main::dbh->begin_work;
178                         $main::dbh->spot_add_indexes;
179                         $main::dbh->commit;
180 #                       $main::dbh->{RaiseError} = 1;
181                         $t = time - $t;
182                         my $min = int($t / 60);
183                         my $sec = $t % 60;
184                         dbg("$total spots converted in $min:$sec");
185                 }
186                 unless ($main::dbh->has_ipaddr) {
187                         $main::dbh->add_ipaddr;
188                         dbg("added ipaddr field to spot table");
189                 }
190         }
191
192         # initialise the cache if required
193         if ($spotcachedays > 0) {
194                 my $t0 = [gettimeofday];
195                 $spotcachedays = 2 if $spotcachedays < 2;
196                 dbg "Spot::init - reading in $spotcachedays days of spots into cache"; 
197                 for (my $i = 0; $i < $spotcachedays; ++$i) {
198                         my $now = $today->sub($i);
199                         my $fh = $fp->open($now);
200                         if ($fh) {
201                                 my @in;
202                                 my $rec;
203                                 for ($rec = 0; <$fh>; ++$rec) {
204                                         chomp;
205                                         my @s = split /\^/;
206                                         if (@s < 14) {
207                                                 my @a = (Prefix::cty_data($s[1]))[1..3];
208                                                 my @b = (Prefix::cty_data($s[4]))[1..3];
209                                                 push @s, $b[1] if @s < 7;
210                                                 push @s, '' if @s < 8;
211                                                 push @s, @a[0,1], @b[0,1] if @s < 12;
212                                                 push @s,  $a[2], $b[2] if @s < 14;
213                                         }
214                                         unshift @in, \@s; 
215                                 }
216                                 $fh->close;
217                                 dbg("Spot::init read $rec spots from " . _cachek($now));
218                                 $spotcache{_cachek($now)} = \@in;
219                         }
220                         $now->add(1);
221                 }
222                 dbg("Spot::init $spotcachedays files of spots read into cache in " . _diffms($t0) . "mS")
223         }
224 }
225
226 sub prefix
227 {
228         return $fp->{prefix};
229 }
230
231 # fix up the full spot data from the basic spot data
232 # input is
233 # freq, call, time, comment, spotter, origin[, ip_address]
234 sub prepare
235 {
236         # $freq, $call, $t, $comment, $spotter, node, ip address = @_
237         my @out = @_[0..4];      # just up to the spotter
238
239         # normalise frequency
240         $out[0] = sprintf "%.1f", $out[0];
241   
242         # remove ssids and /xxx if present on spotter
243         $out[4] =~ s/-\d+$//o;
244
245         # remove leading and trailing spaces from comment field
246         $out[3] = unpad($out[3]);
247         
248         # add the 'dxcc' country on the end for both spotted and spotter, then the cluster call
249         my @spd = Prefix::cty_data($out[1]);
250         push @out, $spd[0];
251         my @spt = Prefix::cty_data($out[4]);
252         push @out, $spt[0];
253         push @out, $_[5];
254         push @out, @spd[1,2], @spt[1,2], $spd[3], $spt[3];
255         push @out, $_[6] if $_[6] && is_ipaddr($_[6]);
256
257         # thus we now have:
258         # freq, call, time, comment, spotter, call country code, spotter country code, origin, call itu, call cqzone, spotter itu, spotter cqzone, call state, spotter state, spotter ip address
259         return @out;
260 }
261
262 sub add
263 {
264         my $buf = join('^', @_);
265         $fp->writeunix($_[2], $buf);
266         if ($spotcachedays > 0) {
267                 my $now = Julian::Day->new($_[2]);
268                 my $day = _cachek($now);
269                 my $r = (exists $spotcache{$day}) ? $spotcache{$day} : ($spotcache{$day} = []);
270                 unshift @$r, \@_;
271         }
272         if ($main::dbh) {
273                 $main::dbh->begin_work;
274                 $main::dbh->spot_insert(\@_);
275                 $main::dbh->commit;
276         }
277         $totalspots++;
278         if ($_[0] <= 30000) {
279                 $hfspots++;
280         } else {
281                 $vhfspots++;
282         }
283         if ($_[3] =~ /(?:QSL|VIA)/i) {
284                 my $q = QSL::get($_[1]) || new QSL $_[1];
285                 $q->update($_[3], $_[2], $_[4]);
286         }
287 }
288
289 # search the spot database for records based on the field no and an expression
290 # this returns a set of references to the spots
291 #
292 # the expression is a legal perl 'if' statement with the possible fields indicated
293 # by $f<n> where :-
294 #
295 #   $f0 = frequency
296 #   $f1 = call
297 #   $f2 = date in unix format
298 #   $f3 = comment
299 #   $f4 = spotter
300 #   $f5 = spotted dxcc country
301 #   $f6 = spotter dxcc country
302 #   $f7 = origin
303 #   $f8 = spotted itu
304 #   $f9 = spotted cq zone
305 #   $f10 = spotter itu
306 #   $f11 = spotter cq zone
307 #   $f12 = spotted us state
308 #   $f13 = spotter us state
309 #   $f14 = ip address
310 #
311 # In addition you can specify a range of days, this means that it will start searching
312 # from <n> days less than today to <m> days less than today
313 #
314 # Also you can select a range of entries so normally you would get the 0th (latest) entry
315 # back to the 5th latest, you can specify a range from the <x>th to the <y>the oldest.
316 #
317 # This routine is designed to be called as Spot::search(..)
318 #
319
320 sub search
321 {
322         my ($expr, $dayfrom, $dayto, $from, $to, $hint, $dofilter, $dxchan) = @_;
323         my @out;
324         my $ref;
325         my $i;
326         my $count;
327         my $today = Julian::Day->new(time());
328         my $fromdate;
329         my $todate;
330
331         $dayfrom = 0 if !$dayfrom;
332         $dayto = $maxdays unless $dayto;
333         $dayto = $dayfrom + $maxdays if $dayto < $dayfrom;
334         $fromdate = $today->sub($dayfrom);
335         $todate = $fromdate->sub($dayto);
336         $from = 0 unless $from;
337         $to = $defaultspots unless $to;
338         $hint = $hint ? "next unless $hint" : "";
339         $expr = "1" unless $expr;
340         
341         $to = $from + $maxspots if $to - $from > $maxspots || $to - $from <= 0;
342
343         if ($main::dbh && $use_db_for_search) {
344                 return $main::dbh->spot_search($expr, $dayfrom, $dayto, $from, $to, $hint, $dofilter, $dxchan);
345         }
346
347         #       $expr =~ s/\$f(\d\d?)/\$ref->[$1]/g; # swap the letter n for the correct field name
348         #  $expr =~ s/\$f(\d)/\$spots[$1]/g;               # swap the letter n for the correct field name
349   
350
351         dbg("Spot::search hint='$hint', expr='$expr', spotno=$from-$to, day=$dayfrom-$dayto\n") if isdbg('search');
352   
353         # build up eval to execute
354
355         dbg("Spot::search Spot eval: $expr") if isdbg('searcheval');
356         $expr =~ s/\$r/\$_[0]/g;
357         my $eval = qq{ sub { return $expr; } };
358         dbg("Spot::search Spot eval: $eval") if isdbg('searcheval');
359         my $ecode = eval $eval;
360         return ("Spot search error", $@) if $@;
361         
362         my $fh;
363         my $now = $fromdate;
364         my $today = Julian::Day->new($main::systime);
365         
366         for ($i = $count = 0; $count < $to && $i < $maxdays; ++$i) { # look thru $maxdays worth of files only
367                 last if $now->cmp($todate) <= 0;
368
369
370                 my $this = $now->sub($i);
371                 my $fn = $fp->fn($this);
372                 my $cachekey = _cachek($this); 
373                 my $rec = 0;
374
375                 if ($spotcachedays > 0 && $spotcache{$cachekey}) {
376                         foreach my $r (@{$spotcache{$cachekey}}) {
377                                 ++$rec;
378                                 if ($dofilter && $dxchan && $dxchan->{spotsfilter}) {
379                                         my ($gotone, undef) = $dxchan->{spotsfilter}->it(@$r);
380                                         next unless $gotone;
381                                 }
382                                 if (&$ecode($r)) {
383                                         ++$count;
384                                         next if $count < $from;
385                                         push @out, $r;
386                                         last if $count >= $to;
387                                 }
388                         }
389                         dbg("Spot::search cache recs read: $rec") if isdbg('search');
390                 } else {
391                         if ($readback) {
392                                 dbg("Spot::search search using tac fn: $fn $i") if isdbg('search');
393                                 $fh = IO::File->new("$readback $fn |");
394                         }
395                         else {
396                                 dbg("Spot::search search fn: $fp->{fn} $i") if isdbg('search');
397                                 $fh = $fp->open($now->sub($i)); # get the next file
398                         }
399                         if ($fh) {
400                                 my $in;
401                                 while (<$fh>) {
402                                         chomp;
403                                         my @r = split /\^/;
404                                         ++$rec;
405                                         if ($dofilter && $dxchan && $dxchan->{spotsfilter}) {
406                                                 my ($gotone, undef) = $dxchan->{spotsfilter}->it(@r);
407                                                 next unless $gotone;
408                                         }
409                                         if (&$ecode(\@r)) {
410                                                 ++$count;
411                                                 next if $count < $from;
412                                                 if ($readback) {
413                                                         push @out, \@r;
414                                                         last if $count >= $to;
415                                                 } else {
416                                                         push @out, \@r;
417                                                         shift @out if $count >= $to;
418                                                 }
419                                         }
420                                 }
421                                 dbg("Spot::search file recs read: $rec") if isdbg('search');
422                                 last if $count >= $to; # stop after to
423                         }
424                 }
425         }
426         return ("Spot search error", $@) if $@;
427
428         @out = sort {$b->[2] <=> $a->[2]} @out if @out;
429         return @out;
430 }
431
432 # change a freq range->regular expression
433 sub ftor
434 {
435         my ($a, $b) = @_;
436         return undef unless $a < $b;
437         $b--;
438         my $d = $b - $a;
439         my @a = split //, $a;
440         my @b = split //, $b;
441         my $out;
442         while (@b > @a) {
443                 $out .= shift @b;
444         }
445         while (@b) {
446                 my $aa = shift @a;
447                 my $bb = shift @b;
448                 if (@b < (length $d)) {
449                         $out .= '\\d';
450                 } elsif ($aa eq $bb) {
451                         $out .= $aa;
452                 } elsif ($aa < $bb) {
453                         $out .= "[$aa-$bb]";
454                 } else {
455                         $out .= "[0-$bb$aa-9]";
456                 }
457         }
458         return $out;
459 }
460
461 # format a spot for user output in list mode
462 sub formatl
463 {
464         my $t = ztime($_[3]);
465         my $d = cldate($_[3]);
466         my $spotter = "<$_[5]>";
467         my $comment = $_[4] || '';
468         $comment =~ s/\t+/ /g;
469         my $cl = length $comment;
470         my $s = sprintf "%9.1f %-11s %s %s", $_[1], $_[2], $d, $t;
471         my $width = ($_[0] ? $_[0] : 80) - length($spotter) - length($s) - 4;
472         
473         $comment = substr $comment, 0, $width if $cl > $width;
474         $comment .= ' ' x ($width-$cl) if $cl < $width;
475
476 #       return sprintf "%8.1f  %-11s %s %s  %-28.28s%7s>", $_[0], $_[1], $d, $t, ($_[3]||''), "<$_[4]" ;
477         return "$s $comment$spotter";
478 }
479
480 # enter the spot for dup checking and return true if it is already a dup
481 sub dup
482 {
483         my ($freq, $call, $d, $text, $by, $node, $just_find) = @_;
484
485         dbg("Spot::dup: freq=$freq call=$call d=$d text='$text' by=$by node=$node" . ($just_find ? " jf=$just_find" : "")) if isdbg('spotdup');
486
487         # dump if too old
488         return 2 if $d < $main::systime - $dupage;
489
490         # turn the time into minutes (should be already but...)
491         $d = int ($d / 60);
492         $d *= 60;
493
494         my $nd = nearest($timegranularity, $d);
495
496         # remove SSID or area
497         $by =~ s|[-/]\d+$||;
498         
499 #       $freq = sprintf "%.1f", $freq;       # normalise frequency
500         $freq = int $freq;       # normalise frequency
501
502         my $qrg = nearest($qrggranularity, $freq); # to the nearest however many hz
503         
504         $call = substr($call, 0, $maxcalllth) if length $call > $maxcalllth;
505
506         
507         chomp $text;
508         $text =~ s/\%([0-9A-F][0-9A-F])/chr(hex($1))/eg;
509         $text = uc unpad($text);
510         my $otext = $text;
511 #       $text = Encode::encode("iso-8859-1", $text) if $main::can_encode && Encode::is_utf8($text, 1);
512         $text =~ s/^\+\w+\s*//;                 # remove leading LoTW callsign
513         $text =~ s/\s{2,}[\dA-Z]?[A-Z]\d?$// if length $text > 24;
514         $text =~ s/[\W\x00-\x2F\x7B-\xFF]//g; # tautology, just to make quite sure!
515         $text = substr($text, 0, $duplth) if length $text > $duplth; 
516         my $ldupkey = $oldstyle ? "X|$call|$by|$node|$freq|$d|$text" : "X|$call|$by|$node|$qrg|$nd|$text";
517
518         dbg("Spot::dup ldupkey $ldupkey") if isdbg('spotdup');
519         
520         my $t = DXDupe::find($ldupkey);
521         return 1 if $t && $t - $main::systime > 0;
522         
523         DXDupe::add($ldupkey, $main::systime+$dupage) unless $just_find;
524         $otext = substr($otext, 0, $duplth) if length $otext > $duplth; 
525         $otext =~ s/\s+$//;
526         if (length $otext && $otext ne $text) {
527                 $ldupkey = $oldstyle ? "X|$freq|$call|$by|$otext" : "X|$qrg|$call|$by|$otext";
528                 $t = DXDupe::find($ldupkey);
529                 return 1 if $t && $t - $main::systime > 0;
530                 DXDupe::add($ldupkey, $main::systime+$dupage) unless $just_find;
531         }
532         return undef;
533 }
534
535 sub dup_find
536 {
537         return dup(@_, 1);
538 }
539
540 sub listdups
541 {
542         return DXDupe::listdups('X', $dupage, @_);
543 }
544
545 sub genstats
546 {
547         my $date = shift;
548         my $in = $fp->open($date) or dbg("Spot::genstats: Cannot open " . $fp->fn($date) . " $!");
549         my $out = $statp->open($date, 'w') or dbg("Spot::genstats: Cannot open " . $statp->fn($date) . " $!");
550         my @freq;
551         my %list;
552         my @tot;
553         
554         if ($in && $out) {
555                 my $i = 0;
556                 @freq = map {[$i++, Bands::get_freq($_)]} qw(136khz 160m 80m 60m 40m 30m 20m 17m 15m 12m 10m 6m 4m 2m 220 70cm 23cm 13cm 9cm 6cm 3cm 12mm 6mm);
557                 while (<$in>) {
558                         chomp;
559                         my ($freq, $by, $dxcc) = (split /\^/)[0,4,6];
560                         my $ref = $list{$by} || [0, $dxcc];
561                         for (@freq) {
562                                 next unless defined $_;
563                                 if ($freq >= $_->[1] && $freq <= $_->[2]) {
564                                         $$ref[$_->[0]+2]++;
565                                         $tot[$_->[0]+2]++;
566                                         $$ref[0]++;
567                                         $tot[0]++;
568                                         $list{$by} = $ref;
569                                         last;
570                                 }
571                         }
572                 }
573
574                 for ($i = 0; $i < @freq+2; $i++) {
575                         $tot[$i] ||= 0;
576                 }
577                 $statp->write($date, join('^', 'TOTALS', @tot));
578
579                 for (sort {$list{$b}->[0] <=> $list{$a}->[0]} keys %list) {
580                         my $ref = $list{$_};
581                         my $call = $_;
582                         for ($i = 0; $i < @freq+2; ++$i) {
583                                 $ref->[$i] ||= 0;
584                         }
585                         $statp->write($date, join('^', $call, @$ref));
586                 }
587                 $statp->close;
588         }
589 }
590
591 # return true if the stat file is newer than than the spot file
592 sub checkstats
593 {
594         my $date = shift;
595         my $in = $fp->mtime($date);
596         my $out = $statp->mtime($date);
597         return defined $out && defined $in && $out >= $in;
598 }
599
600 # daily processing
601 sub daily
602 {
603         my $date = Julian::Day->new($main::systime)->sub(1);
604         genstats($date) unless checkstats($date);
605         clean_cache();
606 }
607
608 sub _cachek
609 {
610         return "$_[0]->[0]|$_[0]->[1]";
611 }
612
613 sub clean_cache
614 {
615         if ($spotcachedays > 0) {
616                 my $now = Julian::Day->new($main::systime);
617                 for (my $i = $spotcachedays; $i < $spotcachedays + 5; ++$i ) {
618                         my $k = _cachek($now->sub($i));
619                         if (exists $spotcache{$k}) {
620                                 dbg("Spot::spotcache deleting day $k, more than $spotcachedays days old");
621                                 delete $spotcache{$k};
622                         }
623                 }
624         }
625 }
626 1;
627
628
629
630