def minion_statistic_check(condition):
    """
        Run calculations for minion statistics, such as speed, occupation, voltage, if 10 of the last 10 MinionStatitics
        are out of the limits
        on the notification condition, send tweet and if run until enabled stop run
        Parameters
        ----------
        condition: communication.models.NotificationConditions
            The condition we are dealing with
        Returns
        -------

        """
    # Limit for condition
    upper_limit = condition.upper_limit
    lower_limit = condition.lower_limit
    if MinionRunStats.objects.filter(run__flowcell=condition.flowcell).count():
        queryset = MinionRunStats.objects.filter(
            run__flowcell=condition.flowcell,
            id__gte=condition.last_minions_stats_id,
        ).order_by("-id")[:10]
    else:
        return
    last_mrs = queryset.last()
    if condition.notification_type == "volt":
        queryset = queryset.values_list("voltage_value", flat=True)
    else:
        queryset = [
            minion_statistic.occupancy() for minion_statistic in queryset
        ]
    upper_list = list(filter(lambda x: x >= upper_limit, queryset))
    lower_list = list(filter(lambda x: x <= lower_limit, queryset))
    if len(upper_list) == 10 or len(lower_list) == 10:
        limit = upper_limit if len(upper_list) == 10 else lower_limit
        twitter_details = UserOptions.objects.get(
            owner=condition.creating_user)
        if twitter_details.twitterhandle and twitter_details.tweet:
            text = return_tweet(
                tweet_type="volt_occu",
                volt_occu="Voltage",
                limit=limit,
                flowcell=condition.flowcell.name,
            )
            message = Message(
                recipient=condition.creating_user,
                sender=condition.creating_user,
                title=text,
                flowcell=condition.flowcell,
            )
            message.save()
        if condition.run_until:
            # TODO if we have a run until bug look here as conditions should really be run aware?
            create_stop_run(last_mrs.minion)
        condition.last_minions_stats_id = queryset.last().id
        condition.completed = True
        condition.save()
Example #2
0
    def toDatabase(self, sender, receiver, message):
        """
        Emulates email sending. "Emails" are just stored as a database record
        @param sender: the sender
        @type sender: User object
        @param receiver: the receiver
        @type receiver: User object
        @param message: the message
        """

        m = Message(sender=sender, receiver=receiver, message=message)
        m.save()
Example #3
0
    def toDatabase(self, sender, receiver, message):
        """
        Emulates email sending. "Emails" are just stored as a database record
        @param sender: the sender
        @type sender: User object
        @param receiver: the receiver
        @type receiver: User object
        @param message: the message
        """

        m = Message( sender=sender, receiver=receiver, message=message )
        m.save()
def coverage_notification(condition):
    """
    Run calculations for coverage statistics - this is pretty gahbahj
    Parameters
    ----------
    condition: communication.models.NotificationConditions
        The condition we are dealing with
    Returns
    -------

    """
    if not condition.coverage_target:
        logger.warning(
            f"Condition {condition.id} of type {condition.notification_type}"
            f" should have a target coverage. Please investigate. Skipping... "
        )
        return
    chromosome = condition.chromosome
    reference = condition.reference
    flowcell = condition.flowcell
    run = flowcell.runs.last()
    barcodes = list(condition.mapping_condition_barcodes.all().values_list(
        "barcode_id", flat=True))
    coverage_reached = check_coverage(flowcell, int(condition.coverage_target),
                                      chromosome, barcodes)
    logger.debug("coverage reached!")
    logger.debug(coverage_reached)
    if not coverage_reached:
        return
    # Reached is a dictionary, one dict for each Paf Summary Cov pulled back in the check_coverage function
    for reached in coverage_reached:
        if reached.get("coverage_reached", False):
            # Let's create a message
            message_text = return_tweet(
                "coverage",
                target_coverage=int(condition.coverage_target),
                reference_file_name=reference.name,
                chromosome=chromosome,
                observed_coverage=reached.get("coverage", "Undefined"),
            )
            message = Message(
                recipient=condition.creating_user,
                sender=condition.creating_user,
                title=message_text,
                flowcell=flowcell,
            )
            message.save()
            if condition.run_until:
                create_stop_run(run.minion)
            condition.completed = True
            condition.save()
Example #5
0
def test_message_notifier(user: User, admin: User, mocker: MockFixture):
    """Should run a notifier on save."""
    mock = mocker.patch("communication.models.Message.notifier")
    message = Message()
    message.recipient = user
    message.sender = admin
    message.subject = "subject"
    message.body = "body"
    message.save()
    assert mock.call_count == 1
Example #6
0
def create_and_save_message(**kwargs):
    """
    Create and save a message to be sent to the user, about a specific action that has happened.
    Parameters
    ----------
    kwargs: dict
        Dictionary of kwargs including, title, user, flowcell, run

    Returns
    -------
    None

    """
    if kwargs["created"]:
        user = kwargs["user"]
        new_message = Message(
            recipient=user,
            sender=user,
            title=kwargs["title"],
            run=kwargs["run"],
            flowcell=kwargs["flowcell"],
        )
        new_message.save()
Example #7
0
 def test_unread_msg_count(self):
     msg = self.create_message(
         self.user,
         self.course, name="msg2",
         publishdate=datetime.now()
     )
     msg2 = self.create_message(
         self.user,
         self.course,
         name="msg3",
         publishdate=datetime.now()
     )
     # should return 2 unread messages
     self.assertEqual(
         2, Message.unread_message_count(self.user, self.course))
Example #8
0
    def render(self, context):
        # user を定義
        user = context.request.user

        # settings.py の内容
        context['settings'] = settings

        # 未対応のお問い合わせ(事務局員限定)
        if user.is_staff:
            context['contact_open_list'] = \
                Contact.get_contact_queryset(user)

        # 未読のメッセージ(ログインユーザー限定)
        if user.is_authenticated:
            context['message_unread_list'] = \
                Message.get_message_list(user, True)

        return ''
Example #9
0
 def test_get_messages(self):
     # unused
     self.create_message(
         self.user,
         self.course,
         name="msg1",
         publishdate=datetime.now()
     )
     msg2 = self.create_message(
         self.user,
         self.course, name="msg2",
         publishdate=datetime.now()
     )
     msg3 = self.create_message(
         self.user,
         self.course,
         name="msg3",
         publishdate=datetime.now()
     )
     # should return the most recent two in descending order of publishdate
     self.assertEqual(
         [msg3, msg2], Message.get_messages(self.user, self.course, 2))
Example #10
0
def test_notify_new_message(
    user: User,
    admin: User,
    mailoutbox: List[EmailMultiAlternatives],
):
    """Should notify about a new message."""
    message = Message()
    message.recipient = user
    message.sender = admin
    message.subject = "subject"
    message.body = "body"

    notify_new_message(message)
    assert len(mailoutbox) == 1
    assert user.email in mailoutbox[0].recipients()

    notify_new_message(message)
    assert len(mailoutbox) == 2
Example #11
0
 def toDatabase(self, sender, receiver, message):
     """
         Emulates email sending
     """
     m = Message(sender=sender, receiver=receiver, message=message)
     m.save()