add model for save errors of parse snapshots

This commit is contained in:
Cayo Puigdefabregas 2022-04-04 13:27:04 +02:00
parent 77a35ec1a3
commit cb5fb14c92
4 changed files with 45 additions and 6 deletions

View File

@ -0,0 +1,22 @@
from citext import CIText
from sqlalchemy import BigInteger, Column, Sequence, SmallInteger
from sqlalchemy.dialects.postgresql import UUID
from ereuse_devicehub.db import db
from ereuse_devicehub.resources.enums import Severity
from ereuse_devicehub.resources.models import Thing
class SnapshotErrors(Thing):
"""A Snapshot errors."""
id = Column(BigInteger, Sequence('snapshot_errors_seq'), primary_key=True)
description = Column(CIText(), default='', nullable=False)
severity = Column(SmallInteger, default=Severity.Info, nullable=False)
snapshot_uuid = Column(UUID(as_uuid=True), nullable=False)
def save(self, commit=False):
db.session.add(self)
if commit:
db.session.commit()

View File

@ -6,6 +6,8 @@ from dmidecode import DMIParse
from ereuse_devicehub.parser import base2 from ereuse_devicehub.parser import base2
from ereuse_devicehub.parser.computer import Computer from ereuse_devicehub.parser.computer import Computer
from ereuse_devicehub.parser.models import SnapshotErrors
from ereuse_devicehub.resources.enums import Severity
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -435,7 +437,7 @@ class ParseSnapshotLsHw:
try: try:
uuid.UUID(uuid) uuid.UUID(uuid)
except AttributeError as err: except AttributeError as err:
self.errors(err) self.errors("{}".format(err))
txt = "Error: Snapshot: {uuid} have this uuid: {device}".format( txt = "Error: Snapshot: {uuid} have this uuid: {device}".format(
uuid=self.uuid, device=uuid uuid=self.uuid, device=uuid
) )
@ -481,7 +483,7 @@ class ParseSnapshotLsHw:
self.DataStorageInterface(interface.upper()) self.DataStorageInterface(interface.upper())
except ValueError as err: except ValueError as err:
txt = "interface {} is not in DataStorageInterface Enum".format(interface) txt = "interface {} is not in DataStorageInterface Enum".format(interface)
self.errors(err) self.errors("{}".format(err))
self.errors(txt) self.errors(txt)
return "ATA" return "ATA"
@ -519,9 +521,13 @@ class ParseSnapshotLsHw:
return action return action
def errors(self, txt=None): def errors(self, txt=None, severity=Severity.Info):
if not txt: if not txt:
return self._errors return self._errors
logger.error(txt) logger.error(txt)
self._errors.append(txt) self._errors.append(txt)
error = SnapshotErrors(
description=txt, snapshot_uuid=self.uuid, severity=severity
)
error.save()

View File

@ -7,12 +7,14 @@ from datetime import datetime
from flask import current_app as app from flask import current_app as app
from flask import g from flask import g
from marshmallow import ValidationError
from sqlalchemy.util import OrderedSet from sqlalchemy.util import OrderedSet
from ereuse_devicehub.db import db from ereuse_devicehub.db import db
from ereuse_devicehub.parser.models import SnapshotErrors
from ereuse_devicehub.parser.parser import ParseSnapshotLsHw from ereuse_devicehub.parser.parser import ParseSnapshotLsHw
from ereuse_devicehub.resources.action.models import Snapshot
from ereuse_devicehub.parser.schemas import Snapshot_lite from ereuse_devicehub.parser.schemas import Snapshot_lite
from ereuse_devicehub.resources.action.models import Snapshot
from ereuse_devicehub.resources.device.models import Computer from ereuse_devicehub.resources.device.models import Computer
from ereuse_devicehub.resources.enums import Severity, SnapshotSoftware from ereuse_devicehub.resources.enums import Severity, SnapshotSoftware
from ereuse_devicehub.resources.user.exceptions import InsufficientPermission from ereuse_devicehub.resources.user.exceptions import InsufficientPermission
@ -82,7 +84,17 @@ class SnapshotView:
self.validate_json(snapshot_json) self.validate_json(snapshot_json)
snapshot_json = self.build_lite() snapshot_json = self.build_lite()
self.snapshot_json = resource_def.schema.load(snapshot_json) try:
self.snapshot_json = resource_def.schema.load(snapshot_json)
except ValidationError as err:
txt = "{}".format(err)
uuid = snapshot_json.get('uuid')
error = SnapshotErrors(
description=txt, snapshot_uuid=uuid, severity=Severity.Error
)
error.save(commit=True)
raise err
self.response = self.build() self.response = self.build()
move_json(self.tmp_snapshots, self.path_snapshot, g.user.email) move_json(self.tmp_snapshots, self.path_snapshot, g.user.email)

File diff suppressed because one or more lines are too long