Files
zeekay 1b367e2b62 chore: rebase the tree onto upstream master
The tree was a debranded snapshot of upstream taken around 2026-02-20, and
upstream has moved about five months since. This replaces it with upstream
master, mechanically re-derived: bin/debrand does the rename, and the
pipeline and squashed migrations are restored from the shipped tree.

Not a merge. The previous import replaced the tree wholesale in a single
commit, so there is no shared content history to merge against. Reproducing
the rename on the new tree is what actually converges.

What survives: .hanzo/workflows, the Dockerfiles and bin/docker* entrypoints,
compose files, LLM.md, and insights/migrations. The squashed initial
migration is load-bearing — the live database records it by name, so
upstream's ~918 individual migrations cannot come back.

Nine products are dropped as unreferenced. Twenty-one more that we do not run
are kept anyway because the core app still imports them; unpicking those is a
refactor and does not belong in a rebase.

This base does not build yet, and the reasons are not in the rename:
upstream now wants Python 3.13.13 against our 3.12.12 and Django 5.2 against
our 4.2, and the dependency manifests still name upstream packages that we
republish under our own names. Those are decisions, not renames.
2026-08-01 15:44:40 -07:00

89 lines
3.1 KiB
Python

"""Fetch survey responses for summarization."""
from datetime import datetime
from typing import cast
from insights.insightsql import ast
from insights.insightsql.parser import parse_select
from insights.insightsql_queries.insights.paginators import InsightsQLHasMorePaginator
from insights.models import Team
def fetch_responses(
survey_id: str,
question_index: int | None,
question_id: str | None,
start_date: datetime,
end_date: datetime,
team: Team,
limit: int = 100,
exclude_values: list[str] | None = None,
exclude_uuids: set[str] | None = None,
) -> list[str]:
"""
Fetch survey responses for a specific question.
Args:
survey_id: The survey ID
question_index: Index of the question (0-based)
question_id: ID of the question
start_date: Start date for filtering responses
end_date: End date for filtering responses
team: The team to query
limit: Maximum number of responses to fetch
exclude_values: List of values to exclude (e.g., predefined choices for choice questions)
exclude_uuids: Set of response UUIDs to exclude (e.g., archived responses)
Returns:
List of response strings
"""
paginator = InsightsQLHasMorePaginator(limit=limit, offset=0)
# Build the base query
# Use uniqueSurveySubmissionsFilter to deduplicate by $survey_submission_id
# This ensures multiple "survey sent" events for the same submission are rolled up
base_query = """
SELECT getSurveyResponse({question_index}, {question_id})
FROM events
WHERE event == 'survey sent'
AND timestamp >= {start_date}
AND timestamp <= {end_date}
AND properties.`$survey_id` = {survey_id}
AND uniqueSurveySubmissionsFilter({survey_id}, {start_date}, {end_date})
AND trim(getSurveyResponse({question_index}, {question_id})) != ''
"""
# Add archived response filter if there are UUIDs to exclude
# UUIDs are pre-validated by Django's UUIDField when stored in SurveyResponseArchive
if exclude_uuids:
base_query += " AND uuid NOT IN {exclude_uuids}"
placeholders: dict[str, ast.Expr] = {
"survey_id": ast.Constant(value=survey_id),
"start_date": ast.Constant(value=start_date),
"end_date": ast.Constant(value=end_date),
"question_index": ast.Constant(value=question_index),
"question_id": ast.Constant(value=question_id),
}
if exclude_uuids:
placeholders["exclude_uuids"] = ast.Tuple(exprs=[ast.Constant(value=uuid) for uuid in exclude_uuids])
q = parse_select(base_query, placeholders)
query_response = paginator.execute_insightsql_query(
team=team,
query_type="survey_response_list_query",
query=cast(ast.SelectQuery, q),
)
responses = [x[0] for x in query_response.results if x[0]]
# Filter out predefined choices for choice questions
if exclude_values:
exclude_set = set(exclude_values)
responses = [r for r in responses if r not in exclude_set]
return responses