Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,25 @@
from pipeline.models import SkillResult as _SkillResult
from pipeline.models import SkillStatus as _SkillStatus
from projects.models import Project as _Project
from trends.models import ContentClusterMembership as _ContentClusterMembership
from trends.models import TopicCentroidSnapshot as _TopicCentroidSnapshot
from trends.models import TopicCluster as _TopicCluster
from trends.models import TopicVelocitySnapshot as _TopicVelocitySnapshot

Project = _Project
ReviewQueue = _ReviewQueue
ReviewReason = _ReviewReason
ReviewResolution = _ReviewResolution
SkillResult = _SkillResult
SkillStatus = _SkillStatus
ContentClusterMembership = _ContentClusterMembership
TopicCluster = _TopicCluster
TopicCentroidSnapshot = _TopicCentroidSnapshot
TopicVelocitySnapshot = _TopicVelocitySnapshot

__all__ = [
"Content",
"ContentClusterMembership",
"Entity",
"EntityAuthoritySnapshot",
"EntityCandidate",
Expand All @@ -53,6 +60,9 @@
"NewsletterIntakeStatus",
"Project",
"RunStatus",
"TopicCluster",
"TopicCentroidSnapshot",
"TopicVelocitySnapshot",
"UserFeedback",
]

Expand Down
18 changes: 18 additions & 0 deletions core/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,13 @@
from newsletters.tasks import process_newsletter_intake
from trends.tasks import (
TOPIC_CENTROID_MIN_UPVOTES,
assign_content_to_topic_cluster,
queue_topic_centroid_recompute,
recompute_topic_centroid,
recompute_topic_clusters,
recompute_topic_velocity,
run_all_topic_centroid_recomputations,
run_all_topic_cluster_recomputations,
)

