Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: not include the service account (#17381)

ecmadaod0453acc6f46
diff --git a/backend/metric/collector/user_count.go b/backend/metric/collector/user_count.go
index cf8d581399..e5a433afa6 100644
--- a/backend/metric/collector/user_count.go
+++ b/backend/metric/collector/user_count.go
@@ -3,6 +3,7 @@ package collector
import (
"context"
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
metricapi "github.com/bytebase/bytebase/backend/metric"
"github.com/bytebase/bytebase/backend/plugin/metric"
"github.com/bytebase/bytebase/backend/store"
@@ -24,15 +25,26 @@ func NewUserCountCollector(store *store.Store) metric.Collector {
// Collect will collect the metric for user.
func (c *userCountCollector) Collect(ctx context.Context) ([]*metric.Metric, error) {
- count, err := c.store.CountActiveUsers(ctx)
+ userStat, err := c.store.StatUsers(ctx)
if err != nil {
return nil, err
}
- return []*metric.Metric{
- {
- Name: metricapi.UserCountMetricName,
- Value: count,
- },
- }, nil
+ metrics := []*metric.Metric{}
+ for _, stat := range userStat {
+ if stat.Type == storepb.PrincipalType_END_USER {
+ metrics = append(metrics, &metric.Metric{
+ Name: metricapi.UserCountMetricName,
+ Value: stat.Count,
+ })
+ }
+ if stat.Type == storepb.PrincipalType_SERVICE_ACCOUNT {
+ metrics = append(metrics, &metric.Metric{
+ Name: metricapi.ServiceAccountCountMetricName,
+ Value: stat.Count,
+ })
+ }
+ }
+
+ return metrics, nil
}
diff --git a/backend/metric/metric.go b/backend/metric/metric.go
index 5e1c438924..cbcc53dd11 100644
--- a/backend/metric/metric.go
+++ b/backend/metric/metric.go
@@ -15,6 +15,8 @@ const (
ProjectCountMetricName metric.Name = "bb.project.count"
// UserCountMetricName is the metric name for user count.
UserCountMetricName metric.Name = "bb.user.count"
+ // ServiceAccountCountMetricName is the metric name for service account count.
+ ServiceAccountCountMetricName metric.Name = "bb.service-account.count"
// OpenAPIMetricName is the metric name for OpenAPI.
OpenAPIMetricName metric.Name = "bb.api.call"
// PrincipalRegistrationMetricName is the metric name for the principal registration event.
diff --git a/backend/store/stats.go b/backend/store/stats.go
index cc7f540291..4c349e0b31 100644
--- a/backend/store/stats.go
+++ b/backend/store/stats.go
@@ -83,9 +83,9 @@ func (s *Store) CountActiveUsers(ctx context.Context) (int, error) {
SELECT
count(DISTINCT principal.id)
FROM principal
- WHERE principal.deleted = $1 AND (principal.type = $2 OR principal.type = $3)`
+ WHERE principal.deleted = $1 AND principal.type = $2`
var count int
- if err := tx.QueryRowContext(ctx, query, false, storepb.PrincipalType_END_USER.String(), storepb.PrincipalType_SERVICE_ACCOUNT.String()).Scan(&count); err != nil {
+ if err := tx.QueryRowContext(ctx, query, false, storepb.PrincipalType_END_USER.String()).Scan(&count); err != nil {
if err == sql.ErrNoRows {
return 0, common.FormatDBErrorEmptyRowWithQuery(query)
}
diff --git a/frontend/src/components/ProfileDropdown.vue b/frontend/src/components/ProfileDropdown.vue
index 775d5e9e8a..45d48bfb84 100644
--- a/frontend/src/components/ProfileDropdown.vue
+++ b/frontend/src/components/ProfileDropdown.vue
@@ -54,7 +54,12 @@ const hideQuickstart = computed(() => {
return true;
}
// Hide quickstart if there are more than 1 active users.
- return actuatorStore.getActiveUserCount({ includeBot: false }) > 1;
+ return (
+ actuatorStore.getActiveUserCount({
+ includeBot: false,
+ includeServiceAccount: false,
+ }) > 1
+ );
});
const hideHelp = useAppFeature("bb.feature.hide-help");
diff --git a/frontend/src/components/Quickstart.vue b/frontend/src/components/Quickstart.vue
index e70329107e..f61f34b8ab 100644
--- a/frontend/src/components/Quickstart.vue
+++ b/frontend/src/components/Quickstart.vue
@@ -264,7 +264,12 @@ const introList = computed(() => {
});
const isFirstUser = computed(() => {
- return actuatorStore.getActiveUserCount({ includeBot: false }) === 1;
+ return (
+ actuatorStore.getActiveUserCount({
+ includeBot: false,
+ includeServiceAccount: false,
+ }) === 1
+ );
});
const showQuickstart = computed(() => {
diff --git a/frontend/src/store/modules/v1/actuator.ts b/frontend/src/store/modules/v1/actuator.ts
index 73357e3b45..1775f24cef 100644
--- a/frontend/src/store/modules/v1/actuator.ts
+++ b/frontend/src/store/modules/v1/actuator.ts
@@ -151,7 +151,13 @@ export const useActuatorV1Store = defineStore("actuator_v1", {
},
},
actions: {
- getActiveUserCount({ includeBot }: { includeBot: boolean }) {
+ getActiveUserCount({
+ includeBot,
+ includeServiceAccount,
+ }: {
+ includeBot: boolean;
+ includeServiceAccount: boolean;
+ }) {
return (this.serverInfo?.userStats ?? []).reduce((count, stat) => {
if (stat.state !== State.ACTIVE) {
return count;
@@ -159,6 +165,12 @@ export const useActuatorV1Store = defineStore("actuator_v1", {
if (!includeBot && stat.userType === UserType.SYSTEM_BOT) {
return count;
}
+ if (
+ !includeServiceAccount &&
+ stat.userType === UserType.SERVICE_ACCOUNT
+ ) {
+ return count;
+ }
count += stat.count;
return count;
}, 0);
diff --git a/frontend/src/views/SettingWorkspaceSubscription.vue b/frontend/src/views/SettingWorkspaceSubscription.vue
index 1bfa4d1337..b576ba874e 100644
--- a/frontend/src/views/SettingWorkspaceSubscription.vue
+++ b/frontend/src/views/SettingWorkspaceSubscription.vue
@@ -191,7 +191,10 @@ const disabled = computed((): boolean => {
});
const activeUserCountWithoutBot = computed(() =>
- actuatorStore.getActiveUserCount({ includeBot: false })
+ actuatorStore.getActiveUserCount({
+ includeBot: false,
+ includeServiceAccount: false,
+ })
);
const userLimit = computed((): string => {
diff --git a/frontend/src/views/SettingWorkspaceUsers.vue b/frontend/src/views/SettingWorkspaceUsers.vue
index cab9c278da..c1dc042456 100644
--- a/frontend/src/views/SettingWorkspaceUsers.vue
+++ b/frontend/src/views/SettingWorkspaceUsers.vue
@@ -394,7 +394,10 @@ onMounted(async () => {
});
const activeUserCount = computed(() => {
- return actuatorStore.getActiveUserCount({ includeBot: true });
+ return actuatorStore.getActiveUserCount({
+ includeBot: true,
+ includeServiceAccount: true,
+ });
});
const inactiveUserCount = computed(() => {
@@ -405,7 +408,10 @@ const remainingUserCount = computed((): number => {
return Math.max(
0,
subscriptionV1Store.userCountLimit -
- actuatorStore.getActiveUserCount({ includeBot: false })
+ actuatorStore.getActiveUserCount({
+ includeBot: false,
+ includeServiceAccount: false,
+ })
);
});