Compare commits

...

2 commits

5 changed files with 99 additions and 14 deletions

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,2 @@
"lot_name";"dhid"
"t1";"ZZZZ"
1 lot_name dhid
2 t1 ZZZZ

View file

@ -0,0 +1,5 @@
"name";"type"
"t1";"temporal"
"o2";"outgoing"
"i3";"incoming"
"o4";"outgoing"
1 name type
2 t1 temporal
3 o2 outgoing
4 i3 incoming
5 o4 outgoing

View file

@ -0,0 +1,2 @@
"dhid";"uuid"
"ZZZZ";"db523f01-92b2-4282-af2a-d9a653f93d45"
1 dhid uuid
2 ZZZZ db523f01-92b2-4282-af2a-d9a653f93d45

View file

@ -13,6 +13,7 @@ from django.contrib.auth import get_user_model
from utils.save_snapshots import move_json, save_in_disk
from evidence.parse import Build
from evidence.models import Annotation
from lot.models import Lot, LotTag, DeviceLot
logger = logging.getLogger(__name__)
@ -20,6 +21,7 @@ logger = logging.getLogger(__name__)
User = get_user_model()
SEPARATOR = ";"
QUOTA = '"'
PATH_SNAPTHOPS = "examples/snapshots"
@ -30,7 +32,7 @@ def get_dict(row, header):
if len(row) != len(header):
return
return {row[i]: header[i] for i in range(len(header))}
return {header[i]: row[i] for i in range(len(header))}
def open_csv(csv):
@ -42,10 +44,11 @@ def open_csv(csv):
if len(rows) < 2:
return []
header = rows[0].split(SEPARATOR)
header = [x.replace(QUOTA, '') for x in rows[0].split(SEPARATOR)]
data = []
for row in rows[1:]:
drow = get_dict(row.split(SEPARATOR), header)
lrow = [x.replace(QUOTA, '') for x in row.split(SEPARATOR)]
drow = get_dict(lrow, header)
if drow:
data.append(drow)
return data
@ -75,7 +78,8 @@ def open_snapshot(uuid):
return snap, snapshot_path
### end read snapshot ###
### migration ###
### migration snapshots ###
def create_custom_id(dhid, uuid, user):
tag = Annotation.objects.filter(
uuid=uuid,
@ -98,7 +102,6 @@ def create_custom_id(dhid, uuid, user):
def migrate_snapshots(row, user):
# import pdb; pdb.set_trace()
if not row or not user:
return
dhid = row.get("dhid")
@ -109,13 +112,69 @@ def migrate_snapshots(row, user):
# insert snapshot
path_name = save_in_disk(snapshot, user.institution.name)
Build(path_name, user)
Build(snapshot, user)
move_json(path_name, user.institution.name)
# insert dhid
create_custom_id(dhid, uuid)
create_custom_id(dhid, uuid, user)
### end migration ###
### end migration snapshots ###
### migration lots ###
def migrate_lots(row, user):
tag = row.get("type", "Temporal")
name = row.get("name")
ltag = LotTag.objects.filter(name=tag, owner=user.institution).first()
if tag and not ltag:
ltag = LotTag.objects.create(
name=tag,
owner=user.institution,
user=user
)
lot = Lot.objects.filter(name=tag, owner=user.institution).first()
if not lot:
lot = Lot.objects.create(
name=name,
owner=user.institution,
user=user,
type=ltag
)
def add_device_in_lot(row, user):
lot_name = row.get("lot_name")
dhid = row.get("dhid")
if not lot_name or not dhid:
return
dev = Annotation.objects.filter(
type=Annotation.Type.SYSTEM,
key='CUSTOM_ID',
value=dhid,
owner=user.institution,
).first()
lot = Lot.objects.filter(
name=lot_name,
owner=user.institution,
user=user,
).first()
if not lot:
lot = Lot.objects.create(
name=lot_name,
owner=user.institution,
user=user,
)
if DeviceLot.objects.filter(lot=lot, device_id=dev.uuid).exists():
return
DeviceLot.objects.create(lot=lot, device_id=dev.uuid)
### end migration lots ###
### initial main ###
@ -138,8 +197,16 @@ def parse_args():
usage="migration-script.py [-h] [--csv CSV]",
description="Csv file with datas to migratie.")
parser.add_argument(
'--csv',
help="path to the data file."
'--csv-dhid',
help="path to the csv file with relation dhid and uuid."
)
parser.add_argument(
'--lots',
help="path to the csv file with relation lot_name and type of lot."
)
parser.add_argument(
'--csv-lots',
help="path to the csv file with relation lot_name and type of lot."
)
parser.add_argument(
'--email',
@ -156,13 +223,21 @@ def main():
prepare_logger()
logger.info("START")
args = parse_args()
PATH_SNAPTHOPS = args.snapshots
user = User.objects.get(email=args.email)
for row in open_csv(args.csv):
migrate_snapshots(row, user)
if args.snapshots:
global PATH_SNAPTHOPS
PATH_SNAPTHOPS = args.snapshots
if args.csv_dhid:
# migration snapthots
for row in open_csv(args.csv):
migrate_snapshots(row, user)
# migration lots
if args.lots:
for row in open_csv(args.lots):
migrate_lots(row, user)
if __name__ == '__main__':
main()