當前位置: 首頁>>代碼示例>>Golang>>正文


Golang models.GetCollectorByNameQuery類代碼示例

本文整理匯總了Golang中github.com/grafana/grafana/pkg/models.GetCollectorByNameQuery的典型用法代碼示例。如果您正苦於以下問題:Golang GetCollectorByNameQuery類的具體用法?Golang GetCollectorByNameQuery怎麽用?Golang GetCollectorByNameQuery使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了GetCollectorByNameQuery類的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類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。