mirror of
https://github.com/pommee/goaway.git
synced 2026-01-11 00:20:31 -06:00
perf: better performance when fetching unique query types
This commit is contained in:
@@ -16,3 +16,8 @@ type DomainHistory struct {
|
||||
Domain string `json:"domain"`
|
||||
Timestamp time.Time `json:"timestamp"`
|
||||
}
|
||||
|
||||
type QueryTypeCount struct {
|
||||
QueryType string `json:"queryType"`
|
||||
Count int `json:"count"`
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ type RequestLog struct {
|
||||
Domain string `gorm:"type:varchar(255);not null;index:idx_domain_timestamp,priority:1;index:idx_client_ip_domain,priority:2" json:"domain" validate:"required"`
|
||||
ClientIP string `gorm:"type:varchar(45);not null;index:idx_client_ip;index:idx_client_ip_domain,priority:1" json:"clientIP" validate:"required,ip"`
|
||||
ClientName string `gorm:"type:varchar(255)" json:"clientName"`
|
||||
QueryType string `gorm:"type:varchar(10)" json:"queryType"`
|
||||
QueryType string `gorm:"type:varchar(10);index:idx_query_type" json:"queryType"`
|
||||
Status string `gorm:"type:varchar(20)" json:"status"`
|
||||
Protocol string `gorm:"type:varchar(10)" json:"protocol"`
|
||||
ResponseTimeNs int64 `gorm:"not null" json:"repsonseTimeNS"`
|
||||
|
||||
@@ -18,7 +18,7 @@ type Repository interface {
|
||||
GetDistinctRequestIP() int
|
||||
GetRequestSummaryByInterval(interval int) ([]model.RequestLogIntervalSummary, error)
|
||||
GetResponseSizeSummaryByInterval(intervalMinutes int) ([]model.ResponseSizeSummary, error)
|
||||
GetUniqueQueryTypes() ([]interface{}, error)
|
||||
GetUniqueQueryTypes() ([]models.QueryTypeCount, error)
|
||||
FetchQueries(q models.QueryParams) ([]model.RequestLogEntry, error)
|
||||
GetUniqueClientNameAndIP() []database.RequestLog
|
||||
FetchAllClients() (map[string]Client, error)
|
||||
@@ -164,44 +164,18 @@ func (r *repository) GetResponseSizeSummaryByInterval(intervalMinutes int) ([]mo
|
||||
return summaries, nil
|
||||
}
|
||||
|
||||
func (r *repository) GetUniqueQueryTypes() ([]interface{}, error) {
|
||||
query := `
|
||||
SELECT COUNT(*) AS count, query_type
|
||||
FROM request_logs
|
||||
WHERE query_type <> ''
|
||||
GROUP BY query_type
|
||||
ORDER BY count DESC`
|
||||
|
||||
rows, err := r.db.Raw(query).Rows()
|
||||
if err != nil {
|
||||
func (r *repository) GetUniqueQueryTypes() ([]models.QueryTypeCount, error) {
|
||||
stats := make([]models.QueryTypeCount, 0, 5)
|
||||
if err := r.db.Model(&database.RequestLog{}).
|
||||
Select("query_type, COUNT(*) as count").
|
||||
Where("query_type != ?", "").
|
||||
Group("query_type").
|
||||
Order("count DESC").
|
||||
Find(&stats).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer func(rows *sql.Rows) {
|
||||
if err := rows.Close(); err != nil {
|
||||
log.Error("Failed to close rows: %v", err)
|
||||
}
|
||||
}(rows)
|
||||
|
||||
var queries []any
|
||||
for rows.Next() {
|
||||
query := struct {
|
||||
QueryType string `json:"queryType"`
|
||||
Count int `json:"count"`
|
||||
}{}
|
||||
|
||||
if err := rows.Scan(&query.Count, &query.QueryType); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
queries = append(queries, query)
|
||||
}
|
||||
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return queries, nil
|
||||
return stats, nil
|
||||
}
|
||||
|
||||
func (r *repository) FetchQueries(q models.QueryParams) ([]model.RequestLogEntry, error) {
|
||||
|
||||
@@ -34,7 +34,7 @@ func (s *Service) GetResponseSizeSummaryByInterval(intervalMinutes int) ([]model
|
||||
return s.repository.GetResponseSizeSummaryByInterval(intervalMinutes)
|
||||
}
|
||||
|
||||
func (s *Service) GetUniqueQueryTypes() ([]interface{}, error) {
|
||||
func (s *Service) GetUniqueQueryTypes() ([]models.QueryTypeCount, error) {
|
||||
return s.repository.GetUniqueQueryTypes()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user