Skip to content

Commit

Permalink
json änderung
Browse files Browse the repository at this point in the history
  • Loading branch information
emi committed Mar 25, 2024
1 parent f5e682d commit 7851888
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
43 changes: 43 additions & 0 deletions cmd/grafanadashboards/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"

Check failure on line 7 in cmd/grafanadashboards/main.go

View workflow job for this annotation

GitHub Actions / Lint Test Build

SA1019: "io/ioutil" has been deprecated since Go 1.19: As of Go 1.16, the same functionality is now provided by package io or package os, and those implementations should be preferred in new code. See the specific function documentation for details. (staticcheck)

Check failure on line 7 in cmd/grafanadashboards/main.go

View workflow job for this annotation

GitHub Actions / Lint Test Build

SA1019: "io/ioutil" has been deprecated since Go 1.19: As of Go 1.16, the same functionality is now provided by package io or package os, and those implementations should be preferred in new code. See the specific function documentation for details. (staticcheck)
"log"
"net/http"
"os"
"path"
"path/filepath"
"strings"

"github.com/iits-consulting/otc-prometheus-exporter/grafana"
"github.com/iits-consulting/otc-prometheus-exporter/otcdoc"
Expand Down Expand Up @@ -77,6 +80,38 @@ func processDocumentationPages(outputPath string) {
}
}
}
func modifyDashboardFiles(directoryPath string) error {
// List all files in the directory
files, err := ioutil.ReadDir(directoryPath)
if err != nil {
return fmt.Errorf("failed to read directory: %v", err)
}

for _, file := range files {
// Check if the file is a JSON file
if filepath.Ext(file.Name()) == ".json" {
// Construct the full path to the JSON file
filePath := filepath.Join(directoryPath, file.Name())

// Read the content of the JSON file
data, err := ioutil.ReadFile(filePath)
if err != nil {
return fmt.Errorf("failed to read file %s: %v", filePath, err)
}

// Modify the JSON content to set legendFormat to {{ resource_id }}
modifiedData := strings.ReplaceAll(string(data), `"legendFormat":"__auto"`, `"legendFormat":"{{ resource_id }}"`)

// Write the modified content back to the JSON file
err = ioutil.WriteFile(filePath, []byte(modifiedData), 0644)
if err != nil {
return fmt.Errorf("failed to write file %s: %v", filePath, err)
}
}
}

return nil
}

func main() {

Expand All @@ -95,4 +130,12 @@ func main() {
log.Fatalln(err)
}

directoryPath := "otc-prometheus-exporter/charts/otc-prometheus-exporter/dashboards" // set generic path or path in project, correct path?
err := modifyDashboardFiles(directoryPath)
if err != nil {
fmt.Println("Error:", err)
} else {
fmt.Println("Dashboard files modified successfully.")
}

}
1 change: 1 addition & 0 deletions grafana/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ type Panel struct {
Targets []Target `json:"targets"`
Title string `json:"title"`
Type string `json:"type"`
ResourceID string `json:"resource_id"`
}
type Templating struct {
List []TemplatingVariable `json:"list"`
Expand Down

0 comments on commit 7851888

Please sign in to comment.