def collect_triggers_and_put(self, fetch_id=None, host_ids=None):
        query = self.__socket.services.columns("plugin_output",
                                               "description",
                                               "last_state_change",
                                               "host_alias",
                                               "host_name",
                                               "state")

        if host_ids is not None:
            filter_condition = "host_name ~ "
            for host_id in enumerate(host_ids):
                if host_id[0] == 0:
                    filter_condition += host_id[1]
                else:
                    filter_condition += "|" + host_id[1]

            query = query.filter(filter_condition)

        all_triggers_should_send = lambda: fetch_id is None
        update_type = "ALL"
        if all_triggers_should_send():
            if self.__trigger_last_info is None:
                self.__trigger_last_info = self.get_last_info("trigger")

            if len(self.__trigger_last_info):
                unix_timestamp = Utils.translate_hatohol_time_to_unix_time(self.__trigger_last_info)
                query = query.filter("last_state_change >= %s" % unix_timestamp)
                update_type = "UPDATED"

        result = query.call()

        triggers = []
        for service in result:
            hapi_status, hapi_severity = \
                self.__parse_status_and_severity(service["state"])

            last_state_change = datetime.datetime.fromtimestamp(service["last_state_change"])
            hapi_time = Utils.conv_to_hapi_time(last_state_change,
                                                self.__time_offset)
            triggers.append({
                "triggerId": service["description"],
                "status": hapi_status,
                "severity": hapi_severity,
                "lastChangeTime": hapi_time,
                "hostId": service["host_name"],
                "hostName": service["host_alias"],
                "brief": service["plugin_output"],
                "extendedInfo": ""
            })
        self.__trigger_last_info = \
            Utils.get_biggest_num_of_dict_array(triggers,
                                                "lastChangeTime")
        self.put_triggers(triggers, update_type=update_type,
                          last_info=self.__trigger_last_info,
                          fetch_id=fetch_id)
    def collect_events_and_put(self, fetch_id=None, last_info=None,
                               count=None, direction="ASC"):
        query = self.__socket.statehist.columns("log_output",
                                                "state",
                                                "time",
                                                "current_host_name",
                                                "current_host_alias",
                                                "service_description")

        if last_info is None:
            last_info = self.get_cached_event_last_info()

        if last_info:
            last_info = json.loads(last_info)

        if not last_info:
            pass
        elif direction == "ASC":
            unix_timestamp = \
                Utils.translate_hatohol_time_to_unix_time(last_info["time"])
            query = query.filter("time >= %s" % unix_timestamp)
        elif direction == "DESC":
            unix_timestamp = \
                Utils.translate_hatohol_time_to_unix_time(last_info["time"])
            query = query.filter("time <= %s" % unix_timestamp)

        result = query.call()
        logger.debug(query)

        try:
            latest_state_index = result.index(last_info)
            result = result[:latest_state_index]
        except ValueError:
            pass

        events = []
        for event in result:
            if not len(event["current_host_name"]):
                continue

            hapi_event_type = self.EVENT_TYPE_MAP.get(event["state"])
            if hapi_event_type is None:
                log.warning("Unknown status: " + event["state"])
                hapi_event_type = "UNKNOWN"

            hapi_status, hapi_severity = \
                self.__parse_status_and_severity(event["state"])

            event_time = datetime.datetime.fromtimestamp(event["time"])
            hapi_time = Utils.conv_to_hapi_time(event_time,
                                                self.__time_offset)
            events.append({
                "eventId": str(uuid.uuid1()),
                "time": hapi_time,
                "type": hapi_event_type,
                "triggerId": event["service_description"],
                "status": hapi_status,
                "severity": hapi_severity,
                "hostId": event["current_host_name"],
                "hostName": event["current_host_alias"],
                "brief": event["log_output"],
                "extendedInfo": ""
            })

        if len(result):
            # livestatus return a sorted list.
            # result[0] is latest statehist.
            self.__latest_statehist = json.dumps(result[0])
        self.put_events(events, fetch_id=fetch_id,
                        last_info_generator=self.return_latest_statehist)