Skip to content

Commit 9239734

Browse files
authored
Merge pull request #18 from OpenDataAlex/process_tracker_python-16
Process tracker python 16
2 parents 56f07b8 + c112820 commit 9239734

File tree

3 files changed

+141
-3
lines changed

3 files changed

+141
-3
lines changed

process_tracker/extract_tracker.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
# Extract Tracking
22
# Used in the creation and editing of extract records. Used in conjunction with process tracking.
33
from datetime import datetime
4-
from os.path import basename, normpath
4+
from os.path import basename, join, normpath
55

66
from process_tracker.data_store import DataStore
77

88
from process_tracker.models.extract import Extract, ExtractProcess, ExtractStatus, Location
99

1010

1111
class ExtractTracker:
12-
# TODO: Add filename/path variable
12+
1313
def __init__(self, process_run, filename, location_path, location_name=None):
1414
"""
1515
ExtractTracker is the primary engine for tracking data extracts
@@ -33,6 +33,7 @@ def __init__(self, process_run, filename, location_path, location_name=None):
3333

3434
self.source = self.process_run.source
3535
self.filename = filename
36+
self.full_filename = join(location_path, filename)
3637

3738
self.location = self.data_store.get_or_create(model=Location
3839
, location_name=location_name

process_tracker/process_tracker.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,3 +253,42 @@ def register_new_process_run(self):
253253
else:
254254
raise Exception('The process %s is currently running.' % self.process_name)
255255
exit()
256+
257+
def set_process_run_low_high_dates(self, low_date=None, high_date=None):
258+
"""
259+
For the given process run, set the process_run_low_date_time and/or process_run_high_date_time.
260+
:param low_date: For the set of data being processed, the lowest datetime tracked. If set multiple times within
261+
a run, will compare the new to old and adjust accordingly.
262+
:type low_date: datetime
263+
:param high_date: For the set of data being processed, the highest datetime tracked.
264+
:type high_date: datetime
265+
:return:
266+
"""
267+
previous_low_date_time = self.process_tracking_run.process_run_low_date_time
268+
previous_high_date_time = self.process_tracking_run.process_run_low_date_time
269+
270+
if low_date is not None and (previous_low_date_time is None or low_date < previous_low_date_time):
271+
self.process_tracking_run.process_run_low_date_time = low_date
272+
273+
if high_date is not None and (previous_high_date_time is None or high_date > previous_high_date_time):
274+
self.process_tracking_run.process_run_high_date_time = high_date
275+
276+
self.session.commit()
277+
278+
def set_process_run_record_count(self, num_records):
279+
"""
280+
For the given process run, set the process_run_record_count for the number of records processed. Will also
281+
update the process' total_record_count - the total number of records ever processed by that process
282+
:param num_records:
283+
:return:
284+
"""
285+
process_run_records = self.process.total_record_count
286+
287+
if process_run_records == 0:
288+
289+
self.process.total_record_count += num_records
290+
else:
291+
self.process.total_record_count = self.process.total_record_count + (num_records - process_run_records)
292+
293+
self.process_tracking_run.process_run_record_count = num_records
294+
self.session.commit()

tests/test_process_tracker.py

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Tests for validating process_tracking works as expected.
22

3-
from datetime import datetime
3+
from datetime import datetime, timedelta
44
import unittest
55

66
from sqlalchemy.orm import Session
@@ -441,3 +441,101 @@ def test_raise_run_error_with_fail(self):
441441
with self.subTest():
442442
self.assertTrue('Process halting. An error triggered the process to fail.' in str(context.exception))
443443

444+
def test_set_run_low_high_dates(self):
445+
"""
446+
Testing that if low and high date are not set, the process_tracking_record low/high dates are set.
447+
:return:
448+
"""
449+
low_date = datetime.now() - timedelta(hours=1)
450+
high_date = datetime.now()
451+
452+
self.process_tracker.set_process_run_low_high_dates(low_date=low_date, high_date=high_date)
453+
454+
given_dates = self.session.query(ProcessTracking.process_run_low_date_time, ProcessTracking.process_run_high_date_time)\
455+
.filter(ProcessTracking.process_tracking_id == self.process_tracker.process_tracking_run.process_tracking_id)
456+
457+
expected_result = [low_date, high_date]
458+
given_result = [given_dates[0].process_run_low_date_time, given_dates[0].process_run_high_date_time]
459+
460+
self.assertEqual(expected_result, given_result)
461+
462+
def test_set_run_low_high_dates_lower_low_date(self):
463+
"""
464+
Testing that if a new low date comes in for a given process_run, set the process_run_low_date_time to the new
465+
low date.
466+
:return:
467+
"""
468+
low_date = datetime.now() - timedelta(hours=1)
469+
lower_low_date = low_date - timedelta(hours=1)
470+
471+
self.process_tracker.set_process_run_low_high_dates(low_date=low_date)
472+
473+
self.process_tracker.set_process_run_low_high_dates(low_date=lower_low_date)
474+
475+
given_dates = self.session.query(ProcessTracking.process_run_low_date_time) \
476+
.filter(ProcessTracking.process_tracking_id == self.process_tracker.process_tracking_run.process_tracking_id)
477+
478+
expected_result = lower_low_date
479+
given_result = given_dates[0].process_run_low_date_time
480+
481+
self.assertEqual(expected_result, given_result)
482+
483+
def test_set_run_low_high_dates_higher_high_date(self):
484+
"""
485+
Testing that if a new low date comes in for a given process_run, set the process_run_low_date_time to the new
486+
low date.
487+
:return:
488+
"""
489+
high_date = datetime.now()
490+
higher_high_date = high_date + timedelta(hours=1)
491+
492+
self.process_tracker.set_process_run_low_high_dates(high_date=high_date)
493+
494+
self.process_tracker.set_process_run_low_high_dates(high_date=higher_high_date)
495+
496+
given_dates = self.session.query(ProcessTracking.process_run_high_date_time) \
497+
.filter(ProcessTracking.process_tracking_id == self.process_tracker.process_tracking_run.process_tracking_id)
498+
499+
expected_result = higher_high_date
500+
given_result = given_dates[0].process_run_high_date_time
501+
502+
self.assertEqual(expected_result, given_result)
503+
504+
def test_set_process_run_record_count(self):
505+
"""
506+
Testing that if record counts are provided for a given process_run, set the process_run_record_count and process'
507+
total_record_counts correctly.
508+
:return:
509+
"""
510+
initial_record_count = 1000
511+
512+
self.process_tracker.set_process_run_record_count(num_records=initial_record_count)
513+
514+
given_counts = self.session.query(ProcessTracking.process_run_record_count, Process.total_record_count) \
515+
.join(Process)\
516+
.filter(ProcessTracking.process_tracking_id == self.process_tracker.process_tracking_run.process_tracking_id)
517+
518+
expected_result = [initial_record_count, initial_record_count]
519+
given_result = [given_counts[0].process_run_record_count, given_counts[0].total_record_count]
520+
521+
self.assertEqual(expected_result, given_result)
522+
523+
def test_set_process_run_record_count_twice(self):
524+
"""
525+
Testing that if record counts get set multiple times, then the process total record count will be set correctly.
526+
:return:
527+
"""
528+
initial_record_count = 1000
529+
modified_record_count = 1500
530+
531+
self.process_tracker.set_process_run_record_count(num_records=initial_record_count)
532+
self.process_tracker.set_process_run_record_count(num_records=modified_record_count)
533+
534+
given_counts = self.session.query(ProcessTracking.process_run_record_count, Process.total_record_count) \
535+
.join(Process)\
536+
.filter(ProcessTracking.process_tracking_id == self.process_tracker.process_tracking_run.process_tracking_id)
537+
538+
expected_result = [modified_record_count, modified_record_count]
539+
given_result = [given_counts[0].process_run_record_count, given_counts[0].total_record_count]
540+
541+
self.assertEqual(expected_result, given_result)

0 commit comments

Comments
 (0)