Filter the feed by company keys, place tokens and seniority, and suggest companies and places
Migration 17 adds roles.place_fold and fills seniority from the title where extract has not read the role. Every connection registers place_match, so the Place filter and the place gate match whole folded words: Lund no longer finds Billund. GET /api/roles takes company_keys, places, seniority, seniority_unknown and pay_unknown; new routes suggest companies and places, label place tokens and count the seniority and pay facets. Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
1 parent
4b9403a0b9
commit
a91b4e99e0
6 files changed
+724
-60
No files matched your search
+161
-27
@@ -11,12 +11,12 @@ from __future__ import annotations
|
||||
|
||||
from typing import Annotated, Any, Literal
|
||||
|
||||
from fastapi import APIRouter, HTTPException, Query, Response
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, Response
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from app import jobs
|
||||
from app import jobs, places
|
||||
from app.models import (
|
||||
AFFINITY, Agent, Application, Company, Gate, Limits, Prompts, Role, RoleRow,
|
||||
AFFINITY, LEVELS, Agent, Application, Company, Gate, Limits, Prompts, Role, RoleRow,
|
||||
HandReason, ScoredRole, Status, Subscription, Weights, company_key,
|
||||
)
|
||||
from app.storage import Store
|
||||
@@ -59,6 +59,76 @@ class VoteBody(BaseModel):
|
||||
agent: int | None = None
|
||||
|
||||
|
||||
def _feed_filters(
|
||||
store: StoreDep,
|
||||
q: str = "",
|
||||
company: str = "",
|
||||
place: str = "",
|
||||
function: str = "",
|
||||
remote: str = "",
|
||||
days: int | None = None,
|
||||
show_muted: bool = False,
|
||||
include_read: bool = True,
|
||||
tags: list[str] = Query(default=[]),
|
||||
tag_mode: Literal["any", "all"] = "any",
|
||||
agent: int | None = None,
|
||||
order: Literal["score", "newest"] = "score",
|
||||
gated: bool = False,
|
||||
judged: Literal["", "liked", "unjudged", "passed"] = "",
|
||||
pay: Literal["", "stated", "normalised", "stated_only"] = "",
|
||||
company_keys: list[str] = Query(default=[]),
|
||||
tokens: list[str] = Query(default=[], alias="places"),
|
||||
seniority: list[str] = Query(default=[]),
|
||||
seniority_unknown: bool = True,
|
||||
pay_unknown: bool = False,
|
||||
) -> dict[str, Any]:
|
||||
"""The feed's filters, as `Store.roles_page`, `count_roles` and
|
||||
`role_facets` take them, read from the query of `GET /api/roles` and
|
||||
`GET /api/roles/facets` alike. A place token that names nothing and a
|
||||
level outside `LEVELS` are a 422 sentence, not an empty feed."""
|
||||
for token in tokens:
|
||||
if not places.known(token):
|
||||
raise HTTPException(422, f"No place is known as {token!r}.")
|
||||
for level in seniority:
|
||||
if level not in LEVELS:
|
||||
raise HTTPException(422, f"Seniority is one of {', '.join(LEVELS)}, not {level!r}.")
|
||||
return {
|
||||
"q": q or None, "company": company or None, "place": place or None,
|
||||
"function": function or None, "remote": remote or None, "days": days,
|
||||
"hide_muted": not show_muted, "include_read": include_read, "tags": tags,
|
||||
"tag_mode": tag_mode, "agent": _agent_id(store, agent),
|
||||
"by_score": agent is not None and order == "score", "gated": gated,
|
||||
"judged": judged or None, "pay": pay or None, "company_keys": company_keys,
|
||||
"places": tokens, "seniority": seniority, "seniority_unknown": seniority_unknown,
|
||||
"pay_unknown": pay_unknown}
|
||||
|
||||
|
||||
FeedFilters = Annotated[dict[str, Any], Depends(_feed_filters)]
|
||||
|
||||
|
||||
class FacetCount(BaseModel):
|
||||
"""One value of a facet and how many roles hold it; `value` "" is unknown."""
|
||||
|
||||
value: str
|
||||
count: int
|
||||
|
||||
|
||||
class Facets(BaseModel):
|
||||
seniority: list[FacetCount]
|
||||
pay: list[FacetCount]
|
||||
|
||||
|
||||
@router.get("/roles/facets")
|
||||
def role_facets(store: StoreDep, filters: FeedFilters) -> Facets:
|
||||
"""How many roles the feed with these parameters holds at each seniority
|
||||
level and each pay state, each facet counted with its own filter left
|
||||
out (`Store.role_facets`). Declared before `/roles/{uid}`, which would
|
||||
otherwise read `facets` as a uid."""
|
||||
found = store.role_facets(**filters)
|
||||
return Facets(**{name: [FacetCount(value=v, count=n) for v, n in counts]
|
||||
for name, counts in found.items()})
|
||||
|
||||
|
||||
@router.get("/roles/{uid}")
|
||||
def get_role(uid: str, store: StoreDep, agent: int | None = None) -> ScoredRole:
|
||||
"""One role, open or closed, for the read page, with `agent`'s vote and
|
||||
@@ -126,21 +196,7 @@ class RolesPage(BaseModel):
|
||||
@router.get("/roles")
|
||||
def list_roles(
|
||||
store: StoreDep,
|
||||
q: str = "",
|
||||
company: str = "",
|
||||
place: str = "",
|
||||
function: str = "",
|
||||
remote: str = "",
|
||||
days: int | None = None,
|
||||
show_muted: bool = False,
|
||||
include_read: bool = True,
|
||||
tags: list[str] = Query(default=[]),
|
||||
tag_mode: Literal["any", "all"] = "any",
|
||||
agent: int | None = None,
|
||||
order: Literal["score", "newest"] = "score",
|
||||
gated: bool = False,
|
||||
judged: Literal["", "liked", "unjudged", "passed"] = "",
|
||||
pay: Literal["", "stated", "normalised", "stated_only"] = "",
|
||||
filters: FeedFilters,
|
||||
offset: Annotated[int, Query(ge=0)] = 0,
|
||||
limit: Annotated[int, Query(ge=1, le=1000)] = 60,
|
||||
) -> RolesPage:
|
||||
@@ -161,15 +217,16 @@ def list_roles(
|
||||
`?tags=a&tags=b`, kept to a role with any of them or, with
|
||||
`tag_mode=all`, every one. `pay` keeps only roles whose `salary_state`
|
||||
is stated or normalised, normalised only, or stated but not normalised
|
||||
(`Store.PAY`)."""
|
||||
agent_id = _agent_id(store, agent)
|
||||
by_score = agent is not None and order == "score"
|
||||
filters: dict[str, Any] = {
|
||||
"q": q or None, "company": company or None, "place": place or None,
|
||||
"function": function or None, "remote": remote or None, "days": days,
|
||||
"hide_muted": not show_muted, "include_read": include_read, "tags": tags,
|
||||
"tag_mode": tag_mode, "agent": agent_id, "by_score": by_score,
|
||||
"gated": gated, "judged": judged or None, "pay": pay or None}
|
||||
(`Store.PAY`); `pay_unknown` lets the roles that state no pay through
|
||||
as well.
|
||||
|
||||
`company` and `place` are substrings, kept for links from other pages.
|
||||
The rail's fields are `company_keys` (repeated, exact keys),
|
||||
`places` (repeated place tokens, `app.places`: `country:DK`,
|
||||
`region:skane`, `place:malmo`, matched on whole folded words) and
|
||||
`seniority` (repeated levels), each several values joined by OR. A role
|
||||
with no level passes the seniority filter unless `seniority_unknown` is
|
||||
false. The parameters are read by `_feed_filters`."""
|
||||
return RolesPage(items=store.roles_page(limit=limit, offset=offset, **filters),
|
||||
total=store.count_roles(**filters))
|
||||
|
||||
@@ -198,6 +255,83 @@ def list_filters(store: StoreDep, show_muted: bool = False,
|
||||
for tag, count, unread in store.tag_counts(hidden_for, show_muted)])
|
||||
|
||||
|
||||
# ------------------------------------------------------------ suggestions
|
||||
|
||||
class CompanySuggestion(BaseModel):
|
||||
"""A company for the rail's Company field: its key, the name shown, its
|
||||
open roles and the agent's affinity."""
|
||||
|
||||
key: str
|
||||
name: str
|
||||
count: int
|
||||
affinity: int
|
||||
|
||||
|
||||
class PlaceSuggestion(BaseModel):
|
||||
"""A place token for the rail's Place field (`places.Suggestion`)."""
|
||||
|
||||
token: str
|
||||
label: str
|
||||
kind: Literal["country", "region", "place"]
|
||||
count: int
|
||||
|
||||
|
||||
class PlaceLabel(BaseModel):
|
||||
token: str
|
||||
label: str
|
||||
|
||||
|
||||
def _starts_a_word(text: str, folded: str) -> bool:
|
||||
"""`text` starts a word, or a run of words, of `folded`: `nor` finds `novo
|
||||
nordisk`, `novo nor` too. Both are folded already."""
|
||||
return f" {text}" in f" {folded}"
|
||||
|
||||
|
||||
@router.get("/suggest/companies")
|
||||
def suggest_companies(
|
||||
store: StoreDep,
|
||||
q: str = "",
|
||||
agent: int | None = None,
|
||||
limit: Annotated[int, Query(ge=1, le=50)] = 12,
|
||||
) -> list[CompanySuggestion]:
|
||||
"""Companies with open roles whose name has a word that starts with `q`,
|
||||
folded as places are (`places.fold`), so `orsted` finds Ørsted and
|
||||
`kobenhavn` finds a name with København in it. By open roles, most
|
||||
first. The companies `agent` (the default agent when absent) hid are
|
||||
left out, as the feed leaves them out. An empty `q` suggests nothing."""
|
||||
text = places.fold(q)
|
||||
if not text:
|
||||
return []
|
||||
found = [CompanySuggestion(key=key, name=names[0] if names else key, count=count,
|
||||
affinity=affinity)
|
||||
for key, names, count, affinity in store.open_companies(_agent_id(store, agent))
|
||||
if any(_starts_a_word(text, places.fold(name)) for name in names)]
|
||||
found.sort(key=lambda c: (-c.count, c.name.casefold()))
|
||||
return found[:limit]
|
||||
|
||||
|
||||
@router.get("/suggest/places")
|
||||
def suggest_places(
|
||||
store: StoreDep,
|
||||
q: str = "",
|
||||
limit: Annotated[int, Query(ge=1, le=50)] = 12,
|
||||
) -> list[PlaceSuggestion]:
|
||||
"""Place tokens for the text typed so far: countries, then regions, then
|
||||
towns, each by open roles (`places.suggest`, over every open role's place
|
||||
line). An empty `q` suggests nothing."""
|
||||
return [PlaceSuggestion(token=s.token, label=s.label, kind=s.kind, count=s.count)
|
||||
for s in places.suggest(q, store.open_places(), limit)]
|
||||
|
||||
|
||||
@router.get("/places/labels")
|
||||
def place_labels(tokens: list[str] = Query(default=[])) -> list[PlaceLabel]:
|
||||
"""The chip text of each token read from the address, in the order
|
||||
asked: `country:DK` is Denmark, `region:skane` Skåne. A token that
|
||||
names nothing is left out, so the page drops its chip rather than send
|
||||
it to `/api/roles` for a 422."""
|
||||
return [PlaceLabel(token=t, label=places.label(t)) for t in tokens if places.known(t)]
|
||||
|
||||
|
||||
# ------------------------------------------------- roles: text and summary
|
||||
|
||||
#: What the read page toasts when a posting page gave nothing readable, by
|
||||
|
||||
Reference in new issue
Block a user