Ejemplo n.º 1
0
func RegisterPlugin(name string, factory PluginFactory) {
	if _, exists := manager.factories[name]; exists {
		pluginErrors = append(pluginErrors, gotelemetry.NewError(500, "Duplicate plugin name `"+name+"`"))
		return
	}

	manager.factories[name] = factory
}
Ejemplo n.º 2
0
func (m *JobManager) addJob(job *Job) error {
	if _, found := m.jobs[job.ID]; found {
		return gotelemetry.NewError(500, "Duplicate job `"+job.ID+"`")
	}

	m.jobs[job.ID] = job

	return nil
}
Ejemplo n.º 3
0
func (p *CrittercismPlugin) SetDate(job *job.Job, f *gotelemetry.Flow) {
	data, found := f.TextData()

	if !found {
		job.ReportError(gotelemetry.NewError(400, "Cannot extract text data from flow"+f.Tag))
		return
	}

	data.Text = time.Now().Format("Monday, January 2")

	job.PostFlowUpdate(f)
	job.Logf("Set app name to flow %s", f.Tag)
}
Ejemplo n.º 4
0
func (p *CrittercismPlugin) SetAppName(job *job.Job, f *gotelemetry.Flow) {
	data, found := f.TextData()

	if !found {
		job.ReportError(gotelemetry.NewError(400, "Cannot extract text data from flow"+f.Tag))
		return
	}

	data.Text = p.appName

	job.PostFlowUpdate(f)
	job.Logf("Set app name to flow %s", f.Tag)
}
Ejemplo n.º 5
0
func (c *CrittercismAPIClient) FetchSumOfGraphIntoFlow(path, name string, interval int, f *gotelemetry.Flow) error {
	if data, found := f.ValueData(); found == true {
		value, err := c.FetchSumOfGraph(path, name, interval)

		if err != nil {
			return err
		}

		data.Value = value

		return nil
	}

	return gotelemetry.NewError(400, "Cannot extract value data from flow"+f.Tag)
}
Ejemplo n.º 6
0
// Adds a task associated with a flow taken from a map of flows. You can obtain a map of flows by calling
// the MapWidgetsToFlows() method of gotelemetry.Board.
func (e *PluginHelper) AddTaskWithClosureForFlowWithTag(c PluginHelperClosureWithFlow, interval time.Duration, flows map[string]*gotelemetry.Flow, tag string) error {
	f, found := flows[tag]

	if !found {
		return gotelemetry.NewError(400, "Flow "+tag+" not found.")
	}

	closure := func(job *Job) {
		c(job, f)
	}

	e.AddTaskWithClosure(closure, interval)

	return nil
}
Ejemplo n.º 7
0
func (p *CrittercismPlugin) PostGraphToBarchart(job *job.Job, path, name, groupBy string, interval int, f *gotelemetry.Flow) {
	if data, found := f.BarchartData(); found == true {
		jq, err := p.api.FetchGraphRaw(path, name, groupBy, interval)

		if err != nil {
			job.ReportError(err)
			return
		}

		slices, err := jq.ArrayOfObjects("data", "slices")

		if err != nil {
			job.ReportError(err)
			return
		}

		bars := []gotelemetry.BarchartBar{}

		count := 10

		for _, slice := range slices {
			bar := gotelemetry.BarchartBar{}

			bar.Color = "#267288"
			bar.Label = slice["label"].(string)
			bar.Value = slice["value"].(float64)

			bars = append(bars, bar)

			count -= 1

			if count == 0 {
				break
			}
		}

		data.Bars = bars

		job.PostFlowUpdate(f)
		job.Logf("Updated flow %s", f.Tag)

		return
	}

	job.ReportError(gotelemetry.NewError(400, "Cannot extract barchart data from flow"+f.Tag))
}
Ejemplo n.º 8
0
func (p *CrittercismPlugin) AppStoreRatings(job *job.Job, f *gotelemetry.Flow) {
	jq, err := p.api.Request("GET", "apps?attributes=appType,rating", nil)

	if err != nil {
		job.ReportError(err)
		return
	}

	source, err := jq.Object()

	if err != nil {
		job.ReportError(err)
		return
	}

	data, found := f.ValueData()

	if !found {
		job.ReportError(gotelemetry.NewError(400, "Cannot extract value data from flow"+f.Tag))
		return
	}

	if appObj, ok := source[p.appId]; ok {
		app := appObj.(map[string]interface{})

		data.Value = app["rating"].(float64)

		switch p.ratingKey {
		case "ios":
			data.Icon = "fa-apple"

		case "android":
			data.Icon = "fa-android"

		case "wp":
			data.Icon = "fa-windows"

		case "html5":
			data.Icon = "fa-html5"
		}
	}

	job.PostFlowUpdate(f)
	job.Logf("Updated flow %s", f.Tag)
}
Ejemplo n.º 9
0
func (j *Job) SendNotification(notification gotelemetry.Notification, channelTag string, flowTag string) bool {
	var err error

	if len(channelTag) > 0 {
		channel := gotelemetry.NewChannel(channelTag)
		err = channel.SendNotification(j.credentials, notification)
	} else if len(flowTag) > 0 {
		err = gotelemetry.SendFlowChannelNotification(j.credentials, flowTag, notification)
	} else {
		err = gotelemetry.NewError(http.StatusBadRequest, "Either channel or flow is required")
	}

	if err != nil {
		j.ReportError(err)
		return true
	}

	return false
}
Ejemplo n.º 10
0
func (c *CrittercismAPIClient) FetchGraphIntoFlow(path, name string, duration int, scale int, f *gotelemetry.Flow) error {
	if data, found := f.GraphData(); found == true {
		series, err := c.FetchGraph(path, name, duration)

		if err != nil {
			return err
		}

		// Eliminate last value
		if len(series) > 1 {
			series = series[:len(series)-1]
		}

		data.Series[0].Values = series
		data.StartTime = time.Now().Add(-time.Duration(scale) * time.Second).Unix()

		return nil
	}

	return gotelemetry.NewError(400, "Cannot extract value data from flow"+f.Tag)
}
Ejemplo n.º 11
0
func ProcessNotificationRequest(configFile *config.ConfigFile, errorChannel chan error, completionChannel chan bool, notificationChannel string, notificationFlow string, notification gotelemetry.Notification) {
	errorChannel <- gotelemetry.NewLogError("Notification mode is on.")

	apiToken, err := configFile.APIToken()

	if err != nil {
		errorChannel <- err
		completionChannel <- true

		return
	}

	credentials, err := gotelemetry.NewCredentials(apiToken)

	if err != nil {
		errorChannel <- err
		completionChannel <- true

		return
	}

	credentials.SetDebugChannel(errorChannel)

	if len(notificationChannel) > 0 {
		channel := gotelemetry.NewChannel(notificationChannel)
		err = channel.SendNotification(credentials, notification)
	} else if len(notificationFlow) > 0 {
		err = gotelemetry.SendFlowChannelNotification(credentials, notificationFlow, notification)
	} else {
		err = gotelemetry.NewError(http.StatusBadRequest, "Either channel or flow is required")
	}

	if err != nil {
		errorChannel <- err
	} else {
		errorChannel <- gotelemetry.NewLogError("Notification sent successfully.")
	}

	completionChannel <- true
}
Ejemplo n.º 12
0
func (p *CrittercismPlugin) DailyMonthlyLoadsUsers(job *job.Job, f *gotelemetry.Flow) {
	dau, err := p.api.FetchLastValueOfGraph("errorMonitoring/graph", "dau", 1440)

	if err != nil {
		job.ReportError(err)
		return
	}

	mau, err := p.api.FetchLastValueOfGraph("errorMonitoring/graph", "mau", 86400)

	if err != nil {
		job.ReportError(err)
		return
	}

	loads, err := p.api.FetchLastValueOfGraph("errorMonitoring/graph", "appLoads", 1440)

	if err != nil {
		job.ReportError(err)
		return
	}

	data, success := f.MultivalueData()

	if !success {
		job.ReportError(gotelemetry.NewError(400, "Cannot extract multivalue data from flow"+f.Tag))
		return
	}

	data.Values[0].Value = loads
	data.Values[1].Value = dau
	data.Values[2].Value = mau

	job.PostFlowUpdate(f)
	job.Logf("Updated flow %s", f.Tag)
}
Ejemplo n.º 13
0
func (p *CrittercismPlugin) DailyMostFrequentCrashes(job *job.Job, f *gotelemetry.Flow) {
	data, found := f.TableData()

	if !found {
		job.ReportError(gotelemetry.NewError(400, "Cannot extract table data from flow"+f.Tag))
	}

	crashes, err := p.api.FetchCrashStatus()

	if err != nil {
		job.ReportError(err)
		return
	}

	crashes = crashes.Aggregate()

	cells := [][]gotelemetry.TableCell{}

	var count = 8

	for _, crash := range crashes {
		name := ""

		if crash.Reason != "" {
			name = crash.Reason
		} else if crash.DisplayReason != nil {
			name = *crash.DisplayReason
		} else if crash.Name != nil {
			name = *crash.Name
		} else {
			name = "N/A (" + crash.Reason + ")"
		}

		if len(name) > tableDataLength {
			name = name[:tableDataLength-1]
		}

		cells = append(
			cells,
			[]gotelemetry.TableCell{
				gotelemetry.TableCell{Value: name},
				gotelemetry.TableCell{Value: crash.SessionCount},
			},
		)

		count -= 1

		if count == 0 {
			break
		}
	}

	for count > 0 {
		cells = append(
			cells,
			[]gotelemetry.TableCell{
				gotelemetry.TableCell{Value: ""},
				gotelemetry.TableCell{Value: ""},
			},
		)

		count -= 1
	}

	data.Cells = cells

	job.PostFlowUpdate(f)
	job.Logf("Updated flow %s", f.Tag)
}
Ejemplo n.º 14
0
func NewJobManager(jobConfig config.ConfigInterface, errorChannel chan error, completionChannel chan bool) (*JobManager, error) {
	result := &JobManager{
		jobs:                 map[string]*Job{},
		completionChannel:    completionChannel,
		jobCompletionChannel: make(chan string),
	}

	apiToken, err := jobConfig.APIToken()

	if err != nil {
		return nil, err
	}

	credentials, err := gotelemetry.NewCredentials(apiToken, jobConfig.APIURL())

	if err != nil {
		return nil, err
	}

	credentials.SetDebugChannel(errorChannel)

	result.credentials = credentials

	submissionInterval := jobConfig.SubmissionInterval()

	if submissionInterval < time.Second {
		errorChannel <- gotelemetry.NewLogError("Submission interval automatically set to 1s. You can change this value by adding a `submission_interval` property to your configuration file.")
		submissionInterval = time.Second
	} else {
		errorChannel <- gotelemetry.NewLogError("Submission interval set to %ds", submissionInterval/time.Second)
	}

	result.accountStreams = map[string]*gotelemetry.BatchStream{}

	for _, jobDescription := range jobConfig.Jobs() {
		jobId := jobDescription.ID()

		if jobId == "" {
			return nil, gotelemetry.NewError(500, "Job ID missing and no `flow_tag` provided.")
		}

		if !config.CLIConfig.Filter.MatchString(jobId) {
			continue
		}

		if config.CLIConfig.ForceRunOnce {
			delete(jobDescription, "refresh")
		}

		channelTag := jobDescription.ChannelTag()

		accountStream, ok := result.accountStreams[channelTag]

		if !ok {
			var err error

			accountStream, err = gotelemetry.NewBatchStream(credentials, channelTag, submissionInterval, errorChannel)

			if err != nil {
				return nil, err
			}

			result.accountStreams[channelTag] = accountStream
		}

		job, err := createJob(result, credentials, accountStream, errorChannel, jobDescription, result.jobCompletionChannel, false)

		if err != nil {
			return nil, err
		}

		if err := result.addJob(job); err != nil {
			return nil, err
		}
	}

	if len(result.jobs) == 0 {
		errorChannel <- gotelemetry.NewLogError("No jobs are being scheduled.")
		return nil, nil
	}

	go result.monitorDoneChannel()

	return result, nil
}
Ejemplo n.º 15
0
// By default, the plugin helper refuses to reconfigure plugins.
func (e *PluginHelper) Reconfigure(job *Job, config map[string]interface{}) error {
	return gotelemetry.NewError(400, "This plugin cannot reconfigure itself.")
}