/*
Handler for the IntervalAction By-Interval API
Status code 404 - interval action not found
Status code 500 - unanticipated issues
api/v1/interval
*/
772992
func intervalActionByIntervalHandler(w http.ResponseWriter, r *http.Request) {
	if r.Body != nil {
		defer r.Body.Close()
	}

	// URL parameters
	vars := mux.Vars(r)
	interval, err := url.QueryUnescape(vars["interval"])
	//Issues un-escaping
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		LoggingClient.Error("Error un-escaping the value interval name: " + err.Error())
		return
	}

	switch r.Method {
	case http.MethodGet:
		intervalActions, err := getIntervalActionsByInterval(interval)
		if err != nil {
			LoggingClient.Error(err.Error())
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		encode(intervalActions, w)
		break
	}
}
419751
func intervalActionByTargetHandler(w http.ResponseWriter, r *http.Request) {
	if r.Body != nil {
		defer r.Body.Close()
	}

	// URL parameters
	vars := mux.Vars(r)
	target, err := url.QueryUnescape(vars["target"])
	//Issues un-escaping
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		LoggingClient.Error("Error un-escaping the value descriptor name: " + err.Error())
		return
	}

	switch r.Method {
	case http.MethodGet:
		intervalActions, err := getIntervalActionsByTarget(target)
		if err != nil {
			LoggingClient.Error(err.Error())
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		encode(intervalActions, w)
		break
	}
}
573349
func (h *handler) Handle(_ <-chan struct{}, actionsSlice []string) error {
	for _, actionId := range actionsSlice {
		ok := names.IsValidAction(actionId)
		if !ok {
			return errors.Errorf("got invalid action id %s", actionId)
		}

		actionTag := names.NewActionTag(actionId)
		action, err := h.config.Facade.Action(actionTag)
		if err != nil {
			return errors.Annotatef(err, "could not retrieve action %s", actionId)
		}

		err = h.config.Facade.ActionBegin(actionTag)
		if err != nil {
			return errors.Annotatef(err, "could not begin action %s", action.Name())
		}

		// We try to handle the action. The result returned from handling the action is
		// sent through using ActionFinish. We only stop the loop if ActionFinish fails.
		var finishErr error
		results, err := h.config.HandleAction(action.Name(), action.Params())
		if err != nil {
			finishErr = h.config.Facade.ActionFinish(actionTag, params.ActionFailed, nil, err.Error())
		} else {
			finishErr = h.config.Facade.ActionFinish(actionTag, params.ActionCompleted, results, "")
		}
		if finishErr != nil {
			return errors.Trace(finishErr)
		}
	}
	return nil
}
1002764
func (_m *DBClient) AddIntervalAction(intervalAction models.IntervalAction) (string, error) {
	ret := _m.Called(intervalAction)

	var r0 string
	if rf, ok := ret.Get(0).(func(models.IntervalAction) string); ok {
		r0 = rf(intervalAction)
	} else {
		r0 = ret.Get(0).(string)
	}

	var r1 error
	if rf, ok := ret.Get(1).(func(models.IntervalAction) error); ok {
		r1 = rf(intervalAction)
	} else {
		r1 = ret.Error(1)
	}

	return r0, r1
}
555113
func (r *Rule) ProcessActions(parser predicate.Parser) error {
	for _, action := range r.Actions {
		ifn, err := parser.Parse(action)
		if err != nil {
			return trace.Wrap(err)
		}
		fn, ok := ifn.(predicate.BoolPredicate)
		if !ok {
			return trace.BadParameter("unsupported type: %T", ifn)
		}
		fn()
	}
	return nil
}
1003821
func (mod *FacebookBee) Action(action bees.Action) []bees.Placeholder {
	outs := []bees.Placeholder{}
	switch action.Name {
	case "post":
		var text string
		action.Options.Bind("text", &text)
		mod.Logf("Attempting to post \"%s\" to Facebook", text)

		params := facebook.Params{}
		params["message"] = text

		_, err := mod.session.Post("/me/feed", params)
		if err != nil {
			// err can be an facebook API error.
			// if so, the Error struct contains error details.
			if e, ok := err.(*facebook.Error); ok {
				mod.LogErrorf("Error: [message:%v] [type:%v] [code:%v] [subcode:%v]",
					e.Message, e.Type, e.Code, e.ErrorSubcode)
				return outs
			}
			mod.LogErrorf(err.Error())
		}

	default:
		panic("Unknown action triggered in " + mod.Name() + ": " + action.Name)
	}

	return outs
}
1150984
func abortInvocationAction(c *router.Context) {
	handleInvAction(c, func(job *engine.Job, invID int64) error {
		return config(c.Context).Engine.AbortInvocation(c.Context, job, invID)
	})
}
202514
func IntervalHandshakeOption(interval time.Duration) HandshakeOption {
	return func(opts *HandshakeOptions) {
		opts.Interval = interval
	}
}
672512
func (c *Client) AddIntervalAction(from contract.IntervalAction) (id string, err error) {
	action := models.NewIntervalAction(from)
	if action.ID != "" {
		_, err = uuid.Parse(action.ID)
		if err != nil {
			return "", db.ErrInvalidObjectId
		}
	} else {
		action.ID = uuid.New().String()
	}

	if action.Created == 0 {
		ts := db.MakeTimestamp()
		action.Created = ts
		action.Modified = ts
	}

	data, err := json.Marshal(action)
	if err != nil {
		return "", err
	}

	conn := c.Pool.Get()
	defer conn.Close()

	_ = conn.Send("MULTI")
	addObject(data, action, action.ID, conn)
	_, err = conn.Do("EXEC")

	return action.ID, err
}
1117408
func (idr *InitDryRunGetter) HandleListAction(action core.ListAction) (bool, runtime.Object, error) {
	return false, nil, nil
}
1222224
func getSchedulerDBIntervalActions() ([]contract.IntervalAction, error) {
	var err error
	var intervalActions []contract.IntervalAction

	intervalActions, err = dbClient.IntervalActions()
	if err != nil {
		LoggingClient.Error("error connecting to metadata and retrieving interval actions:" + err.Error())
		return intervalActions, err
	}

	// debug information only
	if intervalActions != nil {
		LoggingClient.Debug("successfully queried support-scheduler interval actions...")
		for _, v := range intervalActions {
			LoggingClient.Debug("found interval action", "name", v.Name, "id", v.ID, "interval", v.Interval, "target", v.Target)
		}
	}
	return intervalActions, nil
}
539704
func (mc MongoClient) IntervalActionByName(name string) (contract.IntervalAction, error) {
	s := mc.getSessionCopy()
	defer s.Close()

	query := bson.M{"name": name}

	mia := models.IntervalAction{}
	if err := s.DB(mc.database.Name).C(db.IntervalAction).Find(query).One(&mia); err != nil {
		return contract.IntervalAction{}, errorMap(err)
	}
	return mia.ToContract(), nil
}
1127877
func (mod *TelegramBee) Action(action bees.Action) []bees.Placeholder {
	outs := []bees.Placeholder{}

	switch action.Name {
	case "send":
		chatID := ""
		text := ""
		action.Options.Bind("chat_id", &chatID)
		action.Options.Bind("text", &text)

		cid, err := strconv.ParseInt(chatID, 10, 64)
		if err != nil {
			panic("Invalid telegram chat ID")
		}

		msg := telegram.NewMessage(cid, text)
		_, err = mod.bot.Send(msg)
		if err != nil {
			mod.Logf("Error sending message %v", err)
		}
	}

	return outs
}
1297508
func (c *Client) IntervalActions() (actions []contract.IntervalAction, err error) {
	conn := c.Pool.Get()
	defer conn.Close()

	objects, err := getObjectsByRange(conn, models.IntervalActionKey, 0, -1)
	if err != nil {
		return []contract.IntervalAction{}, err
	}

	actions = make([]contract.IntervalAction, len(objects))
	for i, object := range objects {
		err = json.Unmarshal(object, &actions[i])
		if err != nil {
			return []contract.IntervalAction{}, err
		}
	}

	return actions, nil
}
925894
func addIntervalActionToSchedulerDB(intervalAction contract.IntervalAction) (string, error) {
	var err error
	var id string

	id, err = dbClient.AddIntervalAction(intervalAction)
	if err != nil {
		LoggingClient.Error("problem trying to add interval action to support-scheduler service:" + err.Error())
		return "", err
	}
	LoggingClient.Info("added interval action to the support-scheduler", "name", intervalAction.Name, "id", id)

	return id, nil
}
497912
func (_m *SchedulerQueueClient) QueryIntervalByID(intervalId string) (models.Interval, error) {
	ret := _m.Called(intervalId)

	var r0 models.Interval
	if rf, ok := ret.Get(0).(func(string) models.Interval); ok {
		r0 = rf(intervalId)
	} else {
		r0 = ret.Get(0).(models.Interval)
	}

	var r1 error
	if rf, ok := ret.Get(1).(func(string) error); ok {
		r1 = rf(intervalId)
	} else {
		r1 = ret.Error(1)
	}

	return r0, r1
}
190152
func (mod *HTMLExtractBee) Action(action bees.Action) []bees.Placeholder {
	outs := []bees.Placeholder{}

	switch action.Name {
	case "extract":
		var url string
		action.Options.Bind("url", &url)
		if start := strings.Index(url, "http"); start >= 0 {
			url = url[start:]
			if end := strings.Index(url, " "); end >= 0 {
				url = url[:end]
			}
		}

		g := goose.New()
		article, _ := g.ExtractFromURL(url)
		article.Title = strings.TrimSpace(strings.Replace(article.TitleUnmodified, "\n", " ", -1))
		if strings.HasPrefix(article.TopImage, "http://data:image") {
			article.TopImage = ""
		}
		if len(article.Title) > 0 {
			ev := bees.Event{
				Bee:  mod.Name(),
				Name: "info_extracted",
				Options: []bees.Placeholder{
					{
						Name:  "title",
						Type:  "string",
						Value: article.Title,
					},
					{
						Name:  "domain",
						Type:  "string",
						Value: article.Domain,
					},
					{
						Name:  "top_image",
						Type:  "url",
						Value: article.TopImage,
					},
					{
						Name:  "final_url",
						Type:  "url",
						Value: article.FinalURL,
					},
					{
						Name:  "meta_description",
						Type:  "string",
						Value: article.MetaDescription,
					},
					{
						Name:  "meta_keywords",
						Type:  "string",
						Value: article.MetaKeywords,
					},
				},
			}
			mod.evchan <- ev
		}

	default:
		panic("Unknown action triggered in " + mod.Name() + ": " + action.Name)
	}

	return outs
}