import datetime
import json

import dateutil.parser

from django.contrib.auth import get_user_model
from django.test import Client, TestCase
from django.utils import timezone

from apps.devicelocation.models import DeviceLocation
from apps.physicaldevice.models import Device
from apps.property.models import GenericProperty
from apps.report.models import GeneratedUserReport
from apps.sqsworker.exceptions import WorkerActionHardError
from apps.stream.models import StreamId, StreamVariable
from apps.streamdata.models import StreamData
from apps.streamevent.models import StreamEventData
from apps.streamfilter.models import *
from apps.streamnote.models import StreamNote
from apps.utils.data_mask.mask_utils import get_data_mask_event, set_data_mask
from apps.utils.gid.convert import *
from apps.utils.test_util import TestMixin

from ..models import *
from ..worker.archive_device_data import ArchiveDeviceDataAction

user_model = get_user_model()


class DataBlockCreateWorkerTests(TestMixin, TestCase):

    def setUp(self):
        self.usersTestSetup()
        self.orgTestSetup()
        self.deviceTemplateTestSetup()
        self.v1 = StreamVariable.objects.create_variable(
            name='Var A', project=self.p1, created_by=self.u2, lid=1,
        )
        self.v2 = StreamVariable.objects.create_variable(
            name='Var B', project=self.p1, created_by=self.u3, lid=2,
        )
        self.pd1 = Device.objects.create_device(project=self.p1, label='d1', template=self.dt1, created_by=self.u2)
        self.pd2 = Device.objects.create_device(project=self.p1, label='d2', template=self.dt1, created_by=self.u2)
        StreamId.objects.create_after_new_device(self.pd1)
        StreamId.objects.create_after_new_device(self.pd2)
        self.s1 = StreamId.objects.filter(variable=self.v1).first()
        self.s2 = StreamId.objects.filter(variable=self.v2).first()

    def tearDown(self):
        StreamFilterAction.objects.all().delete()
        StreamFilterTrigger.objects.all().delete()
        StreamFilter.objects.all().delete()
        StreamId.objects.all().delete()
        StreamVariable.objects.all().delete()
        GenericProperty.objects.all().delete()
        Device.objects.all().delete()
        StreamData.objects.all().delete()
        StreamEventData.objects.all().delete()
        self.deviceTemplateTestTearDown()
        self.orgTestTearDown()
        self.userTestTearDown()

    def testDataBlockActionBadArguments(self):
        with self.assertRaises(WorkerActionHardError):
            ArchiveDeviceDataAction.schedule(args={})
        with self.assertRaises(WorkerActionHardError):
            ArchiveDeviceDataAction.schedule(args={'foobar': 5})
        with self.assertRaises(WorkerActionHardError):
            ArchiveDeviceDataAction.schedule(args={'data_block_slug': 'b--0000-0000-0000-0001', 'extra-bad-arg': 'foo'})

        self.assertTrue(ArchiveDeviceDataAction._arguments_ok({'data_block_slug': 'b--0000-0000-0000-0001'}))

        action = ArchiveDeviceDataAction()
        self.assertIsNotNone(action)
        with self.assertRaises(WorkerActionHardError):
            action.execute(arguments={'foobar': 5})

    def testDataBlockActionNoDataBlock(self):

        action = ArchiveDeviceDataAction()
        self.assertIsNotNone(action)
        with self.assertRaises(WorkerActionHardError):
            action.execute({'data_block_slug': 'b--0000-0000-0000-0001'})

    def testDataBlockActionMigrateProperties(self):

        db1 = DataBlock.objects.create(org=self.o1, title='test', device=self.pd1, block=1, created_by=self.u1)

        GenericProperty.objects.create_int_property(slug=self.pd1.slug,
                                                    created_by=self.u1,
                                                    name='prop1', value=4)
        GenericProperty.objects.create_str_property(slug=self.pd1.slug,
                                                    created_by=self.u1,
                                                    name='prop2', value='4')
        GenericProperty.objects.create_bool_property(slug=self.pd1.slug,
                                                     created_by=self.u1, is_system=True,
                                                     name='@prop3', value=True)
        self.assertEqual(GenericProperty.objects.object_properties_qs(self.pd1).count(), 3)
        self.assertEqual(GenericProperty.objects.object_properties_qs(db1).count(), 0)

        action = ArchiveDeviceDataAction()
        action._block = db1
        action._device = self.pd1
        action._migrate_properties()
        self.assertEqual(GenericProperty.objects.object_properties_qs(self.pd1).count(), 1)
        self.assertEqual(GenericProperty.objects.object_properties_qs(db1).count(), 3)

    def testDataBlockActionMigrateStreams(self):

        device = Device.objects.create_device(project=self.p1, label='d3', template=self.dt1, created_by=self.u2)
        block = DataBlock.objects.create(org=self.o1, title='test', device=device, block=1, created_by=self.u1)
        stream1 = StreamId.objects.create_stream(
            project=self.p1, variable=self.v1, device=device, created_by=self.u2
        )
        stream2 = StreamId.objects.create_stream(
            project=self.p1, variable=self.v2, device=device, created_by=self.u2
        )
        var3 = StreamVariable.objects.create_variable(
            name='Var C', project=self.p1, created_by=self.u2, lid=3,
        )
        stream3 = StreamId.objects.create_stream(
            project=self.p1, variable=var3, device=device, created_by=self.u2
        )
        self.assertEqual(self.p1.variables.count(), 3)
        count0 = StreamId.objects.count()
        self.assertEqual(device.streamids.count(), 3)

        action = ArchiveDeviceDataAction()
        action._block = block
        action._device = device
        action._clone_streams()
        self.assertEqual(StreamId.objects.count(), count0 + 3)

    def testDataBlockActionMigrateStreamData(self):

        device = Device.objects.create_device(project=self.p1, label='d3', template=self.dt1, created_by=self.u2)
        block = DataBlock.objects.create(org=self.o1, title='test', device=device, block=1, created_by=self.u1)
        stream1 = StreamId.objects.create_stream(
            project=self.p1, variable=self.v1, device=device, created_by=self.u2
        )
        stream2 = StreamId.objects.create_stream(
            project=self.p1, variable=self.v2, device=device, created_by=self.u2
        )

        StreamData.objects.create(
            stream_slug=stream1.slug,
            type='ITR',
            timestamp=timezone.now(),
            streamer_local_id=5,
            int_value=5
        )
        StreamData.objects.create(
            stream_slug=stream1.slug,
            type='ITR',
            timestamp=timezone.now(),
            streamer_local_id=6,
            int_value=6
        )
        StreamData.objects.create(
            stream_slug=stream2.slug,
            type='ITR',
            timestamp=timezone.now(),
            streamer_local_id=7,
            int_value=7
        )
        StreamData.objects.create(
            stream_slug=stream1.slug,
            type='ITR',
            timestamp=timezone.now(),
            streamer_local_id=8,
            int_value=8
        )
        StreamData.objects.create(
            stream_slug=stream2.slug,
            type='ITR',
            timestamp=timezone.now(),
            streamer_local_id=9,
            int_value=9
        )

        action = ArchiveDeviceDataAction()
        action._block = block
        action._device = device
        action._clone_streams()

        self.assertEqual(StreamData.objects.filter(stream_slug=stream1.slug).count(), 3)
        self.assertEqual(StreamData.objects.filter(stream_slug=stream2.slug).count(), 2)

        action._migrate_stream_data()

        self.assertEqual(StreamData.objects.filter(stream_slug=stream1.slug).count(), 0)
        self.assertEqual(StreamData.objects.filter(stream_slug=stream2.slug).count(), 0)

        new_stream1 = block.get_stream_slug_for(self.v1.formatted_lid)
        self.assertEqual(StreamData.objects.filter(stream_slug=new_stream1).count(), 3)
        new_stream2 = block.get_stream_slug_for(self.v2.formatted_lid)
        self.assertEqual(StreamData.objects.filter(stream_slug=new_stream2).count(), 2)

        self.assertEqual(StreamData.objects.filter(stream_slug=new_stream1).first().project_slug, '')

    def testDataBlockActionMigrateStreamEvents(self):

        device = Device.objects.create_device(project=self.p1, label='d3', template=self.dt1, created_by=self.u2)
        block = DataBlock.objects.create(org=self.o1, title='test', device=device, block=1, created_by=self.u1)
        stream1 = StreamId.objects.create_stream(
            project=self.p1, variable=self.v1, device=device, created_by=self.u2
        )
        stream2 = StreamId.objects.create_stream(
            project=self.p1, variable=self.v2, device=device, created_by=self.u2
        )

        StreamEventData.objects.create(
            timestamp=timezone.now(),
            device_timestamp=10,
            stream_slug=stream1.slug,
            streamer_local_id=2
        )
        StreamEventData.objects.create(
            timestamp=timezone.now(),
            device_timestamp=10,
            stream_slug=stream1.slug,
            streamer_local_id=3
        )
        StreamEventData.objects.create(
            timestamp=timezone.now(),
            device_timestamp=10,
            stream_slug=stream2.slug,
            streamer_local_id=4
        )

        action = ArchiveDeviceDataAction()
        action._block = block
        action._device = device
        action._clone_streams()

        self.assertEqual(StreamEventData.objects.filter(stream_slug=stream1.slug).count(), 2)
        self.assertEqual(StreamEventData.objects.filter(stream_slug=stream2.slug).count(), 1)

        action._migrate_stream_events()

        self.assertEqual(StreamEventData.objects.filter(stream_slug=stream1.slug).count(), 0)
        self.assertEqual(StreamEventData.objects.filter(stream_slug=stream2.slug).count(), 0)

        new_stream1 = block.get_stream_slug_for(self.v1.formatted_lid)
        self.assertEqual(StreamEventData.objects.filter(stream_slug=new_stream1).count(), 2)
        new_stream2 = block.get_stream_slug_for(self.v2.formatted_lid)
        self.assertEqual(StreamEventData.objects.filter(stream_slug=new_stream2).count(), 1)

    def testDataBlockActionMigrateStreamNote(self):

        device = Device.objects.create_device(project=self.p1, label='d3', template=self.dt1, created_by=self.u2)
        block = DataBlock.objects.create(org=self.o1, title='test', device=device, block=1, created_by=self.u1)
        stream1 = StreamId.objects.create_stream(
            project=self.p1, variable=self.v1, device=device, created_by=self.u2
        )
        StreamId.objects.create_stream(
            project=self.p1, variable=self.v2, device=device, created_by=self.u2
        )

        StreamNote.objects.create(
            target_slug=device.slug,
            timestamp=timezone.now(),
            created_by=self.u2,
            note='System 1',
            type='sc'
        )
        StreamNote.objects.create(
            target_slug=stream1.slug,
            timestamp=timezone.now(),
            created_by=self.u2,
            note='Note 2'
        )
        StreamNote.objects.create(
            target_slug=stream1.slug,
            timestamp=timezone.now(),
            created_by=self.u1,
            note='Note 3'
        )
        StreamNote.objects.create(
            target_slug=device.slug,
            timestamp=timezone.now(),
            created_by=self.u2,
            note='Note 4'
        )
        self.assertEqual(StreamNote.objects.count(), 4)

        action = ArchiveDeviceDataAction()
        action._block = block
        action._device = device
        action._clone_streams()

        self.assertEqual(StreamNote.objects.filter(target_slug=stream1.slug).count(), 2)
        self.assertEqual(StreamNote.objects.filter(target_slug=device.slug).count(), 2)

        action._migrate_stream_notes()

        self.assertEqual(StreamNote.objects.filter(target_slug=stream1.slug).count(), 0)
        self.assertEqual(StreamNote.objects.filter(target_slug=device.slug).count(), 1)

        new_stream1 = block.get_stream_slug_for(self.v1.formatted_lid)
        self.assertEqual(StreamNote.objects.count(), 4)
        self.assertEqual(StreamNote.objects.filter(target_slug=new_stream1).count(), 2)
        self.assertEqual(StreamNote.objects.filter(target_slug=block.slug).count(), 1)

    def testDataBlockActionMigrateDeviceLocations(self):

        device = Device.objects.create_device(project=self.p1, label='d3', template=self.dt1, created_by=self.u2)
        block = DataBlock.objects.create(org=self.o1, title='test', device=device, block=1, created_by=self.u1)

        DeviceLocation.objects.create(
            timestamp=timezone.now(),
            target_slug=device.slug,
            lat=12.1234, lon=10.000,
            user=self.u2
        )
        DeviceLocation.objects.create(
            timestamp=timezone.now(),
            target_slug=device.slug,
            lat=12.1234, lon=11.000,
            user=self.u2
        )
        DeviceLocation.objects.create(
            timestamp=timezone.now(),
            target_slug=device.slug,
            lat=12.1234, lon=12.000,
            user=self.u2
        )

        self.assertEqual(DeviceLocation.objects.count(), 3)

        action = ArchiveDeviceDataAction()
        action._block = block
        action._device = device

        self.assertEqual(DeviceLocation.objects.filter(target_slug=device.slug).count(), 3)

        action._migrate_device_locations()

        self.assertEqual(DeviceLocation.objects.filter(target_slug=device.slug).count(), 0)
        self.assertEqual(DeviceLocation.objects.filter(target_slug=block.slug).count(), 3)

    def testDataBlockActionMigrateReports(self):

        db1 = DataBlock.objects.create(org=self.pd1.org, title='test', device=self.pd1, block=1, created_by=self.u2)

        GeneratedUserReport.objects.create(
            org=self.pd1.org,
            label='My report 1',
            source_ref=self.pd1.slug,
            created_by=self.u2
        )
        GeneratedUserReport.objects.create(
            org=self.pd1.org,
            label='My report 2',
            source_ref=self.pd1.slug,
            created_by=self.u2
        )

        self.assertEqual(GeneratedUserReport.objects.filter(source_ref=self.pd1.slug).count(), 2)
        self.assertEqual(GeneratedUserReport.objects.filter(source_ref=db1.slug).count(), 0)

        action = ArchiveDeviceDataAction()
        action._block = db1
        action._device = self.pd1
        action._migrate_reports()
        self.assertEqual(GeneratedUserReport.objects.filter(source_ref=self.pd1.slug).count(), 0)
        self.assertEqual(GeneratedUserReport.objects.filter(source_ref=db1.slug).count(), 2)

    def testDataBlockActionTestAll(self):
        sg = SensorGraph.objects.create(name='SG 1',
                                        major_version=1,
                                        created_by=self.u1, org=self.o1)
        device = Device.objects.create_device(project=self.p1, label='d3', template=self.dt1, sg=sg, created_by=self.u2)
        block = DataBlock.objects.create(org=self.o1, title='test', device=device, block=1, created_by=self.u1)
        stream1 = StreamId.objects.create_stream(
            project=self.p1, variable=self.v1, device=device, created_by=self.u2
        )
        stream2 = StreamId.objects.create_stream(
            project=self.p1, variable=self.v2, device=device, created_by=self.u2
        )

        GenericProperty.objects.create_int_property(slug=device.slug,
                                                    created_by=self.u1,
                                                    name='prop1', value=4)
        GenericProperty.objects.create_str_property(slug=device.slug,
                                                    created_by=self.u1,
                                                    name='prop2', value='4')
        GenericProperty.objects.create_bool_property(slug=device.slug,
                                                     created_by=self.u1,
                                                     name='prop3', value=True)
        StreamEventData.objects.create(
            timestamp=timezone.now(),
            device_timestamp=10,
            stream_slug=stream1.slug,
            streamer_local_id=2
        )
        StreamEventData.objects.create(
            timestamp=timezone.now(),
            device_timestamp=10,
            stream_slug=stream1.slug,
            streamer_local_id=3
        )
        StreamEventData.objects.create(
            timestamp=timezone.now(),
            device_timestamp=10,
            stream_slug=stream2.slug,
            streamer_local_id=4
        )
        StreamData.objects.create(
            stream_slug=stream1.slug,
            type='ITR',
            timestamp=timezone.now(),
            streamer_local_id=5,
            int_value=5
        )
        StreamData.objects.create(
            stream_slug=stream1.slug,
            type='ITR',
            timestamp=timezone.now(),
            streamer_local_id=6,
            int_value=6
        )
        StreamData.objects.create(
            stream_slug=stream2.slug,
            type='ITR',
            timestamp=timezone.now(),
            streamer_local_id=7,
            int_value=7
        )
        StreamData.objects.create(
            stream_slug=stream1.slug,
            type='ITR',
            timestamp=timezone.now(),
            streamer_local_id=8,
            int_value=8
        )
        StreamData.objects.create(
            stream_slug=stream2.slug,
            type='ITR',
            timestamp=timezone.now(),
            streamer_local_id=9,
            int_value=9
        )
        StreamNote.objects.create(
            target_slug=stream1.slug,
            timestamp=timezone.now(),
            created_by=self.u2,
            note='Note 1'
        )
        StreamNote.objects.create(
            target_slug=stream1.slug,
            timestamp=timezone.now(),
            created_by=self.u2,
            note='Note 2'
        )
        StreamNote.objects.create(
            target_slug=stream1.slug,
            timestamp=timezone.now(),
            created_by=self.u2,
            note='Note 3'
        )
        StreamNote.objects.create(
            target_slug=device.slug,
            timestamp=timezone.now(),
            created_by=self.u1,
            note='Note 4'
        )

        self.assertEqual(GenericProperty.objects.object_properties_qs(device).count(), 3)
        self.assertEqual(GenericProperty.objects.object_properties_qs(block).count(), 0)

        self.assertEqual(device.streamids.count(), 2)

        self.assertEqual(StreamData.objects.filter(stream_slug=stream1.slug).count(), 3)
        self.assertEqual(StreamData.objects.filter(stream_slug=stream2.slug).count(), 2)
        self.assertEqual(StreamEventData.objects.filter(stream_slug=stream1.slug).count(), 2)
        self.assertEqual(StreamEventData.objects.filter(stream_slug=stream2.slug).count(), 1)
        self.assertEqual(StreamNote.objects.filter(target_slug=stream1.slug).count(), 3)
        self.assertEqual(StreamNote.objects.filter(target_slug=device.slug).count(), 1)

        action = ArchiveDeviceDataAction()
        action._block = block
        action._device = device
        action.execute(arguments={'data_block_slug': block.slug})

        self.assertEqual(GenericProperty.objects.object_properties_qs(device).count(), 0)
        self.assertEqual(GenericProperty.objects.object_properties_qs(block).count(), 3)

        self.assertEqual(device.streamids.count(), 4)

        self.assertEqual(StreamData.objects.filter(stream_slug=stream1.slug).count(), 0)
        self.assertEqual(StreamData.objects.filter(stream_slug=stream2.slug).count(), 0)
        self.assertEqual(StreamEventData.objects.filter(stream_slug=stream1.slug).count(), 0)
        self.assertEqual(StreamEventData.objects.filter(stream_slug=stream2.slug).count(), 0)
        self.assertEqual(StreamNote.objects.filter(target_slug=stream1.slug).count(), 0)
        self.assertEqual(StreamNote.objects.filter(target_slug=device.slug).count(), 1)

        new_stream1 = block.get_stream_slug_for(self.v1.formatted_lid)
        self.assertEqual(StreamId.objects.filter(slug=new_stream1).count(), 1)
        new_stream2 = block.get_stream_slug_for(self.v2.formatted_lid)
        self.assertEqual(StreamId.objects.filter(slug=new_stream2).count(), 1)

        self.assertEqual(StreamData.objects.filter(stream_slug=new_stream1).count(), 3)
        self.assertEqual(StreamEventData.objects.filter(stream_slug=new_stream1).count(), 2)
        self.assertEqual(StreamNote.objects.filter(target_slug=new_stream1).count(), 3)
        self.assertEqual(StreamData.objects.filter(stream_slug=new_stream2).count(), 2)
        self.assertEqual(StreamEventData.objects.filter(stream_slug=new_stream2).count(), 1)

        block = DataBlock.objects.first()
        self.assertIsNotNone(block.completed_on)
        self.assertIsNotNone(block.sg)
        self.assertEqual(block.sg, sg)

    def testDataBlockActionTestDataMask(self):
        sg = SensorGraph.objects.create(name='SG 1',
                                        major_version=1,
                                        created_by=self.u1, org=self.o1)
        device = Device.objects.create_device(project=self.p1, label='d3', template=self.dt1, sg=sg, created_by=self.u2)
        block = DataBlock.objects.create(org=self.o1, title='test', device=device, block=1, created_by=self.u1)
        stream1 = StreamId.objects.create_stream(
            project=self.p1, variable=self.v1, device=device, created_by=self.u2
        )

        dt1 = dateutil.parser.parse('2017-09-28T10:00:00Z')
        dt2 = dateutil.parser.parse('2017-09-28T11:00:00Z')
        dt3 = dateutil.parser.parse('2017-09-30T10:00:00Z')
        dt4 = dateutil.parser.parse('2017-09-30T10:10:00Z')
        dt5 = dateutil.parser.parse('2017-09-30T10:20:00Z')

        set_data_mask(device, '2017-09-28T10:30:00Z', '2017-09-30T10:15:00Z', [], [], self.u1)

        StreamData.objects.create(
            stream_slug=stream1.slug,
            type='Num',
            timestamp=dt1,
            int_value=5
        )
        StreamData.objects.create(
            stream_slug=stream1.slug,
            type='Num',
            timestamp=dt2,
            int_value=6
        )
        StreamData.objects.create(
            stream_slug=stream1.slug,
            type='Num',
            timestamp=dt3,
            int_value=7
        )
        StreamData.objects.create(
            stream_slug=stream1.slug,
            type='Num',
            timestamp=dt4,
            int_value=8
        )
        StreamData.objects.create(
            stream_slug=stream1.slug,
            type='Num',
            timestamp=dt5,
            int_value=9
        )

        self.assertEqual(device.streamids.count(), 1)

        data_mask_event = get_data_mask_event(device)
        mask_slug = data_mask_event.stream_slug
        self.assertEqual(StreamData.objects.filter(stream_slug=stream1.slug).count(), 5)
        self.assertEqual(StreamEventData.objects.filter(stream_slug=mask_slug).count(), 1)

        action = ArchiveDeviceDataAction()
        action._block = block
        action._device = device
        action.execute(arguments={'data_block_slug': block.slug})

        self.assertEqual(device.streamids.count(), 2)

        self.assertEqual(StreamData.objects.filter(stream_slug=stream1.slug).count(), 0)
        self.assertEqual(StreamEventData.objects.filter(stream_slug=mask_slug).count(), 0)

        data_mask_event = get_data_mask_event(block)
        self.assertEqual(StreamEventData.objects.filter(stream_slug=data_mask_event.stream_slug).count(), 1)


