Beispiel #1
0
    def test_small(self):
        size, stale = calculate_resource_size(self.root)
        self.assertEqual(10, size)
        self.assertFalse(stale)

        # again, should be cached
        size, stale = calculate_resource_size(self.root)
        self.assertEqual(10, size)
        self.assertFalse(stale)
Beispiel #2
0
    def size(self, request, pk=None):
        if not pk:
            raise Http404

        task_info = None
        node = self.get_object()

        # currently we restrict triggering calculations through the API to the channel root node
        if not node.is_root_node():
            raise Http404

        # we don't force the calculation, so if the channel is large, it returns the cached size
        size, stale = calculate_resource_size(node=node, force=False)
        if stale:
            # When stale, that means the value is not up-to-date with modified files in the DB,
            # and the channel is significantly large, so we'll queue an async task for calculation.
            # We don't really need more than one queued async calculation task, so we use
            # get_or_create_async_task to ensure a task is queued, as well as return info about it
            task_args = dict(node_id=node.pk, channel_id=node.channel_id)
            task_info = get_or_create_async_task("calculate-resource-size",
                                                 self.request.user,
                                                 **task_args)

        changes = []
        if task_info is not None:
            changes.append(self.create_task_event(task_info))

        return Response({"size": size, "stale": stale, "changes": changes})
Beispiel #3
0
 def test_missing__too_big__no_force(self, cache, helper):
     self.node.get_descendant_count.return_value = STALE_MAX_CALCULATION_SIZE + 1
     cache().get_size.return_value = None
     cache().get_modified.return_value = None
     size, stale = calculate_resource_size(self.node)
     self.assertIsNone(size)
     self.assertTrue(stale)
Beispiel #4
0
 def test_cached(self, cache, helper):
     cache().get_size.return_value = 123
     cache().get_modified.return_value = '2021-01-01 00:00:00'
     helper().modified_since.return_value = False
     size, stale = calculate_resource_size(self.node)
     self.assertEqual(123, size)
     self.assertFalse(stale)
Beispiel #5
0
 def test_stale__too_big__no_force(self, cache, helper):
     self.node.get_descendant_count.return_value = STALE_MAX_CALCULATION_SIZE + 1
     cache().get_size.return_value = 123
     cache().get_modified.return_value = '2021-01-01 00:00:00'
     helper().modified_since.return_value = True
     size, stale = calculate_resource_size(self.node)
     self.assertEqual(123, size)
     self.assertTrue(stale)
Beispiel #6
0
 def assertCalculation(self, cache, helper, force=False):
     helper().get_size.return_value = 456
     now_val = isoparse('2021-01-01T00:00:00')
     with mock.patch("contentcuration.utils.nodes.timezone.now") as now:
         now.return_value = now_val
         size, stale = calculate_resource_size(self.node, force=force)
     self.assertEqual(456, size)
     self.assertFalse(stale)
     cache().set_size.assert_called_once_with(456)
     cache().set_modified.assert_called_once_with(now_val)
Beispiel #7
0
def calculate_resource_size_task(node_id, channel_id):
    node = ContentNode.objects.get(pk=node_id)
    size, _ = calculate_resource_size(node=node, force=True)
    return size