fix parsing

This commit is contained in:
Cayo Puigdefabregas 2023-07-12 14:18:11 +02:00
parent 89109bd1b4
commit a7f9b8b725
2 changed files with 74 additions and 60 deletions

View File

@ -39,7 +39,7 @@ from ereuse_devicehub.inventory.models import (
TransferCustomerDetails, TransferCustomerDetails,
) )
from ereuse_devicehub.parser.models import PlaceholdersLog, SnapshotsLog from ereuse_devicehub.parser.models import PlaceholdersLog, SnapshotsLog
from ereuse_devicehub.parser.parser import ParseSnapshot, ParseSnapshotLsHw from ereuse_devicehub.parser.parser import ParseSnapshot # , ParseSnapshotLsHw
from ereuse_devicehub.parser.schemas import Snapshot_lite from ereuse_devicehub.parser.schemas import Snapshot_lite
from ereuse_devicehub.resources.action.models import Snapshot, Trade from ereuse_devicehub.resources.action.models import Snapshot, Trade
from ereuse_devicehub.resources.action.schemas import Snapshot as SnapshotSchema from ereuse_devicehub.resources.action.schemas import Snapshot as SnapshotSchema
@ -349,7 +349,6 @@ class UploadSnapshotForm(SnapshotMixin, FlaskForm):
self.get_fields_extra(debug, snapshot_json) self.get_fields_extra(debug, snapshot_json)
try: try:
# import pdb; pdb.set_trace()
snapshot_json = schema.load(snapshot_json) snapshot_json = schema.load(snapshot_json)
response = self.build( response = self.build(
snapshot_json, create_new_device=self.create_new_devices snapshot_json, create_new_device=self.create_new_devices

View File

@ -1,13 +1,15 @@
import json import json
import logging import logging
import uuid import uuid
from datetime import datetime
import numpy import numpy
from dmidecode import DMIParse from dmidecode import DMIParse
from flask import request from flask import request
from marshmallow.exceptions import ValidationError from marshmallow.exceptions import ValidationError
from ereuse_devicehub.parser import base2 from ereuse_devicehub.ereuse_utils.nested_lookup import get_nested_dicts_with_key_value
from ereuse_devicehub.parser import base2, unit
from ereuse_devicehub.parser.computer import Computer from ereuse_devicehub.parser.computer import Computer
from ereuse_devicehub.parser.models import SnapshotsLog from ereuse_devicehub.parser.models import SnapshotsLog
from ereuse_devicehub.resources.action.schemas import Snapshot from ereuse_devicehub.resources.action.schemas import Snapshot
@ -103,7 +105,7 @@ class ParseSnapshot:
"speed": self.get_ram_speed(ram), "speed": self.get_ram_speed(ram),
"manufacturer": ram.get("Manufacturer", self.default), "manufacturer": ram.get("Manufacturer", self.default),
"serialNumber": ram.get("Serial Number", self.default), "serialNumber": ram.get("Serial Number", self.default),
"interface": self.get_ram_type(ram), "interface": ram.get("Type", "DDR"),
"format": ram.get("Form Factor", "DIMM"), "format": ram.get("Form Factor", "DIMM"),
"model": ram.get("Part Number", self.default), "model": ram.get("Part Number", self.default),
} }
@ -131,19 +133,28 @@ class ParseSnapshot:
) )
def get_graphic(self): def get_graphic(self):
for ch in self.lshw.get('children', []): nodes = get_nested_dicts_with_key_value(self.lshw, 'class', 'display')
for c in ch.get('children', []): for c in nodes:
if c['class'] != 'display': if not c['configuration'].get('driver', None):
continue continue
self.components.append(
{ self.components.append(
"actions": [], {
"type": "GraphicCard", "actions": [],
"manufacturer": c.get("vendor", self.default), "type": "GraphicCard",
"model": c.get("product", self.default), "memory": self.get_memory_video(c),
"serialNumber": c.get("serial", self.default), "manufacturer": c.get("vendor", self.default),
} "model": c.get("product", self.default),
) "serialNumber": c.get("serial", self.default),
}
)
def get_memory_video(self, c):
# get info of lspci
# pci_id = c['businfo'].split('@')[1]
# lspci.get(pci_id) | grep size
# lspci -v -s 00:02.0
return None
def get_data_storage(self): def get_data_storage(self):
for sm in self.smart: for sm in self.smart:
@ -170,41 +181,39 @@ class ParseSnapshot:
) )
def get_networks(self): def get_networks(self):
for ch in self.lshw.get('children', []): nodes = get_nested_dicts_with_key_value(self.lshw, 'class', 'network')
for c in ch.get('children', []): for c in nodes:
if c['class'] == 'networks': capacity = c.get('capacity')
capacity = c.get('capacity') units = c.get('units')
units = c.get('units') speed = None
speed = None if capacity and units:
if capacity and units: speed = unit.Quantity(capacity, units).to('Mbit/s').m
speed = base2.unit.Quantity(capacity, units).to('Mbit/s').m wireless = bool(c.get('configuration', {}).get('wireless', False))
wireless = bool(c.get('configuration', {}).get('wireless', False)) self.components.append(
self.components.append( {
{ "actions": [],
"actions": [], "type": "NetworkAdapter",
"type": "NetworkAdapter", "model": c.get('product'),
"model": c.get('product'), "manufacturer": c.get('vendor'),
"manufacturer": c.get('vendor'), "serialNumber": c.get('serial'),
"serialNumber": c.get('serial'), "speed": speed,
"speed": speed, "variant": c.get('version', 1),
"variant": c.get('version', 1), "wireless": wireless,
"wireless": wireless, }
} )
)
def get_sound_card(self): def get_sound_card(self):
for ch in self.lshw.get('children', []): nodes = get_nested_dicts_with_key_value(self.lshw, 'class', 'multimedia')
for c in ch.get('children', []): for c in nodes:
if c['class'] == 'multimedia': self.components.append(
self.components.append( {
{ "actions": [],
"actions": [], "type": "SoundCard",
"type": "SoundCard", "model": c.get('product'),
"model": c.get('product'), "manufacturer": c.get('vendor'),
"manufacturer": c.get('vendor'), "serialNumber": c.get('serial'),
"serialNumber": c.get('serial'), }
} )
)
def get_display(self): # noqa: C901 def get_display(self): # noqa: C901
TECHS = 'CRT', 'TFT', 'LED', 'PDP', 'LCD', 'OLED', 'AMOLED' TECHS = 'CRT', 'TFT', 'LED', 'PDP', 'LCD', 'OLED', 'AMOLED'
@ -212,6 +221,7 @@ class ParseSnapshot:
for c in self.monitors: for c in self.monitors:
resolution_width, resolution_height = (None,) * 2 resolution_width, resolution_height = (None,) * 2
refresh, serial, model, manufacturer, size = (None,) * 5 refresh, serial, model, manufacturer, size = (None,) * 5
year, week, production_date = (None,) * 3
for x in c: for x in c:
if "Vendor: " in x: if "Vendor: " in x:
@ -231,10 +241,20 @@ class ParseSnapshot:
refresh = int(float(x.split(',')[-1].strip()[:-3])) refresh = int(float(x.split(',')[-1].strip()[:-3]))
except Exception: except Exception:
pass pass
if 'Year of Manufacture' in x:
year = x.split(': ')[1]
if 'Week of Manufacture' in x:
week = x.split(': ')[1]
if "Size: " in x: if "Size: " in x:
size = self.get_size_monitor(x) size = self.get_size_monitor(x)
technology = next((t for t in TECHS if t in c[0]), None) technology = next((t for t in TECHS if t in c[0]), None)
if year and week:
d = '{} {} 0'.format(year, week)
production_date = datetime.strptime(d, '%Y %W %w').isoformat()
self.components.append( self.components.append(
{ {
"actions": [], "actions": [],
@ -243,10 +263,11 @@ class ParseSnapshot:
"manufacturer": manufacturer, "manufacturer": manufacturer,
"serialNumber": serial, "serialNumber": serial,
'size': size, 'size': size,
'resolution_width': resolution_width, 'resolutionWidth': resolution_width,
'resolution_height': resolution_height, 'resolutionHeight': resolution_height,
"productionDate": production_date,
'technology': technology, 'technology': technology,
'refresh_rate': refresh, 'refreshRate': refresh,
} }
) )
@ -352,12 +373,6 @@ class ParseSnapshot:
size = ram.get("Speed", "0") size = ram.get("Speed", "0")
return int(size.split(" ")[0]) return int(size.split(" ")[0])
def get_ram_type(self, ram):
TYPES = {'ddr', 'sdram', 'sodimm'}
for t in TYPES:
if t in ram.get("Type", "DDR"):
return t
def get_cpu_speed(self, cpu): def get_cpu_speed(self, cpu):
speed = cpu.get('Max Speed', "0") speed = cpu.get('Max Speed', "0")
return float(speed.split(" ")[0]) / 1024 return float(speed.split(" ")[0]) / 1024
@ -665,7 +680,7 @@ class ParseSnapshotLsHw:
self.components.append( self.components.append(
{ {
"actions": [self.get_test_data_storage(sm)], "actions": [],
"type": self.get_data_storage_type(sm), "type": self.get_data_storage_type(sm),
"model": model, "model": model,
"manufacturer": manufacturer, "manufacturer": manufacturer,