Пример #1
0
//run service
func (this *CsvInput) Run() {
	log.Infoln("CsvInput is starting")
	log.Infof("open file %s", this.Config.FileName)

	g := float64(this.Config.Period)
	if bts, err := ioutil.ReadFile(this.Config.FileName); err == nil {
		strs := strings.Split(string(bts), "\n")
		for common.IsRun {
			beginget := time.Now()
			mod := this.TIME.Minute() % 3
			//			log.Infof("w count is %d", mod+1)
			for i := 0; i <= mod; i++ {
				row := new(common.DataRows)
				row.Time = time.Now().Format(this.Config.TimeFormat)
				row.RowMap = this.RowMap
				for _, str := range strs {
					if this.Config.OutputColumn == nil {
						log.Infoln("output column is nil")
						break
					}
					cols := strings.Split(str, ",")
					var r []interface{}
					for _, c := range cols {
						if d, err := strconv.ParseFloat(c, 10); err == nil {
							r = append(r, d)
						} else {
							r = append(r, 0)
						}
					}
					row.Rows = append(row.Rows, r)
				}
				this.Output <- row
				//log.Infof("push row is %v", row.Time)
			}
			//计算下一次运行的时间

			this.TIME = this.TIME.Add(time.Minute * time.Duration(g))
			log.Infof("next time is %v", this.TIME)
			sec := time.Since(beginget).Seconds()

			//计算下一次运行的时间
			if sec < g*60 { //如果一个处理周期内处理完,就延时,没有处理完就立即执行,如果正式运行期间还需要进行时间纠正
				log.Infof("sleep seconds %f", (g*60)-sec)
				time.Sleep(time.Second * (time.Duration(g*60 - sec))) //sleep one minute-sec
			}
		}

	} else {
		log.Error(err.Error())
	}
	close(this.Output)
}
Пример #2
0
//run service
func (this *TestInput) Run() {
	log.Infoln("TestInput is starting")
	g := float64(this.Config.Period)
	for common.IsRun {
		beginget := time.Now()
		for i := 0; i < 60; i++ {
			row := new(common.DataRows)
			row.RowMap = this.RowMap
			if this.Config.OutputColumn == nil {
				log.Infoln("output column is nil")
				break
			}
			rnd := rand.New(rand.NewSource(int64(time.Now().Nanosecond())))
			for j := 0; j < 100000; j++ {
				var r []interface{}
				if this.Config.OutputColumn.Sum != nil {
					for k := 0; k < len(this.Config.OutputColumn.Sum); k++ {
						r = append(r, rnd.Float32())
					}
				}
				if this.Config.OutputColumn.Max != nil {
					for k := 0; k < len(this.Config.OutputColumn.Max); k++ {
						r = append(r, rnd.Float32())
					}
				}
				if this.Config.OutputColumn.Min != nil {
					for k := 0; k < len(this.Config.OutputColumn.Min); k++ {
						r = append(r, rnd.Float32())
					}
				}

				if this.Config.OutputKey != nil {
					for _, k := range this.Config.OutputKey {
						var v interface{}
						if k == this.Config.TimeKey {
							v = time.Now()
						} else {
							v = i * j
						}
						r = append(r, v)
					}
				}
				row.Rows = append(row.Rows, r)
			}

			row.Time = this.TIME.Format(this.Config.TimeFormat)

			//log.Infof("testinput 正在输出数据...%v", row.Time)
			this.Output <- row

		}
		//计算下一次运行的时间

		this.TIME = this.TIME.Add(time.Minute * time.Duration(g))
		log.Infof("next time is %v", this.TIME)
		//计算时间间隔
		sec := time.Since(beginget).Seconds()

		//计算下一次运行的时间
		if sec < g*60 { //如果一个处理周期内处理完,就延时,没有处理完就立即执行,如果正式运行期间还需要进行时间纠正
			log.Infof("sleep seconds %f", (g*60)-sec)
			time.Sleep(time.Second * (time.Duration(g*60 - sec))) //sleep one minute-sec
		}

	}
	close(this.Output)
}