IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Aug 26, 2010, 9:18:39 AM (16 years ago)
Author:
Serge CHASTEL
Message:

Merging trunk in branch

Location:
branches/sc_branches/trunkTest
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/sc_branches/trunkTest

  • branches/sc_branches/trunkTest/tools/czartool/CzarDb.pm

    r28921 r29060  
    1 #!/usr/bin/perl i-w
     1#!/usr/bin/perl -w
    22
    33package czartool::CzarDb;
     
    66use strict;
    77
    8 my @stages = ("chip", "cam", "fake", "warp", "stack", "diff", "magic", "magicDS", "dist"); # TODO put elsewhere
     8my @stages = ("burntool", "chip", "cam", "fake", "warp", "stack", "diff", "magic", "magicDS", "dist"); # TODO put elsewhere
    99
    1010use base 'czartool::MySQLDb';
     
    491491}
    492492
     493
     494###########################################################################
     495#
     496# Deletes all but one row per interval from all stage tables for all labels between the two dates
     497#
     498###########################################################################
     499sub cleanupDateRange {
     500    my ($self, $startDay, $endDay, $interval) = @_;
     501
     502    my $thisDay = $startDay;
     503    my $quit = 0;
     504    while(!$quit) {
     505
     506        if (!$self->isBefore($thisDay, $endDay)) {
     507       
     508            $quit = 1;
     509        }
     510
     511        print "* Running cleanup for $thisDay with an interval of $interval\n";
     512        $self->cleanupADay($thisDay, $interval);
     513        $thisDay = $self->addInterval($thisDay, "1 DAY");
     514
     515    }
     516}
     517###########################################################################
     518#
     519# Deletes all but one row per interval from all stage tables for all labels between the provided day
     520#
     521###########################################################################
     522sub cleanupADay {
     523    my ($self, $startDay, $interval) = @_;
     524
     525    my $endDay =  $self->addInterval($startDay, "1 DAY");
     526
     527    my $labels = undef;
     528    my $fromTime = $startDay;
     529    my $toTime = undef;
     530    my $totalDeleted = undef;
     531    my $quit = 0;
     532    while(!$quit) {
     533
     534        $toTime = $self->addInterval($fromTime, $interval);
     535        if (!$self->isBefore($toTime, $endDay)) {
     536       
     537            $toTime = $endDay;
     538            $quit = 1;
     539        }
     540   
     541        my $stage = undef;
     542        $totalDeleted = 0;
     543        foreach $stage (@stages) {
     544
     545            if (!$self->getLabelsInThisTimePeriod($stage, $fromTime, $toTime, \$labels)) {next;}
     546
     547            my $label = undef;
     548            my $row = undef;
     549            foreach $row ( @{$labels} ) {
     550                my ($label) = @{$row};
     551
     552                my $query = $self->{_db}->prepare(<<SQL);
     553                SELECT COUNT(*)
     554                    FROM $stage
     555                    WHERE timestamp > '$fromTime'
     556                    AND timestamp <= '$toTime'
     557                    AND label = '$label'
     558SQL
     559
     560                    $query->execute;
     561
     562                my $toDelete = scalar $query->fetchrow_array() - 1;
     563                if ($toDelete < 1) {next;}
     564
     565                $query = $self->{_db}->prepare(<<SQL);
     566                DELETE FROM $stage
     567                    WHERE timestamp > '$fromTime'
     568                    AND timestamp <= '$toTime'
     569                    AND label = '$label' ORDER BY timestamp DESC LIMIT $toDelete
     570SQL
     571                    $query->execute;
     572
     573                $totalDeleted += $toDelete;
     574            }
     575        }
     576        print "   * Deleted $totalDeleted between $fromTime and  $toTime\n";
     577        $fromTime = $toTime;
     578    }
     579}
     580
     581###########################################################################
     582#
     583# Returns an array of labels present during the provided time frame
     584#
     585###########################################################################
     586sub getLabelsInThisTimePeriod {
     587    my ($self, $stage, $fromTime, $toTime, $labels) = @_;
     588
     589    my $query = $self->{_db}->prepare(<<SQL);
     590    SELECT DISTINCT label
     591        FROM $stage
     592        WHERE timestamp > '$fromTime'
     593        AND timestamp <= '$toTime';
     594SQL
     595
     596        if (!$query->execute) {
     597
     598            return 0;
     599        }
     600
     601    ${$labels} = $query->fetchall_arrayref();
     602
     603    return 1;
     604}
     605
    493606###########################################################################
    494607#
     
    500613
    501614    my $currentRevision = -1;
    502     my $latestRevision = 10;
    503 
    504     while ($currentRevision != $latestRevision) {
     615
     616    while (1) {
    505617
    506618        $currentRevision = $self->getRevision();
     
    517629        elsif ($currentRevision == 8) {$self->createRevision_9();}
    518630        elsif ($currentRevision == 9) {$self->createRevision_10();}
     631        elsif ($currentRevision == 10) {$self->createRevision_11();}
     632        else {last;}
    519633    }
    520634}
     
    841955}
    842956
     957#######################################################################################
     958#
     959# Create revision 11 of the database
     960#
     961#######################################################################################
     962sub createRevision_11 {
     963    my ($self) = @_;
     964
     965    print "* Creating revision 11 of '$self->{_dbName}'\n";
     966
     967    # same shape as other stage tables to enable easy update
     968    my $query = $self->{_db}->prepare(<<SQL);
     969    CREATE TABLE burntool (
     970            timestamp TIMESTAMP DEFAULT NOW(),
     971            label VARCHAR(128) DEFAULT "NONE",
     972            pending BIGINT NOT NULL,
     973            processed BIGINT NOT NULL,
     974            faults BIGINT NOT NULL);
     975SQL
     976
     977      $query->execute;
     978        $query = $self->{_db}->prepare(<<SQL);
     979        CREATE INDEX burntoolIndex ON burntool (timestamp, label);
     980SQL
     981
     982      $query->execute;
     983
     984    $self->setRevision(11);
     985}
     986
     987
     988
    8439891;
    844990
Note: See TracChangeset for help on using the changeset viewer.