Example #1
0
func UpdateMonitor(c *middleware.Context, cmd m.UpdateMonitorCommand) Response {
	cmd.OrgId = c.OrgId

	err := bus.Dispatch(&cmd)
	if err != nil {
		return ApiError(500, "Failed to update monitor", err)
	}

	return ApiSuccess("Monitor updated")
}
Example #2
0
func UpdateMonitor(c *middleware.Context, cmd m.UpdateMonitorCommand) Response {
	cmd.OrgId = c.OrgId
	if cmd.EndpointId == 0 {
		return ApiError(400, "EndpointId not set.", nil)
	}
	if cmd.MonitorTypeId == 0 {
		return ApiError(400, "MonitorTypeId not set.", nil)
	}
	if cmd.Frequency == 0 {
		return ApiError(400, "Frequency not set.", nil)
	}
	err := bus.Dispatch(&cmd)
	if err != nil {
		return ApiError(500, "Failed to update monitor", err)
	}

	return ApiSuccess("Monitor updated")
}
Example #3
0
func UpdateMonitor(cmd *m.UpdateMonitorCommand) error {
	return inTransaction2(func(sess *session) error {
		//validate Endpoint.
		endpointQuery := m.GetEndpointByIdQuery{
			Id:    cmd.EndpointId,
			OrgId: cmd.OrgId,
		}
		err := GetEndpointById(&endpointQuery)
		if err != nil {
			return err
		}
		currentEndpoint := endpointQuery.Result

		q := m.GetMonitorByIdQuery{
			Id:    cmd.Id,
			OrgId: cmd.OrgId,
		}
		err = GetMonitorById(&q)
		if err != nil {
			return err
		}
		lastState := q.Result

		if lastState.EndpointId != cmd.EndpointId {
			return m.ErrorEndpointCantBeChanged
		}

		//validate collectors.
		filtered_collectors := make([]*collectorList, 0, len(cmd.CollectorIds))
		if len(cmd.CollectorIds) > 0 {
			sess.Table("collector")
			sess.In("id", cmd.CollectorIds).Where("org_id=? or public=1", cmd.OrgId)
			sess.Cols("id")
			err = sess.Find(&filtered_collectors)

			if err != nil {
				return err
			}
		}

		if len(filtered_collectors) < len(cmd.CollectorIds) {
			return m.ErrMonitorCollectorsInvalid
		}

		//get settings definition for thie monitorType.
		var typeSettings []*m.MonitorTypeSetting
		sess.Table("monitor_type_setting")
		sess.Where("monitor_type_id=?", cmd.MonitorTypeId)
		err = sess.Find(&typeSettings)
		if err != nil {
			return nil
		}
		if len(typeSettings) < 1 {
			log.Info("no monitorType settings found for type: %d", cmd.MonitorTypeId)
			return m.ErrMonitorSettingsInvalid
		}

		// push the typeSettings into a Map with the variable name as key
		settingMap := make(map[string]*m.MonitorTypeSetting)
		for _, s := range typeSettings {
			settingMap[s.Variable] = s
		}

		//validate the settings.
		seenMetrics := make(map[string]bool)
		for _, v := range cmd.Settings {
			def, ok := settingMap[v.Variable]
			if ok != true {
				log.Info("Unkown variable %s passed.", v.Variable)
				return m.ErrMonitorSettingsInvalid
			}
			//TODO:(awoods) make sure the value meets the definition.
			seenMetrics[def.Variable] = true
		}

		//make sure all required variables were provided.
		//add defaults for missing optional variables.
		for k, s := range settingMap {
			if _, ok := seenMetrics[k]; ok != true {
				log.Info("%s not in settings", k)
				if s.Required {
					// required setting variable missing.
					return m.ErrMonitorSettingsInvalid
				}
				cmd.Settings = append(cmd.Settings, &m.MonitorSettingDTO{
					Variable: k,
					Value:    s.DefaultValue,
				})
			}
		}

		mon := &m.Monitor{
			Id:             cmd.Id,
			EndpointId:     cmd.EndpointId,
			OrgId:          cmd.OrgId,
			MonitorTypeId:  cmd.MonitorTypeId,
			Offset:         cmd.EndpointId % cmd.Frequency,
			Settings:       cmd.Settings,
			HealthSettings: cmd.HealthSettings,
			Updated:        time.Now(),
			Enabled:        cmd.Enabled,
			State:          lastState.State,
			StateChange:    lastState.StateChange,
			StateCheck:     lastState.StateCheck,
			Frequency:      cmd.Frequency,
		}

		var rawSql = "DELETE FROM monitor_collector WHERE monitor_id=?"
		if _, err := sess.Exec(rawSql, cmd.Id); err != nil {
			return err
		}
		if len(cmd.CollectorIds) > 0 {
			monitor_collectors := make([]*m.MonitorCollector, len(cmd.CollectorIds))
			for i, l := range cmd.CollectorIds {
				monitor_collectors[i] = &m.MonitorCollector{
					MonitorId:   cmd.Id,
					CollectorId: l,
				}
			}
			sess.Table("monitor_collector")
			if _, err := sess.Insert(&monitor_collectors); err != nil {
				return err
			}
		}

		rawSql = "DELETE FROM monitor_collector_tag WHERE monitor_id=?"
		if _, err := sess.Exec(rawSql, cmd.Id); err != nil {
			return err
		}
		if len(cmd.CollectorTags) > 0 {
			monitor_collector_tags := make([]*m.MonitorCollectorTag, len(cmd.CollectorTags))
			for i, t := range cmd.CollectorTags {
				monitor_collector_tags[i] = &m.MonitorCollectorTag{
					MonitorId: cmd.Id,
					Tag:       t,
				}
			}

			sess.Table("monitor_collector_tag")
			if _, err := sess.Insert(&monitor_collector_tags); err != nil {
				return err
			}
		}

		// get collectorIds from tags
		tagCollectors, err := getCollectorIdsFromTags(cmd.OrgId, cmd.CollectorTags, sess)
		if err != nil {
			return err
		}

		collectorIdMap := make(map[int64]bool)
		collectorList := make([]int64, 0)
		lastCollectors := make(map[int64]bool)
		for _, id := range lastState.Collectors {
			lastCollectors[id] = false
		}

		for _, id := range cmd.CollectorIds {
			collectorIdMap[id] = true
			collectorList = append(collectorList, id)
		}

		for _, id := range tagCollectors {
			if _, ok := collectorIdMap[id]; !ok {
				collectorList = append(collectorList, id)
			}
		}

		sess.Table("monitor")
		sess.UseBool("enabled")
		if _, err = sess.Where("id=? and org_id=?", mon.Id, mon.OrgId).Update(mon); err != nil {
			return err
		}

		sess.publishAfterCommit(&events.MonitorUpdated{
			MonitorPayload: events.MonitorPayload{
				Id:            mon.Id,
				EndpointId:    mon.EndpointId,
				OrgId:         mon.OrgId,
				EndpointSlug:  currentEndpoint.Slug,
				MonitorTypeId: mon.MonitorTypeId,
				CollectorIds:  cmd.CollectorIds,
				CollectorTags: cmd.CollectorTags,
				Collectors:    collectorList,
				Settings:      mon.Settings,
				Frequency:     mon.Frequency,
				Enabled:       mon.Enabled,
				Offset:        mon.Offset,
				Updated:       mon.Updated,
			},
			Timestamp: mon.Updated,
			LastState: &events.MonitorPayload{
				Id:            lastState.Id,
				EndpointId:    lastState.EndpointId,
				OrgId:         lastState.OrgId,
				EndpointSlug:  lastState.EndpointSlug,
				MonitorTypeId: lastState.MonitorTypeId,
				CollectorIds:  lastState.CollectorIds,
				CollectorTags: lastState.CollectorTags,
				Collectors:    lastState.Collectors,
				Settings:      lastState.Settings,
				Frequency:     lastState.Frequency,
				Enabled:       lastState.Enabled,
				Offset:        lastState.Offset,
				Updated:       lastState.Updated,
			},
		})

		return err
	})
}