add: сбор статистики и вывод агрегированных данных на главную (в футер)
All checks were successful
Build ETPGRF-site / build (push) Successful in 1m47s
All checks were successful
Build ETPGRF-site / build (push) Successful in 1m47s
This commit is contained in:
@@ -12,8 +12,8 @@ services:
|
||||
labels:
|
||||
- "com.centurylinklabs.watchtower.scope=etpgrf"
|
||||
|
||||
# Запускаем collectstatic перед стартом
|
||||
command: sh -c "python etpgrf_site/manage.py collectstatic --noinput && gunicorn --bind 0.0.0.0:8000 --chdir /app/etpgrf_site etpgrf_site.wsgi"
|
||||
# Запускаем миграции, потом collectstatic, потом сервер
|
||||
command: sh -c "python etpgrf_site/manage.py migrate --noinput && python etpgrf_site/manage.py collectstatic --noinput && gunicorn --bind 0.0.0.0:8000 --chdir /app/etpgrf_site etpgrf_site.wsgi"
|
||||
|
||||
volumes:
|
||||
# База данных (папка data должна быть создана на хосте)
|
||||
@@ -72,7 +72,6 @@ services:
|
||||
# Берем учетные данные из .env файла
|
||||
- REPO_USER=${REPO_USER}
|
||||
- REPO_PASS=${REPO_PASS}
|
||||
- WATCHTOWER_REGISTRY_URL=git.cube2.ru
|
||||
# Ограничиваем область видимости только этим проектом
|
||||
- WATCHTOWER_SCOPE=etpgrf
|
||||
# Если нужно указать реестр явно (обычно watchtower сам понимает из имени образа)
|
||||
|
||||
@@ -1,3 +1,33 @@
|
||||
from django.contrib import admin
|
||||
from .models import DailyStat
|
||||
|
||||
# Register your models here.
|
||||
@admin.register(DailyStat)
|
||||
class DailyStatAdmin(admin.ModelAdmin):
|
||||
list_display = (
|
||||
'date',
|
||||
'index_views',
|
||||
'process_requests',
|
||||
'copy_count',
|
||||
'chars_in',
|
||||
'chars_out',
|
||||
'chars_copied',
|
||||
'avg_processing_time_ms_formatted',
|
||||
)
|
||||
list_filter = ('date',)
|
||||
search_fields = ('date',)
|
||||
ordering = ('-date',)
|
||||
|
||||
# Делаем поля только для чтения
|
||||
readonly_fields = [field.name for field in DailyStat._meta.fields]
|
||||
|
||||
def has_add_permission(self, request):
|
||||
# Запрещаем добавлять записи вручную
|
||||
return False
|
||||
|
||||
def has_delete_permission(self, request, obj=None):
|
||||
# Запрещаем удалять записи
|
||||
return False
|
||||
|
||||
@admin.display(description='Среднее время (мс)', ordering='total_processing_time_ms')
|
||||
def avg_processing_time_ms_formatted(self, obj):
|
||||
return f"{obj.avg_processing_time_ms:.2f}"
|
||||
|
||||
@@ -62,10 +62,18 @@
|
||||
</div>
|
||||
|
||||
{# Футер #}
|
||||
<footer class="footer mt-auto py-3">
|
||||
<div class="container">
|
||||
<span class="text-muted small">© Sergei Erjemin, 2025–{% now 'Y' %}.</span>
|
||||
<span class="text-muted small float-end">v0.1.2</span>
|
||||
<footer class="footer mt-auto py-2 mt-4">
|
||||
<div class="container d-flex justify-content-between align-items-center">
|
||||
<span class="text-muted small nowrap">© Sergei Erjemin, 2025–{% now 'Y' %}.</span>
|
||||
|
||||
<span class="text-muted small nowrap"><i class="bi bi-tags me-1" title="Версия библиотеки etpgrf / Версия сайта"></i>v0.1.3 / v0.1.3</span>
|
||||
|
||||
{# Сводная статистика (HTMX) #}
|
||||
<span class="text-muted small" hx-get="{% url 'stats_summary' %}" hx-trigger="load">
|
||||
...
|
||||
</span>
|
||||
|
||||
|
||||
</div>
|
||||
</footer>
|
||||
|
||||
|
||||
12
etpgrf_site/typograph/templates/typograph/stats_summary.html
Normal file
12
etpgrf_site/typograph/templates/typograph/stats_summary.html
Normal file
@@ -0,0 +1,12 @@
|
||||
<span class="me-3" title="Просмотров">
|
||||
<i class="bi bi-eye me-1"></i>{{ views }}
|
||||
</span>
|
||||
<span class="me-3 nowrap" title="На вход обработано текстов/символов">
|
||||
<i class="bi bi-box-arrow-in-right me-1"></i>{{ processed }}/{{ chars_in }}
|
||||
</span>
|
||||
<span class="me-3" title="На выход получено символов">
|
||||
<i class="bi bi-box-arrow-right me-1"></i>{{ chars_out }}
|
||||
</span>
|
||||
<span class="nowrap" title="Скопировано в буфер текстов/символов">
|
||||
<i class="bi bi-clipboard-check me-1"></i>{{ copied }}/{{ chars_copied }}
|
||||
</span>
|
||||
@@ -2,6 +2,8 @@ from django.urls import path
|
||||
from . import views
|
||||
|
||||
urlpatterns = [
|
||||
path(route='', view=views.index, name='index'),
|
||||
path(route='process/', view=views.process_text, name='process_text'),
|
||||
path('', views.index, name='index'),
|
||||
path('process/', views.process_text, name='process_text'),
|
||||
path('stats/summary/', views.get_stats_summary, name='stats_summary'),
|
||||
path('stats/track-copy/', views.track_copy, name='track_copy'),
|
||||
]
|
||||
|
||||
@@ -1,12 +1,79 @@
|
||||
from django.shortcuts import render
|
||||
from django.http import HttpResponse
|
||||
from django.http import HttpResponse, JsonResponse
|
||||
from django.db.models import F, Sum
|
||||
from django.utils import timezone
|
||||
from django.views.decorators.http import require_POST
|
||||
from etpgrf.typograph import Typographer
|
||||
from etpgrf.layout import LayoutProcessor
|
||||
from etpgrf.hyphenation import Hyphenator
|
||||
from .models import DailyStat
|
||||
|
||||
|
||||
def index(request):
|
||||
# Увеличиваем счетчик просмотров главной
|
||||
try:
|
||||
today = timezone.now().date()
|
||||
stat, created = DailyStat.objects.get_or_create(date=today)
|
||||
DailyStat.objects.filter(pk=stat.pk).update(index_views=F('index_views') + 1)
|
||||
except Exception as e:
|
||||
print(f"Stat error: {e}")
|
||||
|
||||
return render(request, template_name='typograph/index.html')
|
||||
|
||||
|
||||
def get_stats_summary(request):
|
||||
"""Возвращает сводную статистику."""
|
||||
# Убираем try...except для отладки
|
||||
stats = DailyStat.objects.aggregate(
|
||||
views=Sum('index_views'),
|
||||
processed=Sum('process_requests'),
|
||||
copied=Sum('copy_count'),
|
||||
chars_in=Sum('chars_in'),
|
||||
chars_out=Sum('chars_out'),
|
||||
chars_copied=Sum('chars_copied')
|
||||
)
|
||||
# print("Aggregated stats:", stats) # DEBUG
|
||||
|
||||
# Функция для форматирования чисел с сокращениями (M, k)
|
||||
def format_large_number(num):
|
||||
if num > 1_000_000:
|
||||
return f"{num / 1_000_000:.3f}M".replace(".", ",")
|
||||
elif num > 1_000:
|
||||
return f"{num / 1_000:.2f}k".replace(".", ",")
|
||||
return str(num)
|
||||
|
||||
context = {
|
||||
'views': f"{(stats['views'] or 0):,}".replace(",", " "),
|
||||
'processed': f"{(stats['processed'] or 0):,}".replace(",", " "),
|
||||
'copied': f"{(stats['copied'] or 0):,}".replace(",", " "),
|
||||
'chars_in': format_large_number(stats['chars_in'] or 0),
|
||||
'chars_out': format_large_number(stats['chars_out'] or 0),
|
||||
'chars_copied': format_large_number(stats['chars_copied'] or 0),
|
||||
}
|
||||
|
||||
return render(request, 'typograph/stats_summary.html', context)
|
||||
|
||||
|
||||
@require_POST
|
||||
def track_copy(request):
|
||||
"""Увеличивает счетчик копирований и количество скопированных символов."""
|
||||
try:
|
||||
char_count = int(request.POST.get('char_count', 0))
|
||||
|
||||
today = timezone.now().date()
|
||||
stat, created = DailyStat.objects.get_or_create(date=today)
|
||||
DailyStat.objects.filter(pk=stat.pk).update(
|
||||
copy_count=F('copy_count') + 1,
|
||||
chars_copied=F('chars_copied') + char_count
|
||||
)
|
||||
return HttpResponse("OK")
|
||||
except (ValueError, TypeError):
|
||||
return HttpResponse("Invalid char_count", status=400)
|
||||
except Exception as e:
|
||||
print(f"Stat error: {e}")
|
||||
return HttpResponse("Error", status=500)
|
||||
|
||||
|
||||
def process_text(request):
|
||||
if request.method == 'POST':
|
||||
text = request.POST.get(key='text', default='')
|
||||
@@ -80,8 +147,25 @@ def process_text(request):
|
||||
|
||||
# Обрабатываем текст
|
||||
processed = typo.process(text)
|
||||
# print("Processed text length:", len(processed))
|
||||
# print("Processed text:", processed)
|
||||
|
||||
# --- СБОР СТАТИСТИКИ ---
|
||||
try:
|
||||
today = timezone.now().date()
|
||||
stat, created = DailyStat.objects.get_or_create(date=today)
|
||||
|
||||
# Обновляем атомарные поля
|
||||
DailyStat.objects.filter(pk=stat.pk).update(
|
||||
process_requests=F('process_requests') + 1,
|
||||
chars_in=F('chars_in') + len(text),
|
||||
chars_out=F('chars_out') + len(processed),
|
||||
# total_processing_time_ms мы пока не считаем, чтобы не усложнять
|
||||
)
|
||||
|
||||
# JSON с настройками пока не пишем, чтобы не усложнять (как договаривались)
|
||||
|
||||
except Exception as e:
|
||||
print(f"Stat error: {e}")
|
||||
# -----------------------
|
||||
|
||||
return render(
|
||||
request,
|
||||
|
||||
@@ -22,8 +22,6 @@ const resultWrapper = document.getElementById('cm-result-wrapper');
|
||||
const btnCopy = document.getElementById('btn-copy');
|
||||
const sourceTextarea = document.querySelector('textarea[name="text"]');
|
||||
|
||||
// console.log("Index.js loaded. btnCopy:", !!btnCopy, "sourceTextarea:", !!sourceTextarea); // DEBUG
|
||||
|
||||
const themeCompartment = new Compartment();
|
||||
function getTheme() {
|
||||
return window.matchMedia('(prefers-color-scheme: dark)').matches ? oneDark : [];
|
||||
@@ -90,7 +88,6 @@ const resultView = new EditorView({
|
||||
|
||||
// Обработка ответа от сервера (HTMX)
|
||||
document.body.addEventListener('htmx:afterSwap', function (evt) {
|
||||
// console.log("HTMX afterSwap event:", evt.detail.target.id); // DEBUG
|
||||
if (evt.detail.target.id === 'result-area') {
|
||||
const newContent = evt.detail.xhr.response;
|
||||
|
||||
@@ -101,7 +98,6 @@ document.body.addEventListener('htmx:afterSwap', function (evt) {
|
||||
|
||||
// Показываем кнопку копирования
|
||||
if (btnCopy) {
|
||||
// console.log("Showing copy button"); // DEBUG
|
||||
btnCopy.classList.remove('d-none');
|
||||
}
|
||||
}
|
||||
@@ -132,13 +128,24 @@ window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', ()
|
||||
if (btnCopy) {
|
||||
btnCopy.addEventListener('click', async () => {
|
||||
const text = resultView.state.doc.toString();
|
||||
// console.log("Copying text:", text.substring(0, 20) + "..."); // DEBUG
|
||||
|
||||
// Отправляем цель в метрику
|
||||
if (typeof window.sendGoal === 'function') {
|
||||
window.sendGoal('etpgrf-copy-pressed');
|
||||
}
|
||||
|
||||
// Отправляем статистику на сервер
|
||||
const ch_count_copy2clipboard = new FormData();
|
||||
ch_count_copy2clipboard.append('char_count', text.length);
|
||||
|
||||
fetch('/stats/track-copy/', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'X-CSRFToken': document.querySelector('[name=csrfmiddlewaretoken]').value
|
||||
},
|
||||
body: ch_count_copy2clipboard
|
||||
}).catch(err => console.error("Ошибка отправки статистики:", err));
|
||||
|
||||
try {
|
||||
await navigator.clipboard.writeText(text);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user