Skip to content

Commit

Permalink
vue
Browse files Browse the repository at this point in the history
  • Loading branch information
hunterlong committed Feb 22, 2020
1 parent cb7787f commit 05c77a5
Show file tree
Hide file tree
Showing 10 changed files with 186 additions and 130 deletions.
26 changes: 16 additions & 10 deletions core/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,27 +272,33 @@ type DateScanObj struct {
}

// GraphDataRaw will return all the hits between 2 times for a Service
func GraphHitsDataRaw(service types.ServiceInterface, start, end time.Time, group string, column string) *DateScanObj {
model := service.(*Service).HitsBetween(start, end, group, column)
model = model.Order("timeframe asc", false).Group("timeframe")
outgoing, err := model.ToChart()
func GraphHitsDataRaw(service types.ServiceInterface, query types.GroupQuery, column string) *DateScanObj {
srv := service.(*Service)

dbQuery := Database(&types.Hit{}).
Where("service = ?", srv.Id).
Between(query.Start, query.End).
MultipleSelects(Database(&types.Hit{}).SelectByTime(query.Group), types.CountAmount()).
GroupByTimeframe()

outgoing, err := dbQuery.ToChart()
if err != nil {
log.Error(err)
}
return &DateScanObj{outgoing}
}

// GraphDataRaw will return all the hits between 2 times for a Service
func GraphFailuresDataRaw(service types.ServiceInterface, start, end time.Time, group string) []*types.TimeValue {
func GraphFailuresDataRaw(service types.ServiceInterface, query types.GroupQuery) []*types.TimeValue {
srv := service.(*Service)

query := Database(&types.Failure{}).
dbQuery := Database(&types.Failure{}).
Where("service = ?", srv.Id).
Between(start, end).
MultipleSelects(types.SelectByTime(group), types.CountAmount()).
GroupByTimeframe().Debug()
Between(query.Start, query.End).
MultipleSelects(Database(&types.Failure{}).SelectByTime(query.Group), types.CountAmount()).
GroupByTimeframe()

outgoing, err := query.ToTimeValue(start, end)
outgoing, err := dbQuery.ToTimeValue(query.Start, query.End)
if err != nil {
log.Error(err)
}
Expand Down
30 changes: 0 additions & 30 deletions core/sparklines.go
Original file line number Diff line number Diff line change
@@ -1,31 +1 @@
package core

import (
"github.com/hunterlong/statping/utils"
"strings"
"time"
)

// SparklineDayFailures returns a string array of daily service failures
func (s *Service) SparklineDayFailures(days int) string {
var arr []string
ago := time.Now().UTC().Add((time.Duration(days) * -24) * time.Hour)
for day := 1; day <= days; day++ {
ago = ago.Add(24 * time.Hour)
failures, _ := s.TotalFailuresOnDate(ago)
arr = append(arr, utils.ToString(failures))
}
return "[" + strings.Join(arr, ",") + "]"
}

// SparklineHourResponse returns a string array for the average response or ping time for a service
func (s *Service) SparklineHourResponse(hours int, method string) string {
var arr []string
end := time.Now().UTC()
start := end.Add(time.Duration(-hours) * time.Hour)
obj := GraphHitsDataRaw(s, start, end, "hour", method)
for _, v := range obj.Array {
arr = append(arr, utils.ToString(v.Value))
}
return "[" + strings.Join(arr, ",") + "]"
}
51 changes: 51 additions & 0 deletions dev/docker-compose-database.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
version: '3.1'

volumes:
mysql_data: {}
postgres_data: {}

services:

mysql:
image: mysql:5.7
volumes:
- mysql_data:/var/lib/mysql
restart: always
ports:
- 3306:3306
environment:
MYSQL_ROOT_PASSWORD: password123
MYSQL_DATABASE: statping
MYSQL_USER: root
MYSQL_PASSWORD: password

postgres:
container_name: postgres
image: postgres:10.0-alpine
ports:
- 5432:5432
volumes:
- postgres_data:/var/lib/postgresql/data/pg_data
environment:
POSTGRES_PASSWORD: password123
POSTGRES_DB: statping
POSTGRES_USER: root
POSTGRES_PORT: 5432
PGDATA: /var/lib/postgresql/data/pg_data

phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin
environment:
- PMA_HOST=mysql
- PMA_USER=root
- PMA_PASSWORD=password123
restart: always
depends_on:
- mysql
links:
- mysql
ports:
- 7474:80
volumes:
- /sessions
49 changes: 46 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,52 @@ statping:
container_name: statping
image: hunterlong/statping
restart: always
volumes:
- statping_data:/app
environment:
DB_CONN: sqlite
LETSENCRYPT_HOST: demo.statping.com
VIRTUAL_HOST: demo.statping.com
VIRTUAL_PORT: 8080

prometheus:
container_name: prometheus
image: prom/prometheus:v2.0.0
restart: always
ports:
- 9090:9090
volumes:
- prometheus_config_data:/etc/prometheus/
- prometheus_data:/prometheus
links:
- statping
depends_on:
- statping

grafana:
container_name: grafana
image: grafana/grafana
restart: always
ports:
- 8080:8080
- 3000:3000
volumes:
- ./statping:/app
- grafana_data:/var/lib/grafana
environment:
DB_CONN: sqlite
- GF_SECURITY_ADMIN_PASSWORD=password123
- GF_USERS_ALLOW_SIGN_UP=false
depends_on:
- prometheus
links:
- prometheus


global:
scrape_interval: 30s
evaluation_interval: 30s

scrape_configs:
- job_name: 'statping'
scrape_interval: 30s
bearer_token: 'SECRET API KEY HERE'
static_configs:
- targets: ['statping:8080']
2 changes: 1 addition & 1 deletion frontend/src/API.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Api {
}

async service_hits(id, start, end, group) {
return axios.get('/api/services/' + id + '/data?start=' + start + '&end=' + end + '&group=' + group).then(response => (response.data))
return axios.get('/api/services/' + id + '/hits_data?start=' + start + '&end=' + end + '&group=' + group).then(response => (response.data))
}

async service_failures_data(id, start, end, group) {
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/Service/ServiceInfo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
}
},
async mounted() {
this.set1 = await this.getHits(24 * 2, "hour")
this.set1 = await this.getHits(24, "minute")
this.set1_name = this.calc(this.set1)
this.set2 = await this.getHits(24 * 7, "hour")
this.set2_name = this.calc(this.set2)
Expand Down
4 changes: 2 additions & 2 deletions handlers/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ func Router() *mux.Router {
api.Handle("/api/services/{id}/hits", scoped(apiServiceHitsHandler)).Methods("GET")

// API SERVICE CHART DATA Routes
api.Handle("/api/services/{id}/data", cached("30s", "application/json", apiServiceDataHandler)).Methods("GET")
api.Handle("/api/services/{id}/hits_data", cached("30s", "application/json", apiServiceDataHandler)).Methods("GET")
api.Handle("/api/services/{id}/failure_data", cached("30s", "application/json", apiServiceFailureDataHandler)).Methods("GET")
api.Handle("/api/services/{id}/ping", cached("30s", "application/json", apiServicePingDataHandler)).Methods("GET")
api.Handle("/api/services/{id}/ping_data", cached("30s", "application/json", apiServicePingDataHandler)).Methods("GET")
api.Handle("/api/services/{id}/heatmap", cached("30s", "application/json", apiServiceHeatmapHandler)).Methods("GET")

// API INCIDENTS Routes
Expand Down
89 changes: 16 additions & 73 deletions handlers/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/hunterlong/statping/types"
"github.com/hunterlong/statping/utils"
"net/http"
"strconv"
"time"
)

Expand Down Expand Up @@ -73,54 +72,6 @@ func reorderServiceHandler(w http.ResponseWriter, r *http.Request) {
returnJson(newOrder, w, r)
}

func servicesViewHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
fields := parseGet(r)
r.ParseForm()

var serv *core.Service
id := vars["id"]
if _, err := strconv.Atoi(id); err == nil {
serv = core.SelectService(utils.ToInt(id))
} else {
serv = core.SelectServiceLink(id)
}
if serv == nil {
w.WriteHeader(http.StatusNotFound)
return
}

startField := utils.ToInt(fields.Get("start"))
endField := utils.ToInt(fields.Get("end"))
group := r.Form.Get("group")

end := utils.Now().UTC()
start := end.Add((-24 * 7) * time.Hour).UTC()

if startField != 0 {
start = time.Unix(startField, 0).UTC()
}
if endField != 0 {
end = time.Unix(endField, 0).UTC()
}
if group == "" {
group = "hour"
}

data := core.GraphHitsDataRaw(serv, start, end, group, "latency")

out := struct {
Service *core.Service
Start string
End string
StartUnix int64
EndUnix int64
Data string
}{serv, start.Format(utils.FlatpickrReadable), end.Format(utils.FlatpickrReadable), start.Unix(), end.Unix(), data.ToString()}

ExecuteResponse(w, r, "service.gohtml", out, nil)
}

func apiServiceHandler(r *http.Request) interface{} {
vars := mux.Vars(r)
servicer := core.SelectService(utils.ToInt(vars["id"]))
Expand Down Expand Up @@ -205,18 +156,9 @@ func apiServiceDataHandler(w http.ResponseWriter, r *http.Request) {
sendErrorJson(errors.New("service data not found"), w, r)
return
}
fields := parseGet(r)
grouping := fields.Get("group")
if grouping == "" {
grouping = "hour"
}
startField := utils.ToInt(fields.Get("start"))
endField := utils.ToInt(fields.Get("end"))

start := time.Unix(startField, 0)
end := time.Unix(endField, 0)
groupQuery := parseGroupQuery(r)

obj := core.GraphHitsDataRaw(service, start, end, grouping, "latency")
obj := core.GraphHitsDataRaw(service, groupQuery, "latency")
returnJson(obj, w, r)
}

Expand All @@ -227,6 +169,13 @@ func apiServiceFailureDataHandler(w http.ResponseWriter, r *http.Request) {
sendErrorJson(errors.New("service data not found"), w, r)
return
}
groupQuery := parseGroupQuery(r)

obj := core.GraphFailuresDataRaw(service, groupQuery)
returnJson(obj, w, r)
}

func parseGroupQuery(r *http.Request) types.GroupQuery {
fields := parseGet(r)
grouping := fields.Get("group")
if grouping == "" {
Expand All @@ -235,11 +184,11 @@ func apiServiceFailureDataHandler(w http.ResponseWriter, r *http.Request) {
startField := utils.ToInt(fields.Get("start"))
endField := utils.ToInt(fields.Get("end"))

start := time.Unix(startField, 0).UTC()
end := time.Unix(endField, 0).UTC()

obj := core.GraphFailuresDataRaw(service, start, end, grouping)
returnJson(obj, w, r)
return types.GroupQuery{
Start: time.Unix(startField, 0).UTC(),
End: time.Unix(endField, 0).UTC(),
Group: grouping,
}
}

func apiServicePingDataHandler(w http.ResponseWriter, r *http.Request) {
Expand All @@ -249,15 +198,9 @@ func apiServicePingDataHandler(w http.ResponseWriter, r *http.Request) {
sendErrorJson(errors.New("service not found"), w, r)
return
}
fields := parseGet(r)
grouping := fields.Get("group")
startField := utils.ToInt(fields.Get("start"))
endField := utils.ToInt(fields.Get("end"))

start := time.Unix(startField, 0)
end := time.Unix(endField, 0)
groupQuery := parseGroupQuery(r)

obj := core.GraphHitsDataRaw(service, start, end, grouping, "ping_time")
obj := core.GraphHitsDataRaw(service, groupQuery, "ping_time")
returnJson(obj, w, r)
}

Expand Down
Loading

0 comments on commit 05c77a5

Please sign in to comment.