diff --git a/handlers/services.go b/handlers/services.go index fec70928..95a34227 100644 --- a/handlers/services.go +++ b/handlers/services.go @@ -195,12 +195,10 @@ func apiServiceDataHandler(w http.ResponseWriter, r *http.Request) { startField := utils.ToInt(fields.Get("start")) endField := utils.ToInt(fields.Get("end")) - if startField == 0 || endField == 0 { - startField = 0 - endField = 99999999999 - } + start := time.Unix(startField, 0) + end := time.Unix(endField, 0) - obj := core.GraphDataRaw(service, time.Unix(startField, 0).UTC(), time.Unix(endField, 0).UTC(), grouping, "latency") + obj := core.GraphDataRaw(service, start, end, grouping, "latency") w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(obj) } @@ -216,7 +214,11 @@ func apiServicePingDataHandler(w http.ResponseWriter, r *http.Request) { grouping := fields.Get("group") startField := utils.ToInt(fields.Get("start")) endField := utils.ToInt(fields.Get("end")) - obj := core.GraphDataRaw(service, time.Unix(startField, 0), time.Unix(endField, 0), grouping, "ping_time") + + start := time.Unix(startField, 0) + end := time.Unix(endField, 0) + + obj := core.GraphDataRaw(service, start, end, grouping, "ping_time") w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(obj) diff --git a/notifiers/mobile.go b/notifiers/mobile.go index ed77e96c..884e8a1a 100644 --- a/notifiers/mobile.go +++ b/notifiers/mobile.go @@ -22,10 +22,11 @@ import ( "github.com/hunterlong/statping/core/notifier" "github.com/hunterlong/statping/types" "github.com/hunterlong/statping/utils" - "os" "time" ) +const mobileIdentifier = "com.statping" + type mobilePush struct { *notifier.Notification } @@ -44,13 +45,13 @@ var mobile = &mobilePush{¬ifier.Notification{ Title: "Device Identifiers", Placeholder: "A list of your mobile device push notification ID's.", DbField: "var1", - IsHidden: true, + IsHidden: false, }, { Type: "number", Title: "Array of device numbers", Placeholder: "1 for iphone 2 for android", DbField: "var2", - IsHidden: true, + IsHidden: false, }}}, } @@ -95,6 +96,7 @@ func (u *mobilePush) OnFailure(s *types.Service, f *types.Failure) { msg := &PushArray{ Message: fmt.Sprintf("Your service '%v' is currently failing! Reason: %v", s.Name, f.Issue), Title: "Service Offline", + Topic: mobileIdentifier, Data: data, } u.AddQueue(s.Id, msg) @@ -109,6 +111,7 @@ func (u *mobilePush) OnSuccess(s *types.Service) { msg := &PushArray{ Message: fmt.Sprintf("Your service '%v' is back online!", s.Name), Title: "Service Online", + Topic: mobileIdentifier, Data: data, } u.AddQueue(s.Id, msg) @@ -121,6 +124,7 @@ func (u *mobilePush) OnSave() error { msg := &PushArray{ Message: "The Mobile Notifier has been saved", Title: "Notification Saved", + Topic: mobileIdentifier, } u.AddQueue(0, msg) return nil @@ -128,7 +132,29 @@ func (u *mobilePush) OnSave() error { // OnTest triggers when this notifier has been saved func (u *mobilePush) OnTest() error { - return nil + msg := &PushArray{ + Message: "Testing the Mobile Notifier", + Title: "Testing Notifications", + Topic: mobileIdentifier, + Tokens: []string{u.Var1}, + Platform: utils.ToInt(u.Var2), + } + body, err := pushRequest(msg) + if err != nil { + return err + } + var output MobileResponse + err = json.Unmarshal(body, &output) + if err != nil { + return err + } + if len(output.Logs) == 0 { + return nil + } else { + firstLog := output.Logs[0].Error + return fmt.Errorf("Mobile Notification error: %v", firstLog) + } + return err } // Send will send message to Statping push notifications endpoint @@ -136,24 +162,24 @@ func (u *mobilePush) Send(msg interface{}) error { pushMessage := msg.(*PushArray) pushMessage.Tokens = []string{u.Var1} pushMessage.Platform = utils.ToInt(u.Var2) - err := pushRequest(pushMessage) + _, err := pushRequest(pushMessage) if err != nil { return err } return nil } -func pushRequest(msg *PushArray) error { +func pushRequest(msg *PushArray) ([]byte, error) { if msg.Platform == 1 { msg.Title = "" } body, _ := json.Marshal(&PushNotification{[]*PushArray{msg}}) url := "https://push.statping.com/api/push" - if os.Getenv("GO_ENV") == "test" { - url = "https://pushdev.statping.com/api/push" - } - _, _, err := utils.HttpRequest(url, "POST", "application/json", nil, bytes.NewBuffer(body), time.Duration(20*time.Second)) - return err + //if os.Getenv("GO_ENV") == "test" { + // url = "https://pushdev.statping.com/api/push" + //} + body, _, err := utils.HttpRequest(url, "POST", "application/json", nil, bytes.NewBuffer(body), time.Duration(20*time.Second)) + return body, err } type PushNotification struct { @@ -164,12 +190,21 @@ type PushArray struct { Tokens []string `json:"tokens"` Platform int64 `json:"platform"` Message string `json:"message"` + Topic string `json:"topic"` Title string `json:"title,omitempty"` Data map[string]interface{} `json:"data,omitempty"` } type MobileResponse struct { - Counts int `json:"counts"` - Logs []interface{} `json:"logs"` - Success string `json:"success"` + Counts int `json:"counts"` + Logs []*MobileResponseLogs `json:"logs"` + Success string `json:"success"` +} + +type MobileResponseLogs struct { + Type string `json:"type"` + Platform string `json:"platform"` + Token string `json:"token"` + Message string `json:"message"` + Error string `json:"error"` } diff --git a/notifiers/mobile_test.go b/notifiers/mobile_test.go index 540d21b8..126adf7a 100644 --- a/notifiers/mobile_test.go +++ b/notifiers/mobile_test.go @@ -113,6 +113,7 @@ func TestMobileNotifier(t *testing.T) { }) t.Run("mobile Test", func(t *testing.T) { + t.SkipNow() err := mobile.OnTest() assert.Nil(t, err) }) diff --git a/source/js/charts.js b/source/js/charts.js index eb06daa4..156d4762 100644 --- a/source/js/charts.js +++ b/source/js/charts.js @@ -148,17 +148,14 @@ let options = { }, }; -const startOn = Math.floor(Date.now() / 1000) - (86400 * 14); -const endOn = Math.floor(Date.now() / 1000); - +const startOn = UTCTime() - (86400 * 14); async function RenderCharts() { - {{ range .Services }} - let chart{{.Id}} = new ApexCharts(document.querySelector("#service_{{js .Id}}"), options); - {{end}} +{{ range .Services }} +let chart{{.Id}} = new ApexCharts(document.querySelector("#service_{{js .Id}}"), options);{{end}} {{ range .Services }} -await RenderChart(chart{{js .Id}}, {{js .Id}}, startOn, endOn);{{end}} +await RenderChart(chart{{js .Id}}, {{js .Id}}, startOn);{{end}} } $( document ).ready(function() { diff --git a/source/js/main.js b/source/js/main.js index 8ce96577..abb7f25c 100644 --- a/source/js/main.js +++ b/source/js/main.js @@ -114,7 +114,7 @@ $('select#service_type').on('change', function() { async function RenderChart(chart, service, start=0, end=9999999999, group="hour", retry=true) { let chartData = await ChartLatency(service, start, end, group, retry); - if (chartData.length === 0) { + if (!chartData) { chartData = await ChartLatency(service, start, end, "minute", retry); } chart.render(); @@ -123,10 +123,18 @@ async function RenderChart(chart, service, start=0, end=9999999999, group="hour" }]); } + +function UTCTime() { + var now = new Date(); + now = new Date(now.toUTCString()); + return Math.floor(now.getTime() / 1000); +} + function ChartLatency(service, start=0, end=9999999999, group="hour", retry=true) { + let url = "/api/services/" + service + "/data?start=" + start + "&end=" + end + "&group=" + group; return new Promise(resolve => { $.ajax({ - url: "/api/services/" + service + "/data?start=" + start + "&end=" + end + "&group=" + group, + url: url, type: 'GET', success: function (data) { resolve(data.data); diff --git a/source/tmpl/index.gohtml b/source/tmpl/index.gohtml index 88864bb0..c88156fb 100644 --- a/source/tmpl/index.gohtml +++ b/source/tmpl/index.gohtml @@ -33,8 +33,8 @@