Release WiFiTrip format v2.0.0
This commit is contained in:
+426
-26
@@ -234,12 +234,6 @@ def render_trips_page(
|
||||
page="trips",
|
||||
)
|
||||
|
||||
session_count = conn.execute(
|
||||
"""
|
||||
SELECT COUNT(*) AS count
|
||||
FROM capture_sessions
|
||||
"""
|
||||
).fetchone()["count"]
|
||||
|
||||
html = page_begin(
|
||||
"Trips",
|
||||
@@ -257,44 +251,450 @@ def render_trips_page(
|
||||
)
|
||||
|
||||
html += """
|
||||
|
||||
<section class="card">
|
||||
|
||||
<h2>Export capture sessions</h2>
|
||||
<h2>Экспорт поездок</h2>
|
||||
|
||||
<p>
|
||||
Export all capture sessions into one
|
||||
<code>.wifitrip</code> package.
|
||||
Выберите поездки, которые нужно включить
|
||||
в пакет <code>.wifitrip</code>.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
The package contains logical session data:
|
||||
GPS track, access-point observations,
|
||||
handshakes and credentials. Physical capture
|
||||
files are not included.
|
||||
Пакет содержит данные поездок: GPS-треки,
|
||||
наблюдения точек доступа и рукопожатия.
|
||||
Файлы захвата и пароли не включаются.
|
||||
</p>
|
||||
|
||||
<div class="stats-grid">
|
||||
"""
|
||||
|
||||
html += _stat_card(
|
||||
"Sessions available",
|
||||
session_count,
|
||||
)
|
||||
|
||||
html += """
|
||||
</div>
|
||||
|
||||
<form
|
||||
id="wifitrip-export-form"
|
||||
method="get"
|
||||
action="/reports/trips/export"
|
||||
>
|
||||
|
||||
<button type="submit" class="button">
|
||||
Export all sessions
|
||||
</button>
|
||||
<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>
|
||||
"""
|
||||
|
||||
|
||||
Reference in New Issue
Block a user