Skip to content
This repository has been archived by the owner on Jan 6, 2022. It is now read-only.

Commit

Permalink
v2.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
cn0047 committed Oct 21, 2018
1 parent 28c8983 commit 61a717d
Show file tree
Hide file tree
Showing 31 changed files with 408 additions and 137 deletions.
23 changes: 18 additions & 5 deletions src/go-app/.gae/static/js/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const DEFAULT_TIME_RANGE = '12h';

/**
* Shows element by id.
*
Expand Down Expand Up @@ -213,6 +211,15 @@ const submitProjectForm = function () {
.then(payload => handleProjectForm(form.name, payload));
};

/**
* Gets current "time range" value.
*
* @returns {String} Current "time range" value.
*/
const getCurrentTimeRange = function () {
return document.querySelector('#charts .timeRangeActive').text;
};

/**
* Application entry point.
*/
Expand All @@ -222,21 +229,27 @@ const app = function () {
return;
}

renderCharts(DEFAULT_TIME_RANGE, 0);
renderCharts(getCurrentTimeRange(), 0);
};

/**
* Handler for dropdown with projects, which performs charts re-rendering.
*/
document.getElementById('selectedProject').addEventListener('change', () => {
renderCharts(DEFAULT_TIME_RANGE, 0);
renderCharts(getCurrentTimeRange(), 0);
});

/**
* Handler for "time range" quick links.
*/
document.querySelectorAll('#charts .timeRange a').forEach((el) => {
el.addEventListener('click', (e) => renderCharts(e.target.text, 0));
el.addEventListener('click', (e) => {
document.querySelectorAll('#charts .timeRange a').forEach((aEl) => {
aEl.className = 'timeRangeDefault';
});
e.target.className = 'timeRangeActive';
renderCharts(e.target.text, 0);
});
});

