Exemplo n.º 1
0
    def run_analysis(self, asset, curve, begin=None, duration=30000):
        if begin is None:
            begin = get_timestamp() - duration

        end = begin + duration

        analysis_results = self.analyzer(
            assetId="{0[asset_type]}/{0[asset_id]}".format(asset),
            channel=curve,
            qualifier=curve,
            computeFields=self.default_state.get("computeFields"),
            begin=begin,
            end=end,
        )

        if analysis_results:
            # Gerar annotation
            analysis_results.update(__src="auto-analysis",
                                    uid=str(uuid4()),
                                    createdAt=get_timestamp())
            with start_action(action_type="create annotation", curve=curve):
                self.annotator(analysis_results, room={"id": self.room_id})

            response_text = "Analysis of curve {} finished".format(curve)

        else:
            response_text = "Analysis of curve {} returned no data".format(
                curve)

        return response_text
Exemplo n.º 2
0
def format_and_send(message, room, author, connection_func):
    event = format_message_event(message,
                                 room,
                                 author,
                                 timestamp=get_timestamp())
    logging.debug("Sending message {}".format(event))
    connection_func(event)
def analyse_channels(assets, active_channels, settings):
    ONE_MINUTE = 60000

    if active_channels:
        assets_by_etype = dict(
            (item.get("event_type"), item) for item in assets)

        channels_by_asset = itertools.groupby(active_channels,
                                              key=lambda x: x.get("__type"))
        for event_type, channels in channels_by_asset:
            asset = assets_by_etype.get(event_type)
            channel = next(channels)
            channel_name = channel.get("mnemonic")
            end = get_timestamp()
            begin = end - ONE_MINUTE

            print(f"Analysing channel {channel_name} for {event_type}")
            analyse_and_annotate(
                settings,
                assetId="{0[asset_type]}/{0[id]}".format(asset),
                channel=channel_name,
                begin=begin,
                end=end,
            )
    else:
        print("No channels to analyse")
        raise SystemExit(0)
Exemplo n.º 4
0
def send_message(message, **kwargs):
    timestamp = kwargs.pop("timestamp", get_timestamp())
    message_type = kwargs.pop("message_type", None)

    if (message_type is None) or (message_type == MESSAGE_TYPES.EVENT):
        maybe_send_message_event(message, timestamp, kwargs.get("settings"))

    if (message_type is None) or (message_type == MESSAGE_TYPES.CHAT):
        maybe_send_chat_message(message, **kwargs)
Exemplo n.º 5
0
def update_room_users(settings, room_id, sender, action):
    connection_func = build_sender_function(settings["live"])
    author = settings["output"]["author"]

    update_message_payload = create_room_update_data(room_id, sender, author,
                                                     action)
    update_event = format_event(update_message_payload, EVENT_TYPES["control"],
                                get_timestamp())
    connection_func(update_event)
Exemplo n.º 6
0
def join_messenger(settings):
    connection_func = build_sender_function(settings["live"])

    bot_data = settings["output"]["author"]
    control_data = {
        "broadcast": True,
        "__skipstorage": True,
        "action": "user_joined_messenger",
        "user": bot_data,
    }

    event = format_event(control_data, EVENT_TYPES["control"], get_timestamp())
    connection_func(event)
Exemplo n.º 7
0
def generate_events(event_type, las_data, chat_data, index_mnemonic, settings,
                    state_manager):
    logging.info("{}: Event generation started".format(event_type))

    source_name = las_data.version.SOURCE.value
    curves_data = dict((item.mnemonic, item.unit) for item in las_data.curves)
    las_df = las_data.df()
    values_iterator = las_df.iterrows()
    curves = las_df.columns

    success = True
    state = state_manager.load()
    last_timestamp = state.get("last_timestamp", 0)
    if last_timestamp > 0:
        logging.info(f"Skipping to index {last_timestamp}")

    while success:
        success, statuses = read_next_frame(values_iterator, curves,
                                            curves_data, index_mnemonic)

        if success:
            next_timestamp = statuses.get(index_mnemonic, {}).get("value", 0)

        if next_timestamp > last_timestamp:
            delay_output(last_timestamp, next_timestamp)

            if last_timestamp == 0:
                message = "Replay from '{}' started at TIME {}".format(
                    source_name, next_timestamp)
                send_message(message,
                             timestamp.get_timestamp(),
                             settings=settings)

            raw.create(event_type, statuses, settings)

            update_chat(chat_data, last_timestamp, next_timestamp,
                        index_mnemonic, settings)
            last_timestamp = next_timestamp
            state_manager.save({"last_timestamp": last_timestamp})
Exemplo n.º 8
0
def update_chat(chat, last_ts, next_ts, index_mnemonic, settings):
    if not chat:
        return

    items_to_send = []
    for item in chat:
        item_index = int(item.get(index_mnemonic, -1))

        if last_ts <= item_index < next_ts:
            items_to_send.append(item)
        elif item_index > next_ts:
            break

    logging.debug("{} messages between {} and {}".format(
        len(items_to_send), last_ts, next_ts))

    for item in items_to_send:
        message = item.get("MESSAGE", "")
        source = item.get("SOURCE", "")
        if message and source:
            messenger.maybe_send_chat_message(message,
                                              timestamp.get_timestamp(),
                                              settings,
                                              author_name=source)
Exemplo n.º 9
0
def format_and_send(event_type, statuses, output_settings):
    timestamp = get_timestamp()
    event = format_event(timestamp, event_type, statuses)
    print("-----------------------------------------------------")
    print(json.dumps(event))
Exemplo n.º 10
0
def analyse_and_annotate(settings, **kwargs):
    analysis = run_analysis(settings, **kwargs)
    analysis.update(__src="auto-analysis", uid=str(uuid.uuid4()), createdAt=get_timestamp())
    return annotation.create(analysis, settings=settings)
    return settings


if __name__ == "__main__":
    """
    Creates annotations in one of the rooms and/or dashboards on Intelie Live
    Reads from standard input and creates an annotation for every line read
    """
    args = parse_arguments(sys.argv)
    settings = build_settings(args)
    __src = getattr(args, "src") or "live-client"
    ts_delta = int(args.ts_delta)

    print(
        "\vAll messages written here will be sent to Live. Press CTRL+D to exit.\n"
    )
    for line in sys.stdin:
        if not line.strip():
            continue

        ts = get_timestamp() + ts_delta
        annotation.create(
            {
                "message": line,
                "__src": __src,
                "timestamp": ts,
                "createdAt": ts
            },
            settings=settings)
Exemplo n.º 12
0
def format_and_send(event_data, event_type, connection_func):
    timestamp = event_data.pop("timestamp", get_timestamp())
    event = format_event(event_data, event_type, timestamp)
    connection_func(event)
Exemplo n.º 13
0
def format_and_send(annotation_data, settings, connection_func=None):
    timestamp = get_timestamp()
    event = format_event(timestamp, annotation_data, settings)

    logging.debug("Creating annotation {}".format(event))
    connection_func(event)