Skip to content

Commit

Permalink
feat(pkger): add support for tasks to parser
Browse files Browse the repository at this point in the history
  • Loading branch information
jsteenb2 committed Dec 23, 2019
1 parent af5b7fd commit 56490ef
Show file tree
Hide file tree
Showing 5 changed files with 444 additions and 24 deletions.
101 changes: 101 additions & 0 deletions pkger/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const (
KindNotificationEndpointSlack Kind = "notification_endpoint_slack"
KindNotificationRule Kind = "notification_rule"
KindPackage Kind = "package"
KindTask Kind = "task"
KindTelegraf Kind = "telegraf"
KindVariable Kind = "variable"
)
Expand All @@ -50,6 +51,7 @@ var kinds = map[Kind]bool{
KindNotificationEndpointSlack: true,
KindNotificationRule: true,
KindPackage: true,
KindTask: true,
KindTelegraf: true,
KindVariable: true,
}
Expand Down Expand Up @@ -116,6 +118,8 @@ func (k Kind) ResourceType() influxdb.ResourceType {
return influxdb.NotificationEndpointResourceType
case KindNotificationRule:
return influxdb.NotificationRuleResourceType
case KindTask:
return influxdb.TasksResourceType
case KindTelegraf:
return influxdb.TelegrafsResourceType
case KindVariable:
Expand Down Expand Up @@ -513,6 +517,7 @@ type Summary struct {
NotificationRules []SummaryNotificationRule `json:"notificationRules"`
Labels []SummaryLabel `json:"labels"`
LabelMappings []SummaryLabelMapping `json:"labelMappings"`
Tasks []SummaryTask `json:"summaryTask"`
TelegrafConfigs []SummaryTelegraf `json:"telegrafConfigs"`
Variables []SummaryVariable `json:"variables"`
}
Expand Down Expand Up @@ -723,6 +728,19 @@ type SummaryLabelMapping struct {
LabelID SafeID `json:"labelID"`
}

// SummaryTask provides a summary of a task.
type SummaryTask struct {
Name string `json:"name"`
Cron string `json:"cron"`
Description string `json:"description"`
Every string `json:"every"`
Offset string `json:"offset"`
Query string `json:"query"`
Status influxdb.Status `json:"status"`

LabelAssociations []SummaryLabel `json:"labelAssociations"`
}

// SummaryTelegraf provides a summary of a pkg telegraf config.
type SummaryTelegraf struct {
TelegrafConfig influxdb.TelegrafConfig `json:"telegrafConfig"`
Expand Down Expand Up @@ -1761,6 +1779,82 @@ func (r mapperNotificationRules) Len() int {
return len(r)
}

const (
fieldTaskCron = "cron"
)

type task struct {
name string
cron string
description string
every time.Duration
offset time.Duration
query string
status string

labels sortedLabels
}

func (t *task) Name() string {
return t.name
}

func (t *task) ResourceType() influxdb.ResourceType {
return KindTask.ResourceType()
}

func (t *task) Status() influxdb.Status {
if t.status == "" {
return influxdb.Active
}
return influxdb.Status(t.status)
}

func (t *task) summarize() SummaryTask {
return SummaryTask{
Name: t.Name(),
Cron: t.cron,
Description: t.description,
Every: durToStr(t.every),
Offset: durToStr(t.offset),
Query: t.query,
Status: t.Status(),

LabelAssociations: toSummaryLabels(t.labels...),
}
}

func (t *task) valid() []validationErr {
var vErrs []validationErr
if t.cron == "" && t.every == 0 {
vErrs = append(vErrs,
validationErr{
Field: fieldEvery,
Msg: "must provide if cron field is not provided",
},
validationErr{
Field: fieldTaskCron,
Msg: "must provide if every field is not provided",
},
)
}

if t.query == "" {
vErrs = append(vErrs, validationErr{
Field: fieldQuery,
Msg: "must provide a non zero value",
})
}

if status := t.Status(); status != influxdb.Active && status != influxdb.Inactive {
vErrs = append(vErrs, validationErr{
Field: fieldStatus,
Msg: "must be 1 of [active, inactive]",
})
}
return vErrs
}

const (
fieldTelegrafConfig = "config"
)
Expand Down Expand Up @@ -2530,6 +2624,13 @@ func toNotificationDuration(dur time.Duration) *notification.Duration {
return &d
}

func durToStr(dur time.Duration) string {
if dur == 0 {
return ""
}
return dur.String()
}

func flt64Ptr(f float64) *float64 {
if f != 0 {
return &f
Expand Down
90 changes: 66 additions & 24 deletions pkger/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ type Pkg struct {
mDashboards []*dashboard
mNotificationEndpoints map[string]*notificationEndpoint
mNotificationRules []*notificationRule
mTasks map[string]*task
mTelegrafs []*telegraf
mVariables map[string]*variable

Expand Down Expand Up @@ -179,6 +180,10 @@ func (p *Pkg) Summary() Summary {
sum.NotificationRules = append(sum.NotificationRules, r.summarize())
}

for _, t := range p.tasks() {
sum.Tasks = append(sum.Tasks, t.summarize())
}

for _, t := range p.telegrafs() {
sum.TelegrafConfigs = append(sum.TelegrafConfigs, t.summarize())
}
Expand Down Expand Up @@ -310,6 +315,17 @@ func (p *Pkg) secrets() map[string]bool {
return secrets
}

func (p *Pkg) tasks() []*task {
tasks := make([]*task, 0, len(p.mTasks))
for _, t := range p.mTasks {
tasks = append(tasks, t)
}

sort.Slice(tasks, func(i, j int) bool { return tasks[i].Name() < tasks[j].Name() })

return tasks
}

func (p *Pkg) telegrafs() []*telegraf {
teles := p.mTelegrafs[:]
sort.Slice(teles, func(i, j int) bool { return teles[i].Name() < teles[j].Name() })
Expand Down Expand Up @@ -437,6 +453,7 @@ func (p *Pkg) graphResources() error {
p.graphDashboards,
p.graphNotificationEndpoints,
p.graphNotificationRules,
p.graphTasks,
p.graphTelegrafs,
}

Expand Down Expand Up @@ -732,37 +749,28 @@ func (p *Pkg) graphNotificationRules() *parseErr {
})
}

func (p *Pkg) graphVariables() *parseErr {
p.mVariables = make(map[string]*variable)
return p.eachResource(KindVariable, 1, func(r Resource) []validationErr {
if _, ok := p.mVariables[r.Name()]; ok {
return []validationErr{{
Field: "name",
Msg: "duplicate name: " + r.Name(),
}}
}

newVar := &variable{
func (p *Pkg) graphTasks() *parseErr {
p.mTasks = make(map[string]*task)
return p.eachResource(KindTask, 1, func(r Resource) []validationErr {
t := &task{
name: r.Name(),
Description: r.stringShort(fieldDescription),
Type: normStr(r.stringShort(fieldType)),
Query: strings.TrimSpace(r.stringShort(fieldQuery)),
Language: normStr(r.stringShort(fieldLanguage)),
ConstValues: r.slcStr(fieldValues),
MapValues: r.mapStrStr(fieldValues),
cron: r.stringShort(fieldTaskCron),
description: r.stringShort(fieldDescription),
every: r.durationShort(fieldEvery),
offset: r.durationShort(fieldOffset),
query: strings.TrimSpace(r.stringShort(fieldQuery)),
status: normStr(r.stringShort(fieldStatus)),
}

failures := p.parseNestedLabels(r, func(l *label) error {
newVar.labels = append(newVar.labels, l)
p.mLabels[l.Name()].setMapping(newVar, false)
//p.mLabels[l.Name()].setVariableMapping(newVar, false)
t.labels = append(t.labels, l)
p.mLabels[l.Name()].setMapping(t, false)
return nil
})
sort.Sort(newVar.labels)

p.mVariables[r.Name()] = newVar
sort.Sort(t.labels)

return append(failures, newVar.valid()...)
p.mTasks[r.Name()] = t
return append(failures, t.valid()...)
})
}

Expand Down Expand Up @@ -794,6 +802,40 @@ func (p *Pkg) graphTelegrafs() *parseErr {
})
}

func (p *Pkg) graphVariables() *parseErr {
p.mVariables = make(map[string]*variable)
return p.eachResource(KindVariable, 1, func(r Resource) []validationErr {
if _, ok := p.mVariables[r.Name()]; ok {
return []validationErr{{
Field: "name",
Msg: "duplicate name: " + r.Name(),
}}
}

newVar := &variable{
name: r.Name(),
Description: r.stringShort(fieldDescription),
Type: normStr(r.stringShort(fieldType)),
Query: strings.TrimSpace(r.stringShort(fieldQuery)),
Language: normStr(r.stringShort(fieldLanguage)),
ConstValues: r.slcStr(fieldValues),
MapValues: r.mapStrStr(fieldValues),
}

failures := p.parseNestedLabels(r, func(l *label) error {
newVar.labels = append(newVar.labels, l)
p.mLabels[l.Name()].setMapping(newVar, false)
//p.mLabels[l.Name()].setVariableMapping(newVar, false)
return nil
})
sort.Sort(newVar.labels)

p.mVariables[r.Name()] = newVar

return append(failures, newVar.valid()...)
})
}

func (p *Pkg) eachResource(resourceKind Kind, minNameLen int, fn func(r Resource) []validationErr) *parseErr {
var pErr parseErr
for i, r := range p.Spec.Resources {
Expand Down
Loading

0 comments on commit 56490ef

Please sign in to comment.