_COMPAT_TASK_EXPORTS = {
Expand All @@ -61,15 +65,25 @@
),
"run_all_ingestions": ("ingestion.tasks", "run_all_ingestions"),
"run_ingestion": ("ingestion.tasks", "run_ingestion"),
"assign_content_to_topic_cluster": (
"trends.tasks",
"assign_content_to_topic_cluster",
),
"TOPIC_CENTROID_MIN_UPVOTES": (
"trends.tasks",
"TOPIC_CENTROID_MIN_UPVOTES",
),
"recompute_topic_clusters": ("trends.tasks", "recompute_topic_clusters"),
"queue_topic_centroid_recompute": (
"trends.tasks",
"queue_topic_centroid_recompute",
),
"recompute_topic_centroid": ("trends.tasks", "recompute_topic_centroid"),
"recompute_topic_velocity": ("trends.tasks", "recompute_topic_velocity"),
"run_all_topic_cluster_recomputations": (
"trends.tasks",
"run_all_topic_cluster_recomputations",
),
"run_all_topic_centroid_recomputations": (
"trends.tasks",
"run_all_topic_centroid_recomputations",
Expand All @@ -79,12 +93,16 @@
__all__ = [
"process_newsletter_intake",
"run_all_ingestions",
"assign_content_to_topic_cluster",
"run_ingestion",
"TOPIC_CENTROID_MIN_UPVOTES",
"queue_topic_centroid_recompute",
"recompute_authority_scores",
"recompute_topic_clusters",
"recompute_topic_centroid",
"recompute_topic_velocity",
"run_all_authority_recomputations",
"run_all_topic_cluster_recomputations",
"run_all_topic_centroid_recomputations",
"run_relevance_scoring_skill",
"run_summarization_skill",
Expand Down
112 changes: 112 additions & 0 deletions core/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from core.models import (
Content,
ContentClusterMembership,
Entity,
EntityAuthoritySnapshot,
EntityCandidate,
Expand All @@ -24,6 +25,8 @@
SkillResult,
SkillStatus,
TopicCentroidSnapshot,
TopicCluster,
TopicVelocitySnapshot,
UserFeedback,
)
from projects.model_support import SourcePluginName
Expand Down Expand Up @@ -540,6 +543,115 @@ def test_topic_centroid_summary_action_returns_latest_snapshot_and_averages(self
self.assertAlmostEqual(response.json()["avg_drift_from_previous"], 0.2)
self.assertAlmostEqual(response.json()["avg_drift_from_week_ago"], 0.3)

def test_topic_cluster_list_returns_current_velocity_annotation(self):
cluster = TopicCluster.objects.create(
project=self.owner_project,
first_seen_at="2026-04-22T00:00:00Z",
last_seen_at="2026-04-24T00:00:00Z",
is_active=True,
member_count=3,
dominant_entity=self.owner_entity,
)
TopicVelocitySnapshot.objects.create(
cluster=cluster,
project=self.owner_project,
window_count=4,
trailing_mean=1.5,
trailing_stddev=0.5,
z_score=3.0,
velocity_score=1.0,
)

response = self.client.get(
reverse(
"v1:project-topic-cluster-list",
kwargs={"project_id": self.owner_project.id},
),
{"ordering": "-velocity_score"},
)

self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(len(response.json()), 1)
self.assertEqual(response.json()[0]["id"], cluster.id)
self.assertEqual(response.json()[0]["member_count"], 3)
self.assertEqual(
response.json()[0]["dominant_entity"]["id"], self.owner_entity.id
)
self.assertAlmostEqual(response.json()[0]["velocity_score"], 1.0)
self.assertAlmostEqual(response.json()[0]["z_score"], 3.0)
self.assertEqual(response.json()[0]["window_count"], 4)

def test_topic_cluster_detail_and_velocity_history_action_return_memberships(self):
cluster = TopicCluster.objects.create(
project=self.owner_project,
first_seen_at="2026-04-22T00:00:00Z",
last_seen_at="2026-04-24T00:00:00Z",
is_active=True,
member_count=1,
dominant_entity=self.owner_entity,
)
ContentClusterMembership.objects.create(
content=self.owner_content,
cluster=cluster,
project=self.owner_project,
similarity=0.92,
)
first_snapshot = TopicVelocitySnapshot.objects.create(
cluster=cluster,
project=self.owner_project,
window_count=2,
trailing_mean=1.0,
trailing_stddev=0.2,
z_score=1.5,
velocity_score=0.75,
)
second_snapshot = TopicVelocitySnapshot.objects.create(
cluster=cluster,
project=self.owner_project,
window_count=3,
trailing_mean=1.0,
trailing_stddev=0.3,
z_score=3.0,
velocity_score=1.0,
)
TopicVelocitySnapshot.objects.filter(pk=first_snapshot.pk).update(
computed_at="2026-04-23T00:00:00Z"
)
TopicVelocitySnapshot.objects.filter(pk=second_snapshot.pk).update(
computed_at="2026-04-24T00:00:00Z"
)

detail_response = self.client.get(
reverse(
"v1:project-topic-cluster-detail",
kwargs={"project_id": self.owner_project.id, "pk": cluster.id},
)
)
history_response = self.client.get(
reverse(
"v1:project-topic-cluster-velocity-history",
kwargs={"project_id": self.owner_project.id, "pk": cluster.id},
),
{"limit": 1},
)

self.assertEqual(detail_response.status_code, status.HTTP_200_OK)
self.assertEqual(detail_response.json()["id"], cluster.id)
self.assertEqual(len(detail_response.json()["memberships"]), 1)
self.assertEqual(
detail_response.json()["memberships"][0]["content"]["id"],
self.owner_content.id,
)
self.assertEqual(len(detail_response.json()["velocity_history"]), 2)
self.assertEqual(
detail_response.json()["velocity_history"][0]["id"],
second_snapshot.id,
)

self.assertEqual(history_response.status_code, status.HTTP_200_OK)
self.assertEqual(len(history_response.json()), 1)
self.assertEqual(history_response.json()[0]["id"], second_snapshot.id)

def test_content_detail_includes_duplicate_state(self):
canonical = self.owner_content
canonical.canonical_url = "https://example.com/owner"
Expand Down
Loading
Loading