Skip to content

Commit

Permalink
Fix ingest status. Fix compile CGO compile issue. Improve HTML output
Browse files Browse the repository at this point in the history
  • Loading branch information
bojand committed Jan 23, 2019
1 parent 47e7bb8 commit 26d8205
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 55 deletions.
2 changes: 0 additions & 2 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ builds:
- amd64
- main: ./cmd/ghz-web
binary: ghz-web
env:
- CGO_ENABLED=0
goos:
- darwin
- linux
Expand Down
2 changes: 1 addition & 1 deletion cmd/ghz-web/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func main() {
}

flag.Usage = func() {
fmt.Fprint(os.Stderr, fmt.Sprintf(usage, runtime.NumCPU()))
fmt.Fprint(os.Stderr, usage)
}

flag.Parse()
Expand Down
22 changes: 16 additions & 6 deletions printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log"
"strings"
"text/tabwriter"
"time"

"github.com/alecthomas/template"
"github.com/bojand/ghz/runner"
Expand Down Expand Up @@ -246,6 +247,7 @@ var tmplFuncMap = template.FuncMap{
"formatPercent": formatPercent,
"formatStatusCode": formatStatusCode,
"formatErrorDist": formatErrorDist,
"formatDate": formatDate,
}

func jsonify(v interface{}, pretty bool) string {
Expand All @@ -267,6 +269,10 @@ func formatMilli(duration float64) string {
return fmt.Sprintf("%4.2f", duration*1000)
}

func formatDate(d time.Time) string {
return d.Format("Mon Jan _2 2006 @ 15:04:05")
}

func formatSeconds(duration float64) string {
return fmt.Sprintf("%4.2f", duration)
}
Expand Down Expand Up @@ -373,6 +379,16 @@ duration (ms),status,error{{ range $i, $v := .Details }}
<body>
<section class="section">
<div class="container">
{{ if .Name }}
<h1 class="title">{{ .Name }}</h1>
{{ end }}
{{ if .Date }}
<h2 class="subtitle">{{ formatDate .Date }}</h2>
{{ end }}
</div>
<br />
<div class="container">
<nav class="breadcrumb has-bullet-separator" aria-label="breadcrumbs">
Expand Down Expand Up @@ -462,12 +478,6 @@ duration (ms),status,error{{ range $i, $v := .Details }}
</a>
<table class="table">
<tbody>
{{ if .Name }}
<tr>
<th>Name</th>
<td>{{ .Name }}</td>
</tr>
{{ end }}
<tr>
<th>Count</th>
<td>{{ .Count }}</td>
Expand Down
14 changes: 11 additions & 3 deletions web/api/ingest.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type IngestDatabase interface {
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
}
Expand Down Expand Up @@ -91,6 +92,9 @@ func (api *IngestAPI) IngestToProject(ctx echo.Context) error {

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)
Expand Down Expand Up @@ -135,9 +139,13 @@ func (api *IngestAPI) ingestToProject(p *model.Project, ir *IngestRequest, ctx e

created, errored := api.DB.CreateDetailsBatch(report.ID, details)

// Update project status
if err := api.DB.UpdateProjectStatus(p.ID, report.Status); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
// 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
Expand Down
52 changes: 52 additions & 0 deletions web/api/ingest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"testing"

"github.com/bojand/ghz/web/database"
"github.com/bojand/ghz/web/model"
"github.com/labstack/echo"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -142,6 +143,8 @@ func TestIngestAPI(t *testing.T) {

assert.NotNil(t, r.Details)
assert.NotEmpty(t, r.Details)

assert.Equal(t, r.Report.Status, r.Project.Status)
}
})

Expand Down Expand Up @@ -210,4 +213,53 @@ func TestIngestAPI(t *testing.T) {
assert.Equal(t, http.StatusNotFound, httpError.Code)
}
})

t.Run("IngestToProject status update", func(t *testing.T) {
dat, err := ioutil.ReadFile("../test/SayHello/report7.json")
assert.NoError(t, err)

e := echo.New()
req := httptest.NewRequest(http.MethodPost, "/projects/"+pid+"/ingest", strings.NewReader(string(dat)))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()

c := e.NewContext(req, rec)
c.SetParamNames("pid")
c.SetParamValues(pid)

if assert.NoError(t, api.IngestToProject(c)) {
assert.Equal(t, http.StatusCreated, rec.Code)

r := new(IngestResponse)
err = json.NewDecoder(rec.Body).Decode(r)

assert.NoError(t, err)

assert.Equal(t, model.Status("fail"), r.Project.Status)

// now ingest an earlier run with no errors / status OK
dat, err = ioutil.ReadFile("../test/SayHello/report3.json")
assert.NoError(t, err)

e = echo.New()
req = httptest.NewRequest(http.MethodPost, "/projects/"+pid+"/ingest", strings.NewReader(string(dat)))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()

c := e.NewContext(req, rec)
c.SetParamNames("pid")
c.SetParamValues(pid)

if assert.NoError(t, api.IngestToProject(c)) {
assert.Equal(t, http.StatusCreated, rec.Code)

r := new(IngestResponse)
err = json.NewDecoder(rec.Body).Decode(r)

assert.NoError(t, err)

assert.Equal(t, model.Status("fail"), r.Project.Status)
}
}
})
}
10 changes: 10 additions & 0 deletions web/database/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ func (d *Database) FindPreviousReport(rid uint) (*model.Report, error) {
return previous, err
}

// FindLatestReportForProject returns the latest / most recent report for project
func (d *Database) FindLatestReportForProject(pid uint) (*model.Report, error) {
list, err := d.listReports(true, pid, 1, 0, "date", "desc")
if err != nil || len(list) == 0 {
return nil, err
}

return list[0], nil
}

// ListReports lists reports using sorting
func (d *Database) ListReports(limit, page uint, sortField, order string) ([]*model.Report, error) {
return d.listReports(false, 0, limit, page, sortField, order)
Expand Down
17 changes: 17 additions & 0 deletions web/database/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,23 @@ func TestDatabase_Report(t *testing.T) {
assert.Equal(t, uint(1), list[0].ID)
})

t.Run("FindLatestReportForProject", func(t *testing.T) {
r := new(model.Report)
r, err = db.FindLatestReportForProject(pid2)

assert.NoError(t, err)
assert.NotNil(t, r)
assert.Equal(t, rid3, r.ID)
})

t.Run("FindLatestReportForProject invalid id", func(t *testing.T) {
r := new(model.Report)
r, err = db.FindLatestReportForProject(12345)

assert.NoError(t, err)
assert.Nil(t, r)
})

t.Run("DeleteReport()", func(t *testing.T) {
p := &model.Report{}
p.ID = rid3
Expand Down
4 changes: 2 additions & 2 deletions web/todo.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
- [ ] Improve website documentation
- [ ] Fix ingest status
- [ ] Make ingest transactional
- [ ] Menu in project listing with Delete option ?
- [ ] Latest run time ago + link in project list (needs DB + API) ?
- [ ] Latest run time ago + link in project list (needs API) ?
- [ ] Add config for host to bind to ?
- [ ] Improve invalid request and bad condition tests
- [x] Fix ingest status
- [x] DELETE
- [x] Lock down demo create
- [x] Docs
Expand Down
98 changes: 57 additions & 41 deletions www/website/static/sample.html

Large diffs are not rendered by default.

0 comments on commit 26d8205

Please sign in to comment.