Exemplo n.º 1
0
    def get_events(self, type=None, mlapp_node=None, agent=None, start_time=None, end_time=None, is_alert=True):
        """
        Fetch events according to a time window and other filters.

        :param mlapp_node: Set the mlapp_node to select events from. If None, events will be fetched for the entire ION.
        :param agent: (optional) Either an agent ID or an agent object. If specified, the query will filter by agent.
        :param start_time: (optional) Filter by start time, this should be a datetime object.
        :param end_time: (optional) Filter by end time, this should be a datetime object.
        :param is_alert: (optional) Filter by is_alert - between alerts and events. Default is True. If None is provided
                         all events are fetched.
        :return: A dataframe containing information about the events obtained.
        :raises: MLOpsException

        :Example:

        >>> from parallelm.mlops import mlops as pm
        >>> from datetime import datetime, timedelta
        >>> node_agent_list = pm.get_agents("0")
        >>> now = datetime.utcnow()
        >>> hour_ago = (now - timedelta(hours=1))
        >>> df = pm.get_events("training_comp",  node_agent_list[0].id, hour_ago, now)

        The above example assumes the MLApp this code is running as part of a node called "my_training".
        1. The code obtains the list of agents which are used by the "my_training" component.
        2. It computes the timestamp of now.
        3. It computes the timestamp of an hour ago.
        4. It calls the get_events method asking for all the events generated by the "0" node of the first
        agent during the past hour.

        """

        self._verify_mlops_is_ready()
        self._verify_time_window(start_time, end_time, allow_none=True)

        start_time_ms_timestamp = time_to_str_timestamp_milli(start_time) if start_time is not None else None
        end_time_ms_timestamp = time_to_str_timestamp_milli(end_time) if end_time is not None else None

        ion = self._mlops_ctx.ion()

        # Translate the mlapp_node to its pipeline_instance_id
        pipeline_inst_id = None
        if mlapp_node is not None:
            mlapp_node = self.get_node(mlapp_node)
            if mlapp_node is None:
                raise MLOpsException("No such {} node: [{}]".format(Constants.ION_LITERAL,
                                                                    mlapp_node))
            pipeline_inst_id = mlapp_node.pipeline_instance_id

        # Translate the agent to its host name
        agent_host = None
        if agent is not None:
            if isinstance(agent, Agent):
                agent_host = agent.hostname
            elif isinstance(agent, six.string_types):
                agent_obj = self._mlops_ctx.get_agent_by_id(agent)
                if agent_obj is None:
                    raise MLOpsException("Agent ID [{}] does not " +
                                         "exists in this {}".format(agent, Constants.ION_LITERAL))
                agent_host = agent_obj.host

        if self._api_test_mode:
            self._logger.info("API testing mode - returning without performing call")
            return

        ef = EventFilter()
        ef.ion_name = None
        ef.ion_inst_id = ion.id
        ef.pipeline_inst_id = pipeline_inst_id
        ef.agent_host = agent_host
        ef.time_window_start = start_time_ms_timestamp
        ef.time_window_end = end_time_ms_timestamp
        ef.is_alert = is_alert

        events_df = self._event_broker.get_events(ef)
        return events_df
Exemplo n.º 2
0
def test_datetime_timestamps():

    ts = time_to_str_timestamp_milli(datetime.now())

    assert ts is not None
Exemplo n.º 3
0
    def get_stats(self, name, mlapp_node, agent, start_time, end_time, no_of_lines=-1):
        """
        Get statistics from MLOps.
        This call provides a way for python code uploaded to MLOps to obtain access to
        MLApp statistics. The get_stats() call allows access to statistics from each of the MLApp components; these may be
        filtered by the agent. The start_time and end_time can be used to define a time window for the returned stats.

        :param name: The name of the statistic - this is the name used in the stats method
        :param mlapp_node: The name of the MLApp node
        :param agent: ID of the agent which collected the statistics
        :param start_time: A datetime object representing the start of the time window
        :param end_time: A datetime object representing the end of the time window
        :param no_of_lines: DEPRECATED
        :return: A dataframe representing the statistics collected over time
        :raises: MLOPsException

        :Example:

        >>> from parallelm.mlops import mlops as pm
        >>> from datetime import datetime, timedelta
        >>> node_agent_list = pm.get_agents("0")
        >>> now = datetime.utcnow()
        >>> hour_ago = (now - timedelta(hours=1))
        >>> df = pm.get_stats("myValue", "0",  node_agent_list[0].id, hour_ago, now)

        The above example assumes the MLApp this code is running as part of has a component called "my_training".
        1. The code obtains the list of agents which are used by the "my_training" component.
        2. It computes the timestamp of now.
        3. It computes the timestamp of an hour ago.
        4. It calls the get_stats method asking for the "myValue" statistic from the first agent.
        The return value is a dataframe object.


        """
        self._verify_mlops_is_ready()

        ion = self._mlops_ctx.ion()

        self._verify_get_stats_args(name, mlapp_node, agent)
        self._verify_time_window(start_time, end_time)

        start_time_ms_timestamp = time_to_str_timestamp_milli(start_time)
        end_time_ms_timestamp = time_to_str_timestamp_milli(end_time)
        self._logger.info("Calling get_stat with start_time: {} end_time: {}".format(
            start_time_ms_timestamp, end_time_ms_timestamp))

        if self._api_test_mode:
            self._logger.info("API testing mode - returning without performing call")
            return

        # A valid system should exist past this point

        agents_per_node = self._assemble_nodes_and_agents(ion, mlapp_node, agent)
        self._logger.info("agents_per_node:" + str(agents_per_node))
        df_helper = DataFrameHelper()

        df_list = []
        for node_name in agents_per_node.keys():
            node_obj = self.get_node(node_name)
            self._logger.info("{} component: {}".format(Constants.ION_LITERAL, node_name))

            for agent_obj in agents_per_node[node_name]:
                self._logger.info("stat: {} {} node: {} agent: {}".format(name,
                                                                          Constants.ION_LITERAL,
                                                                          node_obj.name,
                                                                          agent_obj.id))

                json = self._mlops_ctx.rest_helper().get_stat(stat_name=name,
                                                              ion_id=ion.id,
                                                              workflow_node_id=node_obj.id,
                                                              agent_id=agent_obj.id,
                                                              pipeline_id=
                                                              node_obj.pipeline_agent_set[
                                                                  agent_obj.id],
                                                              start_time=start_time_ms_timestamp,
                                                              end_time=end_time_ms_timestamp)

                # TODO: next set of patches will convert the JSON into a dataframe with the correct format
                self._logger.info(pprint.pformat(json))
                df = df_helper.create_data_frame(json, no_of_lines)
                df_helper.add_col_with_same_value(df, DataframeColNames.ION_NODE, node_name)
                df_helper.add_col_with_same_value(df, DataframeColNames.AGENT, agent_obj.id)
                df_list.append(df)
        final_df = pd.concat(df_list, ignore_index=True)
        return final_df