From ba1df4efb0517242dbcc708f1ce3c8873291a706 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard?= Date: Thu, 21 Sep 2023 16:46:43 +0200 Subject: [PATCH 1/3] fix(be,fe): upsert dashboard on provision, export with uuid from frontend --- .../DescriptionOfDashboard/index.tsx | 5 +++- frontend/src/types/api/dashboard/getAll.ts | 1 + pkg/query-service/app/dashboards/model.go | 25 +++++++--------- pkg/query-service/app/dashboards/provision.go | 29 ++++++++++++++----- 4 files changed, 38 insertions(+), 22 deletions(-) diff --git a/frontend/src/container/NewDashboard/DescriptionOfDashboard/index.tsx b/frontend/src/container/NewDashboard/DescriptionOfDashboard/index.tsx index 2faafd7cbb3..b66304a0c23 100644 --- a/frontend/src/container/NewDashboard/DescriptionOfDashboard/index.tsx +++ b/frontend/src/container/NewDashboard/DescriptionOfDashboard/index.tsx @@ -18,7 +18,10 @@ function DescriptionOfDashboard(): JSX.Element { ); const [selectedDashboard] = dashboards; - const selectedData = selectedDashboard.data; + const selectedData = { + ...selectedDashboard.data, + uuid: selectedDashboard.uuid, + }; const { title, tags, description } = selectedData; const [isJSONModalVisible, isIsJSONModalVisible] = useState(false); diff --git a/frontend/src/types/api/dashboard/getAll.ts b/frontend/src/types/api/dashboard/getAll.ts index 886de6ec072..5aee6b4241a 100644 --- a/frontend/src/types/api/dashboard/getAll.ts +++ b/frontend/src/types/api/dashboard/getAll.ts @@ -46,6 +46,7 @@ export interface Dashboard { } export interface DashboardData { + uuid?: string; description?: string; tags?: string[]; name?: string; diff --git a/pkg/query-service/app/dashboards/model.go b/pkg/query-service/app/dashboards/model.go index 2403775cb89..0bd69a43db3 100644 --- a/pkg/query-service/app/dashboards/model.go +++ b/pkg/query-service/app/dashboards/model.go @@ -23,12 +23,14 @@ import ( var db *sqlx.DB // User for mapping job,instance from grafana -var instanceEQRE = regexp.MustCompile("instance(?s)=(?s)\\\"{{.instance}}\\\"") -var nodeEQRE = regexp.MustCompile("instance(?s)=(?s)\\\"{{.node}}\\\"") -var jobEQRE = regexp.MustCompile("job(?s)=(?s)\\\"{{.job}}\\\"") -var instanceRERE = regexp.MustCompile("instance(?s)=~(?s)\\\"{{.instance}}\\\"") -var nodeRERE = regexp.MustCompile("instance(?s)=~(?s)\\\"{{.node}}\\\"") -var jobRERE = regexp.MustCompile("job(?s)=~(?s)\\\"{{.job}}\\\"") +var ( + instanceEQRE = regexp.MustCompile("instance(?s)=(?s)\\\"{{.instance}}\\\"") + nodeEQRE = regexp.MustCompile("instance(?s)=(?s)\\\"{{.node}}\\\"") + jobEQRE = regexp.MustCompile("job(?s)=(?s)\\\"{{.job}}\\\"") + instanceRERE = regexp.MustCompile("instance(?s)=~(?s)\\\"{{.instance}}\\\"") + nodeRERE = regexp.MustCompile("instance(?s)=~(?s)\\\"{{.node}}\\\"") + jobRERE = regexp.MustCompile("job(?s)=~(?s)\\\"{{.job}}\\\"") +) // InitDB sets up setting up the connection pool global variable. func InitDB(dataSourceName string) (*sqlx.DB, error) { @@ -140,6 +142,9 @@ func CreateDashboard(data map[string]interface{}, fm interfaces.FeatureLookup) ( dash.UpdatedAt = time.Now() dash.UpdateSlug() dash.Uuid = uuid.New().String() + if data["uuid"] != nil { + dash.Uuid = data["uuid"].(string) + } map_data, err := json.Marshal(dash.Data) if err != nil { @@ -156,13 +161,11 @@ func CreateDashboard(data map[string]interface{}, fm interfaces.FeatureLookup) ( // db.Prepare("Insert into dashboards where") result, err := db.Exec("INSERT INTO dashboards (uuid, created_at, updated_at, data) VALUES ($1, $2, $3, $4)", dash.Uuid, dash.CreatedAt, dash.UpdatedAt, map_data) - if err != nil { zap.S().Errorf("Error in inserting dashboard data: ", dash, err) return nil, &model.ApiError{Typ: model.ErrorExec, Err: err} } lastInsertId, err := result.LastInsertId() - if err != nil { return nil, &model.ApiError{Typ: model.ErrorExec, Err: err} } @@ -177,7 +180,6 @@ func CreateDashboard(data map[string]interface{}, fm interfaces.FeatureLookup) ( } func GetDashboards() ([]Dashboard, *model.ApiError) { - dashboards := []Dashboard{} query := `SELECT * FROM dashboards` @@ -190,7 +192,6 @@ func GetDashboards() ([]Dashboard, *model.ApiError) { } func DeleteDashboard(uuid string, fm interfaces.FeatureLookup) *model.ApiError { - dashboard, dErr := GetDashboard(uuid) if dErr != nil { zap.S().Errorf("Error in getting dashboard: ", uuid, dErr) @@ -200,7 +201,6 @@ func DeleteDashboard(uuid string, fm interfaces.FeatureLookup) *model.ApiError { query := `DELETE FROM dashboards WHERE uuid=?` result, err := db.Exec(query, uuid) - if err != nil { return &model.ApiError{Typ: model.ErrorExec, Err: err} } @@ -222,7 +222,6 @@ func DeleteDashboard(uuid string, fm interfaces.FeatureLookup) *model.ApiError { } func GetDashboard(uuid string) (*Dashboard, *model.ApiError) { - dashboard := Dashboard{} query := `SELECT * FROM dashboards WHERE uuid=?` @@ -235,7 +234,6 @@ func GetDashboard(uuid string) (*Dashboard, *model.ApiError) { } func UpdateDashboard(uuid string, data map[string]interface{}, fm interfaces.FeatureLookup) (*Dashboard, *model.ApiError) { - map_data, err := json.Marshal(data) if err != nil { zap.S().Errorf("Error in marshalling data field in dashboard: ", data, err) @@ -331,7 +329,6 @@ func (d *Dashboard) UpdateSlug() { } func IsPostDataSane(data *map[string]interface{}) error { - val, ok := (*data)["title"] if !ok || val == nil { return fmt.Errorf("title not found in post data") diff --git a/pkg/query-service/app/dashboards/provision.go b/pkg/query-service/app/dashboards/provision.go index d8869f048b8..a2c7a3a4a6a 100644 --- a/pkg/query-service/app/dashboards/provision.go +++ b/pkg/query-service/app/dashboards/provision.go @@ -7,6 +7,7 @@ import ( "go.signoz.io/signoz/pkg/query-service/constants" "go.signoz.io/signoz/pkg/query-service/interfaces" + "go.signoz.io/signoz/pkg/query-service/model" "go.uber.org/zap" ) @@ -38,22 +39,36 @@ func readCurrentDir(dir string, fm interfaces.FeatureLookup) error { continue } - _, apiErr := GetDashboard(data["uuid"].(string)) - if apiErr == nil { - zap.S().Infof("Creating Dashboards: Error in file: %s\t%s", filename, "Dashboard already present in database") + id := data["uuid"] + if id == nil { + _, apiErr := CreateDashboard(data, fm) + if apiErr != nil { + zap.S().Errorf("Creating Dashboards: Error in file: %s\t%s", filename, apiErr.Err) + } continue } - _, apiErr = CreateDashboard(data, fm) + apiErr := upsertDashboard(id.(string), data, filename, fm) if apiErr != nil { - zap.S().Errorf("Creating Dashboards: Error in file: %s\t%s", filename, apiErr.Err) - continue + zap.S().Errorf("Creating Dashboards: Error upserting dashboard: %s\t%s", filename, apiErr.Err) } - } return nil } +func upsertDashboard(uuid string, data map[string]interface{}, filename string, fm interfaces.FeatureLookup) *model.ApiError { + _, apiErr := GetDashboard(uuid) + if apiErr == nil { + zap.S().Infof("Creating Dashboards: Already exists: %s\t%s", filename, "Dashboard already present in database, Updating dashboard") + _, apiErr := UpdateDashboard(uuid, data, fm) + return apiErr + } + + zap.S().Infof("Creating Dashboards: UUID not found: %s\t%s", filename, "Dashboard not present in database, Creating dashboard") + _, apiErr = CreateDashboard(data, fm) + return apiErr +} + func LoadDashboardFiles(fm interfaces.FeatureLookup) error { dashboardsPath := constants.GetOrDefaultEnv("DASHBOARDS_PATH", "./config/dashboards") return readCurrentDir(dashboardsPath, fm) From c73ddb7db8f76775de5d60aa4448f10c56ba3d94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard?= Date: Wed, 1 Nov 2023 09:00:41 +0100 Subject: [PATCH 2/3] chore(fe): formatting in dashboard description --- .../NewDashboard/DescriptionOfDashboard/index.tsx | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/frontend/src/container/NewDashboard/DescriptionOfDashboard/index.tsx b/frontend/src/container/NewDashboard/DescriptionOfDashboard/index.tsx index cbb899b61f7..7bee660af2c 100644 --- a/frontend/src/container/NewDashboard/DescriptionOfDashboard/index.tsx +++ b/frontend/src/container/NewDashboard/DescriptionOfDashboard/index.tsx @@ -15,10 +15,12 @@ import ShareModal from './ShareModal'; function DescriptionOfDashboard(): JSX.Element { const { selectedDashboard } = useDashboard(); - const selectedData = selectedDashboard ? { - ...selectedDashboard.data, - uuid: selectedDashboard.uuid, - } : undefined ; + const selectedData = selectedDashboard + ? { + ...selectedDashboard.data, + uuid: selectedDashboard.uuid, + } + : undefined; const { title, tags, description } = selectedData || {}; const [isJSONModalVisible, isIsJSONModalVisible] = useState(false); From f542a646648100c7e6be0dbe10d8c6f384ff5794 Mon Sep 17 00:00:00 2001 From: wbtan7 Date: Thu, 21 Mar 2024 01:25:59 +0800 Subject: [PATCH 3/3] fix: miss out while merging --- .../container/NewDashboard/DashboardDescription/index.tsx | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/frontend/src/container/NewDashboard/DashboardDescription/index.tsx b/frontend/src/container/NewDashboard/DashboardDescription/index.tsx index 996c508da40..c916ec75013 100644 --- a/frontend/src/container/NewDashboard/DashboardDescription/index.tsx +++ b/frontend/src/container/NewDashboard/DashboardDescription/index.tsx @@ -23,7 +23,12 @@ function DashboardDescription(): JSX.Element { handleDashboardLockToggle, } = useDashboard(); - const selectedData = selectedDashboard?.data || ({} as DashboardData); + const selectedData = selectedDashboard + ? { + ...selectedDashboard.data, + uuid: selectedDashboard.uuid, + } + : ({} as DashboardData); const { title = '', tags, description } = selectedData || {};