aboutsummaryrefslogtreecommitdiff
path: root/pyenc/static/js/script2.js
blob: 6f06fc76b5904166c4c010bd04e990c5ebedbd24 (plain)
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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]])
    //     }
    // }

});