Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

Count active staff accounts in the usage snapshot (#724)

Christian McDermott733fb807c69a
diff --git a/core/switch_core/telemetry/catalogue.py b/core/switch_core/telemetry/catalogue.py
index 8b2f4a544..82a150434 100644
--- a/core/switch_core/telemetry/catalogue.py
+++ b/core/switch_core/telemetry/catalogue.py
@@ -154,6 +154,8 @@ _SNAPSHOT_COUNTS = (
# Switch accounts, not chat identities — see `chat_identity_*` for those.
"user_active_1d",
"user_active_7d",
+ "user_internal_active_1d",
+ "user_internal_active_7d",
"chat_identity_count",
"chat_identity_active_1d",
"chat_identity_active_7d",
diff --git a/core/switch_core/telemetry/snapshot.py b/core/switch_core/telemetry/snapshot.py
index 7b57f7b77..85238810e 100644
--- a/core/switch_core/telemetry/snapshot.py
+++ b/core/switch_core/telemetry/snapshot.py
@@ -117,6 +117,10 @@ class UsageCounts:
# per-tenant count would add it once for each.
user_active_1d: int = 0
user_active_7d: int = 0
+ # Of those, accounts on the company's own email domains, so active staff
+ # can be taken out of the headline without a per-event flag.
+ user_internal_active_1d: int = 0
+ user_internal_active_7d: int = 0
active_user_ids_1d: set[str] = field(default_factory=set)
active_user_ids_7d: set[str] = field(default_factory=set)
# Chat identities — a Slack, Mattermost or other platform account — that
@@ -171,6 +175,8 @@ class UsageCounts:
"user_internal_count": self.user_internal_count,
"user_active_1d": self.user_active_1d,
"user_active_7d": self.user_active_7d,
+ "user_internal_active_1d": self.user_internal_active_1d,
+ "user_internal_active_7d": self.user_internal_active_7d,
"chat_identity_count": self.chat_identity_count,
"chat_identity_active_1d": self.chat_identity_active_1d,
"chat_identity_active_7d": self.chat_identity_active_7d,
@@ -756,6 +762,8 @@ def _merge_tenant_counts(total: UsageCounts, tenant: UsageCounts) -> None:
"user_internal_count",
"user_active_1d",
"user_active_7d",
+ "user_internal_active_1d",
+ "user_internal_active_7d",
"active_user_ids_1d",
"active_user_ids_7d",
"room_users_max",
@@ -772,6 +780,18 @@ def _merge_tenant_counts(total: UsageCounts, tenant: UsageCounts) -> None:
total.connector_counts[platform] += count
+async def _internal_among(session: AsyncSession, user_ids: set[str]) -> int:
+ """How many of `user_ids` are accounts on the company's own domains."""
+ if not user_ids:
+ return 0
+ return await _scalar(
+ session,
+ select(func.count())
+ .select_from(User)
+ .where(User.id.in_(user_ids), internal_email_condition(User.email)),
+ )
+
+
async def collect_usage(
session_factory: async_sessionmaker[AsyncSession], *, now: datetime | None = None
) -> UsageCounts:
@@ -830,6 +850,12 @@ async def collect_usage(
.select_from(User)
.where(internal_email_condition(User.email)),
)
+ counts.user_internal_active_1d = await _internal_among(
+ session, counts.active_user_ids_1d
+ )
+ counts.user_internal_active_7d = await _internal_among(
+ session, counts.active_user_ids_7d
+ )
counts.user_active_1d = len(counts.active_user_ids_1d)
counts.user_active_7d = len(counts.active_user_ids_7d)
counts.duration_ms = round((time.monotonic() - started) * 1000)
diff --git a/core/tests/switch_core/telemetry/test_snapshot_accounts.py b/core/tests/switch_core/telemetry/test_snapshot_accounts.py
index f24afa548..d55c48aba 100644
--- a/core/tests/switch_core/telemetry/test_snapshot_accounts.py
+++ b/core/tests/switch_core/telemetry/test_snapshot_accounts.py
@@ -216,6 +216,57 @@ async def test_staff_accounts_are_counted_apart(
assert counts.user_internal_count == 3
+async def test_active_staff_are_counted_apart_from_active_customers(
+ session_factory: async_sessionmaker[AsyncSession],
+) -> None:
+ staff = await _account(session_factory, f"{uuid.uuid4().hex[:6]}@sandboxaq.com")
+ staff_last_week = await _account(
+ session_factory, f"{uuid.uuid4().hex[:6]}@eng.sandboxquantum.com"
+ )
+ customer = await _account(session_factory, f"{uuid.uuid4().hex[:6]}@example.com")
+ await _account(session_factory, f"{uuid.uuid4().hex[:6]}@sandboxaq.com")
+ await _chat_account_speaks(session_factory, claimed_by=[staff])
+ await _chat_account_speaks(session_factory, claimed_by=[customer])
+ await _chat_account_speaks(
+ session_factory,
+ claimed_by=[staff_last_week],
+ spoke_at=datetime.now(UTC) - timedelta(days=3),
+ )
+
+ counts = await collect_usage(session_factory)
+
+ assert counts.user_internal_count == 3
+ assert (counts.user_active_1d, counts.user_internal_active_1d) == (2, 1)
+ assert (counts.user_active_7d, counts.user_internal_active_7d) == (3, 2)
+
+
+async def test_staff_active_in_two_tenants_is_one_active_staff_member(
+ session_factory: async_sessionmaker[AsyncSession],
+) -> None:
+ await _other_tenant(session_factory)
+ staff = await _account(session_factory, f"{uuid.uuid4().hex[:6]}@sandboxaq.com")
+ await _chat_account_speaks(session_factory, claimed_by=[staff])
+ await _chat_account_speaks(
+ session_factory, claimed_by=[staff], tenant_id=OTHER_TENANT
+ )
+
+ counts = await collect_usage(session_factory)
+
+ assert counts.user_internal_active_1d == 1
+ assert counts.user_internal_active_7d == 1
+
+
+async def test_no_activity_means_no_active_staff(
+ session_factory: async_sessionmaker[AsyncSession],
+) -> None:
+ await _account(session_factory, f"{uuid.uuid4().hex[:6]}@sandboxaq.com")
+
+ counts = await collect_usage(session_factory)
+
+ assert counts.user_internal_active_1d == 0
+ assert counts.user_internal_active_7d == 0
+
+
async def test_a_tenant_that_fails_after_collecting_its_accounts_adds_none_of_them(
session_factory: async_sessionmaker[AsyncSession],
monkeypatch: pytest.MonkeyPatch,
diff --git a/docs/old/telemetry-events.md b/docs/old/telemetry-events.md
index d9c9b229d..cb83ccc5f 100644
--- a/docs/old/telemetry-events.md
+++ b/docs/old/telemetry-events.md
@@ -111,6 +111,8 @@ signals tell it apart, none of them an identifier:
server Console runs for the user is given Console's answer.
- **`user_internal_count`** in the daily snapshot: accounts on those domains,
so a deployment that serves both staff and customers can be split.
+ `user_internal_active_1d` / `_7d` do the same for the active counts, so
+ customer activity is `user_active_*` minus them.
- Each deployment's `flint.client_id`, for the company's own deployments by
name.
@@ -325,6 +327,8 @@ never invisible.
| `user_internal_count` | number | of those, accounts on the company's own email domains — see "Internal usage" |
| `user_active_1d` | number | distinct Switch **accounts** that used a room with an agent in 24h: an account counts when a chat account it has claimed spoke. One person on two platforms is one account |
| `user_active_7d` | number | same over 7 days |
+| `user_internal_active_1d` | number | of `user_active_1d`, accounts on the company's own email domains |
+| `user_internal_active_7d` | number | same over 7 days |
| `chat_identity_count` | number | chat identities — Slack, Mattermost and other platform accounts — that exist, claimed by an account or not |
| `chat_identity_active_1d` | number | distinct chat identities that used a room with an agent in 24h. Most belong to no Switch account and one person may have several, which is why this runs above `user_active_1d`. Servers released before this change reported this figure as `user_active_1d` |
| `chat_identity_active_7d` | number | same over 7 days |