예제 #1
0
//运行计算
func (this *TimeCache) Run() {
	log.Infoln("cache is started", this.Id)
	period := 1
	if p, ok := Get(FormatKey("System", "Period", this.Id)); ok {
		period = compute.AsInt(p)
	}
	timeFormat := common.GetTimeFormat(period)

	for rs := range this.input { //接收数据
		//log.Infof("time changed ", rs.Time)
		currTime, err := time.Parse(timeFormat, rs.Time)
		if err != nil {
			log.Error("cache time format is error", err.Error())
			currTime = time.Now()
		}
		cacheItem, ok := this.caches[rs.Time] //这个时间点是否已记录
		if !ok {
			cacheItem = &TimeCacheItem{Cache: make(map[string]interface{})}
			this.caches[rs.Time] = cacheItem
			cacheItem.ExpiredTime = time.Now().Add(time.Minute * common.StoreMinute)
		}

		for _, vs := range rs.Rows { //分析每一条数据
			row := rs.CreateDataRow(vs)
			key := row.GetKey(this.KeyName)
			//log.Infoln("add to cache", key)
			this.lock.Lock()
			if r, ok := cacheItem.Cache[key]; ok { //如果同类数据已存在,就进行数据合并
				row.Merge(r.(*common.DataRow).Row)
			}
			cacheItem.Cache[key] = row
			this.lock.Unlock()
			//log.Infoln(key, row.Row)
			//	log.Info("key=%s", key)
			go saveToStore(FormatStoreKey(this.Id, key), row, period*common.StoreCount) //保存到外部存贮,并保存粒度*保存个数分钟
		}

		//通知外到有新数据到来
		if TimeChanged != nil {
			TimeChanged(&common.TimeMessage{Key: this.Id, Time: currTime})
		}
	}
}
예제 #2
0
//时间线,单指标,单网元
//查询一段时间内指定网元,某指标的走势
func GetTimeLineData(dp *common.DataParam, t time.Time) *common.OutputParam {
	tmp := []*common.DataRow{}
	//log.Info("timeline @", dp.Id, t)
	period := 1
	if p, ok := cache.Get(cache.FormatKey("System", "Period", dp.DataKey)); ok {
		period = compute.AsInt(p)
	}
	for i := 0; i < dp.TimeRange; i++ { //取出所有的数据
		tmpTime := t.Add(time.Minute * time.Duration(-i))
		//		key := cache.FormatKey(tmpTime.Format(common.GetTimeFormat(period)), dp.DataKey, dp.LimitId[0])
		//log.Info("search key is ", key)
		if r, ok := cache.GetDataRow(dp.DataKey, tmpTime.Format(common.GetTimeFormat(period)), dp.LimitId[0]); ok {
			tmp = append(tmp, r)
			//log.Infoln("find cache", r.Row)
		}
	}
	//log.Info("result1")
	//排序
	sort.Sort(common.ByTime{tmp})
	//log.Info("result2")
	result := new(common.OutputParam)
	if len(tmp) > 0 {
		result.RowMap = make(map[string]int)
		for i, k := range dp.OutputKey {
			result.RowMap[k] = i
		}
		result.Time = t
	}
	//log.Info("result3")
	//限制输出
	//start := 0
	//if len(tmp) >= dp.DataRange {
	//	start = len(tmp) - dp.DataRange
	//}
	//for i := start; i < len(tmp); i++ {
	//	result.Rows = append(result.Rows, createRow(dp, tmp[i]))
	//}
	for _, r := range tmp {
		result.Rows = append(result.Rows, createRow(dp, r))
	}
	//log.Info("result")
	return result
}
예제 #3
0
//单时间点,单指标,多网元
//主要查多个网元指定时间的排序
func GetOrderData(dp *common.DataParam, t time.Time) *common.OutputParam {
	tmp := []*common.DataRow{}
	period := 1
	if p, ok := cache.Get(cache.FormatKey("System", "Period", dp.DataKey)); ok {
		period = compute.AsInt(p)
	}
	//	limit := []string{}
	if len(dp.LimitNodeId) > 0 { //双重限制,与LimitId同时起作用

	} else {
		for _, v := range dp.LimitId { //取出所有的数据
			//			key := cache.FormatKey(t.Format(common.GetTimeFormat(period)), dp.DataKey, v)
			if r, ok := cache.GetDataRow(dp.DataKey, t.Format(common.GetTimeFormat(period)), v); ok { //都是最新一个时间点的,都在内存
				tmp = append(tmp, r)
			}
		}
	}
	//排序
	sort.Sort(common.ByKey{tmp, dp.OrderKey})
	//限制输出
	result := new(common.OutputParam)
	if len(tmp) > 0 {
		result.RowMap = make(map[string]int)
		for i, k := range dp.OutputKey {
			result.RowMap[k] = i
		}
		result.Time = time.Now()
	}
	if dp.OrderBy == common.Desc {
		for i := len(tmp); i > 0 && i > len(tmp)-dp.DataRange; i-- {
			result.Rows = append(result.Rows, createRow(dp, tmp[i-1]))
		}
	} else {
		for i := 0; i < len(tmp) && i < dp.DataRange; i++ {
			result.Rows = append(result.Rows, createRow(dp, tmp[i]))
		}
	}
	return result
}
예제 #4
0
//单时间点,多指标,单网元
//取一个网元指定时间的多指标,主要用于查询某网元的详细指标
func GetColumnsData(dp *common.DataParam, t time.Time) *common.OutputParam {
	tmp := []*common.DataRow{}
	period := 1
	if p, ok := cache.Get(cache.FormatKey("System", "Period", dp.DataKey)); ok {
		period = compute.AsInt(p)
	}
	//key := cache.FormatKey(t.Format(common.GetTimeFormat(period)), dp.DataKey, dp.LimitId[0])
	//log.Info("id=%s", key)
	if r, ok := cache.GetDataRow(dp.DataKey, t.Format(common.GetTimeFormat(period)), dp.LimitId[0]); ok {
		tmp = append(tmp, r)
	}

	//限制输出
	result := new(common.OutputParam)
	if len(tmp) > 0 {
		result.RowMap = make(map[string]int)
		for i, k := range dp.OutputKey {
			result.RowMap[k] = i
		}
		result.Time = time.Now()
		result.Rows = append(result.Rows, createRow(dp, tmp[0]))
	}
	return result
}