Пример #1
0
func (m *mongoStore) GetUnitOfTime(uuid common.UUID) (common.UnitOfTime, error) {
	item, err := m.uotCache.Fetch(string(uuid), m.cacheExpiry, func() (uot interface{}, err error) {
		var (
			res interface{}
			c   int
		)
		uot = common.UOT_S
		query := m.metadata.Find(bson.M{"uuid": uuid}).Select(bson.M{"Properties.UnitofTime": 1})
		if c, err = query.Count(); err != nil {
			return
		} else if c == 0 {
			err = fmt.Errorf("no stream named %v", uuid)
			return
		}
		err = query.One(&res)
		if props, found := res.(bson.M)["Properties"]; found {
			if entry, found := props.(bson.M)["UnitofTime"]; found {
				if uotInt, isInt := entry.(int); isInt {
					uot = common.UnitOfTime(uotInt)
				} else {
					err = fmt.Errorf("Invalid UnitOfTime retrieved? %v", entry)
					return
				}
				uot = common.UnitOfTime(entry.(int))
				if uot == 0 {
					uot = common.UOT_S
				}
			}
		}
		return
	})
	if item != nil && err == nil {
		if item.Value().(common.UnitOfTime) == 0 {
			return common.UOT_S, err
		}
		return item.Value().(common.UnitOfTime), err
	}
	return common.UOT_S, err
}
Пример #2
0
func (a *Archiver) packStatsResults(params *common.DataParams, readings []common.StatisticalNumbersResponse) common.SmapMessageList {
	var result = common.SmapMessageList{}
	for _, resp := range readings {
		if len(resp.Readings) > 0 {
			msg := &common.SmapMessage{UUID: resp.UUID}
			for _, rdg := range resp.Readings {
				rdg.ConvertTime(common.UnitOfTime(params.ConvertToUnit))
				msg.Readings = append(msg.Readings, rdg)
			}
			// apply data limit if exists
			if params.DataLimit > 0 && len(msg.Readings) > params.DataLimit {
				msg.Readings = msg.Readings[:params.DataLimit]
			}
			result = append(result, msg)
		}
	}
	log.Debugf("Returning %d readings", len(result))
	return result
}
Пример #3
0
func getProperties(msg map[string]interface{}) (properties *common.SmapProperties, err error) {
	properties = &common.SmapProperties{}
	if prop, found := msg["Properties"]; found {
		if propmap, ok := prop.(map[string]interface{}); ok {
			// UnitofTime
			if uot, found := propmap["UnitofTime"]; found {
				uotstr, ok := uot.(string)
				if !ok {
					err = errors.New("UnitofTime was not string")
				} else {
					var uot common.UnitOfTime
					uot, err = common.ParseUOT(uotstr)
					properties.UnitOfTime = common.UnitOfTime(uot)
				}
			}
			// UnitofMeasure
			if uom, found := propmap["UnitofMeasure"]; found {
				uomstr, ok := uom.(string)
				if !ok {
					err = errors.New("UnitofMeasure was not string")
				} else {
					properties.UnitOfMeasure = uomstr
				}
			}
			// StreamType
			if st, found := propmap["StreamType"]; found {
				ststr, ok := st.(string)
				if !ok || (ststr != "numeric" && ststr != "object") {
					err = errors.New("StreamType was not 'numeric' or 'object'")
				} else if ststr == "numeric" {
					properties.StreamType = common.NUMERIC_STREAM
				} else if ststr == "object" {
					properties.StreamType = common.OBJECT_STREAM
				}
			}
		}
		return properties, nil
	}
	return properties, PropertiesNotFound
}