Skip to content

Commit 7501ceb

Browse files
fix: fix handling of table calcs and filters on dashboard import
1 parent 83bea90 commit 7501ceb

1 file changed

Lines changed: 49 additions & 4 deletions

File tree

internal/cmd/dashboard.go

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -351,11 +351,17 @@ var dashboardImportCmd = &cobra.Command{
351351
file := args[0]
352352
folderID := args[1]
353353

354+
if cfgDebug {
355+
fmt.Fprintf(os.Stderr, "Reading dashboard file %s\n", file)
356+
}
354357
b, err := util.ReadFileOrStdin(file)
355358
if err != nil {
356359
return fmt.Errorf("failed to read file %s: %w", file, err)
357360
}
358361

362+
if cfgDebug {
363+
fmt.Fprintf(os.Stderr, "Parsing dashboard JSON\n")
364+
}
359365
var m map[string]interface{}
360366
if err := json.Unmarshal(b, &m); err != nil {
361367
return fmt.Errorf("invalid json in %s: %w", file, err)
@@ -365,6 +371,9 @@ var dashboardImportCmd = &cobra.Command{
365371
return fmt.Errorf("file contains no dashboard_elements! Is this a look?")
366372
}
367373

374+
if cfgDebug {
375+
fmt.Fprintf(os.Stderr, "Fetching authenticated user (Me)\n")
376+
}
368377
me, err := c.SDK.Me("id", nil)
369378
if err != nil || me.Id == nil {
370379
return fmt.Errorf("failed to get me: %v", err)
@@ -385,6 +394,10 @@ var dashboardImportCmd = &cobra.Command{
385394
var conflictingFolder string
386395

387396
if slug != "" {
397+
if cfgDebug {
398+
fmt.Fprintf(os.Stderr, "Searching for existing dashboard by slug '%s'\n", slug)
399+
}
400+
388401
dashes, _ := c.SDK.SearchDashboards(v4.RequestSearchDashboards{Slug: &slug}, nil)
389402
if len(dashes) > 0 {
390403
match := dashes[0]
@@ -400,6 +413,10 @@ var dashboardImportCmd = &cobra.Command{
400413
}
401414

402415
if existingDash == nil && title != "" {
416+
if cfgDebug {
417+
fmt.Fprintf(os.Stderr, "Searching for existing dashboard by title '%s'\n", title)
418+
}
419+
403420
dashes, _ := c.SDK.SearchDashboards(v4.RequestSearchDashboards{Title: &title, FolderId: &folderID}, nil)
404421
if len(dashes) > 0 {
405422
existingDash = &dashes[0]
@@ -422,6 +439,10 @@ var dashboardImportCmd = &cobra.Command{
422439
var resultDash *v4.Dashboard
423440

424441
if existingDash != nil {
442+
if cfgDebug {
443+
fmt.Fprintf(os.Stderr, "Updating existing dashboard %s in folder %s\n", *existingDash.Id, folderID)
444+
}
445+
425446
if !dashboardImportForce {
426447
return fmt.Errorf("dashboard '%s' already exists in folder %s. Use --force to overwrite", title, folderID)
427448
}
@@ -454,6 +475,10 @@ var dashboardImportCmd = &cobra.Command{
454475
}
455476
}
456477
} else {
478+
if cfgDebug {
479+
fmt.Fprintf(os.Stderr, "Creating new dashboard in folder %s\n", folderID)
480+
}
481+
457482
created, err := c.SDK.CreateDashboard(wd, nil)
458483
if err != nil {
459484
return fmt.Errorf("failed to create dashboard: %w", err)
@@ -463,7 +488,10 @@ var dashboardImportCmd = &cobra.Command{
463488

464489
resID := *resultDash.Id
465490

466-
if filtersVal, ok := m["dashboard_filters"].([]interface{}); ok {
491+
if filtersVal, ok := m["dashboard_filters"].([]interface{}); ok && len(filtersVal) > 0 {
492+
if cfgDebug {
493+
fmt.Fprintf(os.Stderr, "Importing %d dashboard filters\n", len(filtersVal))
494+
}
467495
for _, fv := range filtersVal {
468496
fb, _ := json.Marshal(fv)
469497
var wdf v4.WriteCreateDashboardFilter
@@ -474,7 +502,10 @@ var dashboardImportCmd = &cobra.Command{
474502
}
475503

476504
var activeLayoutID string
477-
if layoutsVal, ok := m["dashboard_layouts"].([]interface{}); ok {
505+
if layoutsVal, ok := m["dashboard_layouts"].([]interface{}); ok && len(layoutsVal) > 0 {
506+
if cfgDebug {
507+
fmt.Fprintf(os.Stderr, "Importing %d dashboard layouts\n", len(layoutsVal))
508+
}
478509
for _, lv := range layoutsVal {
479510
lb, _ := json.Marshal(lv)
480511
var wdl v4.WriteDashboardLayout
@@ -503,7 +534,10 @@ var dashboardImportCmd = &cobra.Command{
503534
if layoutObj != nil && layoutObj.Id != nil {
504535
activeLayoutID = *layoutObj.Id
505536
if lm, ok := lv.(map[string]interface{}); ok {
506-
if componentsVal, ok := lm["dashboard_layout_components"].([]interface{}); ok {
537+
if componentsVal, ok := lm["dashboard_layout_components"].([]interface{}); ok && len(componentsVal) > 0 {
538+
if cfgDebug {
539+
fmt.Fprintf(os.Stderr, "Importing %d dashboard layout components\n", len(componentsVal))
540+
}
507541
matchedElems := make(map[int]bool)
508542
for cIdx, cv := range componentsVal {
509543
cb, _ := json.Marshal(cv)
@@ -556,13 +590,15 @@ var dashboardImportCmd = &cobra.Command{
556590
}
557591

558592
if elemMap != nil {
593+
if cfgDebug {
594+
fmt.Fprintf(os.Stderr, "Creating dashboard element '%v'\n", elemMap["title"])
595+
}
559596
eb, _ := json.Marshal(elemMap)
560597
var wde v4.WriteDashboardElement
561598
_ = json.Unmarshal(eb, &wde)
562599
wde.DashboardId = &resID
563600

564601
wde.ResultMakerId = nil
565-
wde.ResultMaker = nil
566602
wde.LookId = nil
567603
wde.QueryId = nil
568604
wde.MergeResultId = nil
@@ -574,6 +610,7 @@ var dashboardImportCmd = &cobra.Command{
574610
lID, err := UpsertLookHelper(c, folderID, myID, lookVal, dashboardImportForce)
575611
if err == nil && lID != "" {
576612
wde.LookId = &lID
613+
wde.ResultMaker = nil
577614
} else if err != nil {
578615
fmt.Fprintf(os.Stderr, "UpsertLookHelper failed: %v\n", err)
579616
}
@@ -585,6 +622,10 @@ var dashboardImportCmd = &cobra.Command{
585622
cq, err := c.SDK.CreateQuery(wq, "", nil)
586623
if err == nil && cq.Id != nil {
587624
wde.QueryId = cq.Id
625+
if wde.ResultMaker != nil {
626+
wde.ResultMaker.SqlQueryId = nil
627+
wde.ResultMaker.Query = nil
628+
}
588629
} else if err != nil {
589630
fmt.Fprintf(os.Stderr, "CreateQuery failed: %v\n", err)
590631
}
@@ -595,6 +636,7 @@ var dashboardImportCmd = &cobra.Command{
595636
cmq, err := c.SDK.CreateMergeQuery(wmq, "", nil)
596637
if err == nil && cmq.Id != nil {
597638
wde.MergeResultId = cmq.Id
639+
wde.ResultMaker = nil
598640
} else if err != nil {
599641
fmt.Fprintf(os.Stderr, "CreateMergeQuery failed: %v\n", err)
600642
}
@@ -636,6 +678,9 @@ var dashboardImportCmd = &cobra.Command{
636678
}
637679

638680
if plansVal, ok := m["scheduled_plans"].([]interface{}); ok && len(plansVal) > 0 {
681+
if cfgDebug {
682+
fmt.Fprintf(os.Stderr, "Importing %d scheduled plans\n", len(plansVal))
683+
}
639684
existingPlans, _ := c.SDK.ScheduledPlansForDashboard(v4.RequestScheduledPlansForDashboard{DashboardId: resID, AllUsers: ptrBool(true)}, nil)
640685

641686
for _, pv := range plansVal {

0 commit comments

Comments
 (0)