|
| 1 | +import logging |
| 2 | +import time |
| 3 | +import unittest |
| 4 | +from datetime import timedelta, date, datetime |
| 5 | +from typing import List |
| 6 | + |
| 7 | +from qcrepocleaner.Ccdb import Ccdb, ObjectVersion |
| 8 | +from qcrepocleaner.rules import multiple_per_run |
| 9 | + |
| 10 | + |
| 11 | +class TestProduction(unittest.TestCase): |
| 12 | + """ |
| 13 | + This test pushes data to the CCDB and then run the Rule Production and then check. |
| 14 | + It does it for several use cases. |
| 15 | + One should truncate /qc/TST/MO/repo/test before running it. |
| 16 | + """ |
| 17 | + |
| 18 | + thirty_minutes = 1800000 |
| 19 | + one_hour = 3600000 |
| 20 | + in_ten_years = 1975323342000 |
| 21 | + one_minute = 60000 |
| 22 | + |
| 23 | + def setUp(self): |
| 24 | + self.ccdb = Ccdb('http://137.138.47.222:8080') |
| 25 | + self.extra = {"interval_between_versions": "90", "migrate_to_EOS": False, "delete_first_last": True} |
| 26 | + self.path = "qc/TST/MO/repo/test" |
| 27 | + |
| 28 | + def test_1_finished_run(self): |
| 29 | + """ |
| 30 | + 1 run of 2.5 hours finished 22 hours ago. |
| 31 | + Expected output: SOR, EOR, 1 in the middle |
| 32 | + """ |
| 33 | + logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', |
| 34 | + datefmt='%d-%b-%y %H:%M:%S') |
| 35 | + logging.getLogger().setLevel(int(10)) |
| 36 | + |
| 37 | + # Prepare data |
| 38 | + test_path = self.path + "/test_1_finished_run" |
| 39 | + self.prepare_data(test_path, [150], [22*60], 123) |
| 40 | + objectsBefore = self.ccdb.getVersionsList(test_path) |
| 41 | + |
| 42 | + stats = multiple_per_run.process(self.ccdb, test_path, delay=60*24, from_timestamp=1, |
| 43 | + to_timestamp=self.in_ten_years, extra_params=self.extra) |
| 44 | + objectsAfter = self.ccdb.getVersionsList(test_path) |
| 45 | + |
| 46 | + self.assertEqual(stats["deleted"], 147) |
| 47 | + self.assertEqual(stats["preserved"], 3) |
| 48 | + self.assertEqual(stats["updated"], 0) |
| 49 | + |
| 50 | + self.assertEqual(objectsAfter[0].validFrom, objectsBefore[1].validFrom) |
| 51 | + self.assertEqual(objectsAfter[2].validFrom, objectsBefore[-2].validFrom) |
| 52 | + |
| 53 | + def test_2_runs(self): |
| 54 | + """ |
| 55 | + 2 runs of 2.5 hours, separated by 3 hours, second finished 20h ago. |
| 56 | + Expected output: SOR, EOR, 1 in the middle for the first one, all for the second |
| 57 | + """ |
| 58 | + logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', |
| 59 | + datefmt='%d-%b-%y %H:%M:%S') |
| 60 | + logging.getLogger().setLevel(int(10)) |
| 61 | + |
| 62 | + # Prepare data |
| 63 | + test_path = self.path + "/test_2_runs" |
| 64 | + self.prepare_data(test_path, [150, 150], [3*60, 20*60], 123) |
| 65 | + |
| 66 | + stats = multiple_per_run.process(self.ccdb, test_path, delay=60*24, from_timestamp=1, |
| 67 | + to_timestamp=self.in_ten_years, extra_params=self.extra) |
| 68 | + |
| 69 | + self.assertEqual(stats["deleted"], 147) |
| 70 | + self.assertEqual(stats["preserved"], 3+150) |
| 71 | + self.assertEqual(stats["updated"], 0) |
| 72 | + |
| 73 | + def test_5_runs(self): |
| 74 | + """ |
| 75 | + 1 hour Run - 1h - 2 hours Run - 2h - 3h10 run - 3h10 - 4 hours run - 4 hours - 5 hours run - 5 h |
| 76 | + All more than 24 hours |
| 77 | + Expected output: 2 + 3 + 4 + 4 + 5 |
| 78 | + """ |
| 79 | + logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', |
| 80 | + datefmt='%d-%b-%y %H:%M:%S') |
| 81 | + logging.getLogger().setLevel(int(10)) |
| 82 | + |
| 83 | + # Prepare data |
| 84 | + test_path = self.path + "/test_5_runs" |
| 85 | + self.prepare_data(test_path, [1*60, 2*60, 3*60+10, 4*60, 5*60], |
| 86 | + [60, 120, 190, 240, 24*60], 123) |
| 87 | + |
| 88 | + stats = multiple_per_run.process(self.ccdb, test_path, delay=60*24, from_timestamp=1, |
| 89 | + to_timestamp=self.in_ten_years, extra_params=self.extra) |
| 90 | + self.assertEqual(stats["deleted"], 60+120+190+240+300-18) |
| 91 | + self.assertEqual(stats["preserved"], 18) |
| 92 | + self.assertEqual(stats["updated"], 0) |
| 93 | + |
| 94 | + # and now re-run it to make sure we preserve the state |
| 95 | + stats = multiple_per_run.process(self.ccdb, test_path, delay=60*24, from_timestamp=1, |
| 96 | + to_timestamp=self.in_ten_years, extra_params=self.extra) |
| 97 | + |
| 98 | + self.assertEqual(stats["deleted"], 0) |
| 99 | + self.assertEqual(stats["preserved"], 18) |
| 100 | + self.assertEqual(stats["updated"], 0) |
| 101 | + |
| 102 | + def test_run_one_object(self): |
| 103 | + """ |
| 104 | + A run with a single object |
| 105 | + Expected output: keep the object |
| 106 | + """ |
| 107 | + logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', |
| 108 | + datefmt='%d-%b-%y %H:%M:%S') |
| 109 | + logging.getLogger().setLevel(int(10)) |
| 110 | + |
| 111 | + # Prepare data |
| 112 | + test_path = self.path + "/test_run_one_object" |
| 113 | + self.prepare_data(test_path, [1], [25*60], 123) |
| 114 | + |
| 115 | + stats = multiple_per_run.process(self.ccdb, test_path, delay=60*24, from_timestamp=1, |
| 116 | + to_timestamp=self.in_ten_years, extra_params=self.extra) |
| 117 | + |
| 118 | + self.assertEqual(stats["deleted"], 0) |
| 119 | + self.assertEqual(stats["preserved"], 1) |
| 120 | + self.assertEqual(stats["updated"], 0) |
| 121 | + |
| 122 | + def test_run_two_object(self): |
| 123 | + """ |
| 124 | + A run with 2 objects |
| 125 | + Expected output: keep the 2 objects |
| 126 | + """ |
| 127 | + logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', |
| 128 | + datefmt='%d-%b-%y %H:%M:%S') |
| 129 | + logging.getLogger().setLevel(int(10)) |
| 130 | + |
| 131 | + # Prepare data |
| 132 | + test_path = self.path + "/test_run_two_object" |
| 133 | + self.prepare_data(test_path, [2], [25*60], 123) |
| 134 | + |
| 135 | + stats = multiple_per_run.process(self.ccdb, test_path, delay=60*24, from_timestamp=1, |
| 136 | + to_timestamp=self.in_ten_years, extra_params=self.extra) |
| 137 | + |
| 138 | + self.assertEqual(stats["deleted"], 0) |
| 139 | + self.assertEqual(stats["preserved"], 2) |
| 140 | + self.assertEqual(stats["updated"], 0) |
| 141 | + |
| 142 | + def test_3_runs_with_period(self): |
| 143 | + """ |
| 144 | + 3 runs more than 24h in the past but only the middle one starts in the period that is allowed. |
| 145 | + Expected output: second run is trimmed, not the other |
| 146 | + """ |
| 147 | + logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', |
| 148 | + datefmt='%d-%b-%y %H:%M:%S') |
| 149 | + logging.getLogger().setLevel(int(10)) |
| 150 | + |
| 151 | + # Prepare data |
| 152 | + test_path = self.path + "/test_3_runs_with_period" |
| 153 | + self.prepare_data(test_path, [30,30, 30], [120,120,25*60], 123) |
| 154 | + |
| 155 | + current_timestamp = int(time.time() * 1000) |
| 156 | + stats = multiple_per_run.process(self.ccdb, test_path, delay=60*24, from_timestamp=current_timestamp-29*60*60*1000, |
| 157 | + to_timestamp=current_timestamp-26*60*60*1000, extra_params=self.extra) |
| 158 | + |
| 159 | + self.assertEqual(stats["deleted"], 28) |
| 160 | + self.assertEqual(stats["preserved"], 90-28) |
| 161 | + self.assertEqual(stats["updated"], 0) |
| 162 | + |
| 163 | + def test_asdf(self): |
| 164 | + logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', |
| 165 | + datefmt='%d-%b-%y %H:%M:%S') |
| 166 | + logging.getLogger().setLevel(int(10)) |
| 167 | + test_path = self.path + "/asdf" |
| 168 | + self.prepare_data(test_path, [70, 70, 70], [6*60, 6*60, 25*60], 55555) |
| 169 | + |
| 170 | + def prepare_data(self, path, run_durations: List[int], time_till_next_run: List[int], first_run_number: int): |
| 171 | + """ |
| 172 | + Prepare a data set populated with a number of runs. |
| 173 | + run_durations contains the duration of each of these runs in minutes |
| 174 | + time_till_next_run is the time between two runs in minutes. |
| 175 | + The first element of time_till_next_run is used to separate the first two runs. |
| 176 | + Both lists must have the same number of elements. |
| 177 | + """ |
| 178 | + |
| 179 | + if len(run_durations) != len(time_till_next_run): |
| 180 | + logging.error(f"run_durations and time_till_next_run must have the same length") |
| 181 | + exit(1) |
| 182 | + |
| 183 | + total_duration = 0 |
| 184 | + for a, b in zip(run_durations, time_till_next_run): |
| 185 | + total_duration += a + b |
| 186 | + logging.info(f"Total duration : {total_duration}") |
| 187 | + |
| 188 | + current_timestamp = int(time.time() * 1000) |
| 189 | + cursor = current_timestamp - total_duration * 60 * 1000 |
| 190 | + first_ts = cursor |
| 191 | + data = {'part': 'part'} |
| 192 | + run = first_run_number |
| 193 | + |
| 194 | + for run_duration, time_till_next in zip(run_durations, time_till_next_run): |
| 195 | + metadata = {'RunNumber': str(run)} |
| 196 | + logging.debug(f"cursor: {cursor}") |
| 197 | + logging.debug(f"time_till_next: {time_till_next}") |
| 198 | + |
| 199 | + for i in range(run_duration): |
| 200 | + to_ts = cursor + 24 * 60 * 60 * 1000 # a day |
| 201 | + metadata2 = {**metadata, 'Created': str(cursor)} |
| 202 | + version_info = ObjectVersion(path=path, validFrom=cursor, validTo=to_ts, metadata=metadata2, |
| 203 | + createdAt=cursor) |
| 204 | + self.ccdb.putVersion(version=version_info, data=data) |
| 205 | + cursor += 1 * 60 * 1000 |
| 206 | + |
| 207 | + run += 1 |
| 208 | + cursor += time_till_next * 60 * 1000 |
| 209 | + |
| 210 | + return first_ts |
| 211 | + |
| 212 | + |
| 213 | +if __name__ == '__main__': |
| 214 | + unittest.main() |
0 commit comments