Example #1
0
 def test_send_payload_stores_unknown_node_into_db(self, mock_handle_receive, mock_send_payload,
                                                   mock_pod_preferences):
     process(Mock())
     assert Node.select().count() == 1
     node = Node.get(Node.host=="sub.example.com")
     assert node
     assert node.https
Example #2
0
 def test_get_send_to_nodes_with_like_returns_nodes_for_post(self):
     Node.create(host="sub.example.com")
     save_post_metadata(DiasporaPost(guid="12345"), "diaspora",
                        ["sub.example.com"])
     nodes = get_send_to_nodes("*****@*****.**",
                               DiasporaLike(target_guid="12345"))
     assert nodes == ["sub.example.com"]
Example #3
0
 def test_send_payload_success_clears_existing_node_failure_count(
     self, mock_handle_receive, mock_send_document, mock_pod_preferences
 ):
     Node.create(host="sub.example.com", last_success=datetime.datetime.now(), failure_count=1)
     process(Mock())
     node = Node.get(Node.host == "sub.example.com")
     assert node.failure_count == 0
Example #4
0
def get_count_stats():
    day_ago = datetime.datetime.now() - datetime.timedelta(hours=24)
    week_ago = datetime.datetime.now() - datetime.timedelta(days=7)
    month_ago = datetime.datetime.now() - datetime.timedelta(days=30)
    incoming = {
        "today": ReceiveStatistic.select().where(ReceiveStatistic.created_at >= day_ago).count(),
        "week": ReceiveStatistic.select().where(ReceiveStatistic.created_at >= week_ago).count(),
        "month": ReceiveStatistic.select().where(ReceiveStatistic.created_at >= month_ago).count(),
        "all_time": ReceiveStatistic.select().count(),
    }
    outgoing = {
        "today": WorkerReceiveStatistic.select().where(WorkerReceiveStatistic.created_at >= day_ago).count(),
        "week": WorkerReceiveStatistic.select().where(WorkerReceiveStatistic.created_at >= week_ago).count(),
        "month": WorkerReceiveStatistic.select().where(WorkerReceiveStatistic.created_at >= month_ago).count(),
        "all_time": WorkerReceiveStatistic.select().count(),
    }
    distinct_nodes = {
        "all": Node.select().count(),
        "https": Node.select().where(Node.https==True).count(),
    }
    processing = {
        "workers": get_worker_count(),
        "queue_jobs": len(public_queue),
    }
    return incoming, outgoing, distinct_nodes, processing
Example #5
0
 def test_send_payload_updates_existing_node_in_db(
     self, mock_handle_receive, mock_send_document, mock_pod_preferences
 ):
     Node.create(host="sub.example.com", last_success=datetime.datetime.now())
     process(Mock())
     node = Node.get(Node.host == "sub.example.com")
     assert node.total_delivered == 1
     assert node.https
Example #6
0
 def test_send_payload_failure_updates_existing_node_failure_count(
     self, mock_handle_receive, mock_send_payload, mock_pod_preferences
 ):
     Node.create(host="sub.example.com", last_success=datetime.datetime.now())
     mock_send_payload.return_value = {"result": False, "https": False}
     process(Mock())
     node = Node.get(Node.host == "sub.example.com")
     assert node.failure_count == 1
Example #7
0
 def test_send_payload_success_clears_existing_node_failure_count(
         self, mock_handle_receive, mock_send_document,
         mock_pod_preferences):
     Node.create(host="sub.example.com",
                 last_success=datetime.datetime.now(),
                 failure_count=1)
     process(Mock())
     node = Node.get(Node.host == "sub.example.com")
     assert node.failure_count == 0
Example #8
0
 def test_send_payload_stores_unknown_node_into_db(self,
                                                   mock_handle_receive,
                                                   mock_send_document,
                                                   mock_pod_preferences):
     process(Mock())
     assert Node.select().count() == 1
     node = Node.get(Node.host == "sub.example.com")
     assert node
     assert node.https
Example #9
0
 def test_send_payload_failure_updates_existing_node_failure_count(
         self, mock_handle_receive, mock_send_payload,
         mock_pod_preferences):
     Node.create(host="sub.example.com",
                 last_success=datetime.datetime.now())
     mock_send_payload.return_value = {"result": False, "https": False}
     process(Mock())
     node = Node.get(Node.host == "sub.example.com")
     assert node.failure_count == 1
Example #10
0
 def test_send_payload_updates_existing_node_in_db(self,
                                                   mock_handle_receive,
                                                   mock_send_document,
                                                   mock_pod_preferences):
     Node.create(host="sub.example.com",
                 last_success=datetime.datetime.now())
     process(Mock())
     node = Node.get(Node.host == "sub.example.com")
     assert node.total_delivered == 1
     assert node.https
