well on the way to having a working cluster database
[spider.git] / perl / gdx.pl
1 #!/usr/bin/perl
2 #
3 # grep for expressions in various fields of the dx file
4 #
5
6 use FileHandle;
7 use DXUtil;
8 use DXDebug;
9 use Spot;
10
11 # initialise spots file
12 STDOUT->autoflush(1);
13
14 #print "reading in spot data ..";
15 #$t = time;
16 #$count = Spot->init();
17 #$t = time - $t;
18 #print "done ($t secs)\n";
19
20 dbgadd('spot');
21
22 $field = $ARGV[0];
23 $expr = $ARGV[1];
24 $time = time;
25
26 #loada();
27 for (;;) {
28   print "expr: ";
29   $expr = <STDIN>;
30   last if $expr =~ /^q/i;
31
32   chomp $expr;
33
34   print "doing field $field with /$expr/\n";
35
36 #a();
37   b();
38 }
39
40 sub b
41 {
42   my @spots;
43   my @dx;
44   my $ref;
45   my $count;
46   my $i;
47   
48   my $t = time;
49   @spots = Spot::search($expr);
50   if ($spots[0] eq "error") {
51     print $spots[1];
52         return;
53   }
54   foreach $ref (@spots) {
55     @dx = @$ref;
56         my $t = ztime($dx[2]);
57         my $d = cldate($dx[2]);
58         print "$dx[0] $dx[1] $d $t $dx[4] <$dx[3]>\n";
59         ++$count;
60   }
61   $t = time - $t;
62   print "$count records found, $t secs\n";
63 }
64
65 sub search
66 {
67   my ($expr, $from, $to) = @_;
68   my $eval;
69   my @out;
70   my @spots;
71   my $ref;
72   my $i;
73
74
75   $expr =~ s/\$f(\d)/zzzref->[$1]/g;               # swap the letter n for the correct field name
76   $expr =~ s/[\@\$\%\{\}]//g;                           # remove any other funny characters
77   $expr =~ s/\&\w+\(//g;                           # remove subroutine calls
78   $expr =~ s/eval//g;                              # remove eval words
79   $expr =~ s/zzzref/\$ref/g;                       # put back the $ref
80   
81   print "expr = $expr\n";
82   
83   # build up eval to execute
84   $eval = qq(my \$c;
85     for (\$c = \$#spots; \$c >= 0; \$c--) {
86           \$ref = \$spots[\$c];
87           if ($expr) {
88         push(\@out, \$ref);
89           }
90   });
91
92   my @today = Julian::unixtoj(time);
93   for ($i = 0; $i < 60; ++$i) {
94     my @now = Julian::sub(@today, $i);
95         my @spots;
96         my $fp = Spot->open(@now);
97         if ($fp) {
98           my $fh = $fp->{fh};
99           my $in;
100           foreach $in (<$fh>) {
101             chomp $in;
102         push @spots, [ split('\^', $in) ];
103           }
104           my $ref;
105           eval $eval;
106           return ("error", $@) if $@;
107         }
108   }
109                                # execute it
110   return @out;
111 }
112
113
114 sub loada
115 {
116   while (<IN>) {
117     chomp;
118         my @dx =  split /\^/;
119         next if $time - $dx[2] > (84600 * 60);  
120         unshift @spots, [ @dx ];
121         ++$count;
122   }
123 }
124
125 sub a
126 {
127   foreach $ref (@spots) {
128     if ($$ref[$field] =~ /$expr/i) {
129           my @dx = @$ref;
130           my $t = ztime($dx[2]);
131           my $d = cldate($dx[2]);
132       print "$dx[0] $dx[1] $d $t $dx[4] <$dx[3]>\n";
133         }
134   }
135 }
136