File: home.js

package info (click to toggle)
python-django-structlog 9.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,004 kB
  • sloc: python: 3,509; sh: 206; javascript: 79; makefile: 19
file content (93 lines) | stat: -rw-r--r-- 2,678 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
const toastContainer = document.getElementById('toast-container')
const toastTemplate = document.getElementById('toastTemplate');
let abortController = null;

function log(title, url, body, isError, duration) {
  const newToast = toastTemplate.cloneNode(true)

  const text = body ? body.toString() : ""

  if (isError) {
    console.error(title, url, body, duration);
  } else {
    console.log(title, url, body, duration);
  }

  if (isError) {
    newToast.classList.add("border-danger")
  } else {
    newToast.classList.add("border-success")
  }
  newToast.removeAttribute('id');
  const toastHeader = newToast.querySelector('.toast-header > .me-auto');
  toastHeader.textContent = `${title} ${url}`
  if (duration) {
    const toastDuration = newToast.querySelector('.duration');
    toastDuration.textContent = `${duration} ms`
  }
  const toastBody = newToast.querySelector('.toast-body');
  if (body) {
    toastBody.textContent = text.slice(0, 400)
  } else {
    newToast.removeChild(toastBody);
  }
  toastContainer.appendChild(newToast);
  const toast = new bootstrap.Toast(newToast)

  toast.show()
}


async function fetchUrl(url) {
  abortController = new AbortController();
  log("request_started", url);
  const start = new Date();

  try {
    const response = await fetch(url, {
      method: 'get',
      headers: {"Content-Type": "application/json"},
      signal: abortController.signal,
    });
    const text = await response.text();
    if (response.ok) {
      log("request_finished", url, text, false, new Date() - start);
    } else {
      log("request_failed", url, text, true, new Date() - start);
    }
  } catch (err) {
    log("request_failed", url, err, true, new Date() - start);
  }
}


async function fetchStreamingUrl(url) {
  const start = new Date();
  try {
    abortController = new AbortController();
    log("streaming_request_started", url);
    const response = await fetch(url, {
      method: 'get',
      signal: abortController.signal,
    });
    log("streaming_request_finished", url, `Status code ${response.status}`, false, new Date() - start);

    const reader = response.body.pipeThrough(new TextDecoderStream()).getReader();

    log("streaming_response_started", url, undefined, false, new Date() - start);
    while (true) {
      const {value, done} = await reader.read();
      if (done) break;
      log("received", url, value, false, new Date() - start);
    }

    log("streaming_response_finished", url, undefined, false, new Date() - start);
  } catch (err) {
    log("request_failed", url, err, true, new Date() - start);
  }
}

function cancelAsync() {
  if (abortController)
    abortController.abort();
}