init()

async function init() { // The page init function
  versionDisplay()
  display(await get())
}

async function get() { // Fetch data from API
  var url = `${window.location.origin}/api/v1/stats`
  var resp = await fetch(url)
  return await resp.json()
}

async function display(data) { // Parses and displays data from API
  document.getElementById('server_host').textContent = data.host
  document.getElementById('server_mode').textContent = data.mode || 'Unknown'
  document.getElementById('ver-bkend').textContent = data.verBkend || 'Unknown'
  let dat = data.dat[0]
  let ver = data.ver[0]
  document.getElementById('time').textContent = await convertUnixLocal(dat.since)
  document.getElementById('ldbws').textContent = dat.ldbws || '0'
  document.getElementById('ldbsvws').textContent = dat.ldbsvws || '0'
  document.getElementById('apicorpus').textContent = dat.corpus_api || '0'
  document.getElementById('corpus').textContent = dat.corpus || '0'
  document.getElementById('stations').textContent = dat.stations || '0'
  document.getElementById('users').textContent = dat.user || '0'
  document.getElementById('meta').textContent = dat.meta || '0'
  document.getElementById('pis').textContent = dat.pis
  document.getElementById('ver-dbman').textContent = ver.dbmanager || 'Unknown'
  document.getElementById('user_count').textContent = data.count.users || 'Unknown'
  document.getElementById('reg_count').textContent = data.count.reg || '0'
  document.getElementById('pis_count').textContent = data.count.pis || 'Unknown'
  document.getElementById('corpus_count').textContent = data.count.corpus || 'Unknown'
  document.getElementById('stations_count').textContent = data.count.stations || 'Unknown'
}