-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from ISSResearch/feat-stat-export
Feat stat export
- Loading branch information
Showing
20 changed files
with
525 additions
and
392 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
from abc import ABC, abstractmethod | ||
from typing import Any, List, Dict | ||
from io import BytesIO | ||
from json import dumps | ||
from xlsxwriter import Workbook | ||
from xlsxwriter.worksheet import Worksheet | ||
|
||
ROW = Dict[str, Any] | ||
DATA = List[ROW] | ||
ENCODING = "utf-8" | ||
IMPLEMENTED = {"json", "csv", "xlsx"} | ||
|
||
|
||
class Export(ABC): | ||
ATTRIBUTE_HEADERS = "Attribute,Level,Validation Images,Validation Videos,Accepted Images, Accepted Videos,Declined Images, Declined Videos,Total\n" | ||
USER_HEADERS = "User,Validation Images,Validation Videos,Accepted Images, Accepted Videos,Declined Images, Declined Videos,Total\n" | ||
|
||
t_val = lambda _, x: (x.get('image', 0), x.get('video', 0)) | ||
sm = lambda _, x, y, z: sum(x + y + z) | ||
|
||
def __init__(self, data: DATA, *args): | ||
self._data = data | ||
self._type = args[0] | ||
|
||
@abstractmethod | ||
def into_response(self) -> BytesIO: ... | ||
|
||
@property | ||
def _data(self) -> DATA: return self.__data | ||
|
||
@_data.setter | ||
def _data(self, data: DATA): self.__data = data | ||
|
||
|
||
class JSON(Export): | ||
def into_response(self) -> BytesIO: | ||
file = BytesIO() | ||
|
||
prepared_data = bytes(dumps(self._data), encoding=ENCODING) | ||
|
||
file.write(prepared_data) | ||
file.seek(0) | ||
|
||
return file | ||
|
||
|
||
class CSV(Export): | ||
def _write_attribute(self, dest: BytesIO, data: ROW): | ||
name = data.get("name") | ||
level_name = data.get("levelName") | ||
children = data.get("children", []) | ||
|
||
val = self.t_val(data.get("v", {})) | ||
acc = self.t_val(data.get("a", {})) | ||
dec = self.t_val(data.get("d", {})) | ||
total = self.sm(val, acc, dec) | ||
|
||
dest.write(bytes( | ||
f"{name},{level_name},{val[0]},{val[1]},{acc[0]},{acc[1]},{dec[0]},{dec[1]},{total}\n", | ||
encoding=ENCODING | ||
)) | ||
|
||
for child in children: self._write_attribute(dest, child) | ||
|
||
def _write_user(self, dest: BytesIO, data: ROW): | ||
name = data.get("name") | ||
|
||
val = self.t_val(data.get("v", {})) | ||
acc = self.t_val(data.get("a", {})) | ||
dec = self.t_val(data.get("d", {})) | ||
total = self.sm(val, acc, dec) | ||
|
||
dest.write(bytes( | ||
f"{name},{val[0]},{val[1]},{acc[0]},{acc[1]},{dec[0]},{dec[1]},{total}\n", | ||
encoding=ENCODING | ||
)) | ||
|
||
def into_response(self) -> BytesIO: | ||
file = BytesIO() | ||
|
||
match self._type: | ||
case "attribute": | ||
headers = self.ATTRIBUTE_HEADERS | ||
write = self._write_attribute | ||
case "user": | ||
headers = self.USER_HEADERS | ||
write = self._write_user | ||
case _: raise AttributeError | ||
|
||
file.write(bytes(headers, encoding=ENCODING)) | ||
for row in self._data: write(file, row) | ||
|
||
file.seek(0) | ||
|
||
return file | ||
|
||
|
||
class XLS(Export): | ||
__row_n = 0 | ||
|
||
@property | ||
def _row_n(self) -> int: return self.__row_n | ||
|
||
@_row_n.setter | ||
def _row_n(self, new: int): self.__row_n = new | ||
|
||
def _write_attribute(self, dest: Worksheet, data: ROW): | ||
name = data.get("name") | ||
level_name = data.get("levelName") | ||
children = data.get("children", []) | ||
|
||
val = self.t_val(data.get("v", {})) | ||
acc = self.t_val(data.get("a", {})) | ||
dec = self.t_val(data.get("d", {})) | ||
total = self.sm(val, acc, dec) | ||
|
||
row = (name, level_name, val[0], val[1], acc[0], acc[1], dec[0], dec[1], total) | ||
|
||
for i, item in enumerate(row): dest.write(self._row_n, i, item) | ||
self._row_n += 1 | ||
|
||
for child in children: self._write_attribute(dest, child) | ||
|
||
def _write_user(self, dest: Worksheet, data: ROW): | ||
name = data.get("name") | ||
|
||
val = self.t_val(data.get("v", {})) | ||
acc = self.t_val(data.get("a", {})) | ||
dec = self.t_val(data.get("d", {})) | ||
total = self.sm(val, acc, dec) | ||
|
||
row = (name, val[0], val[1], acc[0], acc[1], dec[0], dec[1], total) | ||
|
||
for i, item in enumerate(row): dest.write(self._row_n, i, item) | ||
self._row_n += 1 | ||
|
||
def into_response(self) -> BytesIO: | ||
file = BytesIO() | ||
|
||
match self._type: | ||
case "attribute": | ||
headers = self.ATTRIBUTE_HEADERS | ||
write = self._write_attribute | ||
case "user": | ||
headers = self.USER_HEADERS | ||
write = self._write_user | ||
case _: raise AttributeError | ||
|
||
xl = Workbook(file) | ||
sheet = xl.add_worksheet() | ||
|
||
headers = headers.split(",") | ||
|
||
for i, header in enumerate(headers): sheet.write(self._row_n, i, header) | ||
self._row_n += 1 | ||
for row in self._data: write(sheet, row) | ||
|
||
xl.close() | ||
file.seek(0) | ||
|
||
return file |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from file.export import JSON, CSV | ||
from file.services import StatsServices | ||
from django.test import TestCase | ||
from attribute.attribute_tests.mock_attribute import MockCase | ||
from json import loads | ||
|
||
|
||
class ExportTest(TestCase): | ||
@classmethod | ||
def setUpClass(cls): | ||
super().setUpClass() | ||
cls.case = MockCase() | ||
cls.attr_stat, _ = StatsServices.from_attribute(cls.case.project.id) | ||
cls.user_stat, _ = StatsServices.from_user(cls.case.project.id) | ||
|
||
# TODO: | ||
def test_xls(self): ... | ||
|
||
def test_csv(self): | ||
attr_res = CSV(self.attr_stat, "attribute").into_response() | ||
user_res = CSV(self.user_stat, "user").into_response() | ||
|
||
attributes = attr_res.read().decode().split("\n") | ||
users = user_res.read().decode().split("\n") | ||
|
||
self.assertTrue(len(attributes) == len(users) == 3) | ||
# TODO: | ||
|
||
def test_json(self): | ||
attr_res = JSON(self.attr_stat, 0).into_response() | ||
user_res = JSON(self.user_stat, 0).into_response() | ||
self.assertEqual(self.attr_stat, loads(attr_res.read().decode())) | ||
self.assertEqual(self.user_stat, loads(user_res.read().decode())) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.