Exemplo n.º 1
0
// Prepared returns true if no othesamples are expected to be added
func (s Suggestion) Prepared() (bool, error) {
	s, ok := GetSuggestion(s.ID)
	if !ok {
		return false, apierrors.NewNotFound("suggestion", s.ID)
	}
	if s.Token == "" {
		return false, apierrors.NewConflict("suggestion", "", errors.New("No session token associated with suggestion"))
	}
	return s.prepared, nil
}
Exemplo n.º 2
0
// AddSample adds a sample to the local cache ready to be sent to central using SendSamples.
func (sugg *Suggestion) SendSamples(client *Client) (int, error) {
	s, ok := GetSuggestion(sugg.ID)
	if !ok {
		return 0, apierrors.NewNotFound("suggestion", sugg.ID)
	}
	if s.Token == "" {
		return 0, apierrors.NewConflict("suggestion", "", errors.New("No session token associated with suggestion"))
	}

	n := 0
	var rerr error

	for _, v := range s.samples {
		r, err := client.CreateSample(shared.StoreSampleRequest{
			Sample: &shared.Sample{
				Token:      s.Token,
				SampleType: v.sampleType,
				Data:       v.data,
				URL:        s.URL,
			},
			ClientAddr: shared.GetPublicIPAddr().String(),
		})
		if err != nil {
			rerr = errors.New("error sending sample: " + err.Error())
			continue
		}

		if !r.Ok {
			rerr = errors.New("error sending sample, not accepted")
			continue

		}
		n++
		modifySuggestion <- removeSample(s, v)
	}

	return n, rerr
}