797 lines
16 KiB
Python
797 lines
16 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
Server-side Trips report page for .wifitrip import/export.
|
|
"""
|
|
|
|
from html import escape
|
|
|
|
from reports.template import (
|
|
ReportContext,
|
|
page_begin,
|
|
page_end,
|
|
)
|
|
|
|
|
|
def _stat_card(label, value):
|
|
return f"""
|
|
<div class="stat-card">
|
|
|
|
<div class="stat-label">
|
|
{escape(str(label))}
|
|
</div>
|
|
|
|
<div class="stat-value">
|
|
{escape(str(value))}
|
|
</div>
|
|
|
|
</div>
|
|
"""
|
|
|
|
|
|
def _message(message):
|
|
if not message:
|
|
return ""
|
|
|
|
message_type = escape(
|
|
str(message.get("type", "info"))
|
|
)
|
|
|
|
message_text = escape(
|
|
str(message.get("text", ""))
|
|
)
|
|
|
|
return f"""
|
|
<section class="card">
|
|
|
|
<div class="info {message_type}">
|
|
{message_text}
|
|
</div>
|
|
|
|
</section>
|
|
"""
|
|
|
|
|
|
def _import_result(result):
|
|
if not result:
|
|
return ""
|
|
|
|
html = """
|
|
<section class="card">
|
|
|
|
<h2>Import result</h2>
|
|
|
|
<div class="stats-grid">
|
|
"""
|
|
|
|
html += _stat_card(
|
|
"Sessions in file",
|
|
result.get("sessions", 0),
|
|
)
|
|
|
|
html += _stat_card(
|
|
"Imported",
|
|
result.get("imported", 0),
|
|
)
|
|
|
|
html += _stat_card(
|
|
"Duplicates",
|
|
result.get("duplicates", 0),
|
|
)
|
|
|
|
html += _stat_card(
|
|
"Failed",
|
|
result.get("failed", 0),
|
|
)
|
|
|
|
html += """
|
|
</div>
|
|
"""
|
|
|
|
failed_sessions = result.get(
|
|
"failed_sessions",
|
|
[],
|
|
)
|
|
|
|
if failed_sessions:
|
|
|
|
html += """
|
|
<h3>Failed sessions</h3>
|
|
|
|
<div class="table-wrapper">
|
|
|
|
<table>
|
|
|
|
<thead>
|
|
<tr>
|
|
<th>Session hash</th>
|
|
<th>Error</th>
|
|
</tr>
|
|
</thead>
|
|
|
|
<tbody>
|
|
"""
|
|
|
|
for item in failed_sessions:
|
|
|
|
html += f"""
|
|
<tr>
|
|
|
|
<td>
|
|
<code>
|
|
{escape(str(item.get("session_hash", "")))}
|
|
</code>
|
|
</td>
|
|
|
|
<td>
|
|
{escape(str(item.get("error", "")))}
|
|
</td>
|
|
|
|
</tr>
|
|
"""
|
|
|
|
html += """
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
</div>
|
|
"""
|
|
|
|
html += """
|
|
</section>
|
|
"""
|
|
|
|
return html
|
|
|
|
|
|
def _validation_result(validation):
|
|
if not validation:
|
|
return ""
|
|
|
|
if not validation.get("valid"):
|
|
|
|
return f"""
|
|
<section class="card">
|
|
|
|
<h2>Package validation</h2>
|
|
|
|
<div class="info error">
|
|
{escape(
|
|
str(
|
|
validation.get(
|
|
"error",
|
|
"Package is invalid."
|
|
)
|
|
)
|
|
)}
|
|
</div>
|
|
|
|
</section>
|
|
"""
|
|
|
|
return f"""
|
|
<section class="card">
|
|
|
|
<h2>Package validation</h2>
|
|
|
|
<div class="info success">
|
|
Package is valid and contains
|
|
{escape(str(validation.get("sessions", 0)))}
|
|
session(s).
|
|
</div>
|
|
|
|
<div class="stats-grid">
|
|
|
|
{_stat_card(
|
|
"Sessions",
|
|
validation.get("sessions", 0)
|
|
)}
|
|
|
|
{_stat_card(
|
|
"Access points",
|
|
validation.get("access_points", 0)
|
|
)}
|
|
|
|
{_stat_card(
|
|
"GPS points",
|
|
validation.get("gps_points", 0)
|
|
)}
|
|
|
|
{_stat_card(
|
|
"Observations",
|
|
validation.get("observations", 0)
|
|
)}
|
|
|
|
{_stat_card(
|
|
"Handshakes",
|
|
validation.get("handshakes", 0)
|
|
)}
|
|
|
|
{_stat_card(
|
|
"Handshake Cracked",
|
|
validation.get("credentials", 0)
|
|
)}
|
|
|
|
</div>
|
|
|
|
</section>
|
|
"""
|
|
|
|
|
|
def render_trips_page(
|
|
conn,
|
|
context=None,
|
|
message=None,
|
|
import_result=None,
|
|
validation=None,
|
|
):
|
|
if context is None:
|
|
|
|
context = ReportContext(
|
|
mode="server",
|
|
page="trips",
|
|
)
|
|
|
|
|
|
html = page_begin(
|
|
"Trips",
|
|
(
|
|
"Export and import capture sessions "
|
|
"as .wifitrip packages."
|
|
),
|
|
"trips",
|
|
context,
|
|
section_title="Reports",
|
|
)
|
|
|
|
html += _message(
|
|
message
|
|
)
|
|
|
|
html += """
|
|
|
|
<section class="card">
|
|
|
|
<h2>Экспорт поездок</h2>
|
|
|
|
<p>
|
|
Выберите поездки, которые нужно включить
|
|
в пакет <code>.wifitrip</code>.
|
|
</p>
|
|
|
|
<p>
|
|
Пакет содержит данные поездок: GPS-треки,
|
|
наблюдения точек доступа и рукопожатия.
|
|
Файлы захвата и пароли не включаются.
|
|
</p>
|
|
|
|
|
|
<form
|
|
id="wifitrip-export-form"
|
|
method="get"
|
|
action="/reports/trips/export"
|
|
>
|
|
|
|
<div class="wifitrip-export-options">
|
|
|
|
<div class="wifitrip-export-mode wifitrip-export-mode-active">
|
|
<label class="wifitrip-mode-tab">
|
|
<input
|
|
type="radio"
|
|
name="mode"
|
|
value="latest"
|
|
checked
|
|
>
|
|
<span>Последние поездки</span>
|
|
</label>
|
|
|
|
<div class="wifitrip-mode-content">
|
|
<div class="session-list-toolbar wifitrip-limit-toolbar">
|
|
<label for="wifitrip-limit">
|
|
Show:
|
|
</label>
|
|
|
|
<select
|
|
id="wifitrip-limit"
|
|
name="limit"
|
|
>
|
|
<option value="1" selected>1</option>
|
|
<option value="3">3</option>
|
|
<option value="5">5</option>
|
|
<option value="10">10</option>
|
|
<option value="20">20</option>
|
|
<option value="50">50</option>
|
|
<option value="100">100</option>
|
|
<option value="custom">Custom</option>
|
|
<option value="all">All</option>
|
|
</select>
|
|
|
|
<input
|
|
type="number"
|
|
id="wifitrip-custom-limit"
|
|
name="custom_limit"
|
|
min="1"
|
|
max="1"
|
|
step="1"
|
|
value="1"
|
|
placeholder="Number"
|
|
aria-label="Custom number of trips"
|
|
style="width: 100px;"
|
|
disabled
|
|
>
|
|
|
|
<span id="wifitrip-limit-count"></span>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="wifitrip-export-mode">
|
|
<label class="wifitrip-mode-tab">
|
|
<input
|
|
type="radio"
|
|
name="mode"
|
|
value="range"
|
|
>
|
|
<span>По диапазону дат</span>
|
|
</label>
|
|
|
|
<div
|
|
id="wifitrip-date-range"
|
|
class="wifitrip-mode-content wifitrip-date-fields"
|
|
>
|
|
<div>
|
|
<label for="wifitrip-date-from">
|
|
Начальная дата
|
|
</label>
|
|
|
|
<input
|
|
type="date"
|
|
id="wifitrip-date-from"
|
|
name="date_from"
|
|
disabled
|
|
>
|
|
</div>
|
|
|
|
<div>
|
|
<label for="wifitrip-date-to">
|
|
Конечная дата
|
|
</label>
|
|
|
|
<input
|
|
type="date"
|
|
id="wifitrip-date-to"
|
|
name="date_to"
|
|
disabled
|
|
>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="wifitrip-export-footer">
|
|
|
|
<p
|
|
id="wifitrip-export-preview"
|
|
aria-live="polite"
|
|
>
|
|
Подсчёт поездок...
|
|
</p>
|
|
|
|
<button
|
|
type="submit"
|
|
id="wifitrip-export-button"
|
|
class="button"
|
|
disabled
|
|
>
|
|
Экспортировать поездки
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
<script>
|
|
(function () {
|
|
const form = document.getElementById(
|
|
"wifitrip-export-form"
|
|
);
|
|
|
|
const limitInput = document.getElementById(
|
|
"wifitrip-limit"
|
|
);
|
|
|
|
const customLimitInput = document.getElementById(
|
|
"wifitrip-custom-limit"
|
|
);
|
|
|
|
const limitCount = document.getElementById(
|
|
"wifitrip-limit-count"
|
|
);
|
|
|
|
const dateFromInput = document.getElementById(
|
|
"wifitrip-date-from"
|
|
);
|
|
|
|
const dateToInput = document.getElementById(
|
|
"wifitrip-date-to"
|
|
);
|
|
|
|
const preview = document.getElementById(
|
|
"wifitrip-export-preview"
|
|
);
|
|
|
|
const exportButton = document.getElementById(
|
|
"wifitrip-export-button"
|
|
);
|
|
|
|
if (
|
|
!form ||
|
|
!limitInput ||
|
|
!customLimitInput ||
|
|
!limitCount ||
|
|
!dateFromInput ||
|
|
!dateToInput ||
|
|
!preview ||
|
|
!exportButton
|
|
) {
|
|
return;
|
|
}
|
|
|
|
let previewRequest = 0;
|
|
let availableSessions = null;
|
|
|
|
function getMode() {
|
|
const selected = form.querySelector(
|
|
'input[name="mode"]:checked'
|
|
);
|
|
|
|
return selected ? selected.value : "latest";
|
|
}
|
|
|
|
function updateControls() {
|
|
const mode = getMode();
|
|
const isRange = mode === "range";
|
|
const isCustomLimit =
|
|
mode === "latest" && limitInput.value === "custom";
|
|
|
|
limitInput.disabled = mode !== "latest";
|
|
customLimitInput.disabled = mode !== "latest";
|
|
customLimitInput.required = isCustomLimit;
|
|
|
|
customLimitInput.max = String(
|
|
Math.max(1, availableSessions || 1)
|
|
);
|
|
|
|
const customOption = limitInput.querySelector(
|
|
'option[value="custom"]'
|
|
);
|
|
|
|
if (customOption) {
|
|
customOption.disabled = availableSessions === 0;
|
|
}
|
|
|
|
dateFromInput.disabled = !isRange;
|
|
dateToInput.disabled = !isRange;
|
|
|
|
dateFromInput.required = isRange;
|
|
dateToInput.required = isRange;
|
|
|
|
if (isRange && dateFromInput.value) {
|
|
dateToInput.min = dateFromInput.value;
|
|
} else {
|
|
dateToInput.removeAttribute("min");
|
|
}
|
|
|
|
if (isRange && dateToInput.value) {
|
|
dateFromInput.max = dateToInput.value;
|
|
} else {
|
|
dateFromInput.removeAttribute("max");
|
|
}
|
|
}
|
|
|
|
async function updatePreview() {
|
|
const requestId = ++previewRequest;
|
|
|
|
updateControls();
|
|
|
|
const mode = getMode();
|
|
const isCustomLimit =
|
|
mode === "latest" && limitInput.value === "custom";
|
|
|
|
if (isCustomLimit) {
|
|
const customLimit = Number(customLimitInput.value);
|
|
|
|
if (availableSessions === null) {
|
|
preview.textContent =
|
|
"Дождитесь получения количества поездок.";
|
|
|
|
exportButton.disabled = true;
|
|
return;
|
|
}
|
|
|
|
if (
|
|
!customLimitInput.value ||
|
|
!Number.isInteger(customLimit) ||
|
|
customLimit < 1 ||
|
|
customLimit > availableSessions
|
|
) {
|
|
preview.textContent =
|
|
"Введите целое число от 1 до " +
|
|
availableSessions + ".";
|
|
|
|
exportButton.disabled = true;
|
|
return;
|
|
}
|
|
}
|
|
|
|
if (
|
|
mode === "range" &&
|
|
(
|
|
!dateFromInput.value ||
|
|
!dateToInput.value
|
|
)
|
|
) {
|
|
preview.textContent =
|
|
"Укажите начальную и конечную даты.";
|
|
|
|
exportButton.disabled = true;
|
|
return;
|
|
}
|
|
|
|
if (
|
|
mode === "range" &&
|
|
dateFromInput.value > dateToInput.value
|
|
) {
|
|
preview.textContent =
|
|
"Начальная дата не может быть позже конечной.";
|
|
|
|
exportButton.disabled = true;
|
|
return;
|
|
}
|
|
|
|
const params = new URLSearchParams();
|
|
|
|
params.set("mode", mode);
|
|
|
|
if (mode === "latest") {
|
|
params.set(
|
|
"limit",
|
|
isCustomLimit
|
|
? customLimitInput.value
|
|
: limitInput.value
|
|
);
|
|
}
|
|
|
|
if (mode === "range") {
|
|
params.set("date_from", dateFromInput.value);
|
|
params.set("date_to", dateToInput.value);
|
|
}
|
|
|
|
preview.textContent = "Подсчёт поездок...";
|
|
exportButton.disabled = true;
|
|
|
|
try {
|
|
const response = await fetch(
|
|
"/reports/trips/preview?" + params.toString(),
|
|
{
|
|
headers: {
|
|
"Accept": "application/json"
|
|
}
|
|
}
|
|
);
|
|
|
|
const result = await response.json();
|
|
|
|
if (requestId !== previewRequest) {
|
|
return;
|
|
}
|
|
|
|
if (!response.ok) {
|
|
throw new Error(
|
|
result.error || "Не удалось подсчитать поездки."
|
|
);
|
|
}
|
|
|
|
const count = Number(result.count);
|
|
const availableCount = Number(result.available_count);
|
|
|
|
if (!Number.isInteger(count) || count < 0) {
|
|
throw new Error(
|
|
"Сервер вернул некорректное количество поездок."
|
|
);
|
|
}
|
|
|
|
if (
|
|
!Number.isInteger(availableCount) ||
|
|
availableCount < 0
|
|
) {
|
|
throw new Error(
|
|
"Сервер вернул некорректное общее количество поездок."
|
|
);
|
|
}
|
|
|
|
availableSessions = availableCount;
|
|
updateControls();
|
|
|
|
limitCount.textContent =
|
|
"Showing " + count + " of " + availableCount;
|
|
|
|
preview.textContent =
|
|
"Будет экспортировано поездок: " + count;
|
|
|
|
exportButton.disabled = count === 0;
|
|
|
|
} catch (error) {
|
|
if (requestId !== previewRequest) {
|
|
return;
|
|
}
|
|
|
|
limitCount.textContent = "";
|
|
|
|
preview.textContent =
|
|
"Ошибка подсчёта: " + error.message;
|
|
|
|
exportButton.disabled = true;
|
|
}
|
|
}
|
|
|
|
form.addEventListener(
|
|
"change",
|
|
function (event) {
|
|
if (event.target === limitInput) {
|
|
if (limitInput.value !== "custom") {
|
|
customLimitInput.value = "";
|
|
} else {
|
|
customLimitInput.focus();
|
|
}
|
|
}
|
|
updatePreview();
|
|
}
|
|
);
|
|
|
|
form.addEventListener(
|
|
"input",
|
|
function (event) {
|
|
if (event.target === customLimitInput) {
|
|
limitInput.value = "custom";
|
|
}
|
|
updatePreview();
|
|
}
|
|
);
|
|
|
|
form.addEventListener(
|
|
"submit",
|
|
function (event) {
|
|
if (
|
|
!form.reportValidity() ||
|
|
exportButton.disabled
|
|
) {
|
|
event.preventDefault();
|
|
return;
|
|
}
|
|
|
|
if (
|
|
getMode() === "latest" &&
|
|
limitInput.value === "custom"
|
|
) {
|
|
const submittedLimit = document.createElement(
|
|
"input"
|
|
);
|
|
|
|
submittedLimit.type = "hidden";
|
|
submittedLimit.name = "limit";
|
|
submittedLimit.value = customLimitInput.value;
|
|
|
|
limitInput.disabled = true;
|
|
form.appendChild(submittedLimit);
|
|
}
|
|
}
|
|
);
|
|
|
|
updatePreview();
|
|
})();
|
|
</script>
|
|
|
|
</section>
|
|
"""
|
|
|
|
html += """
|
|
<section class="card">
|
|
|
|
<h2>Import capture sessions</h2>
|
|
|
|
<p>
|
|
Import one <code>.wifitrip</code> package.
|
|
The complete package is validated before
|
|
any database changes are made.
|
|
</p>
|
|
|
|
<form
|
|
method="post"
|
|
action="/reports/trips/import"
|
|
enctype="multipart/form-data"
|
|
>
|
|
|
|
<label for="wifitrip_file">
|
|
.wifitrip file
|
|
</label>
|
|
|
|
<div class="wifitrip-file-row">
|
|
|
|
<input
|
|
type="file"
|
|
id="wifitrip_file"
|
|
name="wifitrip_file"
|
|
accept=".wifitrip,application/zip"
|
|
required
|
|
>
|
|
|
|
<label
|
|
for="wifitrip_file"
|
|
class="button"
|
|
>
|
|
Browse
|
|
</label>
|
|
|
|
<span
|
|
class="wifitrip-file-name"
|
|
id="wifitrip-file-name"
|
|
>
|
|
Файл не выбран
|
|
</span>
|
|
|
|
</div>
|
|
|
|
<div style="margin-top: 1rem;">
|
|
|
|
<button type="submit" class="button">
|
|
Import package
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
<script>
|
|
const wifitripFileInput = document.getElementById(
|
|
"wifitrip_file"
|
|
);
|
|
|
|
const wifitripFileName = document.getElementById(
|
|
"wifitrip-file-name"
|
|
);
|
|
|
|
if (wifitripFileInput && wifitripFileName) {
|
|
wifitripFileInput.addEventListener(
|
|
"change",
|
|
function () {
|
|
if (this.files.length > 0) {
|
|
wifitripFileName.textContent =
|
|
this.files[0].name;
|
|
} else {
|
|
wifitripFileName.textContent =
|
|
"Файл не выбран";
|
|
}
|
|
}
|
|
);
|
|
}
|
|
</script>
|
|
|
|
</section>
|
|
"""
|
|
|
|
html += _validation_result(
|
|
validation
|
|
)
|
|
|
|
html += _import_result(
|
|
import_result
|
|
)
|
|
|
|
html += page_end()
|
|
|
|
return html
|