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 more fields to slow query weekly mail (#8444)
zll6007b2ad7478658
diff --git a/backend/runner/mail/slow_query_weekly_mail.go b/backend/runner/mail/slow_query_weekly_mail.goindex f936e2e8b5..2dcbc0e90c 100644--- a/backend/runner/mail/slow_query_weekly_mail.go+++ b/backend/runner/mail/slow_query_weekly_mail.go@@ -13,6 +13,7 @@ import ("time""google.golang.org/protobuf/encoding/protojson"+ "google.golang.org/protobuf/types/known/durationpb""github.com/bytebase/bytebase/backend/common/log""github.com/bytebase/bytebase/backend/component/state"@@ -347,6 +348,17 @@ func (s *SlowQueryWeeklyMailSender) generateWeeklyEmailForProject(ctx context.Cosort.Slice(logs, func(i, j int) bool {return logs[i].Statistics.MaximumQueryTime.AsDuration() > logs[j].Statistics.MaximumQueryTime.AsDuration()})++ total := totalValue{+ totalCount: 0,+ totalQueryTime: 0,+ }++ for _, log := range logs {+ total.totalCount += log.Statistics.Count+ total.totalQueryTime += log.Statistics.AverageQueryTime.AsDuration() * time.Duration(log.Statistics.Count)+ }+if len(logs) > 5 {logs = logs[:5]}@@ -361,6 +373,11 @@ func (s *SlowQueryWeeklyMailSender) generateWeeklyEmailForProject(ctx context.Coitem = strings.ReplaceAll(item, "{{DB_NAME}}", "")}item = strings.ReplaceAll(item, "{{SLOW_QUERY}}", log.Statistics.SqlFingerprint)+ item = strings.ReplaceAll(item, "{{TOTAL_QUERY_COUNT}}", fmt.Sprintf("%d", log.Statistics.Count))+ item = strings.ReplaceAll(item, "{{QUERY_COUNT}}", fmt.Sprintf("%.2f%%", (float64(log.Statistics.Count)/float64(total.totalCount))*100))+ item = strings.ReplaceAll(item, "{{MAX_QUERY_TIME}}", durationText(log.Statistics.MaximumQueryTime))+ item = strings.ReplaceAll(item, "{{AVG_QUERY_TIME}}", durationText(log.Statistics.AverageQueryTime))+ item = strings.ReplaceAll(item, "{{QUERY_TIME}}", fmt.Sprintf("%.2f%%", (float64(log.Statistics.AverageQueryTime.AsDuration()*time.Duration(log.Statistics.Count))/float64(total.totalQueryTime))*100))if _, err := buf.WriteString(item); err != nil {return "", err}@@ -379,6 +396,21 @@ func (s *SlowQueryWeeklyMailSender) generateWeeklyEmailForProject(ctx context.Coreturn buf.String(), nil}+type totalValue struct {+ totalQueryTime time.Duration+ totalCount int64+}++func durationText(duration *durationpb.Duration) string {+ if duration == nil {+ return "-"+ }+ secs := duration.Seconds+ nanos := duration.Nanos+ total := float64(secs) + float64(nanos/1e9)+ return fmt.Sprintf("%.2fs", total)+}+func engineTypeString(engine db.Type) string {switch engine {case db.MySQL:@@ -606,6 +638,17 @@ func (s *SlowQueryWeeklyMailSender) generateEnvironmentContent(sort.Slice(logs, func(i, j int) bool {return logs[i].Statistics.MaximumQueryTime.AsDuration() > logs[j].Statistics.MaximumQueryTime.AsDuration()})++ total := totalValue{+ totalCount: 0,+ totalQueryTime: 0,+ }++ for _, log := range logs {+ total.totalCount += log.Statistics.Count+ total.totalQueryTime += log.Statistics.AverageQueryTime.AsDuration() * time.Duration(log.Statistics.Count)+ }+if len(logs) > 5 {logs = logs[:5]}@@ -618,6 +661,11 @@ func (s *SlowQueryWeeklyMailSender) generateEnvironmentContent(item = strings.ReplaceAll(string(databaseTableItem), "{{DB_NAME}}", "")}item = strings.ReplaceAll(item, "{{SLOW_QUERY}}", log.Statistics.SqlFingerprint)+ item = strings.ReplaceAll(item, "{{TOTAL_QUERY_COUNT}}", fmt.Sprintf("%d", log.Statistics.Count))+ item = strings.ReplaceAll(item, "{{QUERY_COUNT}}", fmt.Sprintf("%.2f%%", (float64(log.Statistics.Count)/float64(total.totalCount))*100))+ item = strings.ReplaceAll(item, "{{MAX_QUERY_TIME}}", durationText(log.Statistics.MaximumQueryTime))+ item = strings.ReplaceAll(item, "{{AVG_QUERY_TIME}}", durationText(log.Statistics.AverageQueryTime))+ item = strings.ReplaceAll(item, "{{QUERY_TIME}}", fmt.Sprintf("%.2f%%", (float64(log.Statistics.AverageQueryTime.AsDuration()*time.Duration(log.Statistics.Count))/float64(total.totalQueryTime))*100))if _, err := buf.WriteString(item); err != nil {return err}diff --git a/backend/runner/mail/templates/for-dba/database_table_header.html b/backend/runner/mail/templates/for-dba/database_table_header.htmlindex cfb5d565ad..54502460d2 100644--- a/backend/runner/mail/templates/for-dba/database_table_header.html+++ b/backend/runner/mail/templates/for-dba/database_table_header.html@@ -2,4 +2,9 @@<tr><th>DB</th><th>Slow Query</th>+ <th>Total Query Count</th>+ <th>Query Count %</th>+ <th>Max Query Time</th>+ <th>Avg Query Time</th>+ <th>Query Time %</th></tr>\ No newline at end of filediff --git a/backend/runner/mail/templates/for-dba/database_table_item.html b/backend/runner/mail/templates/for-dba/database_table_item.htmlindex 8baccd8a1b..4c8951671c 100644--- a/backend/runner/mail/templates/for-dba/database_table_item.html+++ b/backend/runner/mail/templates/for-dba/database_table_item.html@@ -1,4 +1,9 @@<tr><td>{{DB_NAME}}</td><td><span class="sql-text">{{SLOW_QUERY}}</span></td>+ <td><span class="sql-text">{{TOTAL_QUERY_COUNT}}</span></td>+ <td><span class="sql-text">{{QUERY_COUNT}}</span></td>+ <td><span class="sql-text">{{MAX_QUERY_TIME}}</span></td>+ <td><span class="sql-text">{{AVG_QUERY_TIME}}</span></td>+ <td><span class="sql-text">{{QUERY_TIME}}</span></td></tr>\ No newline at end of filediff --git a/backend/runner/mail/templates/for-project-owner/environment_header.html b/backend/runner/mail/templates/for-project-owner/environment_header.htmlindex 5c21fbfd7c..db8257fa34 100644--- a/backend/runner/mail/templates/for-project-owner/environment_header.html+++ b/backend/runner/mail/templates/for-project-owner/environment_header.html@@ -5,4 +5,9 @@<th>Type</th><th>DB</th><th>Slow Query</th>+ <th>Total Query Count</th>+ <th>Query Count</th>+ <th>Max Query Time</th>+ <th>Avg Query Time</th>+ <th>Query Time</th></tr>\ No newline at end of filediff --git a/backend/runner/mail/templates/for-project-owner/table_item.html b/backend/runner/mail/templates/for-project-owner/table_item.htmlindex 0dc54c1246..6f6f2097c2 100644--- a/backend/runner/mail/templates/for-project-owner/table_item.html+++ b/backend/runner/mail/templates/for-project-owner/table_item.html@@ -2,4 +2,9 @@<td>{{DB_TYPE}}</td><td>{{DB_NAME}}</td><td><span class="sql-text">{{SLOW_QUERY}}</span></td>+ <td><span class="sql-text">{{TOTAL_QUERY_COUNT}}</span></td>+ <td><span class="sql-text">{{QUERY_COUNT}}</span></td>+ <td><span class="sql-text">{{MAX_QUERY_TIME}}</span></td>+ <td><span class="sql-text">{{AVG_QUERY_TIME}}</span></td>+ <td><span class="sql-text">{{QUERY_TIME}}</span></td></tr>\ No newline at end of file