Project

General

Profile

« Previous | Next » 

Revision 1f0de62b

Added by Benjamin Dauvergne over 12 years ago

scripts: to accomodate CMPP, load acts in batch

View differences:

scripts/import_rs.py
90 90

  
91 91
dic_worker = {}
92 92

  
93
def load_csv(db, name):
93
def load_csv(db, name, offset=0, limit=9999999, id_column=0):
94 94
    records = []
95 95
    idx = {}
96 96

  
......
99 99
    cols = csvlines.next()
100 100
    i = 0
101 101
    for line in csvlines:
102
        # ignore line for timetable
102
        if not (offset <= int(line[id_column]) < offset+limit):
103
            continue
103 104
        data = _get_dict(cols, line)
104 105
        records.append(data)
105 106
        idx[data['id']] = i
......
375 376
        print ' Rdv individuels invalides', invalid_single
376 377

  
377 378
        # create single rdv
378
        limit = 100000
379
        limit = 1000000
379 380
        # single RS
380 381
        i = 0 
381 382
        rows = []
......
481 482
        print "Actes before delete", qs.count()
482 483
        qs.delete()
483 484
        print "Actes afterdelete", qs.count()
484
        actes_data, actes_idx, actes_cols = load_csv(db, 'actes')
485
        actes_cols.extend(['workers','invalid'])
486
        invalid_actes_writer.writerow(map(lambda x: x.encode('utf-8'), actes_cols))
487
        actes_details_data, _, _ = load_csv(db, 'details_actes')
488
        handle_details(actes_data, actes_idx, actes_details_data, 'acte_id')
489
        act_to_event = dict()
490
        for row in rs_data:
491
            if row.get('event') and row['base_id']:
492
                act_to_event[row['base_id']] = row['event']
493
        rows = []
494
        actes = []
495
        validation_state = []
496
        doctors = []
497
        i = 0
498
        j = 0
499
        k = 0
500
        DoctorThrough = Act.doctors.through
501
        for row in actes_data:
502
            row.setdefault('workers', set())
503
            row['date'] = _to_date(row['date_acte'])
504
            row['time'] = _to_time(row['heure'])
505
            row['duration'] = _to_duration(row['duree'])
506
            row['is_billed'] = row['marque'] == '1'
507
            row['validation_locked'] = row['date'] < date(2013, 1, 3)
508
            set_enfant(row)
509
            set_act_type(row)
510
            row['parent_event'] = act_to_event.get(row['id'])
511
            row['state'] = map_cs[service.name].get(row['cs'],
512
                    'VALIDE')
513
            duration = row['duration']
514
            if duration:
515
                duration = duration.seconds // 60
516
            if row.get('invalid'):
517
                invalid_actes_writer.writerow([ unicode(row[col]).encode('utf-8') for col in actes_cols ])
518
                continue
519
            i += 1
520
            if row['parent_event']:
521
                j += 1
522
            else:
523
                t = row['time']
524
                if t:
525
                    query = 'EXTRACT(hour from start_datetime) = %i and EXTRACT(minute from start_datetime) = %i' % (t.hour, t.minute)
526
                    qs = EventWithAct.objects.for_today(row['date']).filter(patient=row['enfant']).extra(where=[query])
527
                    if qs:
528
                        try:
529
                            row['parent_event'] = qs.get()
530
                        except:
531
                            print qs
532
                            import pdb
533
                            pdb.set_trace()
534

  
535
                        k += 1
536
            act = Act.objects.create(
537
                    date=row['date'],
538
                    time=row['time'],
539
                    _duration=duration,
540
                    is_billed=row['is_billed'],
541
                    act_type=row['act_type'],
542
                    patient=row['enfant'],
543
                    validation_locked=row['validation_locked'],
544
                    parent_event=row['parent_event'])
545
            rows.append(row)
546
            actes.append(act)
547
            validation_state.append(
548
                    ActValidationState(act=act,
549
                        state_name=row['state'],
550
                        previous_state=None))
551
            for worker in row['workers']:
