mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
K8s/Dashboard: Improve legacy error handling (#99658)
This commit is contained in:
parent
6ba18d05be
commit
5cd1efb2d1
@ -102,10 +102,10 @@ func (a *dashboardSqlAccess) getRows(ctx context.Context, sql *legacysql.LegacyD
|
||||
return nil, fmt.Errorf("execute template %q: %w", tmpl.Name(), err)
|
||||
}
|
||||
q := rawQuery
|
||||
// if true {
|
||||
// pretty := sqltemplate.RemoveEmptyLines(rawQuery)
|
||||
// fmt.Printf("DASHBOARD QUERY: %s [%+v] // %+v\n", pretty, req.GetArgs(), query)
|
||||
// }
|
||||
if false {
|
||||
pretty := sqltemplate.RemoveEmptyLines(rawQuery)
|
||||
fmt.Printf("DASHBOARD QUERY: %s [%+v] // %+v\n", pretty, req.GetArgs(), query)
|
||||
}
|
||||
|
||||
rows, err := sql.DB.GetSqlxSession().Query(ctx, q, req.GetArgs()...)
|
||||
if err != nil {
|
||||
@ -132,12 +132,16 @@ type rowsWrapper struct {
|
||||
a *dashboardSqlAccess
|
||||
rows *sql.Rows
|
||||
history bool
|
||||
count int
|
||||
|
||||
canReadDashboard func(scopes ...string) bool
|
||||
|
||||
// Current
|
||||
row *dashboardRow
|
||||
err error
|
||||
|
||||
// max 100 rejected?
|
||||
rejected []dashboardRow
|
||||
}
|
||||
|
||||
func (a *dashboardSqlAccess) GetResourceStats(ctx context.Context, namespace string, minCount int) ([]resource.ResourceStats, error) {
|
||||
@ -159,10 +163,16 @@ func (r *rowsWrapper) Next() bool {
|
||||
|
||||
// breaks after first readable value
|
||||
for r.rows.Next() {
|
||||
r.count++
|
||||
|
||||
r.row, err = r.a.scanRow(r.rows, r.history)
|
||||
if err != nil {
|
||||
r.err = err
|
||||
return false
|
||||
if len(r.rejected) > 1000 || r.row == nil {
|
||||
r.err = fmt.Errorf("too many rejected rows (%d) %w", len(r.rejected), err)
|
||||
return false
|
||||
}
|
||||
r.rejected = append(r.rejected, *r.row)
|
||||
continue
|
||||
}
|
||||
|
||||
if r.row != nil {
|
||||
@ -177,7 +187,7 @@ func (r *rowsWrapper) Next() bool {
|
||||
continue
|
||||
}
|
||||
|
||||
// returns the first folder it can
|
||||
// returns the first visible dashboard
|
||||
return true
|
||||
}
|
||||
}
|
||||
@ -245,7 +255,7 @@ func (a *dashboardSqlAccess) scanRow(rows *sql.Rows, history bool) (*dashboardRo
|
||||
var createdByID sql.NullInt64
|
||||
var message sql.NullString
|
||||
|
||||
var plugin_id string
|
||||
var plugin_id sql.NullString
|
||||
var origin_name sql.NullString
|
||||
var origin_path sql.NullString
|
||||
var origin_ts sql.NullInt64
|
||||
@ -319,17 +329,17 @@ func (a *dashboardSqlAccess) scanRow(rows *sql.Rows, history bool) (*dashboardRo
|
||||
repo.Path = originPath
|
||||
}
|
||||
meta.SetRepositoryInfo(repo)
|
||||
} else if plugin_id != "" {
|
||||
} else if plugin_id.String != "" {
|
||||
meta.SetRepositoryInfo(&utils.ResourceRepositoryInfo{
|
||||
Name: "plugin",
|
||||
Path: plugin_id,
|
||||
Path: plugin_id.String,
|
||||
})
|
||||
}
|
||||
|
||||
if len(data) > 0 {
|
||||
err = dash.Spec.UnmarshalJSON(data)
|
||||
if err != nil {
|
||||
return row, err
|
||||
return row, fmt.Errorf("JSON unmarshal error for: %s // %w", dash.Name, err)
|
||||
}
|
||||
}
|
||||
dash.Spec.Remove("id")
|
||||
@ -453,12 +463,12 @@ func (a *dashboardSqlAccess) GetLibraryPanels(ctx context.Context, query Library
|
||||
return nil, fmt.Errorf("expected non zero orgID")
|
||||
}
|
||||
|
||||
sql, err := a.sql(ctx)
|
||||
sqlx, err := a.sql(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req := newLibraryQueryReq(sql, &query)
|
||||
req := newLibraryQueryReq(sqlx, &query)
|
||||
rawQuery, err := sqltemplate.Execute(sqlQueryPanels, req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("execute template %q: %w", sqlQueryPanels.Name(), err)
|
||||
@ -466,7 +476,7 @@ func (a *dashboardSqlAccess) GetLibraryPanels(ctx context.Context, query Library
|
||||
q := rawQuery
|
||||
|
||||
res := &dashboard.LibraryPanelList{}
|
||||
rows, err := sql.DB.GetSqlxSession().Query(ctx, q, req.GetArgs()...)
|
||||
rows, err := sqlx.DB.GetSqlxSession().Query(ctx, q, req.GetArgs()...)
|
||||
defer func() {
|
||||
if rows != nil {
|
||||
_ = rows.Close()
|
||||
@ -479,7 +489,7 @@ func (a *dashboardSqlAccess) GetLibraryPanels(ctx context.Context, query Library
|
||||
type panel struct {
|
||||
ID int64
|
||||
UID string
|
||||
FolderUID string
|
||||
FolderUID sql.NullString
|
||||
|
||||
Created time.Time
|
||||
CreatedBy string
|
||||
@ -551,7 +561,9 @@ func (a *dashboardSqlAccess) GetLibraryPanels(ctx context.Context, query Library
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
meta.SetFolder(p.FolderUID)
|
||||
if p.FolderUID.Valid {
|
||||
meta.SetFolder(p.FolderUID.String)
|
||||
}
|
||||
meta.SetCreatedBy(p.CreatedBy)
|
||||
meta.SetGeneration(1)
|
||||
meta.SetDeprecatedInternalID(p.ID) //nolint:staticcheck
|
||||
@ -570,7 +582,7 @@ func (a *dashboardSqlAccess) GetLibraryPanels(ctx context.Context, query Library
|
||||
}
|
||||
}
|
||||
if query.UID == "" {
|
||||
rv, err := sql.GetResourceVersion(ctx, "library_element", "updated")
|
||||
rv, err := sqlx.GetResourceVersion(ctx, "library_element", "updated")
|
||||
if err == nil {
|
||||
res.ResourceVersion = strconv.FormatInt(rv, 10)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user