Exemple #1
0
func (t *Ticker) Process(ctx *core.Context, tuple *core.Tuple, w core.Writer) error {
	var i int64
	for ; atomic.LoadInt32(&t.stopped) == 0; i++ {
		newTuple := core.NewTuple(data.Map{"tick": data.Int(i)})
		if err := w.Write(ctx, newTuple); err != nil {
			return err
		}
		time.Sleep(t.interval)
	}
	return nil
}
Exemple #2
0
func (p *publicStream) GenerateStream(ctx *core.Context, w core.Writer) error {
	anaconda.SetConsumerKey(p.keys.ConsumerKey)
	anaconda.SetConsumerSecret(p.keys.ConsumerSecret)
	api := anaconda.NewTwitterApi(p.keys.AccessToken, p.keys.AccessTokenSecret)
	defer api.Close()

	s := api.PublicStreamSample(url.Values{})
	defer s.Stop()
	for twRaw := range s.C {
		tw, ok := twRaw.(anaconda.Tweet)
		if !ok { // only processes tweets
			continue
		}

		createdAt, err := tw.CreatedAtTime()
		if err != nil {
			ctx.ErrLog(err).WithField("node_type", core.NTSource).
				WithField("node_name", p.ioParams.Name).
				Error("Cannot parse created at")
			continue
		}

		// anaconda.Tweet needs to be converted to data.Map via JSON.
		js, err := json.Marshal(&tw)
		if err != nil {
			ctx.ErrLog(err).WithField("node_type", core.NTSource).
				WithField("node_name", p.ioParams.Name).
				Error("Cannot re-convert a tweet to JSON")
			continue
		}

		m := data.Map{}
		if err := json.Unmarshal(js, &m); err != nil {
			ctx.ErrLog(err).WithField("node_type", core.NTSource).
				WithField("node_name", p.ioParams.Name).
				Error("Cannot parse converted JSON")
			continue
		}

		t := core.NewTuple(m)
		t.Timestamp = createdAt
		if err := w.Write(ctx, t); err != nil {
			return err
		}
	}
	return nil
}
Exemple #3
0
func (l *LoremSource) GenerateStream(ctx *core.Context, w core.Writer) error {
	for {
		var text []string
		for l := rand.Intn(5) + 5; l > 0; l-- {
			text = append(text, Lorem[rand.Intn(len(Lorem))])
		}

		t := core.NewTuple(data.Map{
			"text": data.String(strings.Join(text, " ")),
		})
		if err := w.Write(ctx, t); err != nil {
			return err
		}

		time.Sleep(l.interval)
	}
}
Exemple #4
0
func (s *SourceCreator) GenerateStream(ctx *core.Context, w core.Writer) error {
	device := new(Device)
	//	devName := []string{"dev1", "dev2", "dev3", "dev4", "dev5"}
	//	devProb := []float64{0.4, 0.3, 0.15, 0.1, 0.05}
	devName := []string{"dev1", "dev2"}
	devProb := []float64{0.5, 0.5}
	pickDev := func() string {
		r := rand.Float64()
		for i, p := range devProb {
			if r < p {
				return devName[i]
			}
			r -= p
		}
		return devName[len(devName)-1]
	}

	//	device.MakeDevice(pickDev())
	device.num = 0
	temp := &device.sensorData[0]
	humid := &device.sensorData[1]

	for {
		device.ID = pickDev()
		device.num += 1
		temp.MakeData("temp", 0, 30)
		humid.MakeData("humid", 0, 100)

		t := core.NewTuple(data.Map{
			"deviceID": data.String(device.ID),
			"num":      data.Int(device.num),
			"time":     data.Float(float64(time.Now().Second()) + float64(time.Now().Nanosecond())/1e+9),
			temp.ID:    data.Float(float64(temp.value)),
			humid.ID:   data.Float(float64(humid.value)),
		})
		if err := w.Write(ctx, t); err != nil {
			return err
		}
		time.Sleep(s.interval)
	}
}
Exemple #5
0
// GenerateStream generates a tuple having random sentences in its field with
// information of a user.
func (s *Sentences) GenerateStream(ctx *core.Context, w core.Writer) error {
	corpus := strings.Split(strings.Replace(`lorem ipsum dolor sit amet
consectetur adipiscing elit sed do eiusmod tempor incididunt ut labore et dolore
magna aliqua ut enim ad minim veniam quis nostrud exercitation ullamco laboris
nisi ut aliquip ex ea commodo consequat duis aute irure dolor in reprehenderit
in voluptate velit esse cillum dolore eu fugiat nulla pariatur excepteur sint
occaecat cupidatat non proident sunt in culpa qui officia deserunt mollit anim
id est laborum`, "\n", " ", -1), " ")
	users := []string{"isabella", "jacob", "sophia", "ethan", "emma"}
	usersProb := []float64{0.4, 0.3, 0.15, 0.1, 0.05}
	pickName := func() string {
		r := rand.Float64()
		for i, p := range usersProb {
			if r < p {
				return users[i]
			}
			r -= p
		}
		return users[len(users)-1]
	}

	for {
		l := rand.Intn(5) + 5
		text := make([]string, l)
		l--
		for ; l >= 0; l-- {
			text[l] = corpus[rand.Intn(len(corpus))]
		}

		t := core.NewTuple(data.Map{
			"name": data.String(pickName()),
			"text": data.String(strings.Join(text, " ")),
		})
		if err := w.Write(ctx, t); err != nil {
			return err
		}

		time.Sleep(s.interval)
	}
}