Finish statistics subsystem refactor.
This commit is contained in:
@@ -74,7 +74,8 @@ from .permissions import (
|
||||
from .settings import setting_thumbnail_size
|
||||
from .statistics import (
|
||||
new_documents_per_month, new_document_pages_per_month,
|
||||
new_document_versions_per_month
|
||||
new_document_versions_per_month, total_document_per_month,
|
||||
total_document_page_per_month, total_document_version_per_month
|
||||
)
|
||||
from .widgets import document_thumbnail
|
||||
|
||||
@@ -352,24 +353,48 @@ class DocumentsApp(MayanAppConfig):
|
||||
links=(link_transformation_list,), sources=(DocumentPage,)
|
||||
)
|
||||
|
||||
namespace = StatisticNamespace(name='documents', label=_('Documents'))
|
||||
namespace = StatisticNamespace(slug='documents', label=_('Documents'))
|
||||
namespace.add_statistic(
|
||||
slug='new-documents-per-month',
|
||||
label=_('New documents per month'),
|
||||
func=new_documents_per_month,
|
||||
renderer=CharJSLine
|
||||
renderer=CharJSLine,
|
||||
minute='0'
|
||||
)
|
||||
namespace.add_statistic(
|
||||
slug='new-document-versions-per-month',
|
||||
label=_('New document versions per month'),
|
||||
func=new_document_versions_per_month,
|
||||
renderer=CharJSLine
|
||||
renderer=CharJSLine,
|
||||
minute='0'
|
||||
)
|
||||
namespace.add_statistic(
|
||||
slug='new-document-pages-per-month',
|
||||
label=_('New document pages per month'),
|
||||
func=new_document_pages_per_month,
|
||||
renderer=CharJSLine
|
||||
renderer=CharJSLine,
|
||||
minute='0'
|
||||
)
|
||||
namespace.add_statistic(
|
||||
slug='total-documents-at-each-month',
|
||||
label=_('Total documents at each month'),
|
||||
func=total_document_per_month,
|
||||
renderer=CharJSLine,
|
||||
minute='0'
|
||||
)
|
||||
namespace.add_statistic(
|
||||
slug='total-document-versions-at-each-month',
|
||||
label=_('Total document versions at each month'),
|
||||
func=total_document_version_per_month,
|
||||
renderer=CharJSLine,
|
||||
minute='0'
|
||||
)
|
||||
namespace.add_statistic(
|
||||
slug='total-document-pages-at-each-month',
|
||||
label=_('Total document pages at each month'),
|
||||
func=total_document_page_per_month,
|
||||
renderer=CharJSLine,
|
||||
minute='0'
|
||||
)
|
||||
|
||||
post_initial_setup.connect(
|
||||
|
||||
@@ -5,45 +5,11 @@ import datetime
|
||||
import qsstats
|
||||
|
||||
from django.db.models import Avg, Count, Max, Min
|
||||
from django.template.defaultfilters import filesizeformat
|
||||
from django.utils import formats
|
||||
from django.utils.encoding import force_text
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from statistics.classes import Statistic
|
||||
|
||||
from .models import Document, DocumentType, DocumentPage, DocumentVersion
|
||||
from .runtime import storage_backend
|
||||
|
||||
|
||||
def get_used_size(path, file_list):
|
||||
total_size = 0
|
||||
for filename in file_list:
|
||||
try:
|
||||
total_size += storage_backend.size(
|
||||
storage_backend.separator.join([path, filename])
|
||||
)
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
return total_size
|
||||
|
||||
|
||||
def storage_count(path='.'):
|
||||
try:
|
||||
directories, files = storage_backend.listdir(path)
|
||||
except OSError:
|
||||
return 0, 0
|
||||
else:
|
||||
total_count = len(files)
|
||||
total_size = get_used_size(path, files)
|
||||
|
||||
for directory in directories:
|
||||
file_count, files_size = storage_count(directory)
|
||||
total_count += file_count
|
||||
total_size += files_size
|
||||
|
||||
return total_count, total_size
|
||||
|
||||
|
||||
def new_documents_per_month():
|
||||
@@ -54,7 +20,7 @@ def new_documents_per_month():
|
||||
|
||||
return {
|
||||
'series': {
|
||||
'Document': map(lambda x: {x[0].month: x[1]}, qss.time_series(start=this_year, end=today, interval='months'))
|
||||
'Documents': map(lambda x: {x[0].month: x[1]}, qss.time_series(start=this_year, end=today, interval='months'))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,7 +33,7 @@ def new_document_versions_per_month():
|
||||
|
||||
return {
|
||||
'series': {
|
||||
'Document': map(lambda x: {x[0].month: x[1]}, qss.time_series(start=this_year, end=today, interval='months'))
|
||||
'Versions': map(lambda x: {x[0].month: x[1]}, qss.time_series(start=this_year, end=today, interval='months'))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,45 +46,83 @@ def new_document_pages_per_month():
|
||||
|
||||
return {
|
||||
'series': {
|
||||
'Document': map(lambda x: {x[0].month: x[1]}, qss.time_series(start=this_year, end=today, interval='months'))
|
||||
'Pages': map(lambda x: {x[0].month: x[1]}, qss.time_series(start=this_year, end=today, interval='months'))
|
||||
}
|
||||
}
|
||||
|
||||
def total_document_per_month():
|
||||
qss = qsstats.QuerySetStats(Document.objects.all(), 'date_added')
|
||||
this_year = datetime.date.today().year
|
||||
|
||||
result = []
|
||||
|
||||
for month in range(1, datetime.date.today().month + 1):
|
||||
next_month = month + 1
|
||||
print month
|
||||
|
||||
if next_month == 12:
|
||||
next_month = 1
|
||||
year = this_year + 1
|
||||
else:
|
||||
next_month = month + 1
|
||||
year = this_year
|
||||
|
||||
result.append({month: qss.until(datetime.date(year, next_month, 1))})
|
||||
|
||||
return {
|
||||
'series': {
|
||||
'Documents': result
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
"""
|
||||
def total_document_version_per_month():
|
||||
qss = qsstats.QuerySetStats(DocumentVersion.objects.all(), 'document__date_added')
|
||||
this_year = datetime.date.today().year
|
||||
|
||||
class DocumentUsageStatistics(Statistic):
|
||||
def get_results(self):
|
||||
results = []
|
||||
result = []
|
||||
|
||||
total_db_documents = Document.objects.only('pk',).count()
|
||||
for month in range(1, datetime.date.today().month + 1):
|
||||
next_month = month + 1
|
||||
print month
|
||||
|
||||
results.extend(
|
||||
[
|
||||
_('Documents in database: %d') % total_db_documents,
|
||||
]
|
||||
)
|
||||
if next_month == 12:
|
||||
next_month = 1
|
||||
year = this_year + 1
|
||||
else:
|
||||
next_month = month + 1
|
||||
year = this_year
|
||||
|
||||
try:
|
||||
total_storage_documents, storage_used_space = storage_count()
|
||||
results.append(
|
||||
_('Documents in storage: %d') % total_storage_documents
|
||||
)
|
||||
results.append(
|
||||
_(
|
||||
'Space used in storage: %s'
|
||||
) % filesizeformat(storage_used_space)
|
||||
)
|
||||
except NotImplementedError:
|
||||
pass
|
||||
result.append({month: qss.until(datetime.date(year, next_month, 1))})
|
||||
|
||||
results.extend(
|
||||
[
|
||||
_(
|
||||
'Document pages in database: %d'
|
||||
) % DocumentPage.objects.only('pk',).count(),
|
||||
]
|
||||
)
|
||||
return {
|
||||
'series': {
|
||||
'Versions': result
|
||||
}
|
||||
}
|
||||
|
||||
return results
|
||||
"""
|
||||
|
||||
def total_document_page_per_month():
|
||||
qss = qsstats.QuerySetStats(DocumentPage.objects.all(), 'document_version__document__date_added')
|
||||
this_year = datetime.date.today().year
|
||||
|
||||
result = []
|
||||
|
||||
for month in range(1, datetime.date.today().month + 1):
|
||||
next_month = month + 1
|
||||
print month
|
||||
|
||||
if next_month == 12:
|
||||
next_month = 1
|
||||
year = this_year + 1
|
||||
else:
|
||||
next_month = month + 1
|
||||
year = this_year
|
||||
|
||||
result.append({month: qss.until(datetime.date(year, next_month, 1))})
|
||||
|
||||
return {
|
||||
'series': {
|
||||
'Pages': result
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user