Skip to content
This repository was archived by the owner on Jul 6, 2021. It is now read-only.

Raw data report #116

Merged
merged 3 commits into from
Dec 6, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 61 additions & 13 deletions pghrep/src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

var DEBUG bool = false

// Output debug message
func dbg(v ...interface{}) {
if DEBUG {
message := ""
Expand All @@ -37,6 +38,9 @@ func dbg(v ...interface{}) {
}
}

// Prepropess file paths
// Allow absulute and relative (of pwd) paths with or wothout file:// prefix
// Return absoulute path of file
func GetFilePath(name string) string {
filePath := name
// remove file:// prefix
Expand All @@ -61,7 +65,9 @@ func GetFilePath(name string) string {
}
}

// Exists reports whether the named file or directory exists.
// Check file exists
// Allow absulute and relative (of pwd) paths with or wothout file:// prefix
// Return boolean value
func FileExists(name string) bool {
filePath := GetFilePath(name)
if _, err := os.Stat(filePath); err != nil {
Expand All @@ -72,6 +78,8 @@ func FileExists(name string) bool {
return true
}

// Parse json data from string to map
// Return map[string]interface{}
func ParseJson(jsonData string) map[string]interface{} {
var data map[string]interface{}
if err := json.Unmarshal([]byte(jsonData), &data); err != nil {
Expand All @@ -82,6 +90,8 @@ func ParseJson(jsonData string) map[string]interface{} {
}
}

// Load json data from file by path
// Return map[string]interface{}
func LoadJsonFile(filePath string) map[string]interface{} {
if FileExists(filePath) {
fileContent, err := ioutil.ReadFile(GetFilePath(filePath)) // just pass the file name
Expand All @@ -94,6 +104,7 @@ func LoadJsonFile(filePath string) map[string]interface{} {
return nil
}

// Load data dependencies
func loadDependencies(data map[string]interface{}) {
dep := data["dependencies"]
dependencies := dep.(map[string]interface{})
Expand All @@ -103,6 +114,7 @@ func loadDependencies(data map[string]interface{}) {
}
}

// Load report templates from files
func loadTemplates() *template.Template {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
Expand Down Expand Up @@ -131,6 +143,39 @@ func loadTemplates() *template.Template {
return templates
}

// Prepare raw json data for every host
func getRawData(data map[string]interface{}) {
// for every host get data
var rawData []interface{}
hosts := pyraconv.ToInterfaceMap(data["hosts"])
dbg("Data hosts: ", hosts)
results := pyraconv.ToInterfaceMap(data["results"])
masterName := pyraconv.ToString(hosts["master"])
masterResults := pyraconv.ToInterfaceMap(results[masterName])
masterData := pyraconv.ToInterfaceMap(masterResults["data"])
masterJson, err := json.Marshal(masterData)
if err == nil {
masterItem := make(map[string]interface{})
masterItem["host"] = masterName
masterItem["data"] = string(masterJson)
rawData = append(rawData, masterItem)
}
replicas := pyraconv.ToStringArray(hosts["replicas"])
for _, host := range replicas {
hostResults := pyraconv.ToInterfaceMap(results[host])
hostData := pyraconv.ToInterfaceMap(hostResults["data"])
hostJson, err := json.Marshal(hostData)
if err == nil {
hostItem := make(map[string]interface{})
hostItem["host"] = host
hostItem["data"] = string(hostJson)
rawData = append(rawData, hostItem)
}
}
data["rawData"] = rawData
}

// Generate md report (file) on base of reportData and save them to file in outputDir
func generateMdReport(checkId string, reportData map[string]interface{}, outputDir string) bool{
var outputFileName string
if strings.HasSuffix(strings.ToLower(outputDir), "/") {
Expand All @@ -151,24 +196,27 @@ func generateMdReport(checkId string, reportData map[string]interface{}, outputD
if templates == nil {
log.Fatal("Can't load template")
}
dbg("Find", checkId + ".tpl")
reporTpl := templates.Lookup(checkId + ".tpl")
if reporTpl != nil {
err = reporTpl.ExecuteTemplate(f, checkId + ".tpl", reportData)
if err != nil {
dbg("Template execute error is", err)
defer os.Remove(outputFileName)
return false
} else {
return true
}
} else {
reportFileName := checkId + ".tpl"
reporTpl := templates.Lookup(reportFileName)
data := reportData
if reporTpl == nil {
dbg("Template " + checkId + ".tpl not found.")
getRawData(data)
reportFileName = "raw.tpl"
reporTpl = templates.Lookup(reportFileName)
}
err = reporTpl.ExecuteTemplate(f, reportFileName, data)
if err != nil {
dbg("Template execute error is", err)
defer os.Remove(outputFileName)
return false
} else {
return true
}
}

// Sort hosts on master and replicas by role and index.
// Return map {"master":name string, "replicas":[replica1 string, replica2 string]}
func determineMasterReplica(data map[string]interface{}) {
hostRoles := make(map[string]interface{})
var sortedReplicas []string
Expand Down
12 changes: 12 additions & 0 deletions pghrep/templates/raw.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Current values
===

Available only raw data.

{{ range $host, $data := .rawData }}
Host: {{ (index $data "host") }}
``` {{ (index $data "data") }} ```
{{ end }}



11 changes: 0 additions & 11 deletions pghrep/templates/report.tpl

This file was deleted.

6 changes: 1 addition & 5 deletions resources/checks/F002_index_bloat.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,4 @@ ${CHECK_HOST_CMD} "${_PSQL} ${PSQL_CONN_OPTIONS} -f -" <<SQL
with data as (
$sql
)
select json_agg(jsondata.json) from (select row_to_json(data) as json from data) jsondata;
SQL

#For get objects change row 9 to `select json_object_agg(data."Index (Table)", data) as json from data;`
#but in this case we have indexes like `i_user_visits_postgrest_auth\n (user_visits)`
select json_object_agg(data."Index (Table)", data) as json from data;