Skip to content

Commit

Permalink
improvements to the reconcile class
Browse files Browse the repository at this point in the history
  • Loading branch information
drkane committed Sep 13, 2023
1 parent 58171a5 commit 12113ab
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,10 @@ Run tests then run a server showing where coverage is missing

hatch run cov-html

Black and ruff should be run before committing any changes.

### Linting/formatting

Black and ruff should be run before committing any changes.

To check for any changes needed:

hatch run lint:style
Expand Down
35 changes: 18 additions & 17 deletions src/datasette_reconcile/reconcile.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,33 @@ def __init__(self, config, database, table, datasette):
self.table = table
self.datasette = datasette

def _get_headers(self):
return {
"Access-Control-Allow-Origin": "*",
}

async def get(self, request):
"""
Takes a request and returns a response based on the queries.
"""

# work out if we are looking for queries
queries = await self.get_queries(request)
queries = await self._get_queries(request)
if queries:
return Response.json(
{q[0]: {"result": q[1]} async for q in self.reconcile_queries(queries)},
headers=self._get_headers(),
)
return self._response({q[0]: {"result": q[1]} async for q in self._reconcile_queries(queries)})
# if we're not then just return the service specification
return self._response(self._service_manifest(request))

def _response(self, response):
return Response.json(
self.service_manifest(request),
headers=self._get_headers(),
response,
headers={
"Access-Control-Allow-Origin": "*",
},
)

async def get_queries(self, request):
async def _get_queries(self, request):
post_vars = await request.post_vars()
queries = post_vars.get("queries", request.args.get("queries"))
if queries:
return json.loads(queries)

async def reconcile_queries(self, queries):
async def _reconcile_queries(self, queries):
select_fields = get_select_fields(self.config)
for query_id, query in queries.items():
limit = min(
Expand Down Expand Up @@ -96,11 +97,11 @@ async def reconcile_queries(self, queries):
order_by=order_by,
limit=limit,
)
query_results = [self.get_query_result(r, query) for r in await self.db.execute(query_sql, params)]
query_results = [self._get_query_result(r, query) for r in await self.db.execute(query_sql, params)]
query_results = sorted(query_results, key=lambda x: -x["score"])
yield query_id, query_results

def get_query_result(self, row, query):
def _get_query_result(self, row, query):
name = str(row[self.config["name_field"]])
name_match = str(name).lower().strip()
query_match = str(query["query"]).lower().strip()
Expand All @@ -116,7 +117,7 @@ def get_query_result(self, row, query):
"match": name_match == query_match,
}

def service_manifest(self, request):
def _service_manifest(self, request):
# @todo: if type_field is set then get a list of types to use in the "defaultTypes" item below.
view_url = self.config.get("view_url")
if not view_url:
Expand Down

0 comments on commit 12113ab

Please sign in to comment.