github.com/turbot/steampipe@v1.7.0-rc.0.0.20240517123944-7cef272d4458/pkg/dashboard/dashboardtypes/snapshot.go (about)

     1  package dashboardtypes
     2  
     3  import (
     4  	"encoding/json"
     5  	"time"
     6  
     7  	steampipecloud "github.com/turbot/steampipe-cloud-sdk-go"
     8  )
     9  
    10  var SteampipeSnapshotSchemaVersion int64 = 20221222
    11  
    12  type SteampipeSnapshot struct {
    13  	SchemaVersion string                   `json:"schema_version"`
    14  	Panels        map[string]SnapshotPanel `json:"panels"`
    15  	Inputs        map[string]interface{}   `json:"inputs"`
    16  	Variables     map[string]string        `json:"variables"`
    17  	SearchPath    []string                 `json:"search_path"`
    18  	StartTime     time.Time                `json:"start_time"`
    19  	EndTime       time.Time                `json:"end_time"`
    20  	Layout        *SnapshotTreeNode        `json:"layout"`
    21  	FileNameRoot  string                   `json:"-"`
    22  	Title         string                   `json:"-"`
    23  }
    24  
    25  // IsExportSourceData implements ExportSourceData
    26  func (*SteampipeSnapshot) IsExportSourceData() {}
    27  
    28  func (s *SteampipeSnapshot) AsCloudSnapshot() (*steampipecloud.WorkspaceSnapshotData, error) {
    29  	jsonbytes, err := json.Marshal(s)
    30  	if err != nil {
    31  		return nil, err
    32  	}
    33  
    34  	res := &steampipecloud.WorkspaceSnapshotData{}
    35  	if err := json.Unmarshal(jsonbytes, res); err != nil {
    36  		return nil, err
    37  	}
    38  
    39  	return res, nil
    40  }
    41  
    42  func (s *SteampipeSnapshot) AsStrippedJson(indent bool) ([]byte, error) {
    43  	res, err := s.AsCloudSnapshot()
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  	if err = StripSnapshot(res); err != nil {
    48  		return nil, err
    49  	}
    50  	if indent {
    51  		return json.MarshalIndent(res, "", "  ")
    52  	}
    53  	return json.Marshal(res)
    54  }
    55  
    56  func StripSnapshot(snapshot *steampipecloud.WorkspaceSnapshotData) error {
    57  	propertiesToStrip := []string{
    58  		"sql",
    59  		"source_definition",
    60  		"documentation",
    61  		"search_path",
    62  		"search_path_prefix"}
    63  	for _, p := range snapshot.Panels {
    64  		panel := p.(map[string]any)
    65  		properties, _ := panel["properties"].(map[string]any)
    66  		for _, property := range propertiesToStrip {
    67  			// look both at top level and under properties
    68  			delete(panel, property)
    69  			if properties != nil {
    70  				delete(properties, property)
    71  			}
    72  		}
    73  	}
    74  	return nil
    75  }