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)
|
return nil, fmt.Errorf("execute template %q: %w", tmpl.Name(), err)
|
||||||
}
|
}
|
||||||
q := rawQuery
|
q := rawQuery
|
||||||
// if true {
|
if false {
|
||||||
// pretty := sqltemplate.RemoveEmptyLines(rawQuery)
|
pretty := sqltemplate.RemoveEmptyLines(rawQuery)
|
||||||
// fmt.Printf("DASHBOARD QUERY: %s [%+v] // %+v\n", pretty, req.GetArgs(), query)
|
fmt.Printf("DASHBOARD QUERY: %s [%+v] // %+v\n", pretty, req.GetArgs(), query)
|
||||||
// }
|
}
|
||||||
|
|
||||||
rows, err := sql.DB.GetSqlxSession().Query(ctx, q, req.GetArgs()...)
|
rows, err := sql.DB.GetSqlxSession().Query(ctx, q, req.GetArgs()...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -132,12 +132,16 @@ type rowsWrapper struct {
|
|||||||
a *dashboardSqlAccess
|
a *dashboardSqlAccess
|
||||||
rows *sql.Rows
|
rows *sql.Rows
|
||||||
history bool
|
history bool
|
||||||
|
count int
|
||||||
|
|
||||||
canReadDashboard func(scopes ...string) bool
|
canReadDashboard func(scopes ...string) bool
|
||||||
|
|
||||||
// Current
|
// Current
|
||||||
row *dashboardRow
|
row *dashboardRow
|
||||||
err error
|
err error
|
||||||
|
|
||||||
|
// max 100 rejected?
|
||||||
|
rejected []dashboardRow
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *dashboardSqlAccess) GetResourceStats(ctx context.Context, namespace string, minCount int) ([]resource.ResourceStats, error) {
|
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
|
// breaks after first readable value
|
||||||
for r.rows.Next() {
|
for r.rows.Next() {
|
||||||
|
r.count++
|
||||||
|
|
||||||
r.row, err = r.a.scanRow(r.rows, r.history)
|
r.row, err = r.a.scanRow(r.rows, r.history)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
r.err = err
|
if len(r.rejected) > 1000 || r.row == nil {
|
||||||
return false
|
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 {
|
if r.row != nil {
|
||||||
@ -177,7 +187,7 @@ func (r *rowsWrapper) Next() bool {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// returns the first folder it can
|
// returns the first visible dashboard
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -245,7 +255,7 @@ func (a *dashboardSqlAccess) scanRow(rows *sql.Rows, history bool) (*dashboardRo
|
|||||||
var createdByID sql.NullInt64
|
var createdByID sql.NullInt64
|
||||||
var message sql.NullString
|
var message sql.NullString
|
||||||
|
|
||||||
var plugin_id string
|
var plugin_id sql.NullString
|
||||||
var origin_name sql.NullString
|
var origin_name sql.NullString
|
||||||
var origin_path sql.NullString
|
var origin_path sql.NullString
|
||||||
var origin_ts sql.NullInt64
|
var origin_ts sql.NullInt64
|
||||||
@ -319,17 +329,17 @@ func (a *dashboardSqlAccess) scanRow(rows *sql.Rows, history bool) (*dashboardRo
|
|||||||
repo.Path = originPath
|
repo.Path = originPath
|
||||||
}
|
}
|
||||||
meta.SetRepositoryInfo(repo)
|
meta.SetRepositoryInfo(repo)
|
||||||
} else if plugin_id != "" {
|
} else if plugin_id.String != "" {
|
||||||
meta.SetRepositoryInfo(&utils.ResourceRepositoryInfo{
|
meta.SetRepositoryInfo(&utils.ResourceRepositoryInfo{
|
||||||
Name: "plugin",
|
Name: "plugin",
|
||||||
Path: plugin_id,
|
Path: plugin_id.String,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(data) > 0 {
|
if len(data) > 0 {
|
||||||
err = dash.Spec.UnmarshalJSON(data)
|
err = dash.Spec.UnmarshalJSON(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return row, err
|
return row, fmt.Errorf("JSON unmarshal error for: %s // %w", dash.Name, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
dash.Spec.Remove("id")
|
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")
|
return nil, fmt.Errorf("expected non zero orgID")
|
||||||
}
|
}
|
||||||
|
|
||||||
sql, err := a.sql(ctx)
|
sqlx, err := a.sql(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
req := newLibraryQueryReq(sql, &query)
|
req := newLibraryQueryReq(sqlx, &query)
|
||||||
rawQuery, err := sqltemplate.Execute(sqlQueryPanels, req)
|
rawQuery, err := sqltemplate.Execute(sqlQueryPanels, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("execute template %q: %w", sqlQueryPanels.Name(), err)
|
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
|
q := rawQuery
|
||||||
|
|
||||||
res := &dashboard.LibraryPanelList{}
|
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() {
|
defer func() {
|
||||||
if rows != nil {
|
if rows != nil {
|
||||||
_ = rows.Close()
|
_ = rows.Close()
|
||||||
@ -479,7 +489,7 @@ func (a *dashboardSqlAccess) GetLibraryPanels(ctx context.Context, query Library
|
|||||||
type panel struct {
|
type panel struct {
|
||||||
ID int64
|
ID int64
|
||||||
UID string
|
UID string
|
||||||
FolderUID string
|
FolderUID sql.NullString
|
||||||
|
|
||||||
Created time.Time
|
Created time.Time
|
||||||
CreatedBy string
|
CreatedBy string
|
||||||
@ -551,7 +561,9 @@ func (a *dashboardSqlAccess) GetLibraryPanels(ctx context.Context, query Library
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
meta.SetFolder(p.FolderUID)
|
if p.FolderUID.Valid {
|
||||||
|
meta.SetFolder(p.FolderUID.String)
|
||||||
|
}
|
||||||
meta.SetCreatedBy(p.CreatedBy)
|
meta.SetCreatedBy(p.CreatedBy)
|
||||||
meta.SetGeneration(1)
|
meta.SetGeneration(1)
|
||||||
meta.SetDeprecatedInternalID(p.ID) //nolint:staticcheck
|
meta.SetDeprecatedInternalID(p.ID) //nolint:staticcheck
|
||||||
@ -570,7 +582,7 @@ func (a *dashboardSqlAccess) GetLibraryPanels(ctx context.Context, query Library
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if query.UID == "" {
|
if query.UID == "" {
|
||||||
rv, err := sql.GetResourceVersion(ctx, "library_element", "updated")
|
rv, err := sqlx.GetResourceVersion(ctx, "library_element", "updated")
|
||||||
if err == nil {
|
if err == nil {
|
||||||
res.ResourceVersion = strconv.FormatInt(rv, 10)
|
res.ResourceVersion = strconv.FormatInt(rv, 10)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user