Skip to content

Commit be8bf6d

Browse files
committed
add constants and refactor dq_reporter.py
1 parent 18f369d commit be8bf6d

File tree

10 files changed

+58
-77
lines changed

10 files changed

+58
-77
lines changed

lambdas/shared/src/common/validator/enums/__init__.py renamed to lambdas/shared/src/common/validator/constants/__init__.py

File renamed without changes.

lambdas/shared/src/common/validator/enums/exception_messages.py renamed to lambdas/shared/src/common/validator/constants/enums.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
1-
# all exceptions and messgaes
21
from enum import IntEnum
2+
from enum import StrEnum
3+
4+
5+
# Used for error report messages in DQ Reporter to categorize error levels
6+
class ErrorLevels(IntEnum):
7+
CRITICAL_ERROR = 0
8+
WARNING = 1
9+
NOTIFICATION = 2
10+
11+
12+
MESSAGES = {
13+
ErrorLevels.CRITICAL_ERROR: "Critical Validation Error [%s]: %s",
14+
ErrorLevels.WARNING: "Non-Critical Validation Error [%s]: %s",
15+
ErrorLevels.NOTIFICATION: "Quality Issue Found [%s]: %s",
16+
}
17+
18+
19+
# Constants for error report messages used in Expression Checker
20+
class MessageLabel(StrEnum):
21+
EXPECTED_LABEL = "Expected- "
22+
FOUND_LABEL = "Found- "
23+
VALUE_MISMATCH_MSG = "Value does not equal expected value, "
324

425

526
class ExceptionLevels(IntEnum):
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error_report = {
2+
"eventId": "",
3+
"validationDate": "",
4+
"validated": "true",
5+
"results": {
6+
"totalErrors": 0,
7+
"completeness": {"errors": 0, "fields": []},
8+
"consistency": {"errors": 0, "fields": []},
9+
"validity": {"errors": 0, "fields": []},
10+
"timeliness_processed": 0,
11+
},
12+
}

lambdas/shared/src/common/validator/enums/error_levels.py

Lines changed: 0 additions & 15 deletions
This file was deleted.

lambdas/shared/src/common/validator/expression_checker.py

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,16 @@
22
import datetime
33
import re
44
import uuid
5-
from enum import Enum
65
from enum import StrEnum
76

8-
from common.validator.enums.exception_messages import MESSAGES
9-
from common.validator.enums.exception_messages import ExceptionLevels
7+
from common.validator.constants.enums import MESSAGES
8+
from common.validator.constants.enums import ExceptionLevels
109
from common.validator.lookup_expressions.key_data import KeyData
1110
from common.validator.lookup_expressions.lookup_data import LookUpData
1211
from common.validator.record_error import ErrorReport
1312
from common.validator.record_error import RecordError
1413

1514

16-
class ExpressionType(Enum):
17-
DATETIME = "DATETIME"
18-
DATE = "DATE"
19-
UUID = "UUID"
20-
INT = "INT"
21-
FLOAT = "FLOAT"
22-
REGEX = "REGEX"
23-
EQUAL = "EQUAL"
24-
NOTEQUAL = "NOTEQUAL"
25-
IN = "IN"
26-
NRANGE = "NRANGE"
27-
INARRAY = "INARRAY"
28-
UPPER = "UPPER"
29-
LOWER = "LOWER"
30-
LENGTH = "LENGTH"
31-
STARTSWITH = "STARTSWITH"
32-
ENDSWITH = "ENDSWITH"
33-
EMPTY = "EMPTY"
34-
NOTEMPTY = "NOTEMPTY"
35-
POSITIVE = "POSITIVE"
36-
GENDER = "GENDER"
37-
NHSNUMBER = "NHSNUMBER"
38-
MAXOBJECTS = "MAXOBJECTS"
39-
POSTCODE = "POSTCODE"
40-
ONLYIF = "ONLYIF"
41-
LOOKUP = "LOOKUP"
42-
KEYCHECK = "KEYCHECK"
43-
44-
4515
class MessageLabel(StrEnum):
4616
EXPECTED_LABEL = "Expected- "
4717
FOUND_LABEL = "Found- "

lambdas/shared/src/common/validator/reporter/dq_reporter.py

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,31 @@
33

44
from dateutil import parser
55

6-
from common.validator.enums.error_levels import ErrorLevels
6+
from common.validator.constants.enums import ErrorLevels
7+
from common.validator.constants.error_report import error_report
78
from common.validator.record_error import ErrorReport
89

910

