Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:22:58

0001 #/usr/bin/perl
0002 
0003 use warnings;
0004 use strict;
0005 $|++;
0006 
0007 package TB04::Importer;
0008 use ConnectionFile;
0009 use Data::Dumper;
0010 
0011 
0012 # the maximum time for MySQL 4.1
0013 our $MAX_DATETIME = "9999-12-31 23:59:59";
0014 
0015 my $cnt_fmt = "%10d ";
0016 my $cnt_del = "\b"x11;
0017 
0018 sub new {
0019   my $proto = shift;
0020   my $class = ref($proto) || $proto;
0021   my $this = {};
0022 
0023   # initialize DB connection and other important stuff here
0024   $this->{buffer} = {};
0025   $this->{condDB} = ConnectionFile::connect();
0026 
0027   bless($this, $class);
0028   return $this;
0029 }
0030 
0031 # flush the stream if we're done
0032 sub DESTROY {
0033   my $this = shift;
0034   $this->flush_stream();
0035   if (defined $this->{count}) {
0036     print "\n";
0037   }
0038 }
0039 
0040 sub start_counter {
0041   my $this = shift;
0042   $this->{count} = 0;
0043   print "\nInsertions:  ";
0044   printf $cnt_fmt, $this->{count};
0045 }
0046 
0047 sub count {
0048   my $this = shift;
0049   $this->{count}++;
0050   print $cnt_del;
0051   printf $cnt_fmt, $this->{count};
0052 }
0053 
0054 sub load_view {
0055   my $this = shift;
0056   my $view_name = shift;
0057   my $view = $this->{condDB}->get_channelView(-name=>"$view_name");
0058   if ($view) {
0059     $this->{views}->{$view_name} = $view;
0060   } else {
0061     die "ERROR:  Failed to load view $view_name\n";
0062   }
0063 }
0064 
0065 sub dump_view {
0066   my $this = shift;
0067   my $view_name = shift;
0068   print "DUMP VIEW $view_name:  ", Dumper($this->{views}->{$view_name}), "\n";
0069 }
0070 
0071 # stream in data from some source (parser).  Data is sent in with time stamps
0072 # and IoV must be constructed from within this class before it is inserted
0073 # into the database.  Data should be sent in chronological order.
0074 sub stream {
0075   my $this = shift;
0076   my ( $cond_name,   # the name of the condition (eg. "HV_vMon")
0077        $view_name,   # the name of the channel view to use (eg. "HV_channl")
0078        $view_ids,    # an array ref to the channel keys
0079        $datetime,    # time in YYYY-MM-DD HH:MM:SS format
0080        $value,       # a value
0081        $error        # the (optional) error to the value
0082      )
0083     = @_;
0084 
0085   my $buffer = $this->{buffer};
0086   my $view_key = join(',', $view_name, @{$view_ids});
0087 
0088   # insert the condition if there is a previos value in the boffer
0089   if (exists $buffer->{$cond_name}->{$view_key}) {
0090     my ($since, $l_value, $l_error)
0091       = @{$buffer->{$cond_name}->{$view_key}};
0092     my $IoV = {since=>$since, till=>$datetime};
0093     if ($since ne $datetime) {
0094       $this->insert($cond_name, $view_name, $view_ids, $IoV, $l_value, $l_error);
0095     } else {
0096       warn "WARN:  Duplicate time in stream:\n\tFILE $ARGV\n".
0097     "\tLINE $.\n\tVAR $cond_name $view_key\n".
0098       "\tTIME $since\n\tskipping...\n";
0099     }
0100   }
0101 
0102   # buffer the condition
0103   $buffer->{$cond_name}->{$view_key} = [$datetime, $value, $error];
0104   
0105   return 1;
0106 }
0107 
0108 # this flushes out the buffer, a zero length IoV is written...
0109 # XXX is this what we want?
0110 sub flush_stream {
0111   my $this = shift;
0112   my $buffer = $this->{buffer};
0113   foreach my $cond_name (keys %{$buffer}) {
0114     foreach my $view_key (keys %{$buffer->{$cond_name}}) {
0115       my @view_info = split /,/, $view_key;
0116       my $view_name = shift @view_info;
0117       my $view_ids = [@view_info];
0118       my ($since, $value, $error)
0119     = @{$buffer->{$cond_name}->{$view_key}};
0120       my $IoV = {since=>$since, till=>$MAX_DATETIME};
0121       $this->insert($cond_name, $view_name, $view_ids,
0122             $IoV, $value, $error);
0123     }
0124   }
0125 
0126   $this->{buffer} = {};  # clear buffer
0127 
0128   return 1;
0129 }
0130 
0131 # inserts into the database, with the IoV already constructed
0132 sub insert {
0133   my $this = shift;
0134   my ( $cond_name,   # the name of the condition (eg. "HV_vMon")
0135        $view_name,   # the name of the channel view to use (eg. "HV_channl")
0136        $view_ids,    # an array ref to the channel keys
0137        $IoV,         # hashref to IoV, { since=>$s, till=>$t }
0138        $value,       # a value
0139        $error        # the (optional) error to the value
0140      )
0141     = @_;
0142 
0143   # print the data we recieved
0144   # $this->dummy($cond_name, $view_name, $view_ids, $IoV, $value, $error);
0145 
0146   # make sure the view is loaded
0147   my $view = $this->{views}->{$view_name};
0148   unless ($view) {
0149     die "ERROR:  view \"$view_name\" was not loaded, $!";
0150   }
0151 
0152   # this is just to avoid a warning about undef hash keys...
0153   my ($id1, $id2, $id3) = map {defined $_ ? $_ : ''} (@{$view_ids}, (undef)x3);
0154 
0155   # get the logic_id based on the view_ids
0156   my $logic_id = $view->{$id1}->{$id2}->{$id3};
0157 
0158   eval {
0159     # insert to the DB
0160     unless (ref $value) {
0161       $this->{condDB}->insert_condition(-name=>$cond_name,
0162                     -logic_id=>$logic_id,
0163                     -IoV=>$IoV,
0164                     -value=>$value,
0165                     -error=>$error);
0166     } else {
0167       $this->{condDB}->insert_condition(-name=>$cond_name,
0168                     -logic_id=>$logic_id,
0169                     -IoV=>$IoV,
0170                     -values=>$value,
0171                     -errors=>$error);
0172     }
0173   };
0174   if ($@) {
0175     warn ("ERROR:  insertion failed.  $@\n".
0176       "Tried to insert:\n".
0177       "\t-name=>$cond_name,\n".
0178       "\t-logic_id=>$logic_id,\n".
0179       "\t-IoV=>$IoV->{since}, $IoV->{till},\n".
0180       "\t-value=>$value,\n".
0181       "\t-error=>$error");
0182   }
0183 
0184   if (defined $this->{count}) {
0185     $this->count();
0186   }
0187 
0188   return 1;
0189 }
0190 
0191 sub dummy {
0192   my $this = shift;
0193   my ($cond_name, $view_name, $view_ids, $IoV, $value, $error) = @_;
0194 
0195   # this is just a dummy until the schema is ready
0196   $value = $value."+-".$error if defined $error;
0197   print join('|',
0198          $cond_name,
0199          $view_name,
0200          join(',', @{$view_ids}),
0201          join(',', "since=$IoV->{since}", "till=$IoV->{till}"),
0202          $value), "\n";
0203 
0204 }
0205 
0206 1;