Fixing order of actions_multiple devices
This commit is contained in:
parent
b233b7bb75
commit
6de83ce4d2
|
@ -0,0 +1,56 @@
|
||||||
|
"""
|
||||||
|
change action_device
|
||||||
|
|
||||||
|
Revision ID: 1bb2b5e0fae7
|
||||||
|
Revises: a0978ac6cf4a
|
||||||
|
Create Date: 2021-11-04 10:32:49.116399
|
||||||
|
|
||||||
|
"""
|
||||||
|
import sqlalchemy as sa
|
||||||
|
from alembic import context
|
||||||
|
from alembic import op
|
||||||
|
from sqlalchemy.dialects import postgresql
|
||||||
|
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = '1bb2b5e0fae7'
|
||||||
|
down_revision = 'a0978ac6cf4a'
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
|
||||||
|
def get_inv():
|
||||||
|
INV = context.get_x_argument(as_dictionary=True).get('inventory')
|
||||||
|
if not INV:
|
||||||
|
raise ValueError("Inventory value is not specified")
|
||||||
|
return INV
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade_data():
|
||||||
|
con = op.get_bind()
|
||||||
|
|
||||||
|
values = f"action_id, {get_inv()}.action.created"
|
||||||
|
table = f"{get_inv()}.action_device"
|
||||||
|
joins = f"inner join {get_inv()}.action"
|
||||||
|
on = f"on {get_inv()}.action_device.action_id = {get_inv()}.action.id"
|
||||||
|
sql = f"select {values} from {table} {joins} {on}"
|
||||||
|
|
||||||
|
actions_devs = con.execute(sql)
|
||||||
|
for a in actions_devs:
|
||||||
|
action_id = a.action_id
|
||||||
|
created = a.created
|
||||||
|
sql = f"update {get_inv()}.action_device set created='{created}' where action_id='{action_id}';"
|
||||||
|
con.execute(sql)
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
op.add_column('action_device',
|
||||||
|
sa.Column('created', sa.TIMESTAMP(timezone=True), server_default=sa.text('CURRENT_TIMESTAMP'),
|
||||||
|
nullable=False, comment='When Devicehub created this.'),
|
||||||
|
schema=f'{get_inv()}')
|
||||||
|
|
||||||
|
upgrade_data()
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
op.drop_column('action_device', 'created', schema=f'{get_inv()}')
|
|
@ -304,6 +304,23 @@ class ActionDevice(db.Model):
|
||||||
device_id = Column(BigInteger, ForeignKey(Device.id), primary_key=True)
|
device_id = Column(BigInteger, ForeignKey(Device.id), primary_key=True)
|
||||||
action_id = Column(UUID(as_uuid=True), ForeignKey(ActionWithMultipleDevices.id),
|
action_id = Column(UUID(as_uuid=True), ForeignKey(ActionWithMultipleDevices.id),
|
||||||
primary_key=True)
|
primary_key=True)
|
||||||
|
device = relationship(Device,
|
||||||
|
backref=backref('actions_device',
|
||||||
|
lazy=True),
|
||||||
|
primaryjoin=Device.id == device_id)
|
||||||
|
action = relationship(Action,
|
||||||
|
backref=backref('actions_device',
|
||||||
|
lazy=True),
|
||||||
|
primaryjoin=Action.id == action_id)
|
||||||
|
created = db.Column(db.TIMESTAMP(timezone=True),
|
||||||
|
nullable=False,
|
||||||
|
index=True,
|
||||||
|
server_default=db.text('CURRENT_TIMESTAMP'))
|
||||||
|
created.comment = """When Devicehub created this."""
|
||||||
|
|
||||||
|
def __init__(self, **kwargs) -> None:
|
||||||
|
self.created = kwargs.get('created', datetime.now(timezone.utc))
|
||||||
|
super().__init__(**kwargs)
|
||||||
|
|
||||||
|
|
||||||
class ActionWithMultipleTradeDocuments(ActionWithMultipleDevices):
|
class ActionWithMultipleTradeDocuments(ActionWithMultipleDevices):
|
||||||
|
|
|
@ -5,7 +5,7 @@ class MetricsMix:
|
||||||
"""we want get the data metrics of one device"""
|
"""we want get the data metrics of one device"""
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
self.actions.sort(key=lambda x: x.created)
|
# self.actions.sort(key=lambda x: x.created)
|
||||||
self.rows = []
|
self.rows = []
|
||||||
self.lifetime = 0
|
self.lifetime = 0
|
||||||
self.last_trade = None
|
self.last_trade = None
|
||||||
|
@ -15,6 +15,7 @@ class MetricsMix:
|
||||||
self.act = None
|
self.act = None
|
||||||
self.end_users = 0
|
self.end_users = 0
|
||||||
self.final_user_code = ''
|
self.final_user_code = ''
|
||||||
|
self.trades = {}
|
||||||
|
|
||||||
def get_template_row(self):
|
def get_template_row(self):
|
||||||
"""
|
"""
|
||||||
|
@ -76,16 +77,7 @@ class Metrics(MetricsMix):
|
||||||
self.rows.append(row)
|
self.rows.append(row)
|
||||||
return
|
return
|
||||||
|
|
||||||
# if self.last_trade['trade_supplier'] == self.act.rol_user.email:
|
# import pdb; pdb.set_trace()
|
||||||
# self.last_trade['status_supplier'] = self.act.type
|
|
||||||
# self.last_trade['status_supplier_created'] = self.act.created
|
|
||||||
# return
|
|
||||||
|
|
||||||
# if self.last_trade['trade_receiver'] == self.act.rol_user.email:
|
|
||||||
# self.last_trade['status_receiver'] = self.act.type
|
|
||||||
# self.last_trade['status_receiver_created'] = self.act.created
|
|
||||||
# return
|
|
||||||
|
|
||||||
if self.last_trade['trade_supplier'] == self.act.author.email:
|
if self.last_trade['trade_supplier'] == self.act.author.email:
|
||||||
self.last_trade['status_supplier'] = self.act.type
|
self.last_trade['status_supplier'] = self.act.type
|
||||||
self.last_trade['status_supplier_created'] = self.act.created
|
self.last_trade['status_supplier_created'] = self.act.created
|
||||||
|
@ -96,7 +88,6 @@ class Metrics(MetricsMix):
|
||||||
self.last_trade['status_receiver_created'] = self.act.created
|
self.last_trade['status_receiver_created'] = self.act.created
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
def get_snapshot(self):
|
def get_snapshot(self):
|
||||||
"""
|
"""
|
||||||
If there are one snapshot get the last lifetime for to do a calcul of time of use.
|
If there are one snapshot get the last lifetime for to do a calcul of time of use.
|
||||||
|
@ -109,6 +100,7 @@ class Metrics(MetricsMix):
|
||||||
"""
|
"""
|
||||||
If the action is one Allocate, need modify the row base.
|
If the action is one Allocate, need modify the row base.
|
||||||
"""
|
"""
|
||||||
|
self.action_create_by = 'Receiver'
|
||||||
self.end_users = self.act.end_users
|
self.end_users = self.act.end_users
|
||||||
self.final_user_code = self.act.final_user_code
|
self.final_user_code = self.act.final_user_code
|
||||||
row = self.get_template_row()
|
row = self.get_template_row()
|
||||||
|
@ -124,6 +116,7 @@ class Metrics(MetricsMix):
|
||||||
"""
|
"""
|
||||||
If the action is one Live, need modify the row base.
|
If the action is one Live, need modify the row base.
|
||||||
"""
|
"""
|
||||||
|
self.action_create_by = 'Receiver'
|
||||||
row = self.get_template_row()
|
row = self.get_template_row()
|
||||||
row['type'] = 'Live'
|
row['type'] = 'Live'
|
||||||
row['finalUserCode'] = self.final_user_code
|
row['finalUserCode'] = self.final_user_code
|
||||||
|
@ -139,6 +132,7 @@ class Metrics(MetricsMix):
|
||||||
"""
|
"""
|
||||||
If the action is one Dellocate, need modify the row base.
|
If the action is one Dellocate, need modify the row base.
|
||||||
"""
|
"""
|
||||||
|
self.action_create_by = 'Receiver'
|
||||||
row = self.get_template_row()
|
row = self.get_template_row()
|
||||||
row['type'] = 'Deallocate'
|
row['type'] = 'Deallocate'
|
||||||
row['start'] = self.act.start_time
|
row['start'] = self.act.start_time
|
||||||
|
@ -159,15 +153,18 @@ class Metrics(MetricsMix):
|
||||||
"""
|
"""
|
||||||
if self.act.author == self.act.user_from:
|
if self.act.author == self.act.user_from:
|
||||||
self.action_create_by = 'Supplier'
|
self.action_create_by = 'Supplier'
|
||||||
|
self.status_receiver = ''
|
||||||
|
|
||||||
row = self.get_template_row()
|
row = self.get_template_row()
|
||||||
self.last_trade = row
|
self.last_trade = row
|
||||||
row['type'] = 'Trade'
|
row['type'] = 'Trade'
|
||||||
row['action_type'] = 'Trade'
|
row['action_type'] = 'Trade'
|
||||||
row['trade_supplier'] = self.act.user_from.email
|
row['trade_supplier'] = self.act.user_from.email
|
||||||
row['trade_receiver'] = self.act.user_to.email
|
row['trade_receiver'] = self.act.user_to.email
|
||||||
row['status_receiver'] = ''
|
row['status_receiver'] = self.status_receiver
|
||||||
row['status_supplier'] = ''
|
row['status_supplier'] = ''
|
||||||
row['trade_confirmed'] = self.get_confirms()
|
row['trade_confirmed'] = self.get_confirms()
|
||||||
|
self.trades[self.act.created] = row
|
||||||
self.rows.append(row)
|
self.rows.append(row)
|
||||||
|
|
||||||
def get_metrics(self):
|
def get_metrics(self):
|
||||||
|
|
|
@ -171,7 +171,16 @@ class Device(Thing):
|
||||||
|
|
||||||
Actions are returned by descending ``created`` time.
|
Actions are returned by descending ``created`` time.
|
||||||
"""
|
"""
|
||||||
return sorted(chain(self.actions_multiple, self.actions_one), key=lambda x: x.created)
|
actions_multiple = copy.copy(self.actions_multiple)
|
||||||
|
actions_one = copy.copy(self.actions_one)
|
||||||
|
|
||||||
|
for ac in actions_multiple:
|
||||||
|
ac.real_created = ac.actions_device[0].created
|
||||||
|
|
||||||
|
for ac in actions_one:
|
||||||
|
ac.real_created = ac.created
|
||||||
|
|
||||||
|
return sorted(chain(actions_multiple, actions_one), key=lambda x: x.real_created)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def problems(self):
|
def problems(self):
|
||||||
|
|
|
@ -435,7 +435,7 @@ class ActionRow(OrderedDict):
|
||||||
self['Action-User-LastOwner-Receiver'] = allocate['trade_receiver']
|
self['Action-User-LastOwner-Receiver'] = allocate['trade_receiver']
|
||||||
self['Action-Create-By'] = allocate['action_create_by']
|
self['Action-Create-By'] = allocate['action_create_by']
|
||||||
self['Trade-Confirmed'] = allocate['trade_confirmed']
|
self['Trade-Confirmed'] = allocate['trade_confirmed']
|
||||||
self['Status-Supplier'] = allocate['status_supplier']
|
self['Status-Created-By-Supplier-About-Reciber'] = allocate['status_supplier']
|
||||||
self['Status-Receiver'] = allocate['status_receiver']
|
self['Status-Receiver'] = allocate['status_receiver']
|
||||||
self['Status Supplier – Created Date'] = allocate['status_supplier_created']
|
self['Status Supplier – Created Date'] = allocate['status_supplier_created']
|
||||||
self['Status Receiver – Created Date'] = allocate['status_receiver_created']
|
self['Status Receiver – Created Date'] = allocate['status_receiver_created']
|
||||||
|
|
|
@ -134,7 +134,7 @@ def test_metrics_action_status(user: UserClient, user2: UserClient):
|
||||||
item='actions/',
|
item='actions/',
|
||||||
accept='text/csv',
|
accept='text/csv',
|
||||||
query=[('filter', {'type': ['Computer']})])
|
query=[('filter', {'type': ['Computer']})])
|
||||||
head = 'DHID;Hid;Document-Name;Action-Type;Action-User-LastOwner-Supplier;Action-User-LastOwner-Receiver;Action-Create-By;Trade-Confirmed;Status-Supplier;Status-Receiver;Status Supplier – Created Date;Status Receiver – Created Date;Trade-Weight;Action-Create;Allocate-Start;Allocate-User-Code;Allocate-NumUsers;UsageTimeAllocate;Type;LiveCreate;UsageTimeHdd\n'
|
head = 'DHID;Hid;Document-Name;Action-Type;Action-User-LastOwner-Supplier;Action-User-LastOwner-Receiver;Action-Create-By;Trade-Confirmed;Status-Created-By-Supplier-About-Reciber;Status-Receiver;Status Supplier – Created Date;Status Receiver – Created Date;Trade-Weight;Action-Create;Allocate-Start;Allocate-User-Code;Allocate-NumUsers;UsageTimeAllocate;Type;LiveCreate;UsageTimeHdd\n'
|
||||||
body = 'O48N2;desktop-lenovo-9644w8n-0169622-00:1a:6b:5e:7f:10;;Status;;foo@foo.com;Receiver;;;Use;;'
|
body = 'O48N2;desktop-lenovo-9644w8n-0169622-00:1a:6b:5e:7f:10;;Status;;foo@foo.com;Receiver;;;Use;;'
|
||||||
assert head in csv_str
|
assert head in csv_str
|
||||||
assert body in csv_str
|
assert body in csv_str
|
||||||
|
|
Reference in a new issue