Skip to content

Commit

Permalink
Use django filter to format archive size and make empty result treatm…
Browse files Browse the repository at this point in the history
…ent consistent with other views
  • Loading branch information
rowanseymour committed Oct 23, 2024
1 parent 955bef9 commit 9f40c27
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 63 deletions.
20 changes: 10 additions & 10 deletions temba/api/v2/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,12 +325,12 @@ class ArchivesEndpoint(ListAPIMixin, BaseEndpoint):
A `GET` returns the archives for your organization with the following fields.
* **archive_type** - the type of the archive, one of `message`or `run` (filterable as `archive_type`).
* **archive_type** - the type of the archive, one of `message` or `run` (filterable as `archive_type`).
* **start_date** - the UTC date of the archive (string) (filterable as `before` and `after`).
* **period** - `daily` for daily archives, `monthly` for monthly archives (filterable as `period`).
* **record_count** - number of records in the archive (int).
* **size** - size of the gziped archive content (int).
* **hash** - MD5 hash of the gziped archive (string).
* **size** - size of the gzipped archive content (int).
* **hash** - MD5 hash of the gzipped archive (string).
* **download_url** - temporary download URL of the archive (string).
Example:
Expand All @@ -345,13 +345,13 @@ class ArchivesEndpoint(ListAPIMixin, BaseEndpoint):
"count": 248,
"results": [
{
"archive_type":"message",
"start_date":"2017-02-20",
"period":"daily",
"record_count":1432,
"size":2304,
"hash":"feca9988b7772c003204a28bd741d0d0",
"download_url":"<redacted>"
"archive_type": "message",
"start_date": "2017-02-20",
"period": "daily",
"record_count": 1432,
"size": 2304,
"hash": "feca9988b7772c003204a28bd741d0d0",
"download_url": "https://..."
},
...
}
Expand Down
34 changes: 9 additions & 25 deletions temba/archives/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from django.db.models import Q
from django.utils import timezone

from temba.utils import json, s3, sizeof_fmt
from temba.utils import json, s3
from temba.utils.s3 import EventStreamReader

KEY_PATTERN = re.compile(r"^(?P<org>\d+)/(?P<type>run|message)_(?P<period>(D|M)\d+)_(?P<hash>[0-9a-f]{32})\.jsonl\.gz$")
Expand All @@ -35,43 +35,27 @@ class Archive(models.Model):
archive_type = models.CharField(choices=TYPE_CHOICES, max_length=16)
created_on = models.DateTimeField(default=timezone.now)

# the length of time this archive covers
period = models.CharField(max_length=1, choices=PERIOD_CHOICES, default=PERIOD_DAILY)
start_date = models.DateField() # the earliest modified_on date for records (inclusive)
record_count = models.IntegerField(default=0) # number of records in this archive
size = models.BigIntegerField(default=0) # size in bytes of the archive contents (after compression)
hash = models.TextField() # MD5 hash of the archive contents (after compression)
url = models.URLField() # full URL of this archive
build_time = models.IntegerField() # time in ms it took to build and upload this archive

# the earliest modified_on date for records in this archive (inclusive)
start_date = models.DateField()

# number of records in this archive
record_count = models.IntegerField(default=0)

# size in bytes of the archive contents (after compression)
size = models.BigIntegerField(default=0)

# MD5 hash of the archive contents (after compression)
hash = models.TextField()

# full URL of this archive
url = models.URLField()
# archive we were rolled up into, if any
rollup = models.ForeignKey("archives.Archive", on_delete=models.PROTECT, null=True)

# whether the records in this archive need to be deleted
needs_deletion = models.BooleanField(default=False)

# number of milliseconds it took to build and upload this archive
build_time = models.IntegerField()

# archive we were rolled up into, if any
rollup = models.ForeignKey("archives.Archive", on_delete=models.PROTECT, null=True)

# when this archive's records where deleted (if any)
deleted_on = models.DateTimeField(null=True)

@classmethod
def storage(cls):
return storages["archives"]

def size_display(self):
return sizeof_fmt(self.size)

def get_storage_location(self) -> tuple:
"""
Returns a tuple of the storage bucket and key
Expand Down
8 changes: 0 additions & 8 deletions temba/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,6 @@ def format_number(val):
return val


def sizeof_fmt(num, suffix="b"):
for unit in ["", "K", "M", "G", "T", "P", "E", "Z"]:
if abs(num) < 1024.0:
return "%3.1f %s%s" % (num, unit, suffix)
num /= 1024.0
return "%.1f %s%s" % (num, "Y", suffix)


def chunk_list(iterable, size):
"""
Splits a very large list into evenly sized chunks.
Expand Down
13 changes: 0 additions & 13 deletions temba/utils/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
percentage,
redact,
set_nested_key,
sizeof_fmt,
str_to_bool,
)
from .checks import storage
Expand All @@ -40,18 +39,6 @@


class InitTest(TembaTest):
def test_sizeof_fmt(self):
self.assertEqual("512.0 b", sizeof_fmt(512))
self.assertEqual("1.0 Kb", sizeof_fmt(1024))
self.assertEqual("1.0 Mb", sizeof_fmt(1024**2))
self.assertEqual("1.0 Gb", sizeof_fmt(1024**3))
self.assertEqual("1.0 Tb", sizeof_fmt(1024**4))
self.assertEqual("1.0 Pb", sizeof_fmt(1024**5))
self.assertEqual("1.0 Eb", sizeof_fmt(1024**6))
self.assertEqual("1.0 Zb", sizeof_fmt(1024**7))
self.assertEqual("1.0 Yb", sizeof_fmt(1024**8))
self.assertEqual("1024.0 Yb", sizeof_fmt(1024**9))

def test_str_to_bool(self):
self.assertFalse(str_to_bool(None))
self.assertFalse(str_to_bool(""))
Expand Down
16 changes: 9 additions & 7 deletions templates/archives/archive_list.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
{% extends "orgs/base/list.html" %}
{% load i18n humanize temba %}

{% block pre-table %}
<div class="mb-4">
{% blocktrans trimmed %}
Archives are created after 90 days of inactivity for messages and flow runs.
{% endblocktrans %}
</div>
{% endblock pre-table %}
{% block table %}
<table class="list lined scrolled">
<thead>
Expand All @@ -19,7 +26,7 @@
{% endif %}
</td>
<td style="text-align:right">{{ archive.record_count|intcomma }}</td>
<td style="text-align:right">{{ archive.size_display }}</td>
<td style="text-align:right">{{ archive.size|filesizeformat }}</td>
<td class="w-10" style="--icon-color:#bbb">
<temba-icon clickable="true"
name="download"
Expand All @@ -29,12 +36,7 @@
</tr>
{% empty %}
<tr class="empty_list">
<td colspan="99" class="text-center">
{% blocktrans trimmed %}
No archives found. Archives are created after 90 days of inactivity for messages and flow runs. Check back later to
see a list of all archives.
{% endblocktrans %}
</td>
<td colspan="99" class="text-center">{% trans "No archives" %}</td>
</tr>
{% endfor %}
</table>
Expand Down

0 comments on commit 9f40c27

Please sign in to comment.