Move code around.
This commit is contained in:
parent
73e694d373
commit
7760ed0b58
2 changed files with 123 additions and 121 deletions
59
confarchive/query.py
Normal file
59
confarchive/query.py
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
"""Database queries."""
|
||||
|
||||
import typing
|
||||
from sqlalchemy.orm.query import Query
|
||||
from sqlalchemy.engine.cursor import CursorResult
|
||||
from sqlalchemy import func
|
||||
|
||||
from . import database, model
|
||||
|
||||
|
||||
def top_speakers() -> Query:
|
||||
"""Find people who spoke at the most conferences."""
|
||||
q: Query = (
|
||||
database.session.query(model.Person, func.count())
|
||||
.join(model.ConferencePerson)
|
||||
.filter(model.Person.id != 1046) # FOSDEM Staff
|
||||
.group_by(model.Person)
|
||||
.order_by(func.count().desc(), model.Person.name)
|
||||
)
|
||||
return q
|
||||
|
||||
|
||||
def top_events() -> Query:
|
||||
"""Most common titles of events."""
|
||||
q: Query = (
|
||||
database.session.query(model.Event.title, func.count())
|
||||
.group_by(model.Event.title)
|
||||
.order_by(func.count().desc())
|
||||
.having(func.count() > 3)
|
||||
)
|
||||
return q
|
||||
|
||||
|
||||
def search_for_events(search_for: str) -> Query:
|
||||
"""Search for events with by title."""
|
||||
q: Query = model.Event.query.filter(
|
||||
model.Event.title.ilike(f"%{search_for}%")
|
||||
).order_by(model.Event.title)
|
||||
return q
|
||||
|
||||
|
||||
def search_for_people(search_for: str) -> Query:
|
||||
"""Search for people by name."""
|
||||
q: Query = model.Person.query.filter(
|
||||
model.Person.name.ilike(f"%{search_for}%")
|
||||
).order_by(model.Person.name)
|
||||
return q
|
||||
|
||||
|
||||
def speaker_counts() -> CursorResult:
|
||||
"""Speaker/conference frequency distribution."""
|
||||
sql = """
|
||||
select num, count(*)
|
||||
from (select person_id, count(*) as num from conference_person group by person_id) a
|
||||
group by num
|
||||
order by num
|
||||
"""
|
||||
|
||||
return typing.cast(CursorResult, database.session.execute(sql))
|
||||
Loading…
Add table
Add a link
Reference in a new issue