552
                doctors.append(DoctorThrough(
553
                    act_id=act.id,
554
                    worker_id=worker.id))
555
        batch_bulk(ActValidationState, validation_state, 500)
556
        batch_bulk(DoctorThrough, doctors, 500)
557

  
558
        print 'Actes:'
559
        print ' - importe ', i
560
        print ' - natual link to rdv', j
561
        print ' - complicated link to rdv', k
485
        limit = 20000
486
        for offset in range(0, 99999999, limit):
487
            actes_data, actes_idx, actes_cols = load_csv(db, 'actes', offset=offset, limit=limit)
488
            print 'Loading', len(actes_data), 'acts'
489
            if not actes_data:
490
                break
491
            actes_cols.extend(['workers','invalid'])
492
            invalid_actes_writer.writerow(map(lambda x: x.encode('utf-8'), actes_cols))
493
            actes_details_data, _, _ = load_csv(db, 'details_actes', offset=offset, limit=limit, id_column=1)
494
            handle_details(actes_data, actes_idx, actes_details_data, 'acte_id')
495
            act_to_event = dict()
496
            for row in rs_data:
497
                if row.get('event') and row['base_id']:
498
                    act_to_event[row['base_id']] = row['event']
499
            rows = []
500
            actes = []
501
            validation_state = []
502
            doctors = []
503
            i = 0
504
            j = 0
505
            k = 0
506
            DoctorThrough = Act.doctors.through
507
            for row in actes_data:
508
                row.setdefault('workers', set())
509
                row['date'] = _to_date(row['date_acte'])
510
                row['time'] = _to_time(row['heure'])
511
                row['duration'] = _to_duration(row['duree'])
512
                row['is_billed'] = row['marque'] == '1'
513
                row['validation_locked'] = row['date'] < date(2013, 1, 3)
514
                set_enfant(row)
515
                set_act_type(row)
516
                row['parent_event'] = act_to_event.get(row['id'])
517
                row['state'] = map_cs[service.name].get(row['cs'],
518
                        'VALIDE')
519
                duration = row['duration']
520
                if duration:
521
                    duration = duration.seconds // 60
522
                if row.get('invalid'):
523
                    invalid_actes_writer.writerow([ unicode(row[col]).encode('utf-8') for col in actes_cols ])
524
                    continue
525
                i += 1
526
                if row['parent_event']:
527
                    j += 1
528
                else:
529
                    t = row['time']
530
                    if t:
531
                        query = 'EXTRACT(hour from start_datetime) = %i and EXTRACT(minute from start_datetime) = %i' % (t.hour, t.minute)
532
                        qs = EventWithAct.objects.for_today(row['date']).filter(patient=row['enfant']).extra(where=[query])
533
                        if qs:
534
                            try:
535
                                row['parent_event'] = qs.get()
536
                            except:
537
                                print qs
538
                                import pdb
539
                                pdb.set_trace()
540

  
541
                            k += 1
542
                act = Act.objects.create(
543
                        date=row['date'],
544
                        time=row['time'],
545
                        _duration=duration,
546
                        is_billed=row['is_billed'],
547
                        act_type=row['act_type'],
548
                        patient=row['enfant'],
549
                        validation_locked=row['validation_locked'],
550
                        parent_event=row['parent_event'])
551
                rows.append(row)
552
                actes.append(act)
553
                validation_state.append(
554
                        ActValidationState(act=act,
555
                            state_name=row['state'],
556
                            previous_state=None))
557
                for worker in row['workers']:
558
                    doctors.append(DoctorThrough(
559
                        act_id=act.id,
560
                        worker_id=worker.id))
561
            batch_bulk(ActValidationState, validation_state, 500)
562
            batch_bulk(DoctorThrough, doctors, 500)
563

  
564
            print 'Actes:'
565
            print ' - importe ', i
566
            print ' - natual link to rdv', j
567
            print ' - complicated link to rdv', k
562 568

  
563 569

  
564 570
    invalid_rs_csv.close()

Also available in: Unified diff