func GetMonitors(c *middleware.Context, query m.GetMonitorsQuery) Response { query.OrgId = c.OrgId query.IsGrafanaAdmin = c.IsGrafanaAdmin if err := bus.Dispatch(&query); err != nil { return ApiError(500, "Failed to query monitors", err) } return Json(200, query.Result) }
func GetMonitors(query *m.GetMonitorsQuery) 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_tag.collector_id)) as tag_collectors, endpoint.slug as endpoint_slug, monitor_type.name as monitor_type_name, monitor.* FROM monitor INNER JOIN endpoint on endpoint.id = monitor.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 collector_tag on collector_tag.tag = monitor_collector_tag.tag AND collector_tag.org_id = monitor.org_id ` whereSql := make([]string, 0) if !query.IsGrafanaAdmin { whereSql = append(whereSql, "monitor.org_id=?") rawParams = append(rawParams, query.OrgId) } if query.Enabled != "" { enabled, err := strconv.ParseBool(query.Enabled) if err != nil { return err } whereSql = append(whereSql, "monitor.enabled=?") rawParams = append(rawParams, enabled) } if len(query.EndpointId) > 0 { p := make([]string, len(query.EndpointId)) for i, e := range query.EndpointId { p[i] = "?" rawParams = append(rawParams, e) } whereSql = append(whereSql, fmt.Sprintf("monitor.endpoint_id IN (%s)", strings.Join(p, ","))) } if len(query.CollectorId) > 0 { rawSql += "LEFT JOIN monitor_collector AS mc ON mc.monitor_id = monitor.id\n" rawSql += `LEFT JOIN (SELECT collector.id AS collector_id, collector_tag.tag as tag, collector_tag.org_id as org_id FROM collector LEFT JOIN collector_tag ON collector.id = collector_tag.collector_id) as ct ON ct.tag = monitor_collector_tag.tag ` p := make([]string, len(query.CollectorId)) for i, c := range query.CollectorId { p[i] = "?" rawParams = append(rawParams, c) } p2 := make([]string, len(query.CollectorId)) for i, e := range query.CollectorId { p2[i] = "?" rawParams = append(rawParams, e) } whereSql = append(whereSql, fmt.Sprintf("((ct.org_id=monitor.org_id OR ct.org_id is NULL) AND (mc.collector_id IN (%s) OR ct.collector_id IN (%s)))", strings.Join(p, ","), strings.Join(p2, ","))) } if query.Modulo > 0 { whereSql = append(whereSql, "(monitor.id % ?) = ?") rawParams = append(rawParams, query.Modulo, query.ModuloOffset) } if len(whereSql) > 0 { rawSql += "WHERE " + strings.Join(whereSql, " AND ") } rawSql += " GROUP BY monitor.id" result := make([]*MonitorWithCollectorDTO, 0) err := sess.Sql(rawSql, rawParams...).Find(&result) if err != nil { return err } monitors := make([]*m.MonitorDTO, 0) //iterate through all of the results and build out our checks model. for _, row := range result { monitorCollectorIds := make([]int64, 0) monitorCollectorsMap := make(map[int64]bool) if row.CollectorIds != "" { for _, l := range strings.Split(row.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 row.CollectorTags != "" { monitorCollectorTags = strings.Split(row.CollectorTags, ",") if row.TagCollectors != "" { for _, l := range strings.Split(row.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 } /* h := m.MonitorHealthSettingDTO{} h.NumCollectors = row.HealthSettings["numCollectors"] h.Steps = row.HealthSettings["steps"] */ monitors = append(monitors, &m.MonitorDTO{ Id: row.Id, EndpointId: row.EndpointId, OrgId: row.OrgId, EndpointSlug: row.EndpointSlug, MonitorTypeId: row.MonitorTypeId, MonitorTypeName: row.MonitorTypeName, CollectorIds: monitorCollectorIds, CollectorTags: monitorCollectorTags, Collectors: mergedCollectors, State: scrutinizeState(time.Now(), row.State, row.StateCheck, row.Frequency), StateChange: row.StateChange, StateCheck: row.StateCheck, Settings: row.Settings, HealthSettings: row.HealthSettings, Frequency: row.Frequency, Enabled: row.Enabled, Offset: row.Offset, Updated: row.Updated, }) } query.Result = monitors return nil }