Skip to content

Commit

Permalink
feat: publish progress over mqtt
Browse files Browse the repository at this point in the history
  • Loading branch information
lekotros committed Jan 26, 2022
1 parent 0d662de commit 32850b0
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
39 changes: 37 additions & 2 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ type validationResult struct {
type progress struct {
count int
completed int
status string
rows map[string]string
jobStatus map[string]string
}

type ValidationContext struct {
Expand Down Expand Up @@ -54,15 +56,41 @@ func (c *ValidationContext) AddDocument(name string, document types.Document) {
func (c *ValidationContext) startProgress(name string, scripts ScriptMap) {
n := len(scripts)
filler := map[string]string{}
jobStatus := map[string]string{}
for _, name := range scripts.Keys() {
filler[name] = "starting"
jobStatus[name] = "running"
}

c.progress[name] = &progress{n + 1, 0, filler}
c.progress[name] = &progress{
count: n + 1,
completed: 0,
status: "running",
rows: filler,
jobStatus: jobStatus,
}
logger.DefaultTerminalOutput().AddBuffer(name, n+2)
}

func (c *ValidationContext) addProgress(name, scriptName, message string, n int) {
func (c *ValidationContext) publishProgress() {
progress := []map[string]interface{}{}
for name, p := range c.progress {
progress = append(progress, map[string]interface{}{
"name": name,
"count": p.count,
"completed": p.completed,
"rows": p.rows,
"status": p.status,
"jobStatus": p.jobStatus,
})
}
publishMessage("progress", map[string]interface{}{
"name": c.name,
"progress": progress,
})
}

func (c *ValidationContext) addProgress(name, scriptName, message string, n int, status string) {
c.Lock()
defer c.Unlock()

Expand All @@ -73,6 +101,9 @@ func (c *ValidationContext) addProgress(name, scriptName, message string, n int)

if scriptName != "" {
p.rows[scriptName] = message
p.jobStatus[scriptName] = status
} else {
p.status = status
}

p.completed += n
Expand Down Expand Up @@ -110,6 +141,10 @@ func (c *ValidationContext) addProgress(name, scriptName, message string, n int)
}

terminal.LogTo(name, logger.NewLogEntry(logger.LogLevelInfo, nil, progress))

if n > 0 {
c.publishProgress()
}
}

func NewValidationContext(name string) *ValidationContext {
Expand Down
2 changes: 1 addition & 1 deletion js.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type jsContext struct {

func (c *jsContext) log(level logger.LogLevel, v string, args ...interface{}) {
msg := fmt.Sprintf(v, args...)
c.context.addProgress(c.name, c.script.name, msg, 0)
c.context.addProgress(c.name, c.script.name, msg, 0, "running")
c.logger.Logf(level, v, args...)
}

Expand Down
11 changes: 9 additions & 2 deletions validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,13 @@ func (v *Validator) Validate(ctx *ValidationContext) {

for i := 0; i < numTasks; i++ {
if vr, ok := (<-res).(*ValidationResult); ok {
status := "valid"
if !vr.Valid {
status = "invalid"
}

ctx.results = append(ctx.results, vr)
ctx.addProgress(vr.Name, "", "", 1)
ctx.addProgress(vr.Name, "", "", 1, status)
}
}

Expand Down Expand Up @@ -118,12 +123,14 @@ func (v *Validator) ValidateDocument(name string, doc types.Document, ctx *Valid
for i := 0; i < numTasks; i++ {
if vr, ok := (<-res).(*RuleValidation); ok {
message := fmt.Sprintf("\033[32mok\033[0m")
status := "valid"
if !vr.Valid {
message = fmt.Sprintf("\033[31mfailed\033[0m with %d errors and 0 warnings", vr.ErrorCount)
status = "invalid"
}

result.ValidationRules = append(result.ValidationRules, vr)
ctx.addProgress(name, vr.Name, message, 1)
ctx.addProgress(name, vr.Name, message, 1, status)
}
}

Expand Down

0 comments on commit 32850b0

Please sign in to comment.