11#!/usr/bin/env python3
22import argparse
33import json
4+ import sqlite3
45
56
67def cli_options ():
@@ -13,6 +14,11 @@ def cli_options():
1314 parser .add_argument (
1415 "-n" , "--datasource-new" , help = "Name of the NEW Grafana Connector Version 2.x"
1516 )
17+ parser .add_argument (
18+ "-t" ,
19+ "--new-dashboard-title" ,
20+ help = "Rename dashboard. If not set add NEW suffix" ,
21+ )
1622 return parser
1723
1824
@@ -45,9 +51,16 @@ def update_host_tags(target):
4551
4652def main ():
4753 args = cli_options ().parse_args ()
48- with open (args .json_file ) as fd :
49- dash = json .load (fd )
54+ con = sqlite3 .connect (args .json_file )
55+ cur = con .cursor ()
56+ row = list (cur .execute ("select data from dashboard where slug = 'cmk'" ))
57+ dash = json .loads (row [0 ][0 ])
5058
59+ # if title := args.new_dashboard_title:
60+ # dash["title"] = title
61+ # else:
62+ dash ["title" ] += " NEW"
63+ dash ["uid" ] += "1"
5164
5265 for panel in dash ["panels" ]:
5366 if panel .get ("datasource" ) == args .datasource_old :
@@ -72,10 +85,26 @@ def main():
7285 config_set (target , ["context" , "siteopt" , "site" ], site )
7386
7487 if combined := target .pop ("combinedgraph" , None ):
75- target ["params" ]["graph_name" ] = combined
88+ config_set (target , ["params" , "graph_name" ], combined )
89+
90+ mode = target .pop ("mode" , "" )
91+ if mode == "metric" :
92+ nested_set (target , ["params" , "graphMode" ], "metric" )
93+
94+ # if mode == "graph":
95+ # nested_set(
96+ # target, ["params", "graph_name"], str(target.pop("graph", "0"))
97+ # )
98+
7699 if presentation := target .pop ("presentiation" , None ):
77100 target ["params" ]["presentation" ] = presentation
78101
102+ cur .execute (
103+ "update dashboard set data = '%s' where slug = 'cmk-new'" % json .dumps (dash )
104+ )
105+ con .commit ()
106+ con .close ()
107+
79108 print (json .dumps (dash ))
80109
81110
0 commit comments