当前位置: 首页>>代码示例>>Golang>>正文


Golang GetCollectorByNameQuery.Result方法代码示例

本文整理汇总了Golang中github.com/grafana/grafana/pkg/models.GetCollectorByNameQuery.Result方法的典型用法代码示例。如果您正苦于以下问题:Golang GetCollectorByNameQuery.Result方法的具体用法?Golang GetCollectorByNameQuery.Result怎么用?Golang GetCollectorByNameQuery.Result使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/grafana/grafana/pkg/models.GetCollectorByNameQuery的用法示例。


在下文中一共展示了GetCollectorByNameQuery.Result方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: GetCollectorByName

func GetCollectorByName(query *m.GetCollectorByNameQuery) error {
	sess := x.Table("collector")
	rawParams := make([]interface{}, 0)
	rawSql := `SELECT
		GROUP_CONCAT(DISTINCT(collector_tag.tag)) as tags,
		collector.*
	FROM collector
	LEFT JOIN collector_tag ON collector.id = collector_tag.collector_id AND collector_tag.org_id=?
	WHERE
		(collector.public=1 OR collector.org_id=?)
	AND
		collector.name=?
	GROUP BY collector.id
	`
	rawParams = append(rawParams, query.OrgId, query.OrgId, query.Name)
	results := make([]CollectorWithTag, 0)
	err := sess.Sql(rawSql, rawParams...).Find(&results)
	if err != nil {
		return err
	}
	if len(results) < 1 {
		return m.ErrCollectorNotFound
	}

	result := results[0]

	tags := make([]string, 0)
	if result.Tags != "" {
		tags = strings.Split(result.Tags, ",")
	}

	query.Result = &m.CollectorDTO{
		Id:            result.Id,
		OrgId:         result.OrgId,
		Name:          result.Name,
		Slug:          result.Slug,
		Tags:          tags,
		Latitude:      result.Latitude,
		Longitude:     result.Longitude,
		Public:        result.Public,
		Online:        result.Online,
		OnlineChange:  result.OnlineChange,
		Enabled:       result.Enabled,
		EnabledChange: result.EnabledChange,
	}

	return err
}
开发者ID:0x20h,项目名称:grafana,代码行数:48,代码来源:collector.go

示例2: register

func register(so socketio.Socket) (*CollectorContext, error) {
	req := so.Request()
	req.ParseForm()
	keyString := req.Form.Get("apiKey")
	name := req.Form.Get("name")
	if name == "" {
		return nil, errors.New("collector name not provided.")
	}

	lastSocketId := req.Form.Get("lastSocketId")

	versionStr := req.Form.Get("version")
	if versionStr == "" {
		return nil, errors.New("version number not provided.")
	}
	versionParts := strings.SplitN(versionStr, ".", 2)
	if len(versionParts) != 2 {
		return nil, errors.New("could not parse version number")
	}
	versionMajor, err := strconv.ParseInt(versionParts[0], 10, 64)
	if err != nil {
		return nil, errors.New("could not parse version number")
	}
	versionMinor, err := strconv.ParseFloat(versionParts[1], 64)
	if err != nil {
		return nil, errors.New("could not parse version number.")
	}

	//--------- set required version of collector.------------//
	//
	if versionMajor < 0 || versionMinor < 1.1 {
		return nil, errors.New("invalid collector version. Please upgrade.")
	}
	//
	//--------- set required version of collector.------------//
	log.Info("collector %s with version %d.%f connected", name, versionMajor, versionMinor)
	if keyString != "" {
		// base64 decode key
		decoded, err := apikeygen.Decode(keyString)
		if err != nil {
			return nil, m.ErrInvalidApiKey
		}
		// fetch key
		keyQuery := m.GetApiKeyByNameQuery{KeyName: decoded.Name, OrgId: decoded.OrgId}
		if err := bus.Dispatch(&keyQuery); err != nil {
			return nil, m.ErrInvalidApiKey
		}
		apikey := keyQuery.Result

		// validate api key
		if !apikeygen.IsValid(decoded, apikey.Key) {
			return nil, m.ErrInvalidApiKey
		}
		// lookup collector
		colQuery := m.GetCollectorByNameQuery{Name: name, OrgId: apikey.OrgId}
		if err := bus.Dispatch(&colQuery); err != nil {
			//collector not found, so lets create a new one.
			colCmd := m.AddCollectorCommand{
				OrgId:   apikey.OrgId,
				Name:    name,
				Enabled: true,
			}
			if err := bus.Dispatch(&colCmd); err != nil {
				return nil, err
			}
			colQuery.Result = colCmd.Result
		}

		sess := &CollectorContext{
			SignedInUser: &m.SignedInUser{
				IsGrafanaAdmin: apikey.IsAdmin,
				OrgRole:        apikey.Role,
				ApiKeyId:       apikey.Id,
				OrgId:          apikey.OrgId,
				Name:           apikey.Name,
			},
			Collector: colQuery.Result,
			Socket:    so,
			SocketId:  so.Id(),
		}
		log.Info("collector %s with id %d owned by %d authenticated successfully.", name, colQuery.Result.Id, apikey.OrgId)
		if lastSocketId != "" {
			log.Info("removing socket with Id %s", lastSocketId)
			cmd := &m.DeleteCollectorSessionCommand{
				SocketId:    lastSocketId,
				OrgId:       sess.OrgId,
				CollectorId: sess.Collector.Id,
			}
			if err := bus.Dispatch(cmd); err != nil {
				log.Error(0, "failed to remove collectors lastSocketId", err)
				return nil, err
			}
		}

		if err := sess.Save(); err != nil {
			return nil, err
		}
		log.Info("saving session to contextCache")
		contextCache.Set(sess.SocketId, sess)
		log.Info("session saved to contextCache")
//.........这里部分代码省略.........
开发者ID:reduxdj,项目名称:grafana,代码行数:101,代码来源:socketio.go


注:本文中的github.com/grafana/grafana/pkg/models.GetCollectorByNameQuery.Result方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。