Ejemplo n.º 1
0
func GetMonitorById(c *middleware.Context) Response {
	id := c.ParamsInt64(":id")

	query := m.GetMonitorByIdQuery{Id: id, OrgId: c.OrgId}
	query.IsGrafanaAdmin = c.IsGrafanaAdmin

	err := bus.Dispatch(&query)
	if err != nil {
		return ApiError(404, "Monitor not found", nil)
	}

	return Json(200, query.Result)
}
Ejemplo n.º 2
0
func GetMonitorById(query *m.GetMonitorByIdQuery) error {
	sess := x.Table("monitor")
	rawParams := make([]interface{}, 0)
	rawSql := `
SELECT
    GROUP_CONCAT(DISTINCT(monitor_collector.collector_id)) as collector_ids,
    GROUP_CONCAT(DISTINCT(monitor_collector_tag.tag)) as collector_tags,
    GROUP_CONCAT(DISTINCT(collector_tags.collector_id)) as tag_collectors,
    endpoint.slug as endpoint_slug,
    monitor_type.name as monitor_type_name,
    monitor.*
FROM monitor
    INNER JOIN endpoint on monitor.endpoint_id = endpoint.id
    LEFT JOIN monitor_type ON monitor.monitor_type_id = monitor_type.id
    LEFT JOIN monitor_collector ON monitor.id = monitor_collector.monitor_id
    LEFT JOIN monitor_collector_tag ON monitor.id = monitor_collector_tag.monitor_id
    LEFT JOIN
        (SELECT
            collector.id AS collector_id,
            collector_tag.tag as tag
        FROM collector
        LEFT JOIN collector_tag ON collector.id = collector_tag.collector_id
        WHERE (collector.public=1 OR collector.org_id=?) AND (collector_tag.org_id=? OR collector_tag.id is NULL)) as collector_tags
    ON collector_tags.tag = monitor_collector_tag.tag
WHERE monitor.id=?
	`
	rawParams = append(rawParams, query.OrgId, query.OrgId, query.Id)

	if !query.IsGrafanaAdmin {
		rawSql += "AND monitor.org_id=?\n"
		rawParams = append(rawParams, query.OrgId)
	}
	rawSql += "GROUP BY monitor.id"

	//store the results into an array of maps.
	results := make([]*MonitorWithCollectorDTO, 0)
	err := sess.Sql(rawSql, rawParams...).Find(&results)
	if err != nil {
		return err
	}
	if len(results) < 1 {
		return m.ErrMonitorNotFound
	}
	result := results[0]

	monitorCollectorIds := make([]int64, 0)
	monitorCollectorsMap := make(map[int64]bool)
	if result.CollectorIds != "" {
		for _, l := range strings.Split(result.CollectorIds, ",") {
			i, err := strconv.ParseInt(l, 10, 64)
			if err != nil {
				return err
			}
			monitorCollectorIds = append(monitorCollectorIds, i)
			monitorCollectorsMap[i] = true
		}
	}

	monitorCollectorTags := make([]string, 0)
	if result.CollectorTags != "" {
		monitorCollectorTags = strings.Split(result.CollectorTags, ",")
		for _, l := range strings.Split(result.TagCollectors, ",") {
			i, err := strconv.ParseInt(l, 10, 64)
			if err != nil {
				return err
			}
			monitorCollectorsMap[i] = true
		}
	}

	mergedCollectors := make([]int64, len(monitorCollectorsMap))
	count := 0
	for k := range monitorCollectorsMap {
		mergedCollectors[count] = k
		count += 1
	}

	query.Result = &m.MonitorDTO{
		Id:              result.Id,
		EndpointId:      result.EndpointId,
		OrgId:           result.OrgId,
		EndpointSlug:    result.EndpointSlug,
		MonitorTypeId:   result.MonitorTypeId,
		MonitorTypeName: result.MonitorTypeName,
		CollectorIds:    monitorCollectorIds,
		CollectorTags:   monitorCollectorTags,
		Collectors:      mergedCollectors,
		State:           scrutinizeState(time.Now(), result.State, result.StateCheck, result.Frequency),
		StateChange:     result.StateChange,
		StateCheck:      result.StateCheck,
		Settings:        result.Settings,
		HealthSettings:  result.HealthSettings,
		Frequency:       result.Frequency,
		Enabled:         result.Enabled,
		Offset:          result.Offset,
		Updated:         result.Updated,
	}

	return nil
}