Skip to content

Commit

Permalink
database:files_db - add actions count view
Browse files Browse the repository at this point in the history
  • Loading branch information
MatteoCampinoti94 committed Dec 6, 2023
1 parent 22545fa commit 77e5e18
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
35 changes: 35 additions & 0 deletions acacore/database/files_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from acacore.models.file import File
from acacore.models.history import HistoryEntry
from acacore.models.metadata import Metadata
from acacore.models.reference_files import TActionType
from acacore.utils.functions import or_none

from .base import Column
Expand All @@ -32,6 +33,11 @@ class ChecksumCount(ACABase):
count: int


class ActionCount(ACABase):
action: TActionType
count: int


class FileDB(FileDBBase):
def __init__(
self,
Expand Down Expand Up @@ -154,6 +160,34 @@ def __init__(
),
],
)
self.actions_count = self.create_view(
"_ActionsCount",
self.files,
ActionCount,
None,
[
Column("action", "varchar", str, str, False, False, False),
],
[
(Column("count", "int", str, str), "DESC"),
],
select_columns=[
Column(
"action",
"varchar",
or_none(str),
or_none(str),
False,
False,
False,
),
SelectColumn(
f'count("{self.files.name}.action")',
int,
"count",
),
],
)

def init(self):
"""Create the default tables and views."""
Expand All @@ -163,6 +197,7 @@ def init(self):
self.identification_warnings.create(True)
self.checksum_count.create(True)
self.signature_count.create(True)
self.actions_count.create(True)
self.metadata.update(self.metadata.model())
self.commit()

Expand Down
4 changes: 4 additions & 0 deletions tests/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

# noinspection PyProtectedMember
from acacore.database.column import _value_to_sql
from acacore.database.files_db import ActionCount
from acacore.database.files_db import ChecksumCount
from acacore.database.files_db import SignatureCount
from acacore.models.file import File
Expand Down Expand Up @@ -75,6 +76,8 @@ def test_database_classes(database_path: Path):
assert issubclass(db.checksum_count.model, ChecksumCount)
assert isinstance(db.signature_count, ModelView)
assert issubclass(db.signature_count.model, SignatureCount)
assert isinstance(db.actions_count, ModelView)
assert issubclass(db.actions_count.model, ActionCount)


# noinspection SqlResolve,SqlNoDataSourceInspection
Expand Down Expand Up @@ -102,6 +105,7 @@ def test_database_tables(database_path: Path):
assert db.identification_warnings.name in views
assert db.checksum_count.name in views
assert db.signature_count.name in views
assert db.actions_count.name in views


def test_database_columns(database_path: Path):
Expand Down

0 comments on commit 77e5e18

Please sign in to comment.