Skip to content

Commit

Permalink
add search
Browse files Browse the repository at this point in the history
  • Loading branch information
andersy005 committed Sep 21, 2023
1 parent 770c6e2 commit eeac1ef
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
26 changes: 25 additions & 1 deletion carbonplan_offsets_db/routers/charts.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import numpy as np
import pandas as pd
from fastapi import APIRouter, Depends, Query, Request
from sqlmodel import Session, and_, case, func
from sqlmodel import Session, and_, case, func, or_

from ..database import get_session
from ..logging import get_logger
Expand Down Expand Up @@ -296,6 +296,11 @@ def get_projects_by_registration_date(
issued_max: int | None = Query(None, description='Maximum number of issued credits'),
retired_min: int | None = Query(None, description='Minimum number of retired credits'),
retired_max: int | None = Query(None, description='Maximum number of retired credits'),
search: str
| None = Query(
None,
description='Case insensitive search string. Currently searches on `project_id` and `name` fields only.',
),
session: Session = Depends(get_session),
):
"""Get aggregated project registration data"""
Expand Down Expand Up @@ -334,6 +339,13 @@ def get_projects_by_registration_date(
query=query, model=Project, attribute=attribute, values=values, operation=operation
)

# Handle 'search' filter separately due to its unique logic
if search:
search_pattern = f'%{search}%'
query = query.filter(
or_(Project.project_id.ilike(search_pattern), Project.name.ilike(search_pattern))
)

return get_binned_data(binning_attribute='registered_at', query=query, freq=freq)


Expand All @@ -354,6 +366,11 @@ def get_credits_by_transaction_date(
transaction_date_to: datetime.date
| datetime.datetime
| None = Query(default=None, description='Format: YYYY-MM-DD'),
search: str
| None = Query(
None,
description='Case insensitive search string. Currently searches on `project_id` and `name` fields only.',
),
session: Session = Depends(get_session),
):
"""Get aggregated credit transaction data"""
Expand Down Expand Up @@ -404,4 +421,11 @@ def get_credits_by_transaction_date(
query=query, model=model, attribute=attribute, values=values, operation=operation
)

# Handle 'search' filter separately due to its unique logic
if search:
search_pattern = f'%{search}%'
query = query.filter(
or_(Project.project_id.ilike(search_pattern), Project.name.ilike(search_pattern))
)

return credits_by_transaction_date(query=query, freq=freq)
3 changes: 1 addition & 2 deletions carbonplan_offsets_db/routers/credits.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import datetime

from fastapi import APIRouter, Depends, Query, Request
from sqlalchemy import or_
from sqlmodel import Session
from sqlmodel import Session, or_

from ..database import get_session
from ..logging import get_logger
Expand Down

0 comments on commit eeac1ef

Please sign in to comment.