Skip to content

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

feat: add task metric (#1371)

qsliu569ee174afc1
diff --git a/metric/collector/task_count.go b/metric/collector/task_count.go
new file mode 100644
index 0000000000..bbebd9cf41
--- /dev/null
+++ b/metric/collector/task_count.go
@@ -0,0 +1,47 @@
+package collector
+
+import (
+ "context"
+
+ metricAPI "github.com/bytebase/bytebase/metric"
+ "github.com/bytebase/bytebase/plugin/metric"
+ "github.com/bytebase/bytebase/plugin/metric/collector"
+ "github.com/bytebase/bytebase/store"
+ "go.uber.org/zap"
+)
+
+// taskCountCollector is the metric data collector for task.
+type taskCountCollector struct {
+ l *zap.Logger
+ store *store.Store
+}
+
+// NewTaskCountCollector creates a new instance of taskCollector.
+func NewTaskCountCollector(l *zap.Logger, store *store.Store) collector.MetricCollector {
+ return &taskCountCollector{
+ l: l,
+ store: store,
+ }
+}
+
+// Collect will collect the metric for task.
+func (c *taskCountCollector) Collect(ctx context.Context) ([]*metric.Metric, error) {
+ var res []*metric.Metric
+
+ taskCountMetricList, err := c.store.CountTaskGroupByTypeAndStatus(ctx)
+ if err != nil {
+ return nil, err
+ }
+
+ for _, taskCountMetric := range taskCountMetricList {
+ res = append(res, &metric.Metric{
+ Name: metricAPI.TaskCountMetricName,
+ Value: taskCountMetric.Count,
+ Labels: map[string]string{
+ "type": string(taskCountMetric.Type),
+ "status": string(taskCountMetric.Status),
+ },
+ })
+ }
+ return res, nil
+}
diff --git a/metric/metric.go b/metric/metric.go
index 0d4f643c68..71eb8b7c30 100644
--- a/metric/metric.go
+++ b/metric/metric.go
@@ -15,6 +15,8 @@ var (
PolicyCountMetricName metric.Name = "bb.policy.count"
// ProjectCountMetricName is the metric name for project count
ProjectCountMetricName metric.Name = "bb.project.count"
+ // TaskCountMetricName is the metric name for database count
+ TaskCountMetricName metric.Name = "bb.task.count"
)
// InstanceCountMetric is the API message for bb.instance.count
@@ -46,3 +48,10 @@ type PolicyCountMetric struct {
EnvironmentName string
Count int
}
+
+// TaskCountMetric is the API message for database count metric
+type TaskCountMetric struct {
+ Type api.TaskType
+ Status api.TaskStatus
+ Count int
+}
diff --git a/server/server.go b/server/server.go
index 00a166bbc2..a422b7a1e5 100644
--- a/server/server.go
+++ b/server/server.go
@@ -329,6 +329,7 @@ func (server *Server) initMetricReporter(workspaceID string) {
metricReporter.Register(metric.IssueCountMetricName, metricCollector.NewIssueCollector(server.l, server.store))
metricReporter.Register(metric.ProjectCountMetricName, metricCollector.NewProjectCollector(server.l, server.store))
metricReporter.Register(metric.PolicyCountMetricName, metricCollector.NewPolicyCollector(server.l, server.store))
+ metricReporter.Register(metric.TaskCountMetricName, metricCollector.NewTaskCountCollector(server.l, server.store))
server.MetricReporter = metricReporter
}
}
diff --git a/store/task.go b/store/task.go
index 16fc85f574..2c369ae7fa 100644
--- a/store/task.go
+++ b/store/task.go
@@ -10,6 +10,7 @@ import (
"github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/common"
+ "github.com/bytebase/bytebase/metric"
"go.uber.org/zap"
)
@@ -158,6 +159,38 @@ func (s *Store) PatchTaskStatus(ctx context.Context, patch *api.TaskStatusPatch)
return task, nil
}
+// CountTaskGroupByTypeAndStatus counts the number of TaskGroup and group by TaskType.
+// Used for the metric collector.
+func (s *Store) CountTaskGroupByTypeAndStatus(ctx context.Context) ([]*metric.TaskCountMetric, error) {
+ tx, err := s.db.BeginTx(ctx, nil)
+ if err != nil {
+ return nil, FormatError(err)
+ }
+ defer tx.PTx.Rollback()
+
+ rows, err := tx.PTx.QueryContext(ctx, `
+ SELECT type, status, COUNT(*)
+ FROM task
+ WHERE (id <= 102 AND updater_id != 1) OR id > 102
+ GROUP BY type, status`,
+ )
+ if err != nil {
+ return nil, FormatError(err)
+ }
+ defer rows.Close()
+
+ var res []*metric.TaskCountMetric
+ for rows.Next() {
+ var metric metric.TaskCountMetric
+ if err := rows.Scan(&metric.Type, &metric.Status, &metric.Count); err != nil {
+ return nil, FormatError(err)
+ }
+ res = append(res, &metric)
+ }
+
+ return res, nil
+}
+
//
// private functions
//