write DXLog 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 FileHandle;
12 use DXVars;
13 use DXDebug;
14 use DXUtil;
15 use DXLog;
16 use Julian;
17 use Prefix;
18 use Carp;
19
20 use strict;
21 use vars qw($fp $maxspots $defaultspots $maxdays $dirprefix);
22
23 $fp = undef;
24 $maxspots = 50;      # maximum spots to return
25 $defaultspots = 10;    # normal number of spots to return
26 $maxdays = 35;        # normal maximum no of days to go back
27 $dirprefix = "spots";
28
29 sub init
30 {
31         mkdir "$dirprefix", 0777 if !-e "$dirprefix";
32         $fp = DXLog::new($dirprefix, "dat", 'd')
33 }
34
35 sub prefix
36 {
37   return $fp->{prefix};
38 }
39
40 # add a spot to the data file (call as Spot::add)
41 sub add
42 {
43   my @spot = @_;    # $freq, $call, $t, $comment, $spotter = @_
44
45   # sure that the numeric things are numeric now (saves time later)
46   $spot[0] = 0 + $spot[0];
47   $spot[2] = 0 + $spot[2];
48   
49   # remove ssid if present on spotter
50   $spot[4] =~ s/-\d+$//o;
51
52   # add the 'dxcc' country on the end
53   my @dxcc = Prefix::extract($spot[1]);
54   push @spot, (@dxcc > 0 ) ? $dxcc[1]->dxcc() : 0;
55
56   my $buf = join("\^", @spot);
57
58   # compare dates to see whether need to open another save file (remember, redefining $fp 
59   # automagically closes the output file (if any)). 
60   my @date = Julian::unixtoj($spot[2]);
61   $fp->write(@date, $buf);
62   
63   return $buf;
64 }
65
66 # search the spot database for records based on the field no and an expression
67 # this returns a set of references to the spots
68 #
69 # the expression is a legal perl 'if' statement with the possible fields indicated
70 # by $f<n> where :-
71 #
72 #   $f0 = frequency
73 #   $f1 = call
74 #   $f2 = date in unix format
75 #   $f3 = comment
76 #   $f4 = spotter
77 #   $f5 = dxcc country
78 #
79 # In addition you can specify a range of days, this means that it will start searching
80 # from <n> days less than today to <m> days less than today
81 #
82 # Also you can select a range of entries so normally you would get the 0th (latest) entry
83 # back to the 5th latest, you can specify a range from the <x>th to the <y>the oldest.
84 #
85 # This routine is designed to be called as Spot::search(..)
86 #
87
88 sub search
89 {
90   my ($expr, $dayfrom, $dayto, $from, $to) = @_;
91   my $eval;
92   my @out;
93   my $ref;
94   my $i;
95   my $count;
96   my @today = Julian::unixtoj(time);
97   my @fromdate;
98   my @todate;
99   
100   if ($dayfrom > 0) {
101     @fromdate = Julian::sub(@today, $dayfrom);
102   } else {
103     @fromdate = @today;
104         $dayfrom = 0;
105   }
106   if ($dayto > 0) {
107     @todate = Julian::sub(@fromdate, $dayto);
108   } else {
109     @todate = Julian::sub(@fromdate, $maxdays);
110   }
111   if ($from || $to) {
112     $to = $from + $maxspots if $to - $from > $maxspots || $to - $from <= 0;
113   } else {
114     $from = 0;
115         $to = $defaultspots;
116   }
117
118   $expr =~ s/\$f(\d)/\$ref->[$1]/g;               # swap the letter n for the correct field name
119 #  $expr =~ s/\$f(\d)/\$spots[$1]/g;               # swap the letter n for the correct field name
120   
121   dbg("search", "expr='$expr', spotno=$from-$to, day=$dayfrom-$dayto\n");
122   
123   # build up eval to execute
124   $eval = qq(
125     my \$c;
126         my \$ref;
127     for (\$c = \$#spots; \$c >= 0; \$c--) {
128           \$ref = \$spots[\$c];
129           if ($expr) {
130             \$count++;
131                 next if \$count < \$from;                  # wait until from 
132         push(\@out, \$ref);
133                 last LOOP if \$count >= \$to;                  # stop after to
134           }
135     }
136   );
137
138   $fp->close;                                      # close any open files
139
140 LOOP:
141   for ($i = 0; $i < $maxdays; ++$i) {             # look thru $maxdays worth of files only
142     my @now = Julian::sub(@fromdate, $i);         # but you can pick which $maxdays worth
143         last if Julian::cmp(@now, @todate) <= 0;         
144         
145         my @spots = ();
146         my $fh = $fp->open(@now);  # get the next file
147         if ($fh) {
148           my $in;
149           while (<$fh>) {
150                   chomp;
151                   push @spots, [ split '\^' ];
152           }
153           eval $eval;               # do the search on this file
154           return ("Spot search error", $@) if $@;
155         }
156   }
157
158   return @out;
159 }
160
161 # format a spot for user output in 'broadcast' mode
162 sub formatb
163 {
164   my @dx = @_;
165   my $t = ztime($dx[2]);
166   return sprintf "DX de %-7.7s: %13.1f %-12.12s %-30s<%s>", $dx[4], $dx[0], $dx[1], $dx[3], $t ;
167 }
168
169 # format a spot for user output in list mode
170 sub formatl
171 {
172   my @dx = @_;
173   my $t = ztime($dx[2]);
174   my $d = cldate($dx[2]);
175   return sprintf "%9.1f %-12s %s %s %-30s<%s>", $dx[0], $dx[1], $d, $t, $dx[3], $dx[4] ;
176 }
177
178 1;