Skip to content

Commit

Permalink
[app][feat] check service enabled first
Browse files Browse the repository at this point in the history
  • Loading branch information
M3ssman committed Apr 9, 2024
1 parent 8f0c162 commit 2820348
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
5 changes: 3 additions & 2 deletions lib/ocr_step.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,16 +351,17 @@ def __init__(self, params: Dict):
self.n_shorts = 0
self.n_lines_out = 0

def is_available(self):
def enabled(self):
"""Connection established ?"""

try:
requests.head(self.service_url)
except requests.ConnectionError:
return False
return True

def execute(self):
if not self.enabled():
return
xml_data = ET.parse(self.path_in)
self.lines = get_lines(xml_data)
if len(self.lines) > 0:
Expand Down
6 changes: 5 additions & 1 deletion ocr_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,11 @@ def _execute_pipeline(*args):
# log current step
if hasattr(step, 'statistics') and len(step.statistics) > 0:
if profile_result and isinstance(step, StepEstimateOCR):
outcome = (file_name,) + step.statistics
_qa_step: StepEstimateOCR = step
if not _qa_step.enabled():
pipeline.logger.warning("[%s] %s configured but disabled",
file_name, _qa_step.__class__.__name__)
outcome = (file_name,) + _qa_step.statistics
pipeline.logger.info("[%s] %s, statistics: %s",
file_name, profile_result,
str(step.statistics))
Expand Down
13 changes: 9 additions & 4 deletions tests/test_steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,17 +439,22 @@ def test_step_estimateocr_empty_alto(empty_ocr):


@mock.patch("requests.head")
def test_service_down(mock_requests):
def test_service_down(mock_head):
"""Determine Behavior when url not accessible"""

# arrange
params = {'service_url': 'http://localhost:8010/v2/check'}
step = StepEstimateOCR(params)
mock_requests.side_effect = requests.ConnectionError
mock_head.side_effect = requests.ConnectionError

# act
step.execute()

# assert
assert not step.is_available()
assert mock_requests.called == 1
assert mock_head.called == 1
assert not step.enabled()
assert step.hit_ratio == -1
assert step.statistics[0] == -1


def test_step_estimateocr_textline_conversions():
Expand Down

0 comments on commit 2820348

Please sign in to comment.