Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • workers/base-worker
1 result
Show changes
Commits on Source (2)
......@@ -10,11 +10,13 @@ from arkindex_worker import logger
class Reporter(object):
def __init__(self, name):
# TODO: use real data from workers
def __init__(
self, name="Unknown worker", slug="unknown-slug", version=None, **kwargs
):
self.report_data = {
"slug": name,
"version": "0.0",
"name": name,
"slug": slug,
"version": version,
"started": datetime.utcnow().isoformat(),
"elements": {},
}
......
......@@ -39,9 +39,6 @@ class ElementsWorker(
def __init__(self, description="Arkindex Elements Worker", support_cache=False):
super().__init__(description, support_cache)
# Add report concerning elements
self.report = Reporter("unknown worker")
# Add mandatory argument to process elements
self.parser.add_argument(
"--elements-list",
......@@ -93,6 +90,13 @@ class ElementsWorker(
), "Worker must be configured to access its process activity state"
return self.process_information.get("activity_state") == "ready"
def configure(self):
super().configure()
# Add report concerning elements
self.report = Reporter(
**self.worker_details, version=getattr(self, "worker_version_id", None)
)
def run(self):
"""
Process every elements from the provided list
......
......@@ -145,16 +145,19 @@ class BaseWorker(object):
f"Loaded worker {worker_version['worker']['name']} revision {worker_version['revision']['hash'][0:7]} from API"
)
self.config = worker_version["configuration"]["configuration"]
self.worker_details = worker_version["worker"]
required_secrets = worker_version["configuration"].get("secrets", [])
elif self.args.config:
# Load config from YAML file
self.config = yaml.safe_load(self.args.config)
self.worker_details = {"name": "Local worker"}
required_secrets = self.config.get("secrets", [])
logger.info(
f"Running with local configuration from {self.args.config.name}"
)
else:
self.config = {}
self.worker_details = {}
required_secrets = []
logger.warning("Running without any extra configuration")
......
......@@ -188,12 +188,11 @@ class ElementMixin(object):
def list_element_children(
self,
element,
best_class=None,
folder=None,
name=None,
recursive=None,
type=None,
with_best_classes=None,
with_classes=None,
with_corpus=None,
with_has_children=None,
with_zone=None,
......@@ -206,11 +205,6 @@ class ElementMixin(object):
element, (Element, CachedElement)
), "element shouldn't be null and should be an Element or CachedElement"
query_params = {}
if best_class is not None:
assert isinstance(best_class, str) or isinstance(
best_class, bool
), "best_class should be of type str or bool"
query_params["best_class"] = best_class
if folder is not None:
assert isinstance(folder, bool), "folder should be of type bool"
query_params["folder"] = folder
......@@ -223,11 +217,9 @@ class ElementMixin(object):
if type:
assert isinstance(type, str), "type should be of type str"
query_params["type"] = type
if with_best_classes is not None:
assert isinstance(
with_best_classes, bool
), "with_best_classes should be of type bool"
query_params["with_best_classes"] = with_best_classes
if with_classes is not None:
assert isinstance(with_classes, bool), "with_classes should be of type bool"
query_params["with_classes"] = with_classes
if with_corpus is not None:
assert isinstance(with_corpus, bool), "with_corpus should be of type bool"
query_params["with_corpus"] = with_corpus
......
......@@ -976,17 +976,6 @@ def test_list_element_children_wrong_element(mock_elements_worker):
)
def test_list_element_children_wrong_best_class(mock_elements_worker):
elt = Element({"id": "12341234-1234-1234-1234-123412341234"})
with pytest.raises(AssertionError) as e:
mock_elements_worker.list_element_children(
element=elt,
best_class=1234,
)
assert str(e.value) == "best_class should be of type str or bool"
def test_list_element_children_wrong_folder(mock_elements_worker):
elt = Element({"id": "12341234-1234-1234-1234-123412341234"})
......@@ -1031,15 +1020,15 @@ def test_list_element_children_wrong_type(mock_elements_worker):
assert str(e.value) == "type should be of type str"
def test_list_element_children_wrong_with_best_classes(mock_elements_worker):
def test_list_element_children_wrong_with_classes(mock_elements_worker):
elt = Element({"id": "12341234-1234-1234-1234-123412341234"})
with pytest.raises(AssertionError) as e:
mock_elements_worker.list_element_children(
element=elt,
with_best_classes="not bool",
with_classes="not bool",
)
assert str(e.value) == "with_best_classes should be of type bool"
assert str(e.value) == "with_classes should be of type bool"
def test_list_element_children_wrong_with_corpus(mock_elements_worker):
......
# -*- coding: utf-8 -*-
import json
import uuid
from datetime import datetime
from tempfile import NamedTemporaryFile
import pytest
from apistar.exceptions import ErrorResponse
......@@ -6,10 +11,16 @@ from arkindex_worker.reporting import Reporter
def test_init():
reporter = Reporter("worker")
version_id = str(uuid.uuid4())
reporter = Reporter(name="Worker", slug="worker-slug", version=version_id)
assert "started" in reporter.report_data
del reporter.report_data["started"]
assert reporter.report_data == {"slug": "worker", "version": "0.0", "elements": {}}
assert reporter.report_data == {
"name": "Worker",
"slug": "worker-slug",
"version": version_id,
"elements": {},
}
def test_process():
......@@ -213,3 +224,32 @@ def test_error():
"content": '{"coffee": "Can\'t touch this"}',
},
]
def test_reporter_save(mocker):
datetime_mock = mocker.MagicMock()
datetime_mock.utcnow.return_value = datetime(2000, 1, 1)
mocker.patch("arkindex_worker.reporting.datetime", datetime_mock)
version_id = str(uuid.uuid4())
reporter = Reporter(name="Worker", slug="worker-slug", version=version_id)
reporter.add_element("myelement", type="text_line", type_count=4)
with NamedTemporaryFile() as f:
reporter.save(f.name)
exported_data = json.load(f)
assert exported_data == {
"name": "Worker",
"slug": "worker-slug",
"started": "2000-01-01T00:00:00",
"version": version_id,
"elements": {
"myelement": {
"classifications": {},
"elements": {"text_line": 4},
"entities": [],
"errors": [],
"metadata": [],
"started": "2000-01-01T00:00:00",
"transcriptions": 0,
}
},
}