Files
ewoooc/templates/dashboard_v2.html

701 lines
26 KiB
HTML

{% extends 'ewoooc_base.html' %}
{% block title %}EwoooC 商品看板{% endblock %}
{% block extra_css %}
<style>
.dashboard-v2-stack {
display: grid;
gap: 24px;
}
.dashboard-section-label {
display: flex;
align-items: baseline;
gap: 10px;
margin-bottom: 12px;
}
.dashboard-section-label .num {
color: var(--momo-text-tertiary);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.08em;
}
.dashboard-section-label .title {
color: var(--momo-text-primary);
font-size: 13px;
font-weight: 800;
letter-spacing: 0;
}
.dashboard-section-label .meta {
margin-left: auto;
color: var(--momo-text-tertiary);
font-size: 10px;
}
.dashboard-kpi-grid {
display: grid;
grid-template-columns: repeat(4, minmax(0, 1fr));
overflow: hidden;
background: var(--momo-bg-surface);
border: 1px solid var(--momo-border-light);
border-radius: 8px;
}
.dashboard-kpi {
min-width: 0;
padding: 20px 24px;
border-right: 1px solid var(--momo-border-light);
}
.dashboard-kpi:last-child {
border-right: 0;
}
.dashboard-kpi.is-accent {
color: var(--momo-text-inverse);
background: var(--momo-ink);
}
.dashboard-kpi-label {
margin-bottom: 10px;
color: var(--momo-text-tertiary);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.10em;
text-transform: uppercase;
}
.dashboard-kpi.is-accent .dashboard-kpi-label,
.dashboard-kpi.is-accent .dashboard-kpi-sub {
color: rgba(250, 247, 240, 0.68);
}
.dashboard-kpi-value {
margin-bottom: 8px;
color: var(--momo-text-primary);
font-size: 44px;
font-weight: 800;
letter-spacing: -0.04em;
line-height: 1;
}
.dashboard-kpi-value.is-danger {
color: var(--momo-danger);
}
.dashboard-kpi-value.is-success {
color: var(--momo-success);
}
.dashboard-kpi.is-accent .dashboard-kpi-value {
color: var(--momo-text-inverse);
}
.dashboard-kpi-sub {
color: var(--momo-text-secondary);
font-size: 11px;
}
.dashboard-focus-grid {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 12px;
}
.dashboard-focus-card,
.dashboard-filter-card,
.dashboard-table-card {
background: var(--momo-bg-surface);
border: 1px solid var(--momo-border-light);
border-radius: 8px;
}
.dashboard-focus-card {
min-width: 0;
padding: 18px;
}
.dashboard-focus-label {
margin-bottom: 8px;
color: var(--momo-text-tertiary);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.10em;
text-transform: uppercase;
}
.dashboard-focus-title {
margin-bottom: 4px;
color: var(--momo-text-primary);
font-size: 16px;
font-weight: 800;
line-height: 1.35;
}
.dashboard-focus-number {
margin-bottom: 6px;
color: var(--momo-danger);
font-size: 24px;
font-weight: 800;
letter-spacing: -0.02em;
line-height: 1;
}
.dashboard-focus-sub {
color: var(--momo-text-secondary);
font-size: 11px;
}
.dashboard-filter-card {
padding: 12px 16px;
}
.dashboard-filter-form {
display: flex;
align-items: center;
gap: 12px;
flex-wrap: wrap;
}
.dashboard-search,
.dashboard-select {
min-height: 34px;
color: var(--momo-text-primary);
background: var(--momo-bg-surface);
border: 1px solid var(--momo-border);
border-radius: 4px;
font-size: 12px;
}
.dashboard-search {
width: min(320px, 100%);
padding: 7px 12px;
}
.dashboard-select {
min-width: 160px;
padding: 7px 12px;
}
.dashboard-segmented {
display: inline-flex;
padding: 2px;
gap: 0;
background: var(--momo-bg-paper);
border: 1px solid var(--momo-border-light);
border-radius: 4px;
}
.dashboard-segmented a {
padding: 5px 12px;
color: var(--momo-text-secondary);
border-radius: 3px;
font-size: 12px;
font-weight: 800;
text-decoration: none;
transition: var(--momo-transition-base);
}
.dashboard-segmented a:hover {
color: var(--momo-text-primary);
background: var(--momo-bg-subtle);
}
.dashboard-segmented a.is-active {
color: var(--momo-text-inverse);
background: var(--momo-ink);
}
.dashboard-action-link,
.dashboard-action-button {
display: inline-flex;
align-items: center;
justify-content: center;
gap: 6px;
min-height: 30px;
padding: 6px 12px;
color: var(--momo-text-primary);
background: var(--momo-bg-surface);
border: 1px solid var(--momo-border);
border-radius: 4px;
font-size: 12px;
font-weight: 800;
text-decoration: none;
transition: var(--momo-transition-base);
}
.dashboard-action-button.is-primary {
color: var(--momo-text-inverse);
background: var(--momo-ink);
border-color: var(--momo-ink);
}
.dashboard-action-link:hover,
.dashboard-action-button:hover {
color: var(--momo-text-primary);
background: var(--momo-bg-subtle);
}
.dashboard-action-button.is-primary:hover {
color: var(--momo-text-inverse);
background: var(--momo-ink-soft);
}
.dashboard-table-head {
display: flex;
align-items: center;
gap: 12px;
padding: 14px 20px;
border-bottom: 1px solid var(--momo-border-light);
flex-wrap: wrap;
}
.dashboard-table-title {
color: var(--momo-text-primary);
font-size: 14px;
font-weight: 800;
}
.dashboard-table-meta {
color: var(--momo-text-secondary);
font-size: 11px;
}
.dashboard-table-wrap {
overflow-x: auto;
}
.dashboard-table {
width: 100%;
min-width: 980px;
border-collapse: collapse;
font-size: var(--momo-font-size-sm);
}
.dashboard-table th {
padding: 11px 14px;
color: var(--momo-text-tertiary);
background: var(--momo-bg-paper);
border-bottom: 1px solid var(--momo-border-light);
font-family: var(--momo-font-family-mono);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.10em;
text-transform: uppercase;
white-space: nowrap;
}
.dashboard-table th a {
color: inherit;
text-decoration: none;
}
.dashboard-table td {
padding: 14px;
border-bottom: 1px solid var(--momo-border-light);
vertical-align: middle;
}
.dashboard-table tbody tr {
transition: var(--momo-transition-base);
}
.dashboard-table tbody tr:hover {
background: var(--momo-bg-paper);
}
.dashboard-category {
display: inline-flex;
max-width: 120px;
padding: 3px 8px;
overflow: hidden;
color: var(--momo-text-primary);
background: var(--momo-bg-subtle);
border: 1px solid var(--momo-border-light);
border-radius: var(--momo-radius-pill);
font-size: 11px;
font-weight: 700;
text-overflow: ellipsis;
white-space: nowrap;
}
.dashboard-product-cell {
display: flex;
align-items: center;
gap: 12px;
min-width: 0;
}
.dashboard-product-thumb {
width: 52px;
height: 52px;
flex: 0 0 auto;
object-fit: cover;
background: var(--momo-bg-paper);
border: 1px solid var(--momo-border-light);
border-radius: 6px;
}
.dashboard-product-name {
display: -webkit-box;
overflow: hidden;
color: var(--momo-text-primary);
font-weight: 800;
line-height: 1.35;
text-decoration: none;
-webkit-box-orient: vertical;
-webkit-line-clamp: 2;
}
.dashboard-product-name:hover {
color: var(--momo-accent);
}
.dashboard-product-id {
margin-top: 4px;
color: var(--momo-text-tertiary);
font-size: 11px;
}
.dashboard-price {
color: var(--momo-text-primary);
font-size: 16px;
font-weight: 800;
}
.dashboard-change-up {
color: var(--momo-danger);
font-weight: 800;
}
.dashboard-change-down {
color: var(--momo-success);
font-weight: 800;
}
.dashboard-empty {
padding: 48px 16px;
color: var(--momo-text-secondary);
text-align: center;
}
.dashboard-pagination {
display: flex;
align-items: center;
justify-content: center;
gap: 8px;
padding: 18px 20px;
}
@media (max-width: 980px) {
.dashboard-kpi-grid,
.dashboard-focus-grid {
grid-template-columns: 1fr 1fr;
}
.dashboard-kpi:nth-child(2) {
border-right: 0;
}
}
@media (max-width: 640px) {
.dashboard-kpi-grid,
.dashboard-focus-grid {
grid-template-columns: 1fr;
}
.dashboard-kpi {
border-right: 0;
border-bottom: 1px solid var(--momo-border-light);
}
.dashboard-kpi:last-child {
border-bottom: 0;
}
.dashboard-search,
.dashboard-select,
.dashboard-segmented {
width: 100%;
}
.dashboard-segmented {
overflow-x: auto;
}
}
</style>
{% endblock %}
{% block ewooo_content %}
<div class="dashboard-v2-stack">
<section>
<div class="dashboard-section-label">
<span class="num momo-mono">01</span>
<span class="title">監控總覽</span>
<span class="meta momo-mono">LIVE · 更新於 {{ datetime_now }}</span>
</div>
<div class="dashboard-kpi-grid">
<div class="dashboard-kpi">
<div class="dashboard-kpi-label momo-mono">監控總數</div>
<div class="dashboard-kpi-value momo-mono">{{ total_products | number_format }}</div>
<div class="dashboard-kpi-sub momo-mono">本週 +{{ week_new_products }}</div>
</div>
<div class="dashboard-kpi is-accent">
<div class="dashboard-kpi-label momo-mono">今日變動</div>
<div class="dashboard-kpi-value momo-mono">{{ active_count | number_format }}</div>
<div class="dashboard-kpi-sub momo-mono">活躍度 {{ activity_rate | round(1) }}%</div>
</div>
<div class="dashboard-kpi">
<div class="dashboard-kpi-label momo-mono">漲價</div>
<div class="dashboard-kpi-value momo-mono is-danger">{{ cnt_increase | number_format }}</div>
<div class="dashboard-kpi-sub momo-mono">平均 +${{ avg_increase | abs | int | number_format }}</div>
</div>
<div class="dashboard-kpi">
<div class="dashboard-kpi-label momo-mono">降價</div>
<div class="dashboard-kpi-value momo-mono is-success">{{ cnt_decrease | number_format }}</div>
<div class="dashboard-kpi-sub momo-mono">平均 -${{ avg_decrease | abs | int | number_format }}</div>
</div>
</div>
</section>
<section>
<div class="dashboard-section-label">
<span class="num momo-mono">02</span>
<span class="title">焦點數據</span>
<span class="meta momo-mono">{{ today_date }}</span>
</div>
<div class="dashboard-focus-grid">
<div class="dashboard-focus-card">
<div class="dashboard-focus-label momo-mono">最活躍分類</div>
{% if most_active_category %}
<div class="dashboard-focus-title">{{ most_active_category }}</div>
<div class="dashboard-focus-sub momo-mono">{{ most_active_count }} 件商品變動</div>
{% else %}
<div class="dashboard-focus-title">尚無分類變動</div>
<div class="dashboard-focus-sub momo-mono">今日沒有可彙整的分類異動</div>
{% endif %}
</div>
<div class="dashboard-focus-card">
<div class="dashboard-focus-label momo-mono">最大變動</div>
{% if max_change_item %}
<div class="dashboard-focus-number momo-mono">
{% if max_change_value > 0 %}+{% else %}-{% endif %}${{ max_change_value | abs | int | number_format }}
</div>
<div class="dashboard-focus-sub" title="{{ max_change_item.record.product.name }}">
{{ max_change_item.record.product.name }}
</div>
{% else %}
<div class="dashboard-focus-title">尚無最大變動</div>
<div class="dashboard-focus-sub momo-mono">今日沒有價格異動</div>
{% endif %}
</div>
<div class="dashboard-focus-card">
<div class="dashboard-focus-label momo-mono">爬蟲排程</div>
{% set momo_stats_list = scheduler_stats.get('momo_task', []) %}
{% if momo_stats_list %}
{% set latest_run = momo_stats_list[0] %}
<div class="dashboard-focus-title momo-mono">{{ latest_run.last_run }}</div>
<div class="dashboard-focus-sub momo-mono">
狀態 {{ latest_run.status | default('未標記') }}
{% if latest_run.scraped_count is defined %} · 掃描 {{ latest_run.scraped_count }} 筆{% endif %}
{% if latest_run.new_products is defined %} · 新增 +{{ latest_run.new_products }}{% endif %}
</div>
{% else %}
<div class="dashboard-focus-title">尚無排程紀錄</div>
<div class="dashboard-focus-sub momo-mono">未讀到 scheduler_stats.json 的 momo_task 紀錄</div>
{% endif %}
</div>
</div>
</section>
<section>
<div class="dashboard-section-label">
<span class="num momo-mono">03</span>
<span class="title">篩選</span>
</div>
<div class="dashboard-filter-card">
<form class="dashboard-filter-form" method="GET" action="/">
<input type="hidden" name="ui" value="v2">
<input class="dashboard-search" type="text" name="q" value="{{ search_query }}" placeholder="搜尋商品名稱或品號...">
<select class="dashboard-select" name="category" onchange="this.form.submit()">
<option value="all">所有分類</option>
{% for cat in categories %}
<option value="{{ cat }}" {% if current_category == cat %}selected{% endif %}>{{ cat }}</option>
{% endfor %}
</select>
<input type="hidden" name="filter" value="{{ current_filter }}">
<input type="hidden" name="sort_by" value="{{ current_sort }}">
<input type="hidden" name="order" value="{{ current_order }}">
<button class="dashboard-action-button" type="submit">
<i class="fas fa-search"></i> 搜尋
</button>
<div class="dashboard-segmented">
<a class="{% if current_filter == 'all' %}is-active{% endif %}" href="{{ url_for('dashboard.index', ui='v2', filter='all', category=current_category, q=search_query, sort_by=current_sort, order=current_order) }}">全部</a>
<a class="{% if current_filter == 'new' %}is-active{% endif %}" href="{{ url_for('dashboard.index', ui='v2', filter='new', category=current_category, q=search_query, sort_by=current_sort, order=current_order) }}">新上架</a>
<a class="{% if current_filter == 'increase' %}is-active{% endif %}" href="{{ url_for('dashboard.index', ui='v2', filter='increase', category=current_category, q=search_query, sort_by=current_sort, order=current_order) }}">漲價</a>
<a class="{% if current_filter == 'decrease' %}is-active{% endif %}" href="{{ url_for('dashboard.index', ui='v2', filter='decrease', category=current_category, q=search_query, sort_by=current_sort, order=current_order) }}">降價</a>
<a class="{% if current_filter == 'delisted' %}is-active{% endif %}" href="{{ url_for('dashboard.index', ui='v2', filter='delisted', category=current_category, q=search_query, sort_by=current_sort, order=current_order) }}">下架</a>
</div>
<button class="dashboard-action-button" type="button" onclick="triggerTask()">
<i class="fas fa-rotate"></i> 更新
</button>
<button class="dashboard-action-button is-primary" type="button" onclick="triggerNotification()">
<i class="fas fa-bell"></i> 發送通知
</button>
</form>
</div>
</section>
<section>
<div class="dashboard-table-card">
<div class="dashboard-table-head">
<span class="momo-mono" style="font-size:11px;font-weight:800;color:var(--momo-text-tertiary);letter-spacing:.08em;">04</span>
<span class="dashboard-table-title">商品列表</span>
<span class="dashboard-table-meta momo-mono">{{ total_items | number_format }} 筆</span>
<div class="momo-topbar-spacer"></div>
<a class="dashboard-action-link" href="/api/export/excel/all">
<i class="fas fa-download"></i> 匯出全部
</a>
<a class="dashboard-action-link" href="/api/export/excel/changes">
<i class="fas fa-arrow-trend-up"></i> 匯出漲跌
</a>
</div>
<div class="dashboard-table-wrap">
<table class="dashboard-table">
<thead>
<tr>
<th>分類</th>
<th>商品名稱</th>
<th class="text-end">
<a href="{{ url_for('dashboard.index', ui='v2', page=1, sort_by='price', order='asc' if current_sort == 'price' and current_order == 'desc' else 'desc', category=current_category, filter=current_filter, q=search_query) }}">當天價格</a>
</th>
<th class="text-end">
<a href="{{ url_for('dashboard.index', ui='v2', page=1, sort_by='yesterday_change', order='asc' if current_sort == 'yesterday_change' and current_order == 'desc' else 'desc', category=current_category, filter=current_filter, q=search_query) }}">昨日漲跌</a>
</th>
<th class="text-end">
<a href="{{ url_for('dashboard.index', ui='v2', page=1, sort_by='week_change', order='asc' if current_sort == 'week_change' and current_order == 'desc' else 'desc', category=current_category, filter=current_filter, q=search_query) }}">週漲跌</a>
</th>
<th class="text-end">
<a href="{{ url_for('dashboard.index', ui='v2', page=1, sort_by='timestamp', order='asc' if current_sort == 'timestamp' and current_order == 'desc' else 'desc', category=current_category, filter=current_filter, q=search_query) }}">更新時間</a>
</th>
<th class="text-end">上架時間</th>
</tr>
</thead>
<tbody>
{% for item in items %}
{% set product = item.record.product %}
{% set image_url = product.image_url or ('https://m.momoshop.com.tw/moscdn/goods/' ~ product.i_code ~ '_m.webp') %}
<tr>
<td><span class="dashboard-category">{{ product.category or '未分類' }}</span></td>
<td>
<div class="dashboard-product-cell">
<img class="dashboard-product-thumb" src="{{ image_url }}" alt="{{ product.name }}" loading="lazy" referrerpolicy="no-referrer">
<div>
<a class="dashboard-product-name" href="{{ product.url }}" target="_blank" rel="noopener noreferrer">{{ product.name }}</a>
<div class="dashboard-product-id momo-mono">ID {{ product.i_code }}</div>
</div>
</div>
</td>
<td class="text-end">
<span class="dashboard-price momo-mono">${{ item.record.price | int | number_format }}</span>
</td>
<td class="text-end momo-mono">
{% if item.yesterday_diff > 0 %}
<span class="dashboard-change-up">▲ +{{ item.yesterday_diff | abs | int | number_format }}</span>
{% elif item.yesterday_diff < 0 %}
<span class="dashboard-change-down">▼ -{{ item.yesterday_diff | abs | int | number_format }}</span>
{% else %}
<span style="color:var(--momo-text-tertiary);">--</span>
{% endif %}
</td>
<td class="text-end momo-mono">
{% set week_diff = item.stats.get('7d_diff', 0) %}
{% if week_diff > 0 %}
<span class="dashboard-change-up">+{{ week_diff | int | number_format }}</span>
{% elif week_diff < 0 %}
<span class="dashboard-change-down">-{{ week_diff | abs | int | number_format }}</span>
{% else %}
<span style="color:var(--momo-text-tertiary);">--</span>
{% endif %}
</td>
<td class="text-end momo-mono" style="color:var(--momo-text-secondary);">
{{ item.record.timestamp.strftime('%m-%d %H:%M') if item.record.timestamp else '--' }}
</td>
<td class="text-end momo-mono" style="color:var(--momo-text-secondary);">
{{ item.safe_created_at.strftime('%m-%d %H:%M') if item.safe_created_at else '--' }}
</td>
</tr>
{% else %}
<tr>
<td colspan="7">
<div class="dashboard-empty">
{% if search_query %}
找不到與「{{ search_query }}」相關的商品
{% else %}
目前沒有符合條件的商品
{% endif %}
</div>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% if total_pages > 1 %}
<div class="dashboard-pagination">
{% if current_page > 1 %}
<a class="dashboard-action-link" href="{{ url_for('dashboard.index', ui='v2', page=current_page - 1, category=current_category, filter=current_filter, q=search_query, sort_by=current_sort, order=current_order) }}">上一頁</a>
{% endif %}
<span class="dashboard-table-meta momo-mono">第 {{ current_page }} / {{ total_pages }} 頁</span>
{% if current_page < total_pages %}
<a class="dashboard-action-link" href="{{ url_for('dashboard.index', ui='v2', page=current_page + 1, category=current_category, filter=current_filter, q=search_query, sort_by=current_sort, order=current_order) }}">下一頁</a>
{% endif %}
</div>
{% endif %}
</div>
</section>
</div>
{% endblock %}
{% block extra_js %}
<script>
function getCSRFToken() {
return document.querySelector('meta[name="csrf-token"]').getAttribute('content');
}
function triggerTask() {
if (confirm('確定要手動執行全站爬蟲嗎?可能需要一段時間。')) {
fetch('/api/run_task', {
method: 'POST',
headers: { 'X-CSRFToken': getCSRFToken() }
})
.then(response => response.json())
.then(data => alert(data.message))
.catch(error => alert('錯誤: ' + error));
}
}
function triggerNotification() {
if (confirm('確定要發送今日商品異動通知嗎?')) {
fetch('/api/trigger_momo_notification', {
method: 'POST',
headers: { 'X-CSRFToken': getCSRFToken() }
})
.then(response => response.json())
.then(data => alert(data.message))
.catch(error => alert('錯誤: ' + error));
}
}
</script>
{% endblock %}