# -*- coding: utf-8 -*-
# Generated by Django 1.11.23 on 2019-10-07 19:46
from __future__ import unicode_literals

from django.db import migrations, models


def set_active_samples(apps, schema_editor):
    Sample = apps.get_model("seqr", "Sample")
    db_alias = schema_editor.connection.alias

    deleted_count, _ = Sample.objects.using(db_alias).exclude(sample_status='loaded').delete()
    print('Deleted {} unloaded samples'.format(deleted_count))

    for dataset_type in ['VARIANTS', 'ALIGN']:
        all_samples = Sample.objects.using(db_alias).filter(
            dataset_type=dataset_type,
        ).prefetch_related('individual', 'individual__family')
        sample_individual_max_loaded_date = {
            agg['individual__guid']: agg['max_loaded_date'] for agg in
            all_samples.values('individual__guid').annotate(max_loaded_date=models.Max('loaded_date'))
        }
        active_samples = [s.id for s in all_samples if s.loaded_date == sample_individual_max_loaded_date[s.individual.guid]]
        Sample.objects.using(db_alias).filter(id__in=active_samples).update(is_active=True)

        print('{} {} samples active status updated'.format(len(active_samples), dataset_type.lower()))


class Migration(migrations.Migration):

    dependencies = [
        ('seqr', '0065_merge_20190827_2031'),
    ]

    atomic = False

    operations = [
        migrations.AddField(
            model_name='sample',
            name='is_active',
            field=models.BooleanField(default=False),
        ),
        migrations.RunPython(set_active_samples, reverse_code=migrations.RunPython.noop),
        migrations.RemoveField(
            model_name='sample',
            name='sample_status',
        ),
    ]

# -*- coding: utf-8 -*-
# Generated by Django 1.11.23 on 2019-10-07 19:46
from __future__ import unicode_literals

from django.db import migrations, models


def set_active_samples(apps, schema_editor):
    Sample = apps.get_model("seqr", "Sample")
    db_alias = schema_editor.connection.alias

    deleted_count, _ = Sample.objects.using(db_alias).exclude(sample_status='loaded').delete()
    print('Deleted {} unloaded samples'.format(deleted_count))

    for dataset_type in ['VARIANTS', 'ALIGN']:
        all_samples = Sample.objects.using(db_alias).filter(
            dataset_type=dataset_type,
        ).prefetch_related('individual', 'individual__family')
        sample_individual_max_loaded_date = {
            agg['individual__guid']: agg['max_loaded_date'] for agg in
            all_samples.values('individual__guid').annotate(max_loaded_date=models.Max('loaded_date'))
        }
        active_samples = [s.id for s in all_samples if s.loaded_date == sample_individual_max_loaded_date[s.individual.guid]]
        Sample.objects.using(db_alias).filter(id__in=active_samples).update(is_active=True)

        print('{} {} samples active status updated'.format(len(active_samples), dataset_type.lower()))


class Migration(migrations.Migration):

    dependencies = [
        ('seqr', '0066_auto_20190924_1422'),
    ]

    atomic = False

    operations = [
        migrations.AddField(
            model_name='sample',
            name='is_active',
            field=models.BooleanField(default=False),
        ),
        migrations.RunPython(set_active_samples, reverse_code=migrations.RunPython.noop),
        migrations.RemoveField(
            model_name='sample',
            name='sample_status',
        ),
    ]

fix db migrations