[(27, 'django.contrib.auth.get_user_model', 'get_user_model', False, 'from django.contrib.auth import get_user_model\n'), (36, 'apps.stream.models.StreamVariable.objects.create_variable', 'StreamVariable.objects.create_variable', False, 'from apps.stream.models import StreamId, StreamVariable\n'), (39, 'apps.stream.models.StreamVariable.objects.create_variable', 'StreamVariable.objects.create_variable', False, 'from apps.stream.models import StreamId, StreamVariable\n'), (42, 'apps.physicaldevice.models.Device.objects.create_device', 'Device.objects.create_device', False, 'from apps.physicaldevice.models import Device\n'), (43, 'apps.physicaldevice.models.Device.objects.create_device', 'Device.objects.create_device', False, 'from apps.physicaldevice.models import Device\n'), (44, 'apps.stream.models.StreamId.objects.create_after_new_device', 'StreamId.objects.create_after_new_device', False, 'from apps.stream.models import StreamId, StreamVariable\n'), (45, 'apps.stream.models.StreamId.objects.create_after_new_device', 'StreamId.objects.create_after_new_device', False, 'from apps.stream.models import StreamId, StreamVariable\n'), (89, 'apps.property.models.GenericProperty.objects.create_int_property', 'GenericProperty.objects.create_int_property', False, 'from apps.property.models import GenericProperty\n'), (92, 'apps.property.models.GenericProperty.objects.create_str_property', 'GenericProperty.objects.create_str_property', False, 'from apps.property.models import GenericProperty\n'), (95, 'apps.property.models.GenericProperty.objects.create_bool_property', 'GenericProperty.objects.create_bool_property', False, 'from apps.property.models import GenericProperty\n'), (110, 'apps.physicaldevice.models.Device.objects.create_device', 'Device.objects.create_device', False, 'from apps.physicaldevice.models import Device\n'), (112, 'apps.stream.models.StreamId.objects.create_stream', 'StreamId.objects.create_stream', False, 'from apps.stream.models import StreamId, StreamVariable\n'), (115, 'apps.stream.models.StreamId.objects.create_stream', 'StreamId.objects.create_stream', False, 'from apps.stream.models import StreamId, StreamVariable\n'), (118, 'apps.stream.models.StreamVariable.objects.create_variable', 'StreamVariable.objects.create_variable', False, 'from apps.stream.models import StreamId, StreamVariable\n'), (121, 'apps.stream.models.StreamId.objects.create_stream', 'StreamId.objects.create_stream', False, 'from apps.stream.models import StreamId, StreamVariable\n'), (125, 'apps.stream.models.StreamId.objects.count', 'StreamId.objects.count', False, 'from apps.stream.models import StreamId, StreamVariable\n'), (136, 'apps.physicaldevice.models.Device.objects.create_device', 'Device.objects.create_device', False, 'from apps.physicaldevice.models import Device\n'), (138, 'apps.stream.models.StreamId.objects.create_stream', 'StreamId.objects.create_stream', False, 'from apps.stream.models import StreamId, StreamVariable\n'), (141, 'apps.stream.models.StreamId.objects.create_stream', 'StreamId.objects.create_stream', False, 'from apps.stream.models import StreamId, StreamVariable\n'), (203, 'apps.physicaldevice.models.Device.objects.create_device', 'Device.objects.create_device', False, 'from apps.physicaldevice.models import Device\n'), (205, 'apps.stream.models.StreamId.objects.create_stream', 'StreamId.objects.create_stream', False, 'from apps.stream.models import StreamId, StreamVariable\n'), (208, 'apps.stream.models.StreamId.objects.create_stream', 'StreamId.objects.create_stream', False, 'from apps.stream.models import StreamId, StreamVariable\n'), (251, 'apps.physicaldevice.models.Device.objects.create_device', 'Device.objects.create_device', False, 'from apps.physicaldevice.models import Device\n'), (253, 'apps.stream.models.StreamId.objects.create_stream', 'StreamId.objects.create_stream', False, 'from apps.stream.models import StreamId, StreamVariable\n'), (256, 'apps.stream.models.StreamId.objects.create_stream', 'StreamId.objects.create_stream', False, 'from apps.stream.models import StreamId, StreamVariable\n'), (307, 'apps.physicaldevice.models.Device.objects.create_device', 'Device.objects.create_device', False, 'from apps.physicaldevice.models import Device\n'), (346, 'apps.report.models.GeneratedUserReport.objects.create', 'GeneratedUserReport.objects.create', False, 'from apps.report.models import GeneratedUserReport\n'), (352, 'apps.report.models.GeneratedUserReport.objects.create', 'GeneratedUserReport.objects.create', False, 'from apps.report.models import GeneratedUserReport\n'), (373, 'apps.physicaldevice.models.Device.objects.create_device', 'Device.objects.create_device', False, 'from apps.physicaldevice.models import Device\n'), (375, 'apps.stream.models.StreamId.objects.create_stream', 'StreamId.objects.create_stream', False, 'from apps.stream.models import StreamId, StreamVariable\n'), (378, 'apps.stream.models.StreamId.objects.create_stream', 'StreamId.objects.create_stream', False, 'from apps.stream.models import StreamId, StreamVariable\n'), (382, 'apps.property.models.GenericProperty.objects.create_int_property', 'GenericProperty.objects.create_int_property', False, 'from apps.property.models import GenericProperty\n'), (385, 'apps.property.models.GenericProperty.objects.create_str_property', 'GenericProperty.objects.create_str_property', False, 'from apps.property.models import GenericProperty\n'), (388, 'apps.property.models.GenericProperty.objects.create_bool_property', 'GenericProperty.objects.create_bool_property', False, 'from apps.property.models import GenericProperty\n'), (518, 'apps.physicaldevice.models.Device.objects.create_device', 'Device.objects.create_device', False, 'from apps.physicaldevice.models import Device\n'), (520, 'apps.stream.models.StreamId.objects.create_stream', 'StreamId.objects.create_stream', False, 'from apps.stream.models import StreamId, StreamVariable\n'), (530, 'apps.utils.data_mask.mask_utils.set_data_mask', 'set_data_mask', False, 'from apps.utils.data_mask.mask_utils import get_data_mask_event, set_data_mask\n'), (532, 'apps.streamdata.models.StreamData.objects.create', 'StreamData.objects.create', False, 'from apps.streamdata.models import StreamData\n'), (538, 'apps.streamdata.models.StreamData.objects.create', 'StreamData.objects.create', False, 'from apps.streamdata.models import StreamData\n'), (544, 'apps.streamdata.models.StreamData.objects.create', 'StreamData.objects.create', False, 'from apps.streamdata.models import StreamData\n'), (550, 'apps.streamdata.models.StreamData.objects.create', 'StreamData.objects.create', False, 'from apps.streamdata.models import StreamData\n'), (556, 'apps.streamdata.models.StreamData.objects.create', 'StreamData.objects.create', False, 'from apps.streamdata.models import StreamData\n'), (565, 'apps.utils.data_mask.mask_utils.get_data_mask_event', 'get_data_mask_event', False, 'from apps.utils.data_mask.mask_utils import get_data_mask_event, set_data_mask\n'), (580, 'apps.utils.data_mask.mask_utils.get_data_mask_event', 'get_data_mask_event', False, 'from apps.utils.data_mask.mask_utils import get_data_mask_event, set_data_mask\n'), (132, 'apps.stream.models.StreamId.objects.count', 'StreamId.objects.count', False, 'from apps.stream.models import StreamId, StreamVariable\n'), (285, 'apps.streamnote.models.StreamNote.objects.count', 'StreamNote.objects.count', False, 'from apps.streamnote.models import StreamNote\n'), (301, 'apps.streamnote.models.StreamNote.objects.count', 'StreamNote.objects.count', False, 'from apps.streamnote.models import StreamNote\n'), (329, 'apps.devicelocation.models.DeviceLocation.objects.count', 'DeviceLocation.objects.count', False, 'from apps.devicelocation.models import DeviceLocation\n'), (46, 'apps.stream.models.StreamId.objects.filter', 'StreamId.objects.filter', False, 'from apps.stream.models import StreamId, StreamVariable\n'), (47, 'apps.stream.models.StreamId.objects.filter', 'StreamId.objects.filter', False, 'from apps.stream.models import StreamId, StreamVariable\n'), (53, 'apps.stream.models.StreamId.objects.all', 'StreamId.objects.all', False, 'from apps.stream.models import StreamId, StreamVariable\n'), (54, 'apps.stream.models.StreamVariable.objects.all', 'StreamVariable.objects.all', False, 'from apps.stream.models import StreamId, StreamVariable\n'), (55, 'apps.property.models.GenericProperty.objects.all', 'GenericProperty.objects.all', False, 'from apps.property.models import GenericProperty\n'), (56, 'apps.physicaldevice.models.Device.objects.all', 'Device.objects.all', False, 'from apps.physicaldevice.models import Device\n'), (57, 'apps.streamdata.models.StreamData.objects.all', 'StreamData.objects.all', False, 'from apps.streamdata.models import StreamData\n'), (58, 'apps.streamevent.models.StreamEventData.objects.all', 'StreamEventData.objects.all', False, 'from apps.streamevent.models import StreamEventData\n'), (148, 'django.utils.timezone.now', 'timezone.now', False, 'from django.utils import timezone\n'), (155, 'django.utils.timezone.now', 'timezone.now', False, 'from django.utils import timezone\n'), (162, 'django.utils.timezone.now', 'timezone.now', False, 'from django.utils import timezone\n'), (169, 'django.utils.timezone.now', 'timezone.now', False, 'from django.utils import timezone\n'), (176, 'django.utils.timezone.now', 'timezone.now', False, 'from django.utils import timezone\n'), (213, 'django.utils.timezone.now', 'timezone.now', False, 'from django.utils import timezone\n'), (219, 'django.utils.timezone.now', 'timezone.now', False, 'from django.utils import timezone\n'), (225, 'django.utils.timezone.now', 'timezone.now', False, 'from django.utils import timezone\n'), (262, 'django.utils.timezone.now', 'timezone.now', False, 'from django.utils import timezone\n'), (269, 'django.utils.timezone.now', 'timezone.now', False, 'from django.utils import timezone\n'), (275, 'django.utils.timezone.now', 'timezone.now', False, 'from django.utils import timezone\n'), (281, 'django.utils.timezone.now', 'timezone.now', False, 'from django.utils import timezone\n'), (311, 'django.utils.timezone.now', 'timezone.now', False, 'from django.utils import timezone\n'), (317, 'django.utils.timezone.now', 'timezone.now', False, 'from django.utils import timezone\n'), (323, 'django.utils.timezone.now', 'timezone.now', False, 'from django.utils import timezone\n'), (392, 'django.utils.timezone.now', 'timezone.now', False, 'from django.utils import timezone\n'), (398, 'django.utils.timezone.now', 'timezone.now', False, 'from django.utils import timezone\n'), (404, 'django.utils.timezone.now', 'timezone.now', False, 'from django.utils import timezone\n'), (412, 'django.utils.timezone.now', 'timezone.now', False, 'from django.utils import timezone\n'), (419, 'django.utils.timezone.now', 'timezone.now', False, 'from django.utils import timezone\n'), (426, 'django.utils.timezone.now', 'timezone.now', False, 'from django.utils import timezone\n'), (433, 'django.utils.timezone.now', 'timezone.now', False, 'from django.utils import timezone\n'), (440, 'django.utils.timezone.now', 'timezone.now', False, 'from django.utils import timezone\n'), (446, 'django.utils.timezone.now', 'timezone.now', False, 'from django.utils import timezone\n'), (452, 'django.utils.timezone.now', 'timezone.now', False, 'from django.utils import timezone\n'), (458, 'django.utils.timezone.now', 'timezone.now', False, 'from django.utils import timezone\n'), (464, 'django.utils.timezone.now', 'timezone.now', False, 'from django.utils import timezone\n'), (98, 'apps.property.models.GenericProperty.objects.object_properties_qs', 'GenericProperty.objects.object_properties_qs', False, 'from apps.property.models import GenericProperty\n'), (99, 'apps.property.models.GenericProperty.objects.object_properties_qs', 'GenericProperty.objects.object_properties_qs', False, 'from apps.property.models import GenericProperty\n'), (105, 'apps.property.models.GenericProperty.objects.object_properties_qs', 'GenericProperty.objects.object_properties_qs', False, 'from apps.property.models import GenericProperty\n'), (106, 'apps.property.models.GenericProperty.objects.object_properties_qs', 'GenericProperty.objects.object_properties_qs', False, 'from apps.property.models import GenericProperty\n'), (186, 'apps.streamdata.models.StreamData.objects.filter', 'StreamData.objects.filter', False, 'from apps.streamdata.models import StreamData\n'), (187, 'apps.streamdata.models.StreamData.objects.filter', 'StreamData.objects.filter', False, 'from apps.streamdata.models import StreamData\n'), (191, 'apps.streamdata.models.StreamData.objects.filter', 'StreamData.objects.filter', False, 'from apps.streamdata.models import StreamData\n'), (192, 'apps.streamdata.models.StreamData.objects.filter', 'StreamData.objects.filter', False, 'from apps.streamdata.models import StreamData\n'), (195, 'apps.streamdata.models.StreamData.objects.filter', 'StreamData.objects.filter', False, 'from apps.streamdata.models import StreamData\n'), (197, 'apps.streamdata.models.StreamData.objects.filter', 'StreamData.objects.filter', False, 'from apps.streamdata.models import StreamData\n'), (236, 'apps.streamevent.models.StreamEventData.objects.filter', 'StreamEventData.objects.filter', False, 'from apps.streamevent.models import StreamEventData\n'), (237, 'apps.streamevent.models.StreamEventData.objects.filter', 'StreamEventData.objects.filter', False, 'from apps.streamevent.models import StreamEventData\n'), (241, 'apps.streamevent.models.StreamEventData.objects.filter', 'StreamEventData.objects.filter', False, 'from apps.streamevent.models import StreamEventData\n'), (242, 'apps.streamevent.models.StreamEventData.objects.filter', 'StreamEventData.objects.filter', False, 'from apps.streamevent.models import StreamEventData\n'), (245, 'apps.streamevent.models.StreamEventData.objects.filter', 'StreamEventData.objects.filter', False, 'from apps.streamevent.models import StreamEventData\n'), (247, 'apps.streamevent.models.StreamEventData.objects.filter', 'StreamEventData.objects.filter', False, 'from apps.streamevent.models import StreamEventData\n'), (292, 'apps.streamnote.models.StreamNote.objects.filter', 'StreamNote.objects.filter', False, 'from apps.streamnote.models import StreamNote\n'), (293, 'apps.streamnote.models.StreamNote.objects.filter', 'StreamNote.objects.filter', False, 'from apps.streamnote.models import StreamNote\n'), (297, 'apps.streamnote.models.StreamNote.objects.filter', 'StreamNote.objects.filter', False, 'from apps.streamnote.models import StreamNote\n'), (298, 'apps.streamnote.models.StreamNote.objects.filter', 'StreamNote.objects.filter', False, 'from apps.streamnote.models import StreamNote\n'), (302, 'apps.streamnote.models.StreamNote.objects.filter', 'StreamNote.objects.filter', False, 'from apps.streamnote.models import StreamNote\n'), (303, 'apps.streamnote.models.StreamNote.objects.filter', 'StreamNote.objects.filter', False, 'from apps.streamnote.models import StreamNote\n'), (335, 'apps.devicelocation.models.DeviceLocation.objects.filter', 'DeviceLocation.objects.filter', False, 'from apps.devicelocation.models import DeviceLocation\n'), (339, 'apps.devicelocation.models.DeviceLocation.objects.filter', 'DeviceLocation.objects.filter', False, 'from apps.devicelocation.models import DeviceLocation\n'), (340, 'apps.devicelocation.models.DeviceLocation.objects.filter', 'DeviceLocation.objects.filter', False, 'from apps.devicelocation.models import DeviceLocation\n'), (359, 'apps.report.models.GeneratedUserReport.objects.filter', 'GeneratedUserReport.objects.filter', False, 'from apps.report.models import GeneratedUserReport\n'), (360, 'apps.report.models.GeneratedUserReport.objects.filter', 'GeneratedUserReport.objects.filter', False, 'from apps.report.models import GeneratedUserReport\n'), (366, 'apps.report.models.GeneratedUserReport.objects.filter', 'GeneratedUserReport.objects.filter', False, 'from apps.report.models import GeneratedUserReport\n'), (367, 'apps.report.models.GeneratedUserReport.objects.filter', 'GeneratedUserReport.objects.filter', False, 'from apps.report.models import GeneratedUserReport\n'), (469, 'apps.property.models.GenericProperty.objects.object_properties_qs', 'GenericProperty.objects.object_properties_qs', False, 'from apps.property.models import GenericProperty\n'), (470, 'apps.property.models.GenericProperty.objects.object_properties_qs', 'GenericProperty.objects.object_properties_qs', False, 'from apps.property.models import GenericProperty\n'), (474, 'apps.streamdata.models.StreamData.objects.filter', 'StreamData.objects.filter', False, 'from apps.streamdata.models import StreamData\n'), (475, 'apps.streamdata.models.StreamData.objects.filter', 'StreamData.objects.filter', False, 'from apps.streamdata.models import StreamData\n'), (476, 'apps.streamevent.models.StreamEventData.objects.filter', 'StreamEventData.objects.filter', False, 'from apps.streamevent.models import StreamEventData\n'), (477, 'apps.streamevent.models.StreamEventData.objects.filter', 'StreamEventData.objects.filter', False, 'from apps.streamevent.models import StreamEventData\n'), (478, 'apps.streamnote.models.StreamNote.objects.filter', 'StreamNote.objects.filter', False, 'from apps.streamnote.models import StreamNote\n'), (479, 'apps.streamnote.models.StreamNote.objects.filter', 'StreamNote.objects.filter', False, 'from apps.streamnote.models import StreamNote\n'), (486, 'apps.property.models.GenericProperty.objects.object_properties_qs', 'GenericProperty.objects.object_properties_qs', False, 'from apps.property.models import GenericProperty\n'), (487, 'apps.property.models.GenericProperty.objects.object_properties_qs', 'GenericProperty.objects.object_properties_qs', False, 'from apps.property.models import GenericProperty\n'), (491, 'apps.streamdata.models.StreamData.objects.filter', 'StreamData.objects.filter', False, 'from apps.streamdata.models import StreamData\n'), (492, 'apps.streamdata.models.StreamData.objects.filter', 'StreamData.objects.filter', False, 'from apps.streamdata.models import StreamData\n'), (493, 'apps.streamevent.models.StreamEventData.objects.filter', 'StreamEventData.objects.filter', False, 'from apps.streamevent.models import StreamEventData\n'), (494, 'apps.streamevent.models.StreamEventData.objects.filter', 'StreamEventData.objects.filter', False, 'from apps.streamevent.models import StreamEventData\n'), (495, 'apps.streamnote.models.StreamNote.objects.filter', 'StreamNote.objects.filter', False, 'from apps.streamnote.models import StreamNote\n'), (496, 'apps.streamnote.models.StreamNote.objects.filter', 'StreamNote.objects.filter', False, 'from apps.streamnote.models import StreamNote\n'), (499, 'apps.stream.models.StreamId.objects.filter', 'StreamId.objects.filter', False, 'from apps.stream.models import StreamId, StreamVariable\n'), (501, 'apps.stream.models.StreamId.objects.filter', 'StreamId.objects.filter', False, 'from apps.stream.models import StreamId, StreamVariable\n'), (503, 'apps.streamdata.models.StreamData.objects.filter', 'StreamData.objects.filter', False, 'from apps.streamdata.models import StreamData\n'), (504, 'apps.streamevent.models.StreamEventData.objects.filter', 'StreamEventData.objects.filter', False, 'from apps.streamevent.models import StreamEventData\n'), (505, 'apps.streamnote.models.StreamNote.objects.filter', 'StreamNote.objects.filter', False, 'from apps.streamnote.models import StreamNote\n'), (506, 'apps.streamdata.models.StreamData.objects.filter', 'StreamData.objects.filter', False, 'from apps.streamdata.models import StreamData\n'), (507, 'apps.streamevent.models.StreamEventData.objects.filter', 'StreamEventData.objects.filter', False, 'from apps.streamevent.models import StreamEventData\n'), (567, 'apps.streamdata.models.StreamData.objects.filter', 'StreamData.objects.filter', False, 'from apps.streamdata.models import StreamData\n'), (568, 'apps.streamevent.models.StreamEventData.objects.filter', 'StreamEventData.objects.filter', False, 'from apps.streamevent.models import StreamEventData\n'), (577, 'apps.streamdata.models.StreamData.objects.filter', 'StreamData.objects.filter', False, 'from apps.streamdata.models import StreamData\n'), (578, 'apps.streamevent.models.StreamEventData.objects.filter', 'StreamEventData.objects.filter', False, 'from apps.streamevent.models import StreamEventData\n'), (581, 'apps.streamevent.models.StreamEventData.objects.filter', 'StreamEventData.objects.filter', False, 'from apps.streamevent.models import StreamEventData\n'), (199, 'apps.streamdata.models.StreamData.objects.filter', 'StreamData.objects.filter', False, 'from apps.streamdata.models import StreamData\n')]