/
ingest.go
216 lines (162 loc) · 4.88 KB
/
ingest.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package api
import (
"net/http"
"github.com/bojand/ghz/runner"
"github.com/bojand/ghz/web/model"
"github.com/labstack/echo"
)
// IngestDatabase interface for encapsulating database access.
type IngestDatabase interface {
CreateProject(*model.Project) error
CreateReport(*model.Report) error
CreateHistogram(*model.Histogram) error
CreateOptions(*model.Options) error
FindProjectByID(uint) (*model.Project, error)
FindLatestReportForProject(uint) (*model.Report, error)
CreateDetailsBatch(uint, []*model.Detail) (uint, uint)
UpdateProjectStatus(uint, model.Status) error
}
// IngestResponse is the response to the ingest endpoint
type IngestResponse struct {
// Created project
Project *model.Project `json:"project"`
// Created report
Report *model.Report `json:"report"`
// Created Options
Options *model.Options `json:"options"`
// Created Histogram
Histogram *model.Histogram `json:"histogram"`
// The summary of created details
Details *DetailsCreated `json:"details"`
}
// DetailsCreated summary of how many details got created and how many failed
type DetailsCreated struct {
// Number of successfully created detail objects
Success uint `json:"success"`
// Number of failed detail objects
Fail uint `json:"fail"`
}
// The IngestAPI provides handlers for ingesting and processing reports.
type IngestAPI struct {
DB IngestDatabase
}
// IngestRequest is the raw report
type IngestRequest runner.Report
// Ingest creates data from raw report
func (api *IngestAPI) Ingest(ctx echo.Context) error {
ir := new(IngestRequest)
if err := bindAndValidateInput(ctx, ir); err != nil {
return err
}
// Project
p := new(model.Project)
if err := api.DB.CreateProject(p); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
return api.ingestToProject(p, ir, ctx)
}
// IngestToProject ingests data into a specific project
func (api *IngestAPI) IngestToProject(ctx echo.Context) error {
var project *model.Project
var err error
if project, err = findProject(api.DB.FindProjectByID, ctx); err != nil {
return err
}
ir := new(IngestRequest)
if err := bindAndValidateInput(ctx, ir); err != nil {
return err
}
return api.ingestToProject(project, ir, ctx)
}
func (api *IngestAPI) ingestToProject(p *model.Project, ir *IngestRequest, ctx echo.Context) error {
// first get latest (we'll need it later)
latest, _ := api.DB.FindLatestReportForProject(p.ID)
// Report
report := convertIngestToReport(p.ID, ir)
if err := api.DB.CreateReport(report); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
// Options
o := &model.Options{
ReportID: report.ID,
}
opts := model.OptionsInfo(ir.Options)
o.Info = &opts
if err := api.DB.CreateOptions(o); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
// Histogram
h := &model.Histogram{
ReportID: report.ID,
}
h.Buckets = make(model.BucketList, len(ir.Histogram))
for i := range ir.Histogram {
h.Buckets[i] = &ir.Histogram[i]
}
if err := api.DB.CreateHistogram(h); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
// Details
details := make([]*model.Detail, len(ir.Details))
for i, v := range ir.Details {
det := model.Detail{ReportID: report.ID, ResultDetail: v}
details[i] = &det
}
created, errored := api.DB.CreateDetailsBatch(report.ID, details)
// Update project status if needed
if latest == nil || report.Date.After(latest.Date) {
if err := api.DB.UpdateProjectStatus(p.ID, report.Status); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
p.Status = report.Status
}
// Response
rres := &IngestResponse{
Project: p,
Report: report,
Options: o,
Histogram: h,
Details: &DetailsCreated{
Success: created,
Fail: errored,
},
}
return ctx.JSON(http.StatusCreated, rres)
}
func convertIngestToReport(pid uint, ir *IngestRequest) *model.Report {
r := new(model.Report)
r.ProjectID = pid
r.Name = ir.Name
r.EndReason = ir.EndReason.String()
r.Date = ir.Date
r.Count = ir.Count
r.Total = ir.Total
r.Average = ir.Average
r.Fastest = ir.Fastest
r.Slowest = ir.Slowest
r.Rps = ir.Rps
r.ErrorDist = ir.ErrorDist
r.StatusCodeDist = ir.StatusCodeDist
r.Tags = ir.Tags
r.LatencyDistribution = make(model.LatencyDistributionList, len(ir.LatencyDistribution))
for i := range ir.LatencyDistribution {
r.LatencyDistribution[i] = &ir.LatencyDistribution[i]
}
// status
r.Status = model.StatusOK
if len(r.ErrorDist) > 0 {
r.Status = model.StatusFail
}
return r
}
func bindAndValidateInput(ctx echo.Context, ir *IngestRequest) error {
if err := ctx.Bind(ir); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}
if ctx.Echo().Validator != nil {
if err := ctx.Validate(ir); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}
}
return nil
}