/
result.go
182 lines (155 loc) · 4.96 KB
/
result.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Go MySQL Batch
//
// Copyright 2015 Guillaume Barre. All rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at http://mozilla.org/MPL/2.0/.package main
package main
import (
"bytes"
"html/template"
"strings"
)
// The results for all batches, and if defined the author
// specifications
type GlobalResults struct {
Author *Author
BatchResultList []*BatchResult
}
// The result for one batch
type BatchResult struct {
BatchName string
Smtp SmtpConfig
Recipients Recipients
URLResultList []*UrlResult
}
// The result for one batch against on database
type UrlResult struct {
URL string
QueryResultList []*QueryResult
}
// The result for one query and eventually its nested query
type QueryResult struct {
Name string
Description string
Message string
Status string
QueryError *QueryError
InnerQueryResult *QueryResult
InnerLevel int
}
// Shows the result of the execution of all batches
func (r *GlobalResults) showContent() {
TraceResult.Printf("----------------------------------------------------------------------------- \n")
TraceResult.Printf("processing results: \n")
TraceResult.Printf("----------------------------------------------------------------------------- \n")
TraceResult.Printf("process results %d\n", len(r.BatchResultList))
for _, re := range r.BatchResultList {
re.showContent()
}
}
// Shows the result of the execution of one batch
func (r *BatchResult) showContent() {
TraceResult.Printf("result for %s:\n", r.BatchName)
for _, ure := range r.URLResultList {
ure.showContent()
}
}
// Shows the result of the execution of one batch against
// one database
func (r *UrlResult) showContent() {
TraceResult.Printf("result on %s:\n", r.URL)
for _, qre := range r.QueryResultList {
qre.showContent()
}
}
// SHows the result of the execution of one query
// and eventually its nested query
func (r *QueryResult) showContent() {
TraceResult.Printf("query name %s:\n", r.Name)
TraceResult.Printf("query description %s:\n", r.Description)
TraceResult.Printf("query status %s:\n", r.Status)
TraceResult.Printf("query message %s:\n", r.Message)
if r.InnerQueryResult != nil {
r.InnerQueryResult.showContent()
}
if r.QueryError != nil {
r.QueryError.showContent()
}
}
// Indicates if the query result has a description
func (r *QueryResult) HasDescription() bool {
return r.Description != ""
}
// Indicates if the query result has an error
func (r *QueryResult) HasError() bool {
return r.QueryError != nil
}
// Indicates if the query result has a message
func (r *QueryResult) HasMessage() bool {
return r.Message != ""
}
// Indicates if the query has passed or not (failed)
func (r *QueryResult) HasFailed() bool {
return r.Status != "PASSED"
}
// Returns the trimed description
func (r *QueryResult) GetTrimedDescription() string {
return strings.TrimSpace(r.Description)
}
// Returns the spacing corresponding to the inner level of the quey
func (r *QueryResult) GetSpacing() template.HTML {
var spacing bytes.Buffer
for i := 1; i < r.InnerLevel; i++ {
spacing.WriteString(" ")
}
return template.HTML(spacing.String())
}
// Adds the inner result to the slice received as parameter
func (r *QueryResult) addInnerResultStack(result *[]*QueryResult) {
*result = append(*result, r)
if r.InnerQueryResult != nil {
r.InnerQueryResult.addInnerResultStack(result)
}
}
// Retuns a stack of all query result nested under this one
func (r *QueryResult) GetInnerResultStack() []*QueryResult {
result := make([]*QueryResult, 0)
result = append(result, r)
if r.InnerQueryResult != nil {
r.InnerQueryResult.addInnerResultStack(&result)
}
return result
}
// Sends email based on the result of all batches
func (r *GlobalResults) sendMail() {
funcMap := template.FuncMap{
"odd": func(i int) bool { return i%2 == 0 },
}
tHeader := template.Must(template.New("header").Parse(emailTemplateHtmlHeader))
tBody := template.Must(template.New("body").Funcs(funcMap).Parse(emailTemplateHtmlBody))
var docHeader bytes.Buffer
tHeader.Execute(&docHeader, r.Author)
for _, br := range r.BatchResultList {
var docBody bytes.Buffer
tBody.Execute(&docBody, br)
TraceActivity.Printf("----------------------------------------------------------------------------- \n")
TraceActivity.Printf("GeneratedCOntent : \n")
TraceActivity.Printf("%s\n", docBody.String())
TraceActivity.Printf("----------------------------------------------------------------------------- \n")
var buffer bytes.Buffer
buffer.WriteString(docHeader.String())
buffer.WriteString(docBody.String())
br.Smtp.sendEmailHtml(br, buffer.String())
}
}
// Return the URL without the user and password
func (u *UrlResult) GetUrlForMail() string {
idx := strings.Index(u.URL, "@")
if idx > -1 {
return "..." + u.URL[idx:len(u.URL)]
} else {
return u.URL
}
}