fix the last 5 minute dups
[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,0));
26 $main::build += $VERSION;
27 $main::branch += $BRANCH;
28
29 use vars qw($fp $statp $maxspots $defaultspots $maxdays $dirprefix $duplth $dupage $filterdef $totalspots $hfspots $vhfspots);
30
31 $fp = undef;
32 $statp = undef;
33 $maxspots = 100;                                        # 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', 'nc', 5],
47                           ['by_dxcc', 'nc', 6],
48                           ['origin', 'c', 7, 9],
49                           ['call_itu', 'ni', 8],
50                           ['call_zone', 'nz', 9],
51                           ['by_itu', 'ni', 10],
52                           ['by_zone', 'nz', 11],
53                           ['channel', 'c', 12],
54                          ], 'Filter::Cmd');
55 $totalspots = $hfspots = $vhfspots = 0;
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, "dys", 'd');
101         system("rm -f $main::data/$dirprefix/200?/*.bys");
102         system("rm -f $main::data/$dirprefix/200?/*.cys");
103 }
104
105 sub prefix
106 {
107         return $fp->{prefix};
108 }
109
110 # fix up the full spot data from the basic spot data
111 sub prepare
112 {
113         # $freq, $call, $t, $comment, $spotter = @_
114         my @out = @_[0..4];      # just up to the spotter
115
116         # normalise frequency
117         $_[0] = sprintf "%.1f", $_[0];
118   
119         # remove ssids if present on spotter
120         $out[4] =~ s/-\d+$//o;
121
122         # remove leading and trailing spaces
123         $_[3] = unpad($_[3]);
124         
125         # add the 'dxcc' country on the end for both spotted and spotter, then the cluster call
126         my @dxcc = Prefix::extract($out[1]);
127         my $spotted_dxcc = (@dxcc > 0 ) ? $dxcc[1]->dxcc() : 666;
128         my $spotted_itu = (@dxcc > 0 ) ? $dxcc[1]->itu() : 0;
129         my $spotted_cq = (@dxcc > 0 ) ? $dxcc[1]->cq() : 0;
130         push @out, $spotted_dxcc;
131         @dxcc = Prefix::extract($out[4]);
132         my $spotter_dxcc = (@dxcc > 0 ) ? $dxcc[1]->dxcc() : 666;
133         my $spotter_itu = (@dxcc > 0 ) ? $dxcc[1]->itu() : 0;
134         my $spotter_cq = (@dxcc > 0 ) ? $dxcc[1]->cq() : 0;
135         push @out, $spotter_dxcc;
136         push @out, $_[5];
137         return (@out, $spotted_itu, $spotted_cq, $spotter_itu, $spotter_cq);
138 }
139
140 sub add
141 {
142         my $buf = join("\^", @_[0..7]);
143         $fp->writeunix($_[2], $buf);
144         $totalspots++;
145         if ($_[0] <= 30000) {
146                 $hfspots++;
147         } else {
148                 $vhfspots++;
149         }
150 }
151
152 # search the spot database for records based on the field no and an expression
153 # this returns a set of references to the spots
154 #
155 # the expression is a legal perl 'if' statement with the possible fields indicated
156 # by $f<n> where :-
157 #
158 #   $f0 = frequency
159 #   $f1 = call
160 #   $f2 = date in unix format
161 #   $f3 = comment
162 #   $f4 = spotter
163 #   $f5 = spotted dxcc country
164 #   $f6 = spotter dxcc country
165 #   $f7 = origin
166 #
167 #
168 # In addition you can specify a range of days, this means that it will start searching
169 # from <n> days less than today to <m> days less than today
170 #
171 # Also you can select a range of entries so normally you would get the 0th (latest) entry
172 # back to the 5th latest, you can specify a range from the <x>th to the <y>the oldest.
173 #
174 # This routine is designed to be called as Spot::search(..)
175 #
176
177 sub search
178 {
179         my ($expr, $dayfrom, $dayto, $from, $to, $hint) = @_;
180         my $eval;
181         my @out;
182         my $ref;
183         my $i;
184         my $count;
185         my $today = Julian::Day->new(time());
186         my $fromdate;
187         my $todate;
188
189         $dayfrom = 0 if !$dayfrom;
190         $dayto = $maxdays unless $dayto;
191         $dayto = $dayfrom + $maxdays if $dayto < $dayfrom;
192         $fromdate = $today->sub($dayfrom);
193         $todate = $fromdate->sub($dayto);
194         $from = 0 unless $from;
195         $to = $defaultspots unless $to;
196         $hint = $hint ? "next unless $hint" : "";
197         $expr = "1" unless $expr;
198         
199         $to = $from + $maxspots if $to - $from > $maxspots || $to - $from <= 0;
200
201         $expr =~ s/\$f(\d)/\$ref->[$1]/g; # swap the letter n for the correct field name
202         #  $expr =~ s/\$f(\d)/\$spots[$1]/g;               # swap the letter n for the correct field name
203   
204         dbg("hint='$hint', expr='$expr', spotno=$from-$to, day=$dayfrom-$dayto\n") if isdbg('search');
205   
206         # build up eval to execute
207         $eval = qq(
208                            while (<\$fh>) {
209                                    $hint;
210                                    chomp;
211                                    push \@spots, [ split '\\^' ];
212                            }
213                            my \$c;
214                            my \$ref;
215                            for (\$c = \$#spots; \$c >= 0; \$c--) {
216                                         \$ref = \$spots[\$c];
217                                         if ($expr) {
218                                                 \$count++;
219                                                 next if \$count < \$from; # wait until from 
220                                                 push(\@out, \$ref);
221                                                 last if \$count >= \$to; # stop after to
222                                         }
223                                 }
224                           );
225
226         $fp->close;                                     # close any open files
227
228         for ($i = $count = 0; $i < $maxdays; ++$i) {    # look thru $maxdays worth of files only
229                 my $now = $fromdate->sub($i); # but you can pick which $maxdays worth
230                 last if $now->cmp($todate) <= 0;         
231         
232                 my @spots = ();
233                 my $fh = $fp->open($now); # get the next file
234                 if ($fh) {
235                         my $in;
236                         eval $eval;                     # do the search on this file
237                         last if $count >= $to; # stop after to
238                         return ("Spot search error", $@) if $@;
239                 }
240         }
241
242         return @out;
243 }
244
245 # change a freq range->regular expression
246 sub ftor
247 {
248         my ($a, $b) = @_;
249         return undef unless $a < $b;
250         $b--;
251         my $d = $b - $a;
252         my @a = split //, $a;
253         my @b = split //, $b;
254         my $out;
255         while (@b > @a) {
256                 $out .= shift @b;
257         }
258         while (@b) {
259                 my $aa = shift @a;
260                 my $bb = shift @b;
261                 if (@b < (length $d)) {
262                         $out .= '\\d';
263                 } elsif ($aa eq $bb) {
264                         $out .= $aa;
265                 } elsif ($aa < $bb) {
266                         $out .= "[$aa-$bb]";
267                 } else {
268                         $out .= "[0-$bb$aa-9]";
269                 }
270         }
271         return $out;
272 }
273
274 # format a spot for user output in list mode
275 sub formatl
276 {
277         my $t = ztime($_[2]);
278         my $d = cldate($_[2]);
279         return sprintf "%8.1f  %-11s %s %s  %-28.28s%7s>", $_[0], $_[1], $d, $t, $_[3], "<$_[4]" ;
280 }
281
282 #
283 # return all the spots from a day's file as an array of references
284 # the parameter passed is a julian day
285 sub readfile($)
286 {
287         my @spots;
288         
289         my $fh = $fp->open(shift); 
290         if ($fh) {
291                 my $in;
292                 while (<$fh>) {
293                         chomp;
294                         push @spots, [ split '\^' ];
295                 }
296         }
297         return @spots;
298 }
299
300 # enter the spot for dup checking and return true if it is already a dup
301 sub dup
302 {
303         my ($freq, $call, $d, $text) = @_; 
304
305         # dump if too old
306         return 2 if $d < $main::systime - $dupage;
307         
308         # turn the time into minutes (should be already but...)
309         $d = int ($d / 60);
310         $d *= 60;
311
312         $freq = sprintf "%.1f", $freq;       # normalise frequency
313         $call = substr($call, 0, 12) if length $call > 12;
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         my %list;
342         my @tot;
343         
344         if ($in && $out) {
345                 my $i = 0;
346                 @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);
347                 while (<$in>) {
348                         chomp;
349                         my ($freq, $by, $dxcc) = (split /\^/)[0,4,6];
350                         my $ref = $list{$by} || [0, $dxcc];
351                         for (@freq) {
352                                 if ($freq >= $_->[1] && $freq <= $_->[2]) {
353                                         $$ref[$_->[0]+2]++;
354                                         $tot[$_->[0]+2]++;
355                                         $$ref[0]++;
356                                         $tot[0]++;
357                                         $list{$by} = $ref;
358                                         last;
359                                 }
360                         }
361                 }
362
363                 for ($i = 0; $i < @freq+2; $i++) {
364                         $tot[$i] ||= 0;
365                 }
366                 $statp->write($date, join('^', 'TOTALS', @tot));
367
368                 for (sort {$list{$b}->[0] <=> $list{$a}->[0]} keys %list) {
369                         my $ref = $list{$_};
370                         my $call = $_;
371                         for ($i = 0; $i < @freq+2; ++$i) {
372                                 $ref->[$i] ||= 0;
373                         }
374                         $statp->write($date, join('^', $call, @$ref));
375                 }
376                 $statp->close;
377         }
378 }
379
380 # return true if the stat file is newer than than the spot file
381 sub checkstats($)
382 {
383         my $date = shift;
384         my $in = $fp->mtime($date);
385         my $out = $statp->mtime($date);
386         return defined $out && defined $in && $out >= $in;
387 }
388
389 # daily processing
390 sub daily
391 {
392         my $date = Julian::Day->new($main::systime)->sub(1);
393         genstats($date) unless checkstats($date);
394 }
395 1;
396
397
398
399