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

167 lines
6.8 KiB
Python

from typing import TYPE_CHECKING, Any
from django.contrib.postgres.fields import ArrayField
from django.db import models
from django.db.models import QuerySet
from insights.models.activity_logging.model_activity import ModelActivityMixin
from insights.models.file_system.constants import DEFAULT_SURFACE
from insights.models.file_system.file_system_mixin import FileSystemSyncMixin
from insights.models.file_system.file_system_representation import FileSystemRepresentation
from insights.models.utils import RootTeamManager, RootTeamMixin, sane_repr
from insights.utils import absolute_uri
if TYPE_CHECKING:
from insights.models.team import Team
class DashboardManager(RootTeamManager):
def get_queryset(self):
return super().get_queryset().exclude(deleted=True)
class Dashboard(FileSystemSyncMixin, ModelActivityMixin, RootTeamMixin, models.Model):
class CreationMode(models.TextChoices):
DEFAULT = "default", "Default"
TEMPLATE = (
"template",
"Template",
) # dashboard was created from a predefined template
DUPLICATE = (
"duplicate",
"Duplicate",
) # dashboard was duplicated from another dashboard
UNLISTED = (
"unlisted",
"Unlisted (product-embedded)",
) # Product dashboards (e.g. AI observability) - hidden from general lists, accessed via tag queries
class RestrictionLevel(models.IntegerChoices):
"""Collaboration restriction level (which is a dashboard setting). Sync with PrivilegeLevel."""
EVERYONE_IN_PROJECT_CAN_EDIT = 21, "Everyone in the project can edit"
ONLY_COLLABORATORS_CAN_EDIT = (
37,
"Only those invited to this dashboard can edit",
)
class PrivilegeLevel(models.IntegerChoices):
"""Collaboration privilege level (which is a user property). Sync with RestrictionLevel."""
CAN_VIEW = 21, "Can view dashboard"
CAN_EDIT = 37, "Can edit dashboard"
name = models.CharField(max_length=400, null=True, blank=True)
description = models.TextField(blank=True)
team = models.ForeignKey("insights.Team", on_delete=models.CASCADE)
pinned = models.BooleanField(default=False)
created_at = models.DateTimeField(auto_now_add=True, blank=True)
created_by = models.ForeignKey("insights.User", on_delete=models.SET_NULL, null=True, blank=True)
deleted = models.BooleanField(default=False)
last_accessed_at = models.DateTimeField(blank=True, null=True)
last_refresh = models.DateTimeField(blank=True, null=True)
filters = models.JSONField(default=dict)
variables = models.JSONField(default=dict, null=True, blank=True)
breakdown_colors = models.JSONField(default=list, null=True, blank=True)
data_color_theme = models.ForeignKey(
"insights.DataColorTheme",
on_delete=models.SET_NULL,
null=True,
blank=True,
)
creation_mode = models.CharField(max_length=16, default="default", choices=CreationMode)
restriction_level = models.PositiveSmallIntegerField(
default=RestrictionLevel.EVERYONE_IN_PROJECT_CAN_EDIT,
choices=RestrictionLevel,
)
insights = models.ManyToManyField(
"product_analytics.Insight", related_name="dashboards", through="DashboardTile", blank=True
) # type: models.ManyToManyField
quick_filter_ids = models.JSONField(default=list, blank=True, null=True)
# Deprecated in favour of app-wide tagging model. See EnterpriseTaggedItem
deprecated_tags: ArrayField = ArrayField(models.CharField(max_length=32), null=True, blank=True, default=list)
deprecated_tags_v2: ArrayField = ArrayField(
models.CharField(max_length=32),
null=True,
blank=True,
default=None,
db_column="tags",
)
# DEPRECATED: using the new "sharing" relation instead
share_token = models.CharField(max_length=400, null=True, blank=True)
# DEPRECATED: using the new "is_sharing_enabled" relation instead
is_shared = models.BooleanField(default=False)
objects = DashboardManager() # type: ignore
objects_including_soft_deleted: models.Manager["Dashboard"] = RootTeamManager()
__repr__ = sane_repr("team_id", "id", "name")
class Meta:
indexes = [
models.Index(
name="idx_dashboard_deleted_team_id",
fields=["-pinned", "name", "deleted", "team_id"],
condition=models.Q(deleted=False),
),
]
db_table = "insights_dashboard"
def __str__(self):
return self.name or str(self.id)
def delete(self, *args, **kwargs):
from insights.models.group_type_mapping import clear_dashboard_from_group_type_mapping
clear_dashboard_from_group_type_mapping(
team_id=self.team_id, dashboard_id=self.id, project_id=self.team.project_id
)
return super().delete(*args, **kwargs)
@classmethod
def get_file_system_unfiled(cls, team: "Team", surface: str = DEFAULT_SURFACE) -> QuerySet["Dashboard"]:
base_qs = cls.objects.filter(team=team, deleted=False).exclude(creation_mode="template")
return cls._filter_unfiled_queryset(base_qs, team, type="dashboard", ref_field="id", surface=surface)
def get_file_system_representation(self) -> FileSystemRepresentation:
should_delete = self.deleted or (self.creation_mode == "template")
return FileSystemRepresentation(
base_folder=self._get_assigned_folder("Unfiled/Dashboards"),
type="dashboard", # sync with APIScopeObject in scopes.py
ref=str(self.id),
name=self.name or "Untitled",
href=f"/dashboard/{self.id}",
meta={
"created_at": str(self.created_at),
"created_by": self.created_by_id,
},
should_delete=should_delete,
)
@property
def is_sharing_enabled(self):
# uses .all and not .first so that prefetching in serializers can be used
sharing_configurations = self.sharingconfiguration_set.all()
return sharing_configurations[0].enabled if sharing_configurations and sharing_configurations[0] else False
@property
def url(self):
return absolute_uri(f"/dashboard/{self.id}")
def get_analytics_metadata(self) -> dict[str, Any]:
"""
Returns serialized information about the object for analytics reporting.
"""
return {
"dashboard_id": self.pk,
"pinned": self.pinned,
"item_count": self.tiles.exclude(insight=None).count(),
"is_shared": self.is_sharing_enabled,
"created_at": self.created_at,
"has_description": self.description != "",
"tags_count": self.tagged_items.count(),
"creation_mode": self.creation_mode,
}