Example #11
0
def get_count_stats():
    day_ago = datetime.datetime.now() - datetime.timedelta(hours=24)
    week_ago = datetime.datetime.now() - datetime.timedelta(days=7)
    month_ago = datetime.datetime.now() - datetime.timedelta(days=30)
    incoming = {
        "today":
        ReceiveStatistic.select().where(
            ReceiveStatistic.created_at >= day_ago).count(),
        "week":
        ReceiveStatistic.select().where(
            ReceiveStatistic.created_at >= week_ago).count(),
        "month":
        ReceiveStatistic.select().where(
            ReceiveStatistic.created_at >= month_ago).count(),
        "all_time":
        ReceiveStatistic.select().count(),
    }
    outgoing = {
        "today":
        WorkerReceiveStatistic.select().where(
            WorkerReceiveStatistic.created_at >= day_ago).count(),
        "week":
        WorkerReceiveStatistic.select().where(
            WorkerReceiveStatistic.created_at >= week_ago).count(),
        "month":
        WorkerReceiveStatistic.select().where(
            WorkerReceiveStatistic.created_at >= month_ago).count(),
        "all_time":
        WorkerReceiveStatistic.select().count(),
    }
    distinct_nodes = {
        "all": Node.select().count(),
        "https": Node.select().where(Node.https == True).count(),
    }
    processing = {
        "workers": get_worker_count(),
        "queue_jobs": len(public_queue),
    }
    return incoming, outgoing, distinct_nodes, processing
Example #12
0
def save_post_metadata(entity, protocol, hosts):
    """Save Post metadata to db.

    :param entity: DiasporaPost entity
    :param protocol: Protocol identifier
    :param hosts: List of hostnames that send was done successfully
    """
    try:
        post, created = Post.get_or_create(guid=entity.guid, protocol=protocol)
        for host in hosts:
            post.nodes.add(Node.get(host=host))
    except Exception as ex:
        logging.warning("Exception when trying to save post '{entity}' into database: {exc}".format(
            entity=entity, exc=ex))
Example #13
0
def save_post_metadata(entity, protocol, hosts):
    """Save Post metadata to db.

    :param entity: DiasporaPost entity
    :param protocol: Protocol identifier
    :param hosts: List of hostnames that send was done successfully
    """
    try:
        post, created = Post.get_or_create(guid=entity.guid, protocol=protocol)
        for host in hosts:
            post.nodes.add(Node.get(host=host))
    except Exception as ex:
        logging.warning("Exception when trying to save post '{entity}' into database: {exc}".format(
            entity=entity, exc=ex))
Example #14
0
def update_node(pod, response):
    """Update Node in database

    :param pod: Hostname
    :param response: Dictionary with booleans "result" and "https"
    """
    try:
        Node.get_or_create(host=pod)
        if response["result"]:
            # Update delivered_count and last_success, nullify failure_count
            Node.update(
                last_success=datetime.datetime.now(), total_delivered=Node.total_delivered + 1,
                failure_count=0, https=response["https"]).where(Node.host==pod).execute()
        else:
            # Update failure_count
            Node.update(
                failure_count=Node.failure_count + 1).where(Node.host==pod).execute()
    except Exception as ex:
        logging.warning("Exception when trying to save or update Node {node} into database: {exc}".format(
            node=pod, exc=ex)
        )
Example #15
0
def update_node(pod, response):
    """Update Node in database

    :param pod: Hostname
    :param response: Dictionary with booleans "result" and "https"
    """
    try:
        Node.get_or_create(host=pod)
        if response["result"]:
            # Update delivered_count and last_success, nullify failure_count
            Node.update(
                last_success=datetime.datetime.now(), total_delivered=Node.total_delivered + 1,
                failure_count=0, https=response["https"]).where(Node.host==pod).execute()
        else:
            # Update failure_count
            Node.update(
                failure_count=Node.failure_count + 1).where(Node.host==pod).execute()
    except Exception as ex:
        logging.warning("Exception when trying to save or update Node {node} into database: {exc}".format(
            node=pod, exc=ex)
        )
Example #16
0
def update_node(node, success):
    """Update Node in database

    :param node: Hostname
    :param success: Was the last call a success (bool)
    """
    try:
        Node.get_or_create(host=node)
        if success:
            # Update delivered_count and last_success, nullify failure_count
            # TODO: remove the whole https tracking - we're only delivering to https now
            Node.update(
                last_success=datetime.datetime.now(), total_delivered=Node.total_delivered + 1,
                failure_count=0, https=True).where(Node.host == node).execute()
        else:
            # Update failure_count
            Node.update(
                failure_count=Node.failure_count + 1).where(Node.host == node).execute()
    except Exception as ex:
        logging.warning("Exception when trying to save or update Node {node} into database: {exc}".format(
            node=node, exc=ex)
        )
Example #17
0
 def test_get_send_to_nodes_with_like_returns_no_nodes_for_unknown_post(self):
     Node.create(host="sub.example.com")
     save_post_metadata(DiasporaPost(guid="12345"), "diaspora", ["sub.example.com"])
     nodes = get_send_to_nodes("*****@*****.**", DiasporaLike(target_guid="54321"))
     assert nodes == []