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

Commit c60f962

Browse files
authored
Merge pull request #116 from postgres-ai/dmius-raw-report
Raw data report
2 parents c0ca13b + 0dedd3c commit c60f962

File tree

4 files changed

+75
-29
lines changed

4 files changed

+75
-29
lines changed

pghrep/src/main.go

Lines changed: 61 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727

2828
var DEBUG bool = false
2929

30+
// Output debug message
3031
func dbg(v ...interface{}) {
3132
if DEBUG {
3233
message := ""
@@ -37,6 +38,9 @@ func dbg(v ...interface{}) {
3738
}
3839
}
3940

41+
// Prepropess file paths
42+
// Allow absulute and relative (of pwd) paths with or wothout file:// prefix
43+
// Return absoulute path of file
4044
func GetFilePath(name string) string {
4145
filePath := name
4246
// remove file:// prefix
@@ -61,7 +65,9 @@ func GetFilePath(name string) string {
6165
}
6266
}
6367

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

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

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

107+
// Load data dependencies
97108
func loadDependencies(data map[string]interface{}) {
98109
dep := data["dependencies"]
99110
dependencies := dep.(map[string]interface{})
@@ -103,6 +114,7 @@ func loadDependencies(data map[string]interface{}) {
103114
}
104115
}
105116

117+
// Load report templates from files
106118
func loadTemplates() *template.Template {
107119
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
108120
if err != nil {
@@ -134,6 +146,39 @@ func loadTemplates() *template.Template {
134146
return templates
135147
}
136148

149+
// Prepare raw json data for every host
150+
func getRawData(data map[string]interface{}) {
151+
// for every host get data
152+
var rawData []interface{}
153+
hosts := pyraconv.ToInterfaceMap(data["hosts"])
154+
dbg("Data hosts: ", hosts)
155+
results := pyraconv.ToInterfaceMap(data["results"])
156+
masterName := pyraconv.ToString(hosts["master"])
157+
masterResults := pyraconv.ToInterfaceMap(results[masterName])
158+
masterData := pyraconv.ToInterfaceMap(masterResults["data"])
159+
masterJson, err := json.Marshal(masterData)
160+
if err == nil {
161+
masterItem := make(map[string]interface{})
162+
masterItem["host"] = masterName
163+
masterItem["data"] = string(masterJson)
164+
rawData = append(rawData, masterItem)
165+
}
166+
replicas := pyraconv.ToStringArray(hosts["replicas"])
167+
for _, host := range replicas {
168+
hostResults := pyraconv.ToInterfaceMap(results[host])
169+
hostData := pyraconv.ToInterfaceMap(hostResults["data"])
170+
hostJson, err := json.Marshal(hostData)
171+
if err == nil {
172+
hostItem := make(map[string]interface{})
173+
hostItem["host"] = host
174+
hostItem["data"] = string(hostJson)
175+
rawData = append(rawData, hostItem)
176+
}
177+
}
178+
data["rawData"] = rawData
179+
}
180+
181+
// Generate md report (file) on base of reportData and save them to file in outputDir
137182
func generateMdReport(checkId string, reportData map[string]interface{}, outputDir string) bool{
138183
var outputFileName string
139184
if strings.HasSuffix(strings.ToLower(outputDir), "/") {
@@ -154,24 +199,27 @@ func generateMdReport(checkId string, reportData map[string]interface{}, outputD
154199
if templates == nil {
155200
log.Fatal("Can't load template")
156201
}
157-
dbg("Find", checkId + ".tpl")
158-
reporTpl := templates.Lookup(checkId + ".tpl")
159-
if reporTpl != nil {
160-
err = reporTpl.ExecuteTemplate(f, checkId + ".tpl", reportData)
161-
if err != nil {
162-
dbg("Template execute error is", err)
163-
defer os.Remove(outputFileName)
164-
return false
165-
} else {
166-
return true
167-
}
168-
} else {
202+
reportFileName := checkId + ".tpl"
203+
reporTpl := templates.Lookup(reportFileName)
204+
data := reportData
205+
if reporTpl == nil {
169206
dbg("Template " + checkId + ".tpl not found.")
207+
getRawData(data)
208+
reportFileName = "raw.tpl"
209+
reporTpl = templates.Lookup(reportFileName)
210+
}
211+
err = reporTpl.ExecuteTemplate(f, reportFileName, data)
212+
if err != nil {
213+
dbg("Template execute error is", err)
170214
defer os.Remove(outputFileName)
171215
return false
216+
} else {
217+
return true
172218
}
173219
}
174220

221+
// Sort hosts on master and replicas by role and index.
222+
// Return map {"master":name string, "replicas":[replica1 string, replica2 string]}
175223
func determineMasterReplica(data map[string]interface{}) {
176224
hostRoles := make(map[string]interface{})
177225
var sortedReplicas []string

pghrep/templates/raw.tpl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Current values
2+
===
3+
4+
Some collected
5+
data with help of checker
6+
7+
{{ range $host, $data := .rawData }}
8+
Host: {{ (index $data "host") }}
9+
``` {{ (index $data "data") }} ```
10+
{{ end }}
11+
12+
13+

pghrep/templates/report.tpl

Lines changed: 0 additions & 11 deletions
This file was deleted.

resources/checks/F002_index_bloat.sh

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,4 @@ ${CHECK_HOST_CMD} "${_PSQL} ${PSQL_CONN_OPTIONS} -f -" <<SQL
44
with data as (
55
$sql
66
)
7-
select json_agg(jsondata.json) from (select row_to_json(data) as json from data) jsondata;
8-
SQL
9-
10-
#For get objects change row 9 to `select json_object_agg(data."Index (Table)", data) as json from data;`
11-
#but in this case we have indexes like `i_user_visits_postgrest_auth\n (user_visits)`
7+
select json_object_agg(data."Index (Table)", data) as json from data;

0 commit comments

Comments
 (0)