Changeset 29022
- Timestamp:
- Aug 23, 2010, 3:40:35 PM (16 years ago)
- File:
-
- 1 edited
-
trunk/tools/czartool/CzarDb.pm (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/czartool/CzarDb.pm
r28921 r29022 841 841 } 842 842 843 844 845 ########################################################################### 846 # 847 # Deletes all but one row per interval from all stage tables for all labels between the two dates 848 # 849 ########################################################################### 850 sub cleanupDateRange { 851 my ($self, $startDay, $endDay, $interval) = @_; 852 853 my $thisDay = $startDay; 854 my $quit = 0; 855 while(!$quit) { 856 857 if (!$self->isBefore($thisDay, $endDay)) { 858 859 $quit = 1; 860 } 861 862 print "* Running cleanup for $thisDay with an interval of $interval\n"; 863 $self->cleanupADay($thisDay, $interval); 864 $thisDay = $self->addInterval($thisDay, "1 DAY"); 865 866 } 867 } 868 ########################################################################### 869 # 870 # Deletes all but one row per interval from all stage tables for all labels between the provided day 871 # 872 ########################################################################### 873 sub cleanupADay { 874 my ($self, $startDay, $interval) = @_; 875 876 my $endDay = $self->addInterval($startDay, "1 DAY"); 877 878 my $labels = undef; 879 my $fromTime = $startDay; 880 my $toTime = undef; 881 my $totalDeleted = undef; 882 my $quit = 0; 883 while(!$quit) { 884 885 $toTime = $self->addInterval($fromTime, $interval); 886 if (!$self->isBefore($toTime, $endDay)) { 887 888 $toTime = $endDay; 889 $quit = 1; 890 } 891 892 my $stage = undef; 893 $totalDeleted = 0; 894 foreach $stage (@stages) { 895 896 if (!$self->getLabelsInThisTimePeriod($stage, $fromTime, $toTime, \$labels)) {next;} 897 898 my $label = undef; 899 my $row = undef; 900 foreach $row ( @{$labels} ) { 901 my ($label) = @{$row}; 902 903 my $query = $self->{_db}->prepare(<<SQL); 904 SELECT COUNT(*) 905 FROM $stage 906 WHERE timestamp > '$fromTime' 907 AND timestamp <= '$toTime' 908 AND label = '$label' 909 SQL 910 911 $query->execute; 912 913 my $toDelete = scalar $query->fetchrow_array() - 1; 914 if ($toDelete < 1) {next;} 915 916 $query = $self->{_db}->prepare(<<SQL); 917 DELETE FROM $stage 918 WHERE timestamp > '$fromTime' 919 AND timestamp <= '$toTime' 920 AND label = '$label' ORDER BY timestamp DESC LIMIT $toDelete 921 SQL 922 $query->execute; 923 924 $totalDeleted += $toDelete; 925 } 926 } 927 print " * Deleted $totalDeleted between $fromTime and $toTime\n"; 928 $fromTime = $toTime; 929 } 930 } 931 932 ########################################################################### 933 # 934 # Returns an array of labels present during the provided time frame 935 # 936 ########################################################################### 937 sub getLabelsInThisTimePeriod { 938 my ($self, $stage, $fromTime, $toTime, $labels) = @_; 939 940 my $query = $self->{_db}->prepare(<<SQL); 941 SELECT DISTINCT label 942 FROM $stage 943 WHERE timestamp > '$fromTime' 944 AND timestamp <= '$toTime'; 945 SQL 946 947 if (!$query->execute) { 948 949 return 0; 950 } 951 952 ${$labels} = $query->fetchall_arrayref(); 953 954 return 1; 955 } 956 843 957 1; 844 958
Note:
See TracChangeset
for help on using the changeset viewer.
