added mrtg stuff
[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
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() : 666;
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() : 666;
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         $totalspots++;
144         if ($_[0] <= 30000) {
145                 $hfspots++;
146         } else {
147                 $vhfspots++;
148         }
149 }
150
151 # search the spot database for records based on the field no and an expression
152 # this returns a set of references to the spots
153 #
154 # the expression is a legal perl 'if' statement with the possible fields indicated
155 # by $f<n> where :-
156 #
157 #   $f0 = frequency
158 #   $f1 = call
159 #   $f2 = date in unix format
160 #   $f3 = comment
161 #   $f4 = spotter
162 #   $f5 = spotted dxcc country
163 #   $f6 = spotter dxcc country
164 #   $f7 = origin
165 #
166 #
167 # In addition you can specify a range of days, this means that it will start searching
168 # from <n> days less than today to <m> days less than today
169 #
170 # Also you can select a range of entries so normally you would get the 0th (latest) entry
171 # back to the 5th latest, you can specify a range from the <x>th to the <y>the oldest.
172 #
173 # This routine is designed to be called as Spot::search(..)
174 #
175
176 sub search
177 {
178         my ($expr, $dayfrom, $dayto, $from, $to, $hint) = @_;
179         my $eval;
180         my @out;
181         my $ref;
182         my $i;
183         my $count;
184         my $today = Julian::Day->new(time());
185         my $fromdate;
186         my $todate;
187
188         $dayfrom = 0 if !$dayfrom;
189         $dayto = $maxdays unless $dayto;
190         $dayto = $dayfrom + $maxdays if $dayto < $dayfrom;
191         $fromdate = $today->sub($dayfrom);
192         $todate = $fromdate->sub($dayto);
193         $from = 0 unless $from;
194         $to = $defaultspots unless $to;
195         $hint = $hint ? "next unless $hint" : "";
196         $expr = "1" unless $expr;
197         
198         $to = $from + $maxspots if $to - $from > $maxspots || $to - $from <= 0;
199
200         $expr =~ s/\$f(\d)/\$ref->[$1]/g; # swap the letter n for the correct field name
201         #  $expr =~ s/\$f(\d)/\$spots[$1]/g;               # swap the letter n for the correct field name
202   
203         dbg("hint='$hint', expr='$expr', spotno=$from-$to, day=$dayfrom-$dayto\n") if isdbg('search');
204   
205         # build up eval to execute
206         $eval = qq(
207                            while (<\$fh>) {
208                                    $hint;
209                                    chomp;
210                                    push \@spots, [ split '\\^' ];
211                            }
212                            my \$c;
213                            my \$ref;
214                            for (\$c = \$#spots; \$c >= 0; \$c--) {
215                                         \$ref = \$spots[\$c];
216                                         if ($expr) {
217                                                 \$count++;
218                                                 next if \$count < \$from; # wait until from 
219                                                 push(\@out, \$ref);
220                                                 last if \$count >= \$to; # stop after to
221                                         }
222                                 }
223                           );
224
225         $fp->close;                                     # close any open files
226
227         for ($i = $count = 0; $i < $maxdays; ++$i) {    # look thru $maxdays worth of files only
228                 my $now = $fromdate->sub($i); # but you can pick which $maxdays worth
229                 last if $now->cmp($todate) <= 0;         
230         
231                 my @spots = ();
232                 my $fh = $fp->open($now); # get the next file
233                 if ($fh) {
234                         my $in;
235                         eval $eval;                     # do the search on this file
236                         last if $count >= $to; # stop after to
237                         return ("Spot search error", $@) if $@;
238                 }
239         }
240
241         return @out;
242 }
243
244 # change a freq range->regular expression
245 sub ftor
246 {
247         my ($a, $b) = @_;
248         return undef unless $a < $b;
249         $b--;
250         my $d = $b - $a;
251         my @a = split //, $a;
252         my @b = split //, $b;
253         my $out;
254         while (@b > @a) {
255                 $out .= shift @b;
256         }
257         while (@b) {
258                 my $aa = shift @a;
259                 my $bb = shift @b;
260                 if (@b < (length $d)) {
261                         $out .= '\\d';
262                 } elsif ($aa eq $bb) {
263                         $out .= $aa;
264                 } elsif ($aa < $bb) {
265                         $out .= "[$aa-$bb]";
266                 } else {
267                         $out .= "[0-$bb$aa-9]";
268                 }
269         }
270         return $out;
271 }
272
273 # format a spot for user output in list mode
274 sub formatl
275 {
276         my $t = ztime($_[2]);
277         my $d = cldate($_[2]);
278         return sprintf "%8.1f  %-11s %s %s  %-28.28s%7s>", $_[0], $_[1], $d, $t, $_[3], "<$_[4]" ;
279 }
280
281 #
282 # return all the spots from a day's file as an array of references
283 # the parameter passed is a julian day
284 sub readfile($)
285 {
286         my @spots;
287         
288         my $fh = $fp->open(shift); 
289         if ($fh) {
290                 my $in;
291                 while (<$fh>) {
292                         chomp;
293                         push @spots, [ split '\^' ];
294                 }
295         }
296         return @spots;
297 }
298
299 # enter the spot for dup checking and return true if it is already a dup
300 sub dup
301 {
302         my ($freq, $call, $d, $text) = @_; 
303
304         # dump if too old
305         return 2 if $d < $main::systime - $dupage;
306  
307         $freq = sprintf "%.1f", $freq;       # normalise frequency
308         $call = substr($call, 0, 12) if length $call > 12;
309         chomp $text;
310         $text =~ s/\%([0-9A-F][0-9A-F])/chr(hex($1))/eg;
311         $text = substr($text, 0, $duplth) if length $text > $duplth; 
312         unpad($text);
313         $text = pack("C*", map {$_ & 127} unpack("C*", $text));
314         $text =~ s/[^a-zA-Z0-9]//g;
315         for (0,60,120,180,240,300) {
316                 my $dt = $d - $_;
317                 my $dupkey = "X$freq|$call|$dt|\L$text";
318                 return 1 if DXDupe::find($dupkey);
319         }
320         my $dupkey = "X$freq|$call|$d|\L$text";
321         DXDupe::add($dupkey, $main::systime+$dupage);
322         return 0;
323 }
324
325 sub listdups
326 {
327         return DXDupe::listdups('X', $dupage, @_);
328 }
329
330 sub genstats($)
331 {
332         my $date = shift;
333         my $in = $fp->open($date);
334         my $out = $statp->open($date, 'w');
335         my @freq = (
336                                 [0, Bands::get_freq('160m')],
337                                 [1, Bands::get_freq('80m')],
338                                 [2, Bands::get_freq('40m')],
339                                 [3, Bands::get_freq('30m')],
340                                 [4, Bands::get_freq('20m')],
341                                 [5, Bands::get_freq('17m')],
342                                 [6, Bands::get_freq('15m')],
343                                 [7, Bands::get_freq('12m')],
344                                 [8, Bands::get_freq('10m')],
345                                 [9, Bands::get_freq('6m')],
346                                 [10, Bands::get_freq('4m')],
347                                 [11, Bands::get_freq('2m')],
348                                 [12, Bands::get_freq('220')],
349                                 [13, Bands::get_freq('70cm')],
350                                 [14, Bands::get_freq('23cm')],
351                                 [15, Bands::get_freq('13cm')],
352                                 [16, Bands::get_freq('9cm')],
353                                 [17, Bands::get_freq('6cm')],
354                                 [18, Bands::get_freq('3cm')],
355                                 [19, Bands::get_freq('12mm')],
356                                 [20, Bands::get_freq('6cm')],
357                            );
358         my %list;
359         my @tot;
360         
361         if ($in && $out) {
362                 while (<$in>) {
363                         chomp;
364                         my ($freq, $by, $dxcc) = (split /\^/)[0,4,6];
365                         my $ref = $list{$by} || [0, $dxcc];
366                         for (@freq) {
367                                 if ($freq >= $_->[1] && $freq <= $_->[2]) {
368                                         $$ref[$_->[0]+2]++;
369                                         $tot[$_->[0]+2]++;
370                                         $$ref[0]++;
371                                         $tot[0]++;
372                                         $list{$by} = $ref;
373                                         last;
374                                 }
375                         }
376                 }
377
378                 my $i;
379                 for ($i = 0; $i < @freq+2; $i++) {
380                         $tot[$i] ||= 0;
381                 }
382                 $statp->write($date, join('^', 'TOTALS', @tot));
383
384                 for (sort {$list{$b}->[0] <=> $list{$a}->[0]} keys %list) {
385                         my $ref = $list{$_};
386                         my $call = $_;
387                         for ($i = 0; $i < @freq+2; ++$i) {
388                                 $ref->[$i] ||= 0;
389                         }
390                         $statp->write($date, join('^', $call, @$ref));
391                 }
392                 $statp->close;
393         }
394 }
395
396 # return true if the stat file is newer than than the spot file
397 sub checkstats($)
398 {
399         my $date = shift;
400         my $in = $fp->mtime($date);
401         my $out = $statp->mtime($date);
402         return defined $out && defined $in && $out >= $in;
403 }
404
405 # daily processing
406 sub daily
407 {
408         my $date = Julian::Day->new($main::systime)->sub(1);
409         genstats($date) unless checkstats($date);
410 }
411 1;
412
413
414
415