Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat: add sheet metric (#1410)
qsliuc775143bab81
diff --git a/metric/collector/sheet_count.go b/metric/collector/sheet_count.gonew file mode 100644index 0000000000..3abe24967e--- /dev/null+++ b/metric/collector/sheet_count.go@@ -0,0 +1,48 @@+package collector++import (+ "context"++ metricAPI "github.com/bytebase/bytebase/metric"+ "github.com/bytebase/bytebase/plugin/metric"+ "github.com/bytebase/bytebase/store"+)++var _ metric.Collector = (*sheetCountCollector)(nil)++// sheetCountCollector is the metric data collector for sheet.+type sheetCountCollector struct {+ store *store.Store+}++// NewSheetCountCollector creates a new instance of sheetCountCollector+func NewSheetCountCollector(store *store.Store) metric.Collector {+ return &sheetCountCollector{+ store: store,+ }+}++// Collect will collect the metric for sheet+func (c *sheetCountCollector) Collect(ctx context.Context) ([]*metric.Metric, error) {+ var res []*metric.Metric++ sheetCountMetricList, err := c.store.CountSheetGroupByRowstatusVisibilitySourceAndType(ctx)+ if err != nil {+ return nil, err+ }++ for _, sheetCountMetric := range sheetCountMetricList {+ res = append(res, &metric.Metric{+ Name: metricAPI.SheetCountMetricName,+ Value: sheetCountMetric.Count,+ Labels: map[string]string{+ "row_status": string(sheetCountMetric.RowStatus),+ "visibility": string(sheetCountMetric.Visibility),+ "source": string(sheetCountMetric.Source),+ "type": string(sheetCountMetric.Type),+ },+ })+ }++ return res, nil+}diff --git a/metric/metric.go b/metric/metric.goindex 1e6e822bec..f271f6caae 100644--- a/metric/metric.go+++ b/metric/metric.go@@ -6,7 +6,7 @@ import ("github.com/bytebase/bytebase/plugin/metric")-var (+const (// InstanceCountMetricName is the metric name for instance countInstanceCountMetricName metric.Name = "bb.instance.count"// IssueCountMetricName is the metric name for issue count@@ -19,6 +19,8 @@ var (TaskCountMetricName metric.Name = "bb.task.count"// DatabaseCountMetricName is the metric name for database countDatabaseCountMetricName metric.Name = "bb.database.count"+ // SheetCountMetricName is the metric name for sheet count+ SheetCountMetricName metric.Name = "bb.sheet.count")// InstanceCountMetric is the API message for bb.instance.count@@ -64,3 +66,12 @@ type DatabaseCountMetric struct {BackupSettingEnabled *bool // nil if BackupPlanPolicyScheduleUnsetCount int}++// SheetCountMetric is the API message for sheet count metric+type SheetCountMetric struct {+ RowStatus api.RowStatus+ Visibility api.SheetVisibility+ Source api.SheetSource+ Type api.SheetType+ Count int+}diff --git a/server/server.go b/server/server.goindex 2eec6584b7..e5fe51f184 100644--- a/server/server.go+++ b/server/server.go@@ -328,6 +328,7 @@ func (s *Server) initMetricReporter(workspaceID string) {metricReporter.Register(metric.PolicyCountMetricName, metricCollector.NewPolicyCountCollector(s.store))metricReporter.Register(metric.TaskCountMetricName, metricCollector.NewTaskCountCollector(s.store))metricReporter.Register(metric.DatabaseCountMetricName, metricCollector.NewDatabaseCountCollector(s.store))+ metricReporter.Register(metric.SheetCountMetricName, metricCollector.NewSheetCountCollector(s.store))s.MetricReporter = metricReporter}}diff --git a/store/sheet.go b/store/sheet.goindex 8cbae515a1..f1f9cd9c15 100644--- a/store/sheet.go+++ b/store/sheet.go@@ -8,6 +8,7 @@ import ("github.com/bytebase/bytebase/api""github.com/bytebase/bytebase/common"+ "github.com/bytebase/bytebase/metric")// sheetRaw is the store model for an Sheet.@@ -148,6 +149,41 @@ func (s *Store) DeleteSheet(ctx context.Context, delete *api.SheetDelete) errorreturn nil}+// CountSheetGroupByRowstatusVisibilitySourceAndType counts the number of sheets group by row_status, visibility, source and type.+func (s *Store) CountSheetGroupByRowstatusVisibilitySourceAndType(ctx context.Context) ([]*metric.SheetCountMetric, 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 row_status, visibility, source, type, COUNT(*) AS count+ FROM sheets+ GROUP BY row_status, visibility, source, type`)+ if err != nil {+ return nil, FormatError(err)+ }+ defer rows.Close()++ var res []*metric.SheetCountMetric+ for rows.Next() {+ var sheetCount metric.SheetCountMetric+ if err := rows.Scan(+ &sheetCount.RowStatus,+ &sheetCount.Visibility,+ &sheetCount.Source,+ &sheetCount.Type,+ &sheetCount.Count,+ ); err != nil {+ return nil, FormatError(err)+ }+ res = append(res, &sheetCount)+ }++ return res, nil+}+//// private functions//