1011
class DQReporter:
12+
"""
13+
Generates error reports based on validation results.
14+
It uses cumulates all error records, assigns them to the appropriate error levels,
15+
checks the time difference between event occurrence and validation time,
16+
and builds a structured error report in JSON format.
17+
"""
18+
1119
def __init__(self):
1220
# parser variables
13-
self.error_report = {
14-
"eventId": "",
15-
"validationDate": "",
16-
"validated": "true",
17-
"results": {
18-
"totalErrors": 0,
19-
"completeness": {"errors": 0, "fields": []},
20-
"consistency": {"errors": 0, "fields": []},
21-
"validity": {"errors": 0, "fields": []},
22-
"timeliness_processed": 0,
23-
},
24-
}
21+
self.error_report = error_report
2522

2623
# create the date difference for the report in minutes
27-
def diff_dates(self, date1, date2):
28-
diff_seconds = abs(date2 - date1).total_seconds()
24+
def diff_dates(self, fhir_event_date, current_date):
25+
diff_seconds = abs(current_date - fhir_event_date).total_seconds()
2926
diff_minutes = diff_seconds / 60
3027
return diff_minutes
3128

32-
def generate_error_report(self, event_id, occurrence, error_records: list[ErrorReport]):
33-
occurrence_date = occurrence
34-
occurrence_date = parser.parse(occurrence_date, ignoretz=True)
29+
def generate_error_report(self, event_id, occurrence_date_time, error_records: list[ErrorReport]):
30+
occurrence_date = parser.parse(occurrence_date_time, ignoretz=True)
3531
validation_date = datetime.datetime.now(tz=None)
3632

3733
time_taken = self.diff_dates(occurrence_date, validation_date)

lambdas/shared/src/common/validator/validator.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
from enum import Enum
88

9-
from common.validator.enums.error_levels import ErrorLevels
10-
from common.validator.enums.exception_messages import MESSAGES
11-
from common.validator.enums.exception_messages import ExceptionLevels
9+
from common.validator.constants.enums import MESSAGES
10+
from common.validator.constants.enums import ErrorLevels
11+
from common.validator.constants.enums import ExceptionLevels
1212
from common.validator.expression_checker import ExpressionChecker
1313
from common.validator.parsers.csv_line_parser import CSVLineParser
1414
from common.validator.parsers.csv_parser import CSVParser

lambdas/shared/tests/test_common/validator/test_expression_checker.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from unittest.mock import MagicMock
33
from unittest.mock import patch
44

5-
from common.validator.enums.exception_messages import ExceptionLevels
5+
from common.validator.constants.enums import ExceptionLevels
66
from common.validator.expression_checker import ExpressionChecker
77
from common.validator.record_error import ErrorReport
88

@@ -31,8 +31,7 @@ def make_checker(self, mock_data=None, summarise=False, report=True):
3131
"""Helper to create an ExpressionChecker with mock parser data."""
3232
return ExpressionChecker(MockParser(mock_data), summarise, report)
3333

34-
# --- DATETIME & UUID -------------------------------------------------
35-
34+
# Date Time Check
3635
def test_datetime_valid(self):
3736
"""Valid ISO date should pass without error."""
3837
checker = self.make_checker({"date_field": "2025-01-01"})
@@ -52,8 +51,7 @@ def test_uuid_valid_and_invalid(self):
5251
self.assertIsNone(checker.validate_expression("UUID", None, "uuid_field", valid_uuid, 1))
5352
self.assertIsInstance(checker.validate_expression("UUID", None, "uuid_field", "not-a-uuid", 1), ErrorReport)
5453

55-
# --- NUMERIC, LENGTH, REGEX ------------------------------------------
56-
54+
# Numeric Length and Regex
5755
def test_integer_length_and_regex_rules(self):
5856
"""Test integer, length, and regex-based validations."""
5957
checker = self.make_checker()
@@ -67,8 +65,7 @@ def test_integer_length_and_regex_rules(self):
6765
# REGEX mismatch -> Error
6866
self.assertIsInstance(checker.validate_expression("REGEX", r"^abc$", "regex_field", "abcd", 1), ErrorReport)
6967

70-
# --- CASE & STRING POSITION RULES ------------------------------------
71-
68+
# Case & String Position Rules
7269
def test_upper_lower_startswith_endswith_rules(self):
7370
"""Validate case and string boundary conditions."""
7471
checker = self.make_checker()

lambdas/shared/tests/test_common/validator/test_parser.py renamed to lambdas/shared/tests/test_common/validator/test_fhir_parser.py

File renamed without changes.

lambdas/shared/tests/test_common/validator/test_validator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from unittest.mock import MagicMock
33
from unittest.mock import patch
44

5-
from common.validator.enums.error_levels import ErrorLevels
5+
from common.validator.constants.enums import ErrorLevels
66
from common.validator.record_error import ErrorReport
77
from common.validator.validator import DataType
88
from common.validator.validator import Validator

0 commit comments

Comments
 (0)