Skip to content
Permalink
Browse files

Multiple quick fixes and more accurate way to record the response time

  • Loading branch information...
Depado committed Mar 7, 2018
1 parent 9737d5b commit 781cd7607bf7c258ceb92ff43ea620c8cec31fa7
Showing with 155 additions and 61 deletions.
  1. +21 −14 Gopkg.lock
  2. +1 −4 main.go
  3. +38 −21 models/service.go
  4. +61 −0 models/transport.go
  5. +34 −22 templates/index.tmpl

Some generated files are not rendered by default. Learn more.

@@ -56,10 +56,7 @@ func main() {

// Set router
r := SetupRouter()
logrus.WithFields(logrus.Fields{
"port": conf.C.Server.Port,
"host": conf.C.Server.Host,
}).Info("Starting server")
logrus.WithFields(logrus.Fields{"port": conf.C.Server.Port, "host": conf.C.Server.Host}).Info("Starting server")
if err = r.Run(fmt.Sprintf("%s:%d", conf.C.Server.Host, conf.C.Server.Port)); err != nil {
logrus.WithError(err).Fatal("Couldn't start server")
}
@@ -1,9 +1,10 @@
package models

import (
"crypto/tls"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"strings"
@@ -132,23 +133,26 @@ func NewServiceFromConf(cs conf.Service) (*Service, error) {

// FetchStatus checks if the service is running
func (s *Service) FetchStatus() {
client := &http.Client{Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}}
clog := logrus.WithFields(logrus.Fields{"action": "status", "service": s.Name})
tp := newTransport()
client := &http.Client{Transport: tp}
start := time.Now()
s.Last = start.Format("2006/01/02 15:04:05")

req, err := http.NewRequest("GET", s.URL, nil)
if err != nil {
log.Printf("[%s][ERROR] While building request : %v\n", s.Name, err)
clog.WithError(err).Error("Couldn't create request")
return
}
start := time.Now()
s.Last = start.Format("2006/01/02 15:04:05")
resp, err := client.Do(req)
if err != nil {
log.Printf("[%s][ERROR] While requesting : %v\n", s.Name, err)
clog.WithError(err).Warn("Couldn't fetch status")
return
}
defer resp.Body.Close()
d := time.Since(start)
io.Copy(ioutil.Discard, resp.Body)

d := tp.ReqDuration()
s.RespTime = d - (d % time.Millisecond)
s.Status = resp.StatusCode
}
@@ -242,29 +246,42 @@ type Services []*Service
// Monitor allows to monitor Services every interval delay
func (ss Services) Monitor() {
for _, s := range ss {
go func(s *Service) {
rtc := time.NewTicker(conf.C.RepoInterval)
for {
if s.URL != "" {
s.FetchStatus()
}
}
for _, s := range ss {
if s.CI != nil {
go s.FetchBuilds()
}
if s.Repo != nil {
go s.FetchCommits()
go s.FetchRepoInfos()
}
}

rtc := time.NewTicker(conf.C.RepoInterval)
stc := time.NewTicker(conf.C.ServiceInterval)
for {
select {
case <-rtc.C:
logrus.WithField("type", "repo").Debug("Started background routine")
for _, s := range ss {
if s.CI != nil {
go s.FetchBuilds()
}
if s.Repo != nil {
go s.FetchCommits()
go s.FetchRepoInfos()
}
<-rtc.C
}
}(s)
}
go func(iss Services) {
rtc := time.NewTicker(conf.C.ServiceInterval)
for {
for _, s := range iss {
case <-stc.C:
logrus.WithField("type", "status").Debug("Started background routine")
for _, s := range ss {
if s.URL != "" {
s.FetchStatus()
}
}
<-rtc.C
}
}(ss)
}
}
@@ -0,0 +1,61 @@
package models

import (
"crypto/tls"
"net"
"net/http"
"time"
)

type customTransport struct {
rtp http.RoundTripper
dialer *net.Dialer
connStart time.Time
connEnd time.Time
reqStart time.Time
reqEnd time.Time
}

func newTransport() *customTransport {
tr := &customTransport{
dialer: &net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
},
}
tr.rtp = &http.Transport{
Proxy: http.ProxyFromEnvironment,
Dial: tr.dial,
TLSHandshakeTimeout: 10 * time.Second,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
return tr
}

func (tr *customTransport) RoundTrip(r *http.Request) (*http.Response, error) {
tr.reqStart = time.Now()
resp, err := tr.rtp.RoundTrip(r)
tr.reqEnd = time.Now()
return resp, err
}

func (tr *customTransport) dial(network, addr string) (net.Conn, error) {
tr.connStart = time.Now()
cn, err := tr.dialer.Dial(network, addr)
tr.connEnd = time.Now()
return cn, err
}

func (tr *customTransport) ReqDuration() time.Duration {
return tr.Duration() - tr.ConnDuration()
}

func (tr *customTransport) ConnDuration() time.Duration {
return tr.connEnd.Sub(tr.connStart)
}

func (tr *customTransport) Duration() time.Duration {
return tr.reqEnd.Sub(tr.reqStart)
}
@@ -18,6 +18,28 @@
background-image: url("static/img/body_bg.png");
background-repeat: repeat;
}
.extra.content > a {
color: rgba(0, 0, 0, 0.4);
transition-property: none !important;
transform: none !important;
animation: none !important;
}
.ui.card > .extra a:not(.ui):hover, .ui.cards > .card > .extra a:not(.ui):hover {
color: rgba(0, 0, 0, 0.4);
}
.ui.card > .content .star.icon:hover, .ui.cards > .card > .content .star.icon:hover {
color: rgba(0, 0, 0, 0.4);
opacity: .75;
}
.repo {
margin-left: 20px;
}
.ui.card > .content, .ui.cards > .card > .top.content {
padding: 1em 1em 0em;
}
.ui.feed {
margin-left: 20px;
}
</style>
</head>

@@ -76,9 +98,9 @@
<div class="label">
<img src="{{ if ne .Author.AvatarURL " " }}{{ .Author.AvatarURL }}{{ else }}/static/img/anon.jpg{{ end }}" alt="avatar">
</div>
<div class="content">
<div class="top content">
<div class="summary">
<a class="user" href="{{ .Author.HTMLURL }}" target="_blank">{{ .Commit.Author.Name }}</a> authored
<a class="user" href="{{ .Author.HTMLURL }}" target="_blank">{{ .Author.Login }}</a> authored
</div>
<div class="meta">
<i class="clock icon"></i> {{ .Commit.Author.Date }}
@@ -99,21 +121,20 @@
<div class="ui fluid centered stackable cards">
{{ range $index, $element := .all }}
<div class="ui card {{ if eq .Status 200 }}green{{ else if eq .URL "" }}green{{ else }}red{{ end }}">
<div class="content">
<div class="top content">
<img class="right floated mini ui image" {{ if .Icon }}src="{{ .Icon }}" alt="{{ .Name }}" {{ end }}>
<div class="header">{{ .Name }}</div>
<div class="meta">
<a href="{{ .URL }}">{{ if eq .ShortURL "" }}-{{ else }}{{ .ShortURL }}{{ end }}</a>
</div>

</div>
<div class="extra content">
<i class="setting icon"></i>{{ if .RespTime }}{{ .RespTime }}{{ else }}-{{ end }}
<span class="right floated">
{{ if .Host }}{{ .Host }}{{ else }}-{{ end}} <i class="server icon"></i>
</span>
<br />
<i class="wait icon"></i>{{ if .Last }}{{ .Last }}{{ else }}-{{ end }}
<i class="clock outline icon"></i>{{ if .Last }}{{ .Last }}{{ else }}-{{ end }}
{{ if eq .URL ""}}
<span class="right floated">- <i class="help icon"></i></span>
{{ else }}
@@ -129,7 +150,6 @@
{{ else }}
<i class="github alternate icon"></i> No Commits
{{ end }}

<span class="right floated">
{{ if .LastBuilds }}
{{ $last := index .LastBuilds 0 }}
@@ -141,27 +161,24 @@
<a href="{{ .CurrentBuildURL }}" style="color:#DB2828;" id="builds-{{ $index }}" class="tooltip-up-right" data-content="View Recent Builds" data-variation="tiny">Failure <i class="configure icon"></i></a>
{{ end }}
{{ else }}
No build <i class="configure icon"></i>
No build <i class="configure icon"></i>
{{ end }}
</span>

</div>
<br />
<div class="extra content" style="text-align: center;">
{{ if .Repo }}
<a href="{{ .Repo.URL }}/stargazers" target="_blank">
<button class="ui tiny teal basic compact labeled icon button"><i class="star icon"></i>{{ .Repo.Stars }}</button>
<a href='https://github.com{{ .Repo.URL }}/stargazers' target="_blank">
<i class="star icon"></i>{{ .Repo.Stars }}
</a>
<a href="{{ .Repo.URL }}/network" target="_blank">
<button class="ui tiny teal basic compact labeled icon button"><i class="fork icon"></i>{{ .Repo.Forks}}</button>
<a href='https://github.com{{ .Repo.URL }}/network' target="_blank" class="repo">
<i class="fork icon"></i>{{ .Repo.Forks}}
</a>
<a href="{{ .Repo.URL }}/watchers" target="_blank">
<button class="ui tiny teal basic compact labeled icon button"><i class="eye icon"></i>{{ .Repo.Watchers }}</button>
<a href='https://github.com{{ .Repo.URL }}/watchers' target="_blank" class="repo">
<i class="eye icon"></i>{{ .Repo.Watchers }}
</a>
{{ else }}
<button class="ui tiny disabled teal basic compact labeled icon button"><i class="star icon"></i>-</button>
<button class="ui tiny disabled teal basic compact labeled icon button"><i class="fork icon"></i>-</button>
<button class="ui tiny disabled teal basic compact labeled icon button"><i class="eye icon"></i>-</button>
<span>&nbsp;</span>
{{ end }}
</div>
</div>
@@ -214,11 +231,6 @@
$("#sidebar-builds-" + buildsid).sidebar('toggle');
evt.preventDefault();
});
$("a[id^='history-']").click(function(evt) {
var historyid = $(this).attr('id').split('-')[1];
$("#sidebar-builds-" + historyid).sidebar('toggle');
evt.preventDefault();
});
</script>
</body>

0 comments on commit 781cd76

Please sign in to comment.
You can’t perform that action at this time.