Esempio n. 1
0
func (p *ProcessPlugin) performScriptTask(j *job.Job) (string, error) {
	if len(p.args) > 0 {
		j.Debugf("Executing `%s` with arguments %#v", p.path, p.args)
	} else {
		j.Debugf("Executing `%s` with no arguments", p.path)
	}

	out, err := exec.Command(p.path, p.args...).Output()

	return string(out), err
}
func (p *CrittercismPlugin) PostSumOfGraph(job *job.Job, path, name string, interval int, f *gotelemetry.Flow) {
	err := p.api.FetchSumOfGraphIntoFlow(path, name, interval, f)

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

	job.PostFlowUpdate(f)
	job.Logf("Updated flow %s", f.Tag)
}
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)
}
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)
}
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))
}
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)
}
Esempio n. 7
0
func (p *ProcessPlugin) performHTTPTask(j *job.Job) (string, error) {
	j.Debugf("Retrieving expression from URL `%s`", p.url)

	r, err := http.Get(p.url)

	if err != nil {
		return "", err
	}

	defer r.Body.Close()

	out, err := ioutil.ReadAll(r.Body)

	if r.StatusCode > 399 {
		return string(out), gotelemetry.NewErrorWithFormat(r.StatusCode, "HTTP request failed with status %d", nil, r.StatusCode)
	}

	return string(out), nil
}
Esempio n. 8
0
func (p *ProcessPlugin) performDataUpdate(j *job.Job, flowTag string, isReplace bool, data map[string]interface{}) {

	if config.CLIConfig.DebugMode == true {
		// Debug Mode. Print data dump. Do not send API update
		jsonOutput, err := json.MarshalIndent(data, "", "  ")

		if err != nil {
			return
		}

		fmt.Printf("\nPrinting the output results of \"%s\":\n", flowTag)
		fmt.Println(string(jsonOutput))
		return
	}

	if isReplace {
		if p.expiration > 0 {
			newExpiration := time.Now().Add(p.expiration)
			newUnixExpiration := newExpiration.Unix()

			j.Debugf("Forcing expiration to %d (%s)", newUnixExpiration, newExpiration)

			data["expires_at"] = newUnixExpiration
		}

		j.QueueDataUpdate(flowTag, data, gotelemetry.BatchTypePOST)
	} else {
		if p.expiration > 0 {
			newExpiration := time.Now().Add(p.expiration)
			newUnixExpiration := newExpiration.Unix()

			j.Debugf("Forcing expiration to %d (%s)", newUnixExpiration, newExpiration)

			data["expires_at"] = newUnixExpiration
		}

		j.QueueDataUpdate(flowTag, data, gotelemetry.BatchTypePATCH)
	}
}
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)
}
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)
}
Esempio n. 11
0
func (p *ProcessPlugin) performAllTasks(j *job.Job) {
	j.Debugf("Starting process plugin...")

	defer p.PluginHelper.TrackTime(j, time.Now(), "Process plugin completed in %s.")

	var response string
	var err error

	if p.path != "" {
		response, err = p.performScriptTask(j)
	} else if p.templateFile != "" {
		response, err = p.performTemplateTask(j)
	} else if p.url != "" {
		response, err = p.performHTTPTask(j)
	} else {
		err = errors.New("Nothing to do!")
	}

	if err != nil {
		if p.flowTag != "" {
			res := err.Error() + " : " + strings.TrimSpace(string(response))

			if res == "" {
				res = "No output detected."
			}

			j.SetFlowError(p.flowTag, map[string]interface{}{"message": res})
		}

		j.ReportError(err)
		return
	}

	j.Debugf("Process output: %s", strings.Replace(response, "\n", "\\n", -1))

	if p.flowTag != "" {
		j.Debugf("Posting flow %s", p.flowTag)
	}

	if err := p.analyzeAndSubmitProcessResponse(j, response); err != nil {
		j.ReportError(errors.New("Unable to analyze process output: " + err.Error()))
	}
}
Esempio n. 12
0
func (p *ProcessPlugin) Init(job *job.Job) error {
	c := job.Config()

	job.Debugf("The configuration is %#v", c)

	var ok bool

	p.flowTag, ok = c["flow_tag"].(string)

	if !ok {
		p.flowTag, _ = c["tag"].(string)
	}

	p.batch, ok = c["batch"].(bool)

	if ok && p.flowTag != "" {
		return errors.New("You cannot specify both `flow_tag` and `batch` properties.")
	}

	exec, _ := c["exec"].(string)
	script, _ := c["script"].(string)

	if exec != "" && script != "" {
		return errors.New("You cannot specify both `script` and `exec` properties.")
	}

	if exec != "" {
		p.path = exec
	} else if script != "" {
		p.path = script
	}

	p.url, _ = c["url"].(string)

	if p.path == "" && p.url == "" {
		return errors.New("You must specify a `script`, `exec`, or `url` property.")
	}

	if p.path != "" && p.url != "" {
		return errors.New("You cannot provide both `script` or `exec` and `url` properties.")
	}

	p.args = []string{}
	p.scriptArgs = map[string]interface{}{}

	if args, ok := c["args"].([]interface{}); ok {
		for _, arg := range args {
			if a, ok := arg.(string); ok {
				p.args = append(p.args, a)
			} else {
				p.args = append(p.args, fmt.Sprintf("%#v", arg))
			}
		}
	} else if args, ok := c["args"].(map[interface{}]interface{}); ok {
		p.scriptArgs = config.MapTemplate(args).(map[string]interface{})
	} else if args, ok := c["args"].(map[string]interface{}); ok {
		p.scriptArgs = args
	}

	if p.path != "" {
		if _, err := os.Stat(p.path); os.IsNotExist(err) {
			return errors.New("File " + p.path + " does not exist.")
		}

		if path.Ext(p.path) == ".lua" {
			p.url = "tpl://" + p.path
			p.path = ""
		} else {
			if len(p.scriptArgs) != 0 {
				return errors.New("You cannot specify an key/value hash of arguments when executing an external process. Provide an array of arguments instead.")
			}
		}
	}

	if p.url != "" {
		if len(p.args) != 0 {
			return errors.New("You cannot specify an array of arguments when executing a template. Provide a key/value hash instead.")
		}

		if strings.HasPrefix(p.url, "tpl://") {
			URL, err := url.Parse(p.url)

			if err != nil {
				return err
			}

			p.templateFile = URL.Host + URL.Path

			if _, err := os.Stat(p.templateFile); os.IsNotExist(err) {
				return errors.New("Template " + p.templateFile + " does not exist.")
			}
		}
	}

	template, templateOK := c["template"]
	variant, variantOK := c["variant"].(string)

	if variantOK && templateOK {
		if p.flowTag == "" {
			return errors.New("The required `flow_tag` property (`string`) is either missing or of the wrong type.")
		}

		if f, err := job.GetOrCreateFlow(p.flowTag, variant, template); err != nil {
			return err
		} else {
			p.flow = f
		}
	}

	if interval, ok := c["interval"].(int); ok {
		p.expiration = time.Duration(interval*3) * time.Second

		p.PluginHelper.AddTaskWithClosure(p.performAllTasks, time.Duration(interval)*time.Second)
	} else if interval, ok := c["interval"].(string); ok {
		if timeInterval, err := config.ParseTimeInterval(interval); err == nil {
			if p.expiration == 0 {
				p.expiration = timeInterval * 3.0
			}

			p.PluginHelper.AddTaskWithClosure(p.performAllTasks, timeInterval)
		} else {
			return err
		}
	} else {
		p.PluginHelper.AddTaskWithClosure(p.performAllTasks, 0)
	}

	if p.expiration > 0 && p.expiration < time.Second*60 {
		p.expiration = time.Second * 60
	}

	switch c["expiration"].(type) {
	case int:
		p.expiration = time.Duration(c["expiration"].(int)) * time.Second

	case int64:
		p.expiration = time.Duration(c["expiration"].(int64)) * time.Second

	case string:
		if timeInterval, err := config.ParseTimeInterval(c["expiration"].(string)); err == nil {
			p.expiration = timeInterval
		} else {
			return errors.New("Invalid expiration value. Must be either a number of seconds or a time interval string.")
		}
	}

	if p.expiration < 0 {
		return errors.New("Invalid expiration time")
	}

	if p.expiration > 0 {
		job.Debugf("Expiration is set to %dµs", p.expiration)
	} else {
		job.Debugf("Expiration is off.")
	}

	return nil
}