Skip to content

Commit

Permalink
Enhances Signals table
Browse files Browse the repository at this point in the history
  • Loading branch information
mvilanova committed Oct 23, 2023
1 parent 0615190 commit 3e9f3b1
Show file tree
Hide file tree
Showing 6 changed files with 846 additions and 5,660 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""Adds search vector column to the signal instance model
Revision ID: f2605bfc1f59
Revises: 064c71206256
Create Date: 2023-10-23 11:15:53.297563
"""
from alembic import op
import sqlalchemy as sa
import sqlalchemy_utils

# revision identifiers, used by Alembic.
revision = "f2605bfc1f59"
down_revision = "064c71206256"
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column(
"signal_instance",
sa.Column("search_vector", sqlalchemy_utils.types.ts_vector.TSVectorType(), nullable=True),
)
op.create_index(
"signal_instance_search_vector_idx",
"signal_instance",
["search_vector"],
unique=False,
postgresql_using="gin",
)
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index(
"signal_instance_search_vector_idx", table_name="signal_instance", postgresql_using="gin"
)
op.drop_column("signal_instance", "search_vector")
# ### end Alembic commands ###
5 changes: 4 additions & 1 deletion src/dispatch/database/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ def composite_search(*, db_session, query_str: str, models: List[Base], current_

# TODO can we do this with composite filtering?
# for model in models:
# query = apply_model_specific_filters(model, query, current_user)
# query = apply_model_specific_filters(model, query, current_user)

return s.search(query=query)

Expand Down Expand Up @@ -497,11 +497,14 @@ def search_filter_sort_paginate(
):
"""Common functionality for searching, filtering, sorting, and pagination."""
model_cls = get_class_by_tablename(model)

try:
query = db_session.query(model_cls)

if query_str:
sort = False if sort_by else True
# if model == "SignalInstance":
# model = "Signal"
query = search(query_str=query_str, query=query, model=model, sort=sort)

query_restricted = apply_model_specific_filters(model_cls, query, current_user, role)
Expand Down
18 changes: 15 additions & 3 deletions src/dispatch/signal/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@

from pydantic import Field
from sqlalchemy import (
JSON,
select,
Boolean,
Column,
DateTime,
ForeignKey,
Integer,
JSON,
PrimaryKeyConstraint,
String,
Table,
UniqueConstraint,
)
from sqlalchemy.dialects.postgresql import JSONB, UUID
from sqlalchemy.orm import relationship
from sqlalchemy.orm import relationship, column_property
from sqlalchemy_utils import TSVectorType

from dispatch.auth.models import DispatchUser
Expand Down Expand Up @@ -236,8 +237,19 @@ class SignalInstance(Base, TimeStampMixin, ProjectMixin):
signal = relationship("Signal", backref="instances")
signal_id = Column(Integer, ForeignKey("signal.id"))

signal_name = column_property(select([Signal.name]).where(Signal.id == signal_id))
signal_description = column_property(select([Signal.description]).where(Signal.id == signal_id))

search_vector = Column(
TSVectorType(
"signal_name",
"signal_description",
weights={"signal_name": "A", "signal_description": "B"},
)
)


# Pydantic models...
# Pydantic models
class Service(DispatchBase):
id: PrimaryKey
description: Optional[str] = Field(None, nullable=True)
Expand Down
Loading

0 comments on commit 3e9f3b1

Please sign in to comment.