aboutsummaryrefslogtreecommitdiff
path: root/pyenc/static/js/script2.js
diff options
context:
space:
mode:
Diffstat (limited to 'pyenc/static/js/script2.js')
-rw-r--r--pyenc/static/js/script2.js164
1 files changed, 164 insertions, 0 deletions
diff --git a/pyenc/static/js/script2.js b/pyenc/static/js/script2.js
new file mode 100644
index 0000000..6f06fc7
--- /dev/null
+++ b/pyenc/static/js/script2.js
@@ -0,0 +1,164 @@
+const distro_names = new Map([['archlinux', 'arch'],
+])
+
+String.prototype.toTitleCase = function () {
+ if (this.length == 0) return ""
+
+ return this[0].toUpperCase() + this.substring(1).toLowerCase()
+}
+
+async function pdb(type, query) {
+ const url = new URL('/pdb', window.location)
+ const payload = JSON.stringify(query)
+ url.searchParams.append('type', type);
+ url.searchParams.append('query', payload);
+ let response = await fetch (url.href)
+ if (! response.ok) {
+ throw response.text()
+ }
+ return response.json()
+
+}
+
+function upcasePuppet(s) {
+ return s.split('::').map(s => s.toTitleCase()).join('::')
+}
+
+function downcasePuppet(s) {
+ return s.split('::').map(s => s.toLowerCase()).join('::')
+}
+
+function formatValue(value, target) {
+ if (value instanceof Array) {
+ let ul = document.createElement('ul');
+ for (let item of value) {
+ let li = document.createElement('li');
+ formatValue(item, li);
+ ul.appendChild(li);
+ }
+ target.appendChild(ul);
+ } else if (value instanceof Object) {
+ let dl = document.createElement('dl');
+ for (let [k, v] of Object.entries(value)) {
+ let dt = document.createElement('dt');
+ let dd = document.createElement('dd');
+ dt.textContent = k;
+ formatValue(v, dd);
+ dl.appendChild(dt);
+ dl.appendChild(dd);
+ }
+ target.appendChild(dl);
+ } else {
+ target.textContent = value;
+ }
+}
+
+
+/* For host page, find all classes that host has, and display it along with all
+ its parameters */
+async function populate_host_classes(class_container, fqdn) {
+ const resources = await pdb('resources', ['extract', ['title', 'parameters'],
+ ['and',
+ ['=', 'certname', fqdn],
+ ['=', 'type', 'Class']]])
+ if (resources.length == 0) {
+ class_container.textContent = 'No classes found'
+ return
+ }
+ let root = document.createElement('dl');
+ class_container.appendChild(root);
+ for (let item of resources) {
+ let dt = document.createElement('dt');
+ let a = document.createElement('a');
+ a.textContent = item['title']
+ a.href = '/class/' + downcasePuppet(item['title'])
+ dt.appendChild(a)
+
+ // let dd = document.createElement('dd');
+ // let dl = document.createElement('dl');
+ // dd.appendChild(dl);
+ // for (let [key, value] of Object.entries(item['parameters'])) {
+ // let dt_ = document.createElement('dt');
+ // dt_.textContent = key;
+ // dl.appendChild(dt_);
+
+ // let dd_ = document.createElement('dd');
+ // formatValue(value, dd_);
+ // dl.appendChild(dd_);
+ // }
+ root.appendChild(dt);
+ // root.appendChild(dd);
+ }
+}
+
+async function populate_class_hosts(host_container, title) {
+ // resources[certname, file]{type = "Class" and title = "Puppet"}
+ const resources = await pdb('resources', ['extract', ['certname', 'file'],
+ ['and',
+ ['=', 'title', upcasePuppet(title)],
+ ['=', 'type', 'Class']]])
+ if (resources.length == 0) {
+ host_container.textContent = 'No hosts uses this class'
+ return
+ }
+
+ let ul = document.createElement('ul');
+ host_container.appendChild(ul);
+ for (let item of resources) {
+ let fqdn = item['certname']
+ let include_position = item['filename']
+
+ let li = document.createElement('li');
+ let a = document.createElement('a');
+ a.textContent = fqdn
+ a.href = `/host/${fqdn}`
+
+ let span = document.createElement('span');
+ span.textContent = include_position;
+ span.classList.add('include-position');
+
+ li.appendChild(a, span);
+ ul.appendChild(li);
+ }
+}
+
+window.addEventListener('load', async function () {
+ {/* for host list */
+ let j = await pdb('facts', ['extract', ['certname', 'value'], ['=', 'name', 'operatingsystem']])
+ for (let item of j) {
+ // NOTE these should always be cached
+ const target = document.querySelector(`img.distroicon[data-host="${item["certname"]}"]`);
+ if (! target) continue;
+
+ let value = item['value']
+ target.alt = value
+ let name = value.toLowerCase()
+ let filename = distro_names.get(name) || name
+ target.src = `/static/distro-icon/128_${filename}.png`
+ }
+ }
+
+ {/* For class list */
+ let m = new Map
+ let j = await pdb('resources',
+ ['extract', [['function', 'count'], 'title'],
+ ['=', 'type', 'Class'],
+ ['group_by', 'title']])
+ for (let item of j) {
+ m.set(item['title'], item['count']);
+ }
+
+ for (let el of document.querySelectorAll('[data-cls]')) {
+ let count = m.get(upcasePuppet(el.dataset.cls)) || '0'
+ el.innerText = `(${count})`;
+ }
+ }
+
+ // {/* For environment list */
+ // for (let env_type of ['facts_environment', 'report_environment']) {
+ // pdb('nodes', ['extract', [['function', 'count'], env_type],
+ // ['group_by', env_type]])
+ // }
+ // }
+
+});