Exemplo n.º 1
0
    def test_fetch_old_sites_count(self):
        self.clear_cache()
        with patch("ureport.utils.datetime_to_ms") as mock_datetime_ms:
            mock_datetime_ms.return_value = 500

            with patch('requests.get') as mock_get:
                mock_get.side_effect = [MockResponse(200, '300'), MockResponse(200, '50\n')]

                with patch('django.core.cache.cache.set') as cache_set_mock:
                    cache_set_mock.return_value = "Set"

                    with patch('django.core.cache.cache.delete') as cache_delete_mock:
                        cache_delete_mock.return_value = "Deleted"

                        fetch_old_sites_count()
                        self.assertEqual(mock_get.call_count, 2)
                        mock_get.assert_any_call('http://ureport.ug/count.txt')
                        mock_get.assert_any_call('http://www.zambiaureport.org/count.txt/')

                        self.assertEqual(cache_set_mock.call_count, 2)
                        cache_set_mock.assert_any_call('org:uganda:reporters:old-site',
                                                       {'time': 500, 'results': dict(size=300)},
                                                       UREPORT_ASYNC_FETCHED_DATA_CACHE_TIME)

                        cache_set_mock.assert_any_call('org:zambia:reporters:old-site',
                                                       {'time': 500, 'results': dict(size=50)},
                                                       UREPORT_ASYNC_FETCHED_DATA_CACHE_TIME)

                        cache_delete_mock.assert_called_once_with(GLOBAL_COUNT_CACHE_KEY)
Exemplo n.º 2
0
def fetch_old_sites_count():
    start = time.time()
    r = get_redis_connection()

    key = 'fetch_old_sites_count_lock'
    lock_timeout = 60

    if not r.get(key):
        with r.lock(key, timeout=lock_timeout):
            fetch_old_sites_count()
            print "Task: fetch_old_sites_count took %ss" % (time.time() - start)
Exemplo n.º 3
0
def fetch_old_sites_count():
    start = time.time()
    r = get_redis_connection()

    key = 'fetch_old_sites_count_lock'
    lock_timeout = 60

    if not r.get(key):
        with r.lock(key, timeout=lock_timeout):
            fetch_old_sites_count()
            print "Task: fetch_old_sites_count took %ss" % (time.time() -
                                                            start)
Exemplo n.º 4
0
    def test_fetch_old_sites_count(self):
        self.clear_cache()

        settings_sites_count = len(list(getattr(settings, "PREVIOUS_ORG_SITES", []))) + len(
            list(getattr(settings, "OTHER_ORG_COUNT_SITES", []))
        )

        with patch("ureport.utils.datetime_to_ms") as mock_datetime_ms:
            mock_datetime_ms.return_value = 500

            with patch("requests.get") as mock_get:
                mock_get.return_value = MockResponse(200, b"300")

                with patch("django.core.cache.cache.set") as cache_set_mock:
                    cache_set_mock.return_value = "Set"

                    with patch("django.core.cache.cache.delete") as cache_delete_mock:
                        cache_delete_mock.return_value = "Deleted"

                        old_site_values = fetch_old_sites_count()
                        self.assertEqual(
                            old_site_values, [{"time": 500, "results": dict(size=300)}] * settings_sites_count
                        )

                        mock_get.assert_called_with("https://nigeria24x7.ureport.in/count/")

                        cache_set_mock.assert_called_with(
                            "org:nigeria24x7:reporters:old-site",
                            {"time": 500, "results": dict(size=300)},
                            UREPORT_ASYNC_FETCHED_DATA_CACHE_TIME,
                        )

                        cache_delete_mock.assert_called_once_with(GLOBAL_COUNT_CACHE_KEY)
Exemplo n.º 5
0
    def test_fetch_old_sites_count(self):
        self.clear_cache()

        settings_sites_count = len(list(getattr(settings, 'PREVIOUS_ORG_SITES', [])))

        with patch("ureport.utils.datetime_to_ms") as mock_datetime_ms:
            mock_datetime_ms.return_value = 500

            with patch('requests.get') as mock_get:
                mock_get.return_value = MockResponse(200, '300')

                with patch('django.core.cache.cache.set') as cache_set_mock:
                    cache_set_mock.return_value = "Set"

                    with patch('django.core.cache.cache.delete') as cache_delete_mock:
                        cache_delete_mock.return_value = "Deleted"

                        old_site_values = fetch_old_sites_count()
                        self.assertEqual(old_site_values,
                                         [{'time': 500, 'results': dict(size=300)}] * settings_sites_count)

                        mock_get.assert_called_with('https://www.zambiaureport.com/count.txt/')

                        cache_set_mock.assert_called_with('org:zambia:reporters:old-site',
                                                          {'time': 500, 'results': dict(size=300)},
                                                          UREPORT_ASYNC_FETCHED_DATA_CACHE_TIME)

                        cache_delete_mock.assert_called_once_with(GLOBAL_COUNT_CACHE_KEY)
Exemplo n.º 6
0
    def test_fetch_old_sites_count(self):
        self.clear_cache()

        settings_sites_count = len(list(getattr(settings, "PREVIOUS_ORG_SITES", [])))

        with patch("ureport.utils.datetime_to_ms") as mock_datetime_ms:
            mock_datetime_ms.return_value = 500

            with patch("requests.get") as mock_get:
                mock_get.return_value = MockResponse(200, b"300")

                with patch("django.core.cache.cache.set") as cache_set_mock:
                    cache_set_mock.return_value = "Set"

                    with patch("django.core.cache.cache.delete") as cache_delete_mock:
                        cache_delete_mock.return_value = "Deleted"

                        old_site_values = fetch_old_sites_count()
                        self.assertEqual(
                            old_site_values, [{"time": 500, "results": dict(size=300)}] * settings_sites_count
                        )

                        mock_get.assert_called_with("https://www.zambiaureport.com/count.txt/")

                        cache_set_mock.assert_called_with(
                            "org:zambia:reporters:old-site",
                            {"time": 500, "results": dict(size=300)},
                            UREPORT_ASYNC_FETCHED_DATA_CACHE_TIME,
                        )

                        cache_delete_mock.assert_called_once_with(GLOBAL_COUNT_CACHE_KEY)
Exemplo n.º 7
0
def refresh_org_reporters(org_id=None):
    start = time.time()
    r = get_redis_connection()

    key = 'refresh_reporters'
    lock_timeout = 900

    if org_id:
        key = 'refresh_reporters:%d' % org_id
        lock_timeout = 30

    if not r.get(key):
        with r.lock(key, timeout=lock_timeout):
            active_orgs = Org.objects.filter(is_active=True)
            if org_id:
                active_orgs = Org.objects.filter(pk=org_id)

            for org in active_orgs:
                fetch_reporter_group(org)

            fetch_old_sites_count()
            print "Task: refresh_org_reporters took %ss" % (time.time() - start)
Exemplo n.º 8
0
    def test_fetch_old_sites_count(self):
        self.clear_cache()

        settings_sites = list(getattr(settings, "COUNTRY_FLAGS_SITES", []))

        with patch("ureport.utils.datetime_to_ms") as mock_datetime_ms:
            mock_datetime_ms.return_value = 500

            with patch("requests.get") as mock_get:
                mock_get.return_value = MockResponse(200, b"300")

                with patch("django.core.cache.cache.set") as cache_set_mock:
                    cache_set_mock.return_value = "Set"

                    with patch("django.core.cache.cache.delete"
                               ) as cache_delete_mock:
                        cache_delete_mock.return_value = "Deleted"

                        old_site_values = fetch_old_sites_count()
                        self.assertEqual(
                            old_site_values,
                            [{
                                "time": 500,
                                "results": dict(size=300)
                            }] * len([
                                elt
                                for elt in settings_sites if elt["count_link"]
                            ]),
                        )

                        mock_get.assert_called_with(
                            "https://www.ureport.in/count/")

                        cache_set_mock.assert_called_with(
                            "org:global:reporters:old-site",
                            {
                                "time": 500,
                                "results": dict(size=300)
                            },
                            UREPORT_ASYNC_FETCHED_DATA_CACHE_TIME,
                        )

                        cache_delete_mock.assert_called_once_with(
                            GLOBAL_COUNT_CACHE_KEY)