6
0
Selaa lähdekoodia

improved performance of getting labels from the DB

Dimitri Korsch 3 vuotta sitten
vanhempi
commit
ed9875a667

+ 1 - 1
pycs/database/Label.py

@@ -68,7 +68,7 @@ class Label(NamedBaseModel):
         "parent_id",
         "reference",
         "hierarchy_level",
-        "children",
+        # "children",
     )
 
     @commit_on_return

+ 0 - 51
pycs/database/Project.py

@@ -1,7 +1,6 @@
 import os
 import shutil
 import typing as T
-import warnings
 
 from datetime import datetime
 
@@ -120,56 +119,6 @@ class Project(NamedBaseModel):
         return self.files.filter(File.id == identifier).one_or_none()
 
 
-    def label_tree(self) -> T.List[Label]:
-        """
-        get a list of root labels associated with this project
-
-        :return: list of labels
-        """
-        warnings.warn("Check performance of this method!")
-        # pylint: disable=no-member
-        return self.labels.filter(Label.parent_id.is_(None)).all()
-
-
-    def label_tree_original(self):
-        """
-        get a list of root labels associated with this project
-
-        :return: list of labels
-        """
-        raise NotImplementedError
-        # pylint: disable=unreachable
-        # pylint: disable=pointless-string-statement
-        """
-        with closing(self.database.con.cursor()) as cursor:
-            cursor.execute('''
-                WITH RECURSIVE
-                    tree AS (
-                        SELECT labels.* FROM labels
-                            WHERE project = ? AND parent IS NULL
-                        UNION ALL
-                        SELECT labels.* FROM labels
-                            JOIN tree ON labels.parent = tree.id
-                    )
-                SELECT * FROM tree
-            ''', [self.id])
-
-            result = []
-            lookup = {}
-
-            for row in cursor.fetchall():
-                label = TreeNodeLabel(self.database, row)
-                lookup[label.id] = label
-
-                if label.parent_id is None:
-                    result.append(label)
-                else:
-                    lookup[label.parent_id].children.append(label)
-
-            return result
-        """
-
-
     def collection(self, identifier: int) -> T.Optional[Collection]:
         """
         get a collection using its unique identifier

+ 0 - 8
pycs/database/util/TreeNodeLabel.py

@@ -1,8 +0,0 @@
-from pycs.database.Label import Label
-
-
-class TreeNodeLabel(Label):
-    """ Extends the TreeNodeLabel class with a children attribute """
-    def __init__(self, database, row):
-        super().__init__(database, row)
-        self.children = []

+ 16 - 2
pycs/frontend/endpoints/labels/ListLabelTree.py

@@ -16,5 +16,19 @@ class ListLabelTree(View):
         # find project
         project = Project.get_or_404(project_id)
 
-        # return labels
-        return jsonify(project.label_tree())
+        labels = [lab.serialize() for lab in project.labels.all()]
+
+        lookup = {}
+        for label in labels:
+            label["children"] = []
+            lookup[label["id"]] = label
+
+        tree = []
+        for label in labels:
+            parent_id = label["parent_id"]
+            if parent_id is None:
+                tree.append(label)
+            else:
+                lookup[parent_id]["children"].append(label)
+
+        return jsonify(tree)

+ 3 - 1
pycs/util/PipelineCache.py

@@ -29,7 +29,9 @@ class PipelineCache:
         self.__lock = Lock()
 
         self._cache_time = cache_time or self.CLOSE_TIMER
-        app.logger.info(f"Initialized Pipeline cache (pipelines are closed after {self._cache_time:.3f} sec)")
+        msg = ("Initialized Pipeline cache "
+            f"(pipelines are closed after {self._cache_time:.3f} sec)")
+        app.logger.info(msg)
 
     def start(self):
         """ starts the main worker method """

+ 16 - 2
tests/client/label_tests.py

@@ -89,15 +89,28 @@ class LabelListTests(_BaseLabelTests):
         super().setupModels()
 
         for i in range(1, 11):
-            parent, is_new = self.project.create_label(name=f"Label{i}", reference=f"label{i}")
+            parent, is_new = self.project.create_label(
+                name=f"Label{i}",
+                reference=f"label{i}",
+                hierarchy_level="Level1",
+            )
             self.assertTrue(is_new)
 
             for j in range(1, 4):
                 label, is_new = self.project.create_label(
-                    name=f"Label{i}_{j}", reference=f"label{i}_{j}",
+                    name=f"Label{i}_{j}",
+                    reference=f"label{i}_{j}",
+                    hierarchy_level="Level2",
                     parent=parent)
                 self.assertTrue(is_new)
 
+                for k in range(1, 4):
+                    final_label, is_new = self.project.create_label(
+                        name=f"Label{i}_{j}_{k}",
+                        reference=f"label{i}_{j}_{k}",
+                        parent=label)
+                    self.assertTrue(is_new)
+
     def test_list_labels(self):
         self.get(url_for("list_labels", project_id=4242), status_code=404)
 
@@ -125,6 +138,7 @@ class LabelListTests(_BaseLabelTests):
 
         self.assertEqual(len(root_labels), len(content))
 
+        Label.serialize_only += ("children",)
         for entry in content:
             label = root_labels[entry["id"]]
             self.assertDictEqual(label.serialize(), entry)