From 6e8e74b60d5569b2769af94200044e4e4cfa4102 Mon Sep 17 00:00:00 2001 From: hunterlong Date: Sun, 8 Mar 2020 11:13:27 -0700 Subject: [PATCH] ye --- cmd/cli_test.go | 10 + cmd/main.go | 20 +- database/sample.go | 66 +++--- database/service.go | 1 - frontend/package.json | 2 + .../src/components/Service/ServiceChart.vue | 3 +- .../src/components/Service/ServiceHeatmap.vue | 25 ++- .../src/components/Service/ServiceInfo.vue | 6 +- frontend/src/main.js | 10 +- frontend/src/mixin.js | 6 + frontend/src/pages/Service.vue | 20 +- frontend/yarn.lock | 189 +++++++----------- go.mod | 6 +- go.sum | 183 +++++++++++++++++ handlers/api_test.go | 70 ++++--- handlers/checkins_test.go | 19 +- handlers/groups.go | 6 +- handlers/groups_test.go | 13 +- handlers/messages_test.go | 48 +++-- handlers/middleware.go | 13 +- handlers/notifiers_test.go | 17 +- handlers/routes.go | 6 +- handlers/services_test.go | 42 +++- types/configs/configs_test.go | 6 +- types/configs/seed_test.go | 36 ++++ types/configs/testdata.yml | 0 types/core/struct.go | 1 + types/groups/database.go | 4 +- types/notifications/struct.go | 2 + types/oauth.go | 1 + utils/log.go | 1 + 31 files changed, 575 insertions(+), 257 deletions(-) delete mode 100644 database/service.go create mode 100644 types/configs/seed_test.go create mode 100644 types/configs/testdata.yml create mode 100644 types/oauth.go diff --git a/cmd/cli_test.go b/cmd/cli_test.go index 7e618dab..2e11cf3e 100644 --- a/cmd/cli_test.go +++ b/cmd/cli_test.go @@ -16,6 +16,8 @@ package main import ( + "fmt" + "github.com/getsentry/sentry-go" "github.com/hunterlong/statping/utils" "github.com/rendon/testcli" "github.com/stretchr/testify/assert" @@ -32,6 +34,14 @@ var ( func init() { dir = utils.Directory //core.SampleHits = 480 + + if err := sentry.Init(sentry.ClientOptions{ + Dsn: errorReporter, + Environment: "testing", + }); err != nil { + fmt.Println(err) + } + } func TestStartServerCommand(t *testing.T) { diff --git a/cmd/main.go b/cmd/main.go index 2571fa7d..761376b2 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -18,9 +18,11 @@ package main import ( "flag" "fmt" + "github.com/getsentry/sentry-go" "os" "os/signal" "syscall" + "time" "github.com/hunterlong/statping/source" @@ -67,9 +69,10 @@ func parseFlags() { } func exit(err error) { - panic(err) - //log.Fatalln(err) - //os.Exit(2) + sentry.CaptureException(err) + log.Fatalln(err) + Close() + os.Exit(2) } // main will run the Statping application @@ -107,6 +110,14 @@ func main() { log.Warnln(err) } + errorEnv := utils.Getenv("GO_ENV", "production").(string) + if err := sentry.Init(sentry.ClientOptions{ + Dsn: errorReporter, + Environment: errorEnv, + }); err != nil { + log.Errorln(err) + } + c, err := configs.LoadConfigs() if err != nil { if err := SetupMode(); err != nil { @@ -129,6 +140,7 @@ func main() { // Close will gracefully stop the database connection, and log file func Close() { + sentry.Flush(3 * time.Second) utils.CloseLogs() database.Close() } @@ -189,3 +201,5 @@ func httpServerProcess(process <-chan bool) { } } } + +const errorReporter = "https://2bedd272821643e1b92c774d3fdf28e7@sentry.statping.com/2" diff --git a/database/sample.go b/database/sample.go index 8dfd9fdd..b6e56559 100644 --- a/database/sample.go +++ b/database/sample.go @@ -29,46 +29,44 @@ var ( ) // InsertSampleHits will create a couple new hits for the sample services -func InsertSampleHits() error { - //tx := Begin(&hits.Hit{}) - //sg := new(sync.WaitGroup) - //for i := int64(1); i <= 5; i++ { - // sg.Add(1) - // service := SelectService(i) - // seed := time.Now().UnixNano() - // log.Infoln(fmt.Sprintf("Adding %v sample hit records to service %v", SampleHits, service.Name)) - // createdAt := sampleStart - // p := utils.NewPerlin(2., 2., 10, seed) - // go func(sg *sync.WaitGroup) { - // defer sg.Done() - // for hi := 0.; hi <= float64(SampleHits); hi++ { - // latency := p.Noise1D(hi / 500) - // createdAt = createdAt.Add(60 * time.Second) - // hit := &hits.Hit{ - // Service: service.Id, - // CreatedAt: createdAt, - // Latency: latency, - // } - // tx = tx.Create(&hit) - // } - // }(sg) - //} - //sg.Wait() - //if err := tx.Commit().Error(); err != nil { - // log.Errorln(err) - // return types.ErrWrap(err, types.ErrorCreateSampleHits) - //} - return nil -} +//func InsertSampleHits() error { +// tx := Begin(&hits.Hit{}) +// sg := new(sync.WaitGroup) +// for i := int64(1); i <= 5; i++ { +// sg.Add(1) +// service := SelectService(i) +// seed := time.Now().UnixNano() +// log.Infoln(fmt.Sprintf("Adding %v sample hit records to service %v", SampleHits, service.Name)) +// createdAt := sampleStart +// p := utils.NewPerlin(2., 2., 10, seed) +// go func(sg *sync.WaitGroup) { +// defer sg.Done() +// for hi := 0.; hi <= float64(SampleHits); hi++ { +// latency := p.Noise1D(hi / 500) +// createdAt = createdAt.Add(60 * time.Second) +// hit := &hits.Hit{ +// Service: service.Id, +// CreatedAt: createdAt, +// Latency: latency, +// } +// tx = tx.Create(&hit) +// } +// }(sg) +// } +// sg.Wait() +// if err := tx.Commit().Error(); err != nil { +// log.Errorln(err) +// return types.ErrWrap(err, types.ErrorCreateSampleHits) +// } +// return nil +//} -// TmpRecords is used for testing Statping. It will create a SQLite database file -// with sample data and store it in the /tmp folder to be used by the tests. //func TmpRecords(dbFile string) error { // var sqlFile = utils.Directory + "/" + dbFile // if err := utils.CreateDirectory(utils.Directory + "/tmp"); err != nil { // log.Error(err) // } -// var tmpSqlFile = utils.Directory + "/tmp/" + types.SqliteFilename +// var tmpSqlFile = utils.Directory + "/tmp/" + dbFile // SampleHits = 480 // // var err error diff --git a/database/service.go b/database/service.go deleted file mode 100644 index 636bab89..00000000 --- a/database/service.go +++ /dev/null @@ -1 +0,0 @@ -package database diff --git a/frontend/package.json b/frontend/package.json index 0a865b23..970bfa01 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -15,6 +15,8 @@ "@fortawesome/free-brands-svg-icons": "^5.12.1", "@fortawesome/free-solid-svg-icons": "^5.12.0", "@fortawesome/vue-fontawesome": "^0.1.9", + "@sentry/browser": "^5.13.2", + "@sentry/integrations": "^5.13.2", "apexcharts": "^3.15.0", "axios": "^0.19.1", "codemirror-colorpicker": "^1.9.66", diff --git a/frontend/src/components/Service/ServiceChart.vue b/frontend/src/components/Service/ServiceChart.vue index 2242faf0..e7d204ed 100644 --- a/frontend/src/components/Service/ServiceChart.vue +++ b/frontend/src/components/Service/ServiceChart.vue @@ -132,8 +132,7 @@ }, methods: { async chartHits(group) { - const start = this.nowSubtract((3600 * 24) * 3) - this.data = await Api.service_hits(this.service.id, this.toUnix(start), this.toUnix(new Date()), group, false) + this.data = await Api.service_hits(this.service.id, this.toUnix(this.service.created_at), this.toUnix(new Date()), group, false) if (this.data.length === 0 && group !== "1h") { await this.chartHits("1h") diff --git a/frontend/src/components/Service/ServiceHeatmap.vue b/frontend/src/components/Service/ServiceHeatmap.vue index 997b7848..f33d85c7 100644 --- a/frontend/src/components/Service/ServiceHeatmap.vue +++ b/frontend/src/components/Service/ServiceHeatmap.vue @@ -86,23 +86,32 @@ }, methods: { async chartHeatmap() { - const start = this.nowSubtract((3600 * 24) * 7) - const data = await Api.service_failures_data(this.service.id, this.toUnix(start), this.toUnix(new Date()), "24h", true) + let start = new Date(new Date().getUTCFullYear(), new Date().getUTCMonth()-3, 1); - window.console.log(data) + let monthData = []; + + for (i=0; i<=3; i++) { + let end = this.lastDayOfMonth(start.getUTCMonth()+start) + const inputdata = this.heatmapData(start,end) + monthData.push(inputdata) + } + + this.series = monthData + this.ready = true + }, + async heatmapData(start, end) { + console.log(start, end) + + const data = await Api.service_failures_data(this.service.id, this.toUnix(start), this.toUnix(end), "24h", true) let dataArr = [] data.forEach(function(d) { - dataArr.push({x: d.timeframe, y: 5+d.amount}); + dataArr.push({x: d.timeframe, y: d.amount}); }); let date = new Date(dataArr[0].x); const output = [{name: date.toLocaleString('en-us', { month: 'long'}), data: dataArr}] - window.console.log(output) - - this.series = output - this.ready = true } } } diff --git a/frontend/src/components/Service/ServiceInfo.vue b/frontend/src/components/Service/ServiceInfo.vue index 949ac813..edfae6f8 100644 --- a/frontend/src/components/Service/ServiceInfo.vue +++ b/frontend/src/components/Service/ServiceInfo.vue @@ -49,11 +49,13 @@ } }, async mounted() { - this.set1 = await this.getHits(24, "1h") + this.set1 = await this.getHits(24, "15m") this.set1_name = this.calc(this.set1) - this.set2 = await this.getHits(24 * 7, "24h") + this.set2 = await this.getHits(24 * 7, "6h") this.set2_name = this.calc(this.set2) this.loaded = true + + window.console.log(this.set1) }, methods: { async getHits(hours, group) { diff --git a/frontend/src/main.js b/frontend/src/main.js index bf0c9e52..6aeaf28f 100644 --- a/frontend/src/main.js +++ b/frontend/src/main.js @@ -5,7 +5,9 @@ import VueObserveVisibility from 'vue-observe-visibility' import App from '@/App.vue' import store from './store' - +import * as Sentry from '@sentry/browser'; +import * as Integrations from '@sentry/integrations'; +const errorReporter = "https://bed4d75404924cb3a799e370733a1b64@sentry.statping.com/3" import router from './routes' import "./mixin" import "./icons" @@ -15,6 +17,12 @@ Vue.component('apexchart', VueApexCharts) Vue.use(VueRouter); Vue.use(VueObserveVisibility); +Sentry.init({ + dsn: errorReporter, + integrations: [new Integrations.Vue({Vue, attachProps: true})], +}); + + Vue.config.productionTip = false new Vue({ router, diff --git a/frontend/src/mixin.js b/frontend/src/mixin.js index 00d0ede0..18cb2082 100644 --- a/frontend/src/mixin.js +++ b/frontend/src/mixin.js @@ -109,6 +109,12 @@ export default Vue.mixin({ }) }) return {data: newSet} + }, + lastDayOfMonth(month) { + return new Date(new Date().getUTCFullYear(), month + 1, 0); + }, + firstDayOfMonth(month) { + return new Date(new Date().getUTCFullYear(), month, 1).getUTCDate(); } } }); diff --git a/frontend/src/pages/Service.vue b/frontend/src/pages/Service.vue index fd294da7..db3a0ff6 100644 --- a/frontend/src/pages/Service.vue +++ b/frontend/src/pages/Service.vue @@ -37,6 +37,10 @@ +
+ +
+