repos
/ analytics-rust master

analytics-rust

mirror archived upstream

Single-binary self-hosted website analytics on Rust axum: collector API, dashboards, world map, and PDF reports.

analyticsaxumdockerrustself-hostedsqliteviteweb-analytics

2.3 KB · 65 lines · JavaScript Raw History
 1document.addEventListener("DOMContentLoaded", function () {
 2  const dateRange = document.getElementById("date-range");
 3  const dateStart = document.getElementById("date-start");
 4  const dateEnd = document.getElementById("date-end");
 5  if (!dateRange) return;
 6
 7  // get the date_range from the current query string date_range
 8  const url = new URL(window.location.href);
 9  const date_range = url.searchParams.get("date_range");
10
11  // set the date_range to the current query string date_range
12  if (date_range) {
13    dateRange.value = date_range;
14  }
15
16  // update the url with the new date_range
17  dateRange.addEventListener("change", function () {
18    // take the value of the date_range, which is in days, and get the
19    // date_start and date_end, the date_end is today and the date_start is
20    // date_end - date_range
21    const date_range = parseInt(dateRange.value);
22    let dateEndValue = new Date();
23    let dateStartValue = new Date(dateEndValue.getTime() - date_range * 86400000);
24
25    dateEndValue = dateEndValue.toISOString().split("T")[0];
26    dateStartValue = dateStartValue.toISOString().split("T")[0];
27
28    dateEnd.value = dateEndValue;
29    dateStart.value = dateStartValue;
30
31    // get parent form and submit
32    const form = dateRange.closest("form");
33    form.submit();
34  });
35
36  // if dateStart and dateEnd are both set and and dateStart or dateEnd change
37  // then submit the parent form
38  dateStart.addEventListener("change", function () {
39    if (dateStart.value && dateEnd.value) {
40      // if dateStart is greater than dateEnd then swap the values
41      if (new Date(dateStart.value) > new Date(dateEnd.value)) {
42        const temp = dateStart.value;
43        dateStart.value = dateEnd.value;
44        dateEnd.value = temp;
45      }
46      dateRange.value = "custom";
47      const form = dateStart.closest("form");
48      form.submit();
49    }
50  });
51  dateEnd.addEventListener("change", function () {
52    if (dateStart.value && dateEnd.value) {
53      // if dateStart is greater than dateEnd then swap the values
54      if (new Date(dateStart.value) > new Date(dateEnd.value)) {
55        const temp = dateStart.value;
56        dateStart.value = dateEnd.value;
57        dateEnd.value = temp;
58      }
59      dateRange.value = "custom";
60      const form = dateEnd.closest("form");
61      form.submit();
62    }
63  });
64});