-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathdata_integrity.py
47 lines (33 loc) · 1.7 KB
/
data_integrity.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import pysolr
import requests
import logging
import logging.config
from django.core.management.base import BaseCommand, CommandError
from django.conf import settings
from councilmatic_core.models import Bill
logging.config.dictConfig(settings.LOGGING)
logging.getLogger("requests").setLevel(logging.WARNING)
logging.getLogger("pysolr").setLevel(logging.WARNING)
logger = logging.getLogger(__name__)
class Command(BaseCommand):
help = 'Checks for alignment between the Solr index and Councilmatic database'
def handle(self, *args, **options):
councilmatic_count = self.count_councilmatic_bills()
try:
solr_url = settings.HAYSTACK_CONNECTIONS['default']['URL']
requests.get(solr_url)
except requests.ConnectionError:
message = "ConnectionError: Unable to connect to Solr at {} when running the data integrity check. Is Solr running?".format(solr_url)
logger.error(message)
raise Exception(message)
solr = pysolr.Solr(solr_url)
solr_index_count = solr.search(q='*:*').hits
if solr_index_count != councilmatic_count:
message = 'Solr index has {solr} entites. The Councilmatic database has {councilmatic} entities. That\'s a problem!'.format(solr=solr_index_count, councilmatic=councilmatic_count)
logger.error(message)
print('. . . . .\n')
raise AssertionError(message)
logger.info('Good news! Solr index has {solr} entites. The Councilmatic database has {councilmatic} entities.'.format(solr=solr_index_count, councilmatic=councilmatic_count))
print('. . . . .\n')
def count_councilmatic_bills(self):
return Bill.objects.all().count()