@@ -44,7 +44,7 @@ func (d *SampleDatasource) Dispose() {
4444// datasource configuration page which allows users to verify that
4545// a datasource is working as expected.
4646func (d * SampleDatasource ) CheckHealth (_ context.Context , req * backend.CheckHealthRequest ) (* backend.CheckHealthResult , error ) {
47- log .DefaultLogger .Info ( "CheckHealth called" , "request" , req )
47+ log .DefaultLogger .Debug ( "Backend called with following request " , "request" , req )
4848
4949 uiProperties := make (map [string ]string )
5050 _ = json .Unmarshal (req .PluginContext .DataSourceInstanceSettings .JSONData , & uiProperties )
@@ -82,56 +82,63 @@ func (d *SampleDatasource) CheckHealth(_ context.Context, req *backend.CheckHeal
8282 return nil , err
8383 }
8484 gitApi .FetchRepo (* repository )
85+ gitApi .PullRepo (* repository )
8586 fileMap := gitApi .GetFileContent ()
8687 grafanaApi .CreateDashboardObjects (fileMap )
8788 log .DefaultLogger .Info ("Dashboards created" )
8889 }
8990
9091 if push == "true" {
92+ log .DefaultLogger .Info ("Push to git repo" , "url" , pushGitURL )
93+
94+ gitApi := NewGitApi (pushGitURL , privateKeyFilePath )
95+ repository , err := gitApi .CloneRepo ()
96+ if err != nil {
97+ return nil , err
98+ }
99+ gitApi .FetchRepo (* repository )
100+
91101 dashboards , err := grafanaApi .SearchDashboardsWithTag (dashboardTag )
92102 if err != nil {
93103 log .DefaultLogger .Error ("search dashboard" , "error" , err .Error ())
94104 }
95105 for _ , dashboard := range dashboards {
96- // get raw Json
97- dashboardJson , boardProperties , err := grafanaApi .GetRawDashboardByID (dashboard .UID )
98- if err != nil {
99- log .DefaultLogger .Error ("get raw dashboard" , "error" , err .Error ())
100- }
101-
102- // get folder name and id, needed for update processes and git folder structure
103- folderName := boardProperties .FolderTitle
104- folderId := boardProperties .FolderID
105-
106- // get dashboard Object TODO: Verify if raw Json manipulation is faster
107- dashboardObject , _ , err := grafanaApi .GetDashboardObjectByID (dashboard .UID )
106+ // get dashboard Object and Properties
107+ dashboardObject , boardProperties , err := grafanaApi .GetDashboardObjectByID (dashboard .UID )
108108 if err != nil {
109109 log .DefaultLogger .Error ("get dashboard" , "error" , err .Error ())
110110 }
111111
112- // delete Tag from dashboard
112+ // delete Tag from dashboard Object
113113 dashboardWithDeletedTag := grafanaApi .DeleteTagFromDashboardObjectByID (dashboardObject , dashboardTag )
114114
115- // update dashboard with deleted Tag
115+ // get folder name and id, required for update processes and git folder structure
116+ folderName := boardProperties .FolderTitle
117+ folderId := boardProperties .FolderID
118+
119+ // update dashboard with deleted Tag in Grafana
116120 _ , err = grafanaApi .UpdateDashboardObjectByID (dashboardWithDeletedTag , folderId )
117121 if err != nil {
118122 log .DefaultLogger .Error ("update dashboard" , "error" , err .Error ())
119123 }
120- log .DefaultLogger .Debug ("Dashboard preparation successfully " )
121124
122- // Todo: If dashboard found, else skip git workflow
123- gitApi := NewGitApi (pushGitURL , privateKeyFilePath )
124- repository , err := gitApi .CloneRepo ()
125+ // get raw Json Dashboard, required for import and export
126+ dashboardJson , _ , err := grafanaApi .GetRawDashboardByID (dashboard .UID )
125127 if err != nil {
126- return nil , err
128+ log . DefaultLogger . Error ( "get raw dashboard" , "error" , err . Error ())
127129 }
128- gitApi .FetchRepo (* repository )
130+
131+ log .DefaultLogger .Debug ("Dashboard preparation successfully " )
129132 gitApi .AddFileWithContent (folderName + "/" + dashboardObject .Title + ".json" , string (dashboardJson ))
133+ log .DefaultLogger .Debug ("Dashboard added to in memory file system" )
134+ }
135+
136+ if len (dashboards ) > 0 {
130137 gitApi .CommitWorktree (* repository , dashboardTag )
131138 gitApi .PushRepo (* repository )
132-
133- log .DefaultLogger .Info ("Dashboard pushed successfully " )
134139 }
140+
141+ log .DefaultLogger .Info ("Dashboard pushed successfully" )
135142 }
136143
137144 return & backend.CheckHealthResult {
0 commit comments