/**
Expand Down
9 changes: 6 additions & 3 deletions src/go-app/.gae/static/s/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,14 @@ a {
text-align: center;
width: 25px;
}
#charts .timeRange a:visited {
#charts .timeRange a:hover {
text-decoration: underline;
}
#charts .timeRangeDefault {
color: #757575;
}
#charts .timeRange a:active {
color: cornflowerblue;
#charts .timeRangeActive {
color: red;
}
#charts .row {
display: flex;
Expand Down
12 changes: 6 additions & 6 deletions src/go-app/.gae/template/home/index.go.html
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@
</select>
<button id="addProject" class="">+</button>
<div class="timeRange pull-right">
<a href="#">1h</a>
<a href="#">6h</a>
<a href="#">12h</a>
<a href="#">1d</a>
<a href="#">1w</a>
<a href="#">1m</a>
<a class="timeRangeDefault" href="#">1h</a>
<a class="timeRangeDefault" href="#">6h</a>
<a class="timeRangeActive" href="#">12h</a>
<a class="timeRangeDefault" href="#">1d</a>
<a class="timeRangeDefault" href="#">1w</a>
<a class="timeRangeDefault" href="#">1m</a>
</div>
</div>
<div id="chartError" class="hide">
Expand Down
5 changes: 5 additions & 0 deletions src/go-app/app/config/taxonomy/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ const (
// DataStoreKindProject const name for DataStore Kind.
DataStoreKindProject = "Project"

// CacheKeyProjectsAll cache key for all projects slice.
CacheKeyProjectsAll = "ProjectsAll"
// CacheKeyPrefixMeasurementLastAt cache key prefix for lastAt measurement time.
CacheKeyPrefixMeasurementLastAt = "MeasurementLastAt"

// MethodHead const for head ping service method.
MethodHead = "head"
// MethodGet const for get ping service method.
Expand Down
5 changes: 4 additions & 1 deletion src/go-app/app/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"github.com/thepkg/rest"
"net/http"

"go-app/app/config"

cah "go-app/controller/ah"
ccharts "go-app/controller/api/charts"
cprojects "go-app/controller/api/projects"
Expand Down Expand Up @@ -32,7 +34,8 @@ func cron() {
}

func worker() {
http.HandleFunc("/worker/ping", cworker.Ping)
// "/worker/ping"
http.HandleFunc(config.WorkerPathPing, cworker.Ping)
}

func home() {
Expand Down
27 changes: 21 additions & 6 deletions src/go-app/app/vo/GetChartVO/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ var (
// This ValueObject contains all possible filters
// to get measurement entities from DataStore.
type Instance struct {
Project string
Limit int
TimeRangeStart time.Time
project string
limit int
timeRangeStart time.Time
}

// New gets new GetChartVO instance.
Expand All @@ -51,7 +51,7 @@ func (i *Instance) initFromGetRequest(r *http.Request) {

func (i *Instance) initProject(v string, err *InvalidVOError.Instance) {
if validator.IsProjectName(v) {
i.Project = v
i.project = v
} else {
err.SetError("project", "Invalid project name.")
}
Expand All @@ -63,7 +63,7 @@ func (i *Instance) initLimit(v string, err *InvalidVOError.Instance) {
if err != nil {
AppError.Panic(err)
}
i.Limit = val
i.limit = val
} else {
err.SetError("limit", "Invalid limit.")
}
Expand All @@ -72,8 +72,23 @@ func (i *Instance) initLimit(v string, err *InvalidVOError.Instance) {
func (i *Instance) initTimeRange(v string, err *InvalidVOError.Instance) {
if validator.IsTimeRange(v) {
d := taxonomy.TimeRanges[v]
i.TimeRangeStart = time.Now().Add(-d)
i.timeRangeStart = time.Now().UTC().Add(-d)
} else {
err.SetError("timeRange", "Invalid timeRange.")
}
}

// GetProject gets field project value.
func (i Instance) GetProject() string {
return i.project
}

// GetLimit gets field limit value.
func (i Instance) GetLimit() int {
return i.limit
}

// GetTimeRangeStart gets field timeRangeStart value.
func (i Instance) GetTimeRangeStart() time.Time {
return i.timeRangeStart
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package AddProjectVO
package ProjectVO

import (
"strconv"
Expand All @@ -18,16 +18,18 @@ var (
}
)

// Instance represents ValueObject to add project.
// Instance represents ValueObject for Project entity.
type Instance struct {
Name string
URL string
Method string
JSON string
Schedule int // seconds
name string
url string
method string
json string
schedule int // seconds
}

// New gets new AddProjectVO instance.
// New gets new ProjectVO instance.
// This ValueObject contains all possible fields
// which may be stored in DataStore.
func New(data map[string]string) Instance {
vo := Instance{}
vo.initFromMap(data)
Expand Down Expand Up @@ -56,7 +58,7 @@ func (i *Instance) initName(data map[string]string, err *InvalidVOError.Instance
return
}
if validator.IsProjectName(v) {
i.Name = v
i.name = v
} else {
err.SetError("name", "Invalid name.")
}
Expand All @@ -69,7 +71,7 @@ func (i *Instance) initURL(data map[string]string, err *InvalidVOError.Instance)
return
}
if validator.IsURL(v) {
i.URL = v
i.url = v
} else {
err.SetError("url", "Invalid url.")
}
Expand All @@ -82,7 +84,7 @@ func (i *Instance) initMethod(data map[string]string, err *InvalidVOError.Instan
return
}
if validator.IsMethod(v) {
i.Method = v
i.method = v
} else {
err.SetError("method", "Invalid method.")
}
Expand All @@ -91,7 +93,7 @@ func (i *Instance) initMethod(data map[string]string, err *InvalidVOError.Instan
func (i *Instance) initJSON(data map[string]string, err *InvalidVOError.Instance) {
v, exists := data[params["json"]]
if exists && v != "" {
i.JSON = v
i.json = v
}
}

Expand All @@ -107,11 +109,36 @@ func (i *Instance) initSchedule(data map[string]string, err *InvalidVOError.Inst
AppError.Panic(er)
}
if val > 0 {
i.Schedule = val * 60 // from minutes to seconds
i.schedule = val * 60 // from minutes to seconds
} else {
err.SetError("schedule", "Invalid schedule, must be greater than 0.")
}
} else {
err.SetError("schedule", "Invalid schedule.")
}
}

// GetName gets field name value.
func (i Instance) GetName() string {
return i.name
}

// GetName gets field url value.
func (i Instance) GetURL() string {
return i.url
}

// GetName gets field method value.
func (i Instance) GetMethod() string {
return i.method
}

// GetName gets field json value.
func (i Instance) GetJSON() string {
return i.json
}

// GetName gets field schedule value.
func (i Instance) GetSchedule() int {
return i.schedule
}
19 changes: 10 additions & 9 deletions src/go-app/controller/ah/warmup.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
"net/http"

"go-app/app/config/taxonomy"
"go-app/service/datastore/Project"
"go-app/app/vo/ProjectVO"
"go-app/service/project"
)

// WarmUp internal AppEngine controller for init purposes.
Expand All @@ -21,12 +22,12 @@ func WarmUp(w http.ResponseWriter, r *http.Request) {
}

func initRealTimeLog(ctx context.Context) {
vo := Project.EntityVO{
Name: "realtimelog-health-check",
URL: "https://realtimelog.herokuapp.com/health-check",
Method: taxonomy.MethodPost,
JSON: `{"msg":"monitoring-ping-1"}`,
Schedule: 250,
}
Project.Update(ctx, vo)
vo := ProjectVO.New(map[string]string{
"name": "realtimelog-health-check",
"url": "https://realtimelog.herokuapp.com/health-check",
"method": taxonomy.MethodPost,
"json": `{"msg":"monitoring-ping-1"}`,
"schedule": "300",
})
project.Update(ctx, vo)
}
11 changes: 5 additions & 6 deletions src/go-app/controller/api/projects/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"google.golang.org/appengine"
"net/http"

"go-app/app/vo/AddProjectVO"
"go-app/service/datastore/Project"
"go-app/app/vo/ProjectVO"
"go-app/service/project"
)

// Post represents REST-API endpoint to create new project.
Expand All @@ -16,9 +16,8 @@ func Post(w http.ResponseWriter, r *http.Request) {
params := make(map[string]string)
rest.MustUnmarshalBody(r, &params)

vo1 := AddProjectVO.New(params)
vo2 := Project.EntityVO(vo1)
Project.Add(ctx, vo2)
vo := ProjectVO.New(params)
project.Add(ctx, vo)

rest.Success(w, http.StatusOK, vo1)
rest.Success(w, http.StatusOK, vo)
}
18 changes: 1 addition & 17 deletions src/go-app/controller/cron/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,18 @@ package cron

import (
"fmt"
"golang.org/x/net/context"
"google.golang.org/appengine"
"net/http"
"time"

"go-app/service/datastore/Measurement"
"go-app/service/datastore/Project"
"go-app/service/queue"
)

// AddPingJobs controller to add batch of ping jobs.
func AddPingJobs(w http.ResponseWriter, r *http.Request) {
ctx := appengine.NewContext(r)

projects := Project.GetAll(ctx)
for _, prj := range projects {
if isItTimeToPing(ctx, prj) {
queue.AddPingJob(ctx, prj)
}
}
queue.AddPingJobs(ctx)

w.WriteHeader(http.StatusNoContent)
fmt.Fprint(w, "")
}

func isItTimeToPing(ctx context.Context, prj Project.Entity) bool {
lastMeasurementAt := Measurement.GetLastAt(ctx, prj.Name)
nextMeasurementAt := lastMeasurementAt.Add(prj.GetScheduleDuration())

return time.Now().After(nextMeasurementAt)
}
Loading

0 comments on commit 61a717d

Please sign in to comment.