Skip to content

Commit

Permalink
OPEN-3336: New command to load search term query log into the Django …
Browse files Browse the repository at this point in the history
…database
  • Loading branch information
thriuin committed May 7, 2024
1 parent 365b685 commit d1424ac
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
18 changes: 18 additions & 0 deletions oc_search/settings-sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,24 @@
'console': {
'class': 'logging.StreamHandler',
},
'query_log': {
'class': 'logging.StreamHandler',
'formatter': 'search_term_formatter',
'encoding': 'utf8',
},
},
'formatters': {
'search_term_formatter': {
'format': '%(asctime)s,%(message)s',
'datefmt': '%Y-%m-%dT%H:%M:%SZ'
}
},
'loggers': {
'search_term_logger': {
'handlers': ['query_log'],
'level': 'INFO',
'propagate': False,
},
},
'root': {
'handlers': ['console'],
Expand Down
30 changes: 30 additions & 0 deletions search/management/commands/import_query_logs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import csv
from datetime import datetime
from django.core.management.base import BaseCommand, CommandError
from django.conf import settings
from django.utils import timezone as utimezone
import os.path
from search.models import SearchLog


class Command(BaseCommand):
help = 'Import search query logs to the database'

def add_arguments(self, parser):
parser.add_argument('--csv', type=str, required=True, help='CSV file with search query logs')

def handle(self, *args, **options):
if not os.path.exists(options['csv']):
raise CommandError(f"CSV file: Cannot find {options['csv']}")
with open(options['csv'], 'r', encoding='utf-8-sig', errors="xmlcharrefreplace") as csv_file:
fieldnames = ('timestamp', 'hostname', 'search_type', 'page_type',
'search_format', 'session_id', 'page_no', 'sort_order',
'search_text', 'facets', 'results_no')
csv_reader = csv.DictReader(csv_file, fieldnames=fieldnames, dialect='excel')
for row in csv_reader:
try:
sll = SearchLog.objects.create(**row)
sll.save()

except ValueError as ve:
print(f"Invalid value {ve}")
15 changes: 15 additions & 0 deletions search/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,3 +445,18 @@ def save(self, *args, **kwargs):
if not self.id and not self.event_timestamp:
self.event_timestamp = utimezone.now()
super().save(*args, **kwargs)


class SearchLog(models.Model):
id = models.AutoField(primary_key=True)
timestamp = models.DateTimeField()
hostname = models.CharField(max_length=512, verbose_name="Hostname", blank=False)
search_type = models.CharField(max_length=32, verbose_name="Search ID", blank=False, default="None")
page_type = models.CharField(max_length=32, verbose_name="Page Type", blank=False, default="search")
search_format = models.CharField(max_length=32, verbose_name="Search Format", blank=False, default="html")
session_id = models.CharField(max_length=128, verbose_name="Session ID", blank=False, default="None")
page_no = models.IntegerField(verbose_name="Page Number", blank=False, default=1)
sort_order = models.CharField(max_length=64, verbose_name="Sort Order", blank=False, default="asc")
search_text = models.TextField(verbose_name="Search Text", blank=False, default="None")
facets = models.TextField(verbose_name="Facets", blank=False, default="None")
results_no = models.IntegerField(verbose_name="Results Number", blank=False, default=0)

0 comments on commit d1424ac

Please sign in to comment.