def test__check_lead_duplication_fail_if_your_leads_found_and_audited_by_others(
         self):
     you = User(id=1)
     your_project = Project(user=you)
     your_pixel = Pixel(project=your_project)
     someone = User(id=2)
     sometwo = User(id=3)
     someone_audit1 = Audit(processed=strptime('2018-02-26'), user=someone)
     sometwo_audit2 = Audit(processed=strptime('2018-02-27'), user=sometwo)
     your_lead1 = Lead(pixel=your_pixel, created=strptime('2018-02-24'))
     your_lead2 = Lead(pixel=your_pixel, created=strptime('2018-02-25'))
     your_lead3 = Lead(pixel=your_pixel, created=strptime('2018-02-26'))
     with mock.patch(
             'collector.models.analytics.Lead.audits') as mock_manager:
         mock_manager.all.return_value = [someone_audit1, sometwo_audit2]
         leads = [
             your_lead1,
             your_lead2,
             your_lead3,
         ]
         res = lead_duplication._check_lead_duplication(you, leads)
         self.assertEquals(
             {
                 'success': False,
                 'resolution': 'Lead was sold 2 times',
                 'created': strptime('2018-02-26'),
                 'your_audits': [],
                 'duplicates':
                 [strptime('2018-02-25'),
                  strptime('2018-02-24')],
                 'sales': [strptime('2018-02-27'),
                           strptime('2018-02-26')]
             }, res)
 def test__find_your_last_lead_none_if_no_your_leads(self):
     you = User(id=1)
     someone = User(id=2)
     someone_project = Project(user=someone)
     someone_pixel = Pixel(project=someone_project)
     someone_lead1 = Lead(pixel=someone_pixel,
                          created=strptime('2018-02-24'))
     someone_lead2 = Lead(pixel=someone_pixel,
                          created=strptime('2018-02-25'))
     leads = [
         someone_lead1,
         someone_lead2,
     ]
     yll = lead_duplication._find_your_last_lead(you, leads)
     self.assertIsNone(yll)
 def test__check_lead_duplication_success_if_one_your_lead_found(self):
     you = User(id=1)
     your_project = Project(user=you)
     your_pixel = Pixel(project=your_project)
     leads = [Lead(pixel=your_pixel, created=strptime('2018-02-26'))]
     res = lead_duplication._check_lead_duplication(you, leads)
     self.assertEquals(
         {
             'success': True,
             'resolution': 'New lead',
             'created': strptime('2018-02-26'),
             'your_audits': [],
             'duplicates': [],
             'sales': []
         }, res)
Esempio n. 4
0
 def test__load_session_ids_must_not_return_session_with_events_older_than_5_minutes(
         self):
     # in db we have session with last event "2018-01-18T09:43"
     now = strptime('2018-01-18 09:49', '%Y-%m-%d %H:%M')
     cmd = fill_leads.Command()
     session_ids = list(cmd._load_session_ids(now))
     self.assertEquals([], session_ids)
Esempio n. 5
0
    def handle(self, fill_date=None, *args, **options):
        now = timezone.now()
        started = now
        if fill_date is None:
            fill_date = (now - timedelta(days=1)).date()
        else:
            fill_date = strptime(fill_date).date()

        IpStat.objects.filter(date=fill_date).delete()

        ip_stat_total = self._load_period_ipstat(fill_date, IP_STAT_MAX_PERIOD)
        ip_stat_30days = self._load_period_ipstat(fill_date,
                                                  timedelta(days=30))
        ip_stat_10days = self._load_period_ipstat(fill_date,
                                                  timedelta(days=10))
        ip_stat_3days = self._load_period_ipstat(fill_date, timedelta(days=3))

        res = {}
        res = self._collect_period_ipstat(res, fill_date, ip_stat_total, '')
        res = self._collect_period_ipstat(res, fill_date, ip_stat_30days, '30')
        res = self._collect_period_ipstat(res, fill_date, ip_stat_10days, '10')
        res = self._collect_period_ipstat(res, fill_date, ip_stat_3days, '3')
        if res:
            IpStat.objects.bulk_create(res.values())

        self._save_csv(res.values(), fill_date)

        logger.info('Fill {} ip_stats in {}s'.format(
            len(res), (timezone.now() - started).total_seconds()))
Esempio n. 6
0
 def test__load_session_ids_must_not_return_session_without_events_between_from_and_to(
         self):
     # in db we have session with last event "2018-01-18T09:43:.."
     now = strptime('2018-01-18 09:45', '%Y-%m-%d %H:%M')
     cmd = fill_leads.Command()
     session_ids = list(
         cmd._load_session_ids(now, '2017-01-17', '2017-01-18'))
     self.assertEquals([], session_ids)
Esempio n. 7
0
 def test__load_session_ids_must_return_session_with_5_minutes_events_old(
         self):
     # in db we have session with last event "2018-01-18T09:43:.."
     now = strptime('2018-01-18 09:45', '%Y-%m-%d %H:%M')
     cmd = fill_leads.Command()
     session_ids = list(cmd._load_session_ids(now))
     self.assertEquals([uuid.UUID('99a9086a-94b7-4807-a1a6-67614b8afaec')],
                       session_ids)
 def test__find_your_last_lead_return_yll_if_your_and_other_leads(self):
     you = User(id=1)
     your_project = Project(user=you)
     your_pixel = Pixel(project=your_project)
     someone = User(id=2)
     someone_project = Project(user=someone)
     someone_pixel = Pixel(project=someone_project)
     your_lead1 = Lead(pixel=your_pixel, created=strptime('2018-02-25'))
     your_lead2 = Lead(pixel=your_pixel, created=strptime('2018-02-24'))
     someone_lead1 = Lead(pixel=someone_pixel,
                          created=strptime('2018-02-26'))
     leads = [
         your_lead1,
         someone_lead1,
         your_lead2,
     ]
     yll = lead_duplication._find_your_last_lead(you, leads)
     self.assertEquals(your_lead1, yll)
 def test__check_lead_duplication_success_if_one_your_lead_found_and_audited_by_you(
         self):
     you = User(id=1)
     your_project = Project(user=you)
     your_pixel = Pixel(project=your_project)
     your_audit = Audit(processed=strptime('2018-02-27'), user=you)
     your_lead = Lead(pixel=your_pixel, created=strptime('2018-02-26'))
     with mock.patch(
             'collector.models.analytics.Lead.audits') as mock_manager:
         mock_manager.all.return_value = [your_audit]
         leads = [your_lead]
         res = lead_duplication._check_lead_duplication(you, leads)
         self.assertEquals(
             {
                 'success': True,
                 'resolution': 'You already audited this lead 1 times',
                 'created': strptime('2018-02-26'),
                 'your_audits': [strptime('2018-02-27')],
                 'duplicates': [],
                 'sales': []
             }, res)
Esempio n. 10
0
    def _load_session_ids(self, now, date_from=None, date_to=None):
        """
        by default we load sessions which last event younger than DEFAULT_PERIOD
        else when events exists between date_from and date_to
        :param now:
        :param date_from:
        :param date_to:
        :return:
        """
        date_from = strptime(date_from) if date_from else now - DEFAULT_PERIOD
        if date_to:
            date_to = strptime(date_to).replace(hour=23,
                                                minute=59,
                                                second=59,
                                                microsecond=999999)
        else:
            date_to = now

        session_ids = Event.objects. \
            filter(finished__range=(date_from, date_to)) \
            .values_list('session_id', flat=True).distinct()

        return session_ids
Esempio n. 11
0
 def test__check_lead_duplication_fail_if_only_others_leads_found(self):
     you = User(id=1)
     someone = User(id=2)
     someone_project = Project(user=someone)
     someone_pixel = Pixel(project=someone_project)
     someone1_lead = Lead(pixel=someone_pixel,
                          created=strptime('2018-02-25'))
     someone2_lead = Lead(pixel=someone_pixel,
                          created=strptime('2018-02-26'))
     leads = [
         someone2_lead,
         someone1_lead,
     ]
     res = lead_duplication._check_lead_duplication(you, leads)
     self.assertEquals(
         {
             'success': False,
             'resolution': 'Lead was sold to you outside the system',
             'created': None,
             'your_audits': [],
             'duplicates': [],
             'sales': [strptime('2018-02-26')]
         }, res)
Esempio n. 12
0
 def test__find_duplicates_exclude_yll_and_alien_leads(self):
     you = User(id=1)
     your_project = Project(user=you)
     your_pixel = Pixel(project=your_project)
     your_lead1 = Lead(pixel=your_pixel, created=strptime('2018-02-24'))
     your_lead2 = Lead(pixel=your_pixel, created=strptime('2018-02-25'))
     yll = Lead(pixel=your_pixel, created=strptime('2018-02-26'))
     someone = User(id=2)
     someone_project = Project(user=someone)
     someone_pixel = Pixel(project=someone_project)
     someone_lead1 = Lead(pixel=someone_pixel,
                          created=strptime('2018-02-26'))
     leads = [
         your_lead2,
         yll,
         someone_lead1,
         your_lead1,
     ]
     duplicates = lead_duplication._find_duplicates(yll, leads)
     self.assertEquals([strptime('2018-02-25'),
                        strptime('2018-02-24')], duplicates)
Esempio n. 13
0
 def test__check_lead_duplication_fail_if_only_your_leads_found(self):
     you = User(id=1)
     your_project = Project(user=you)
     your_pixel = Pixel(project=your_project)
     your_lead1 = Lead(pixel=your_pixel, created=strptime('2018-02-24'))
     your_lead2 = Lead(pixel=your_pixel, created=strptime('2018-02-25'))
     your_lead3 = Lead(pixel=your_pixel, created=strptime('2018-02-26'))
     leads = [
         your_lead1,
         your_lead2,
         your_lead3,
     ]
     res = lead_duplication._check_lead_duplication(you, leads)
     self.assertEquals(
         {
             'success': False,
             'resolution': 'Lead has 2 duplicates',
             'created': strptime('2018-02-26'),
             'your_audits': [],
             'duplicates': [strptime('2018-02-25'),
                            strptime('2018-02-24')],
             'sales': []
         }, res)