Skip to content

Commit 99121ef

Browse files
authored
Add Background Check Stats report generation
1 parent e76fdcb commit 99121ef

1 file changed

Lines changed: 151 additions & 0 deletions

File tree

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
mainQuery = model.SqlContent('BackgroundChecks-Status')
2+
3+
model.Title = "Background Checks Stats"
4+
5+
mainQuery = mainQuery.replace("-- INTO ", "INTO ")
6+
7+
sql = """
8+
{0};
9+
10+
SELECT
11+
IIF(Status = 'Invalid', 'Invalid', 'Valid') as status,
12+
IIF(Employee = 'Employee', 'Employee', 'Volunteer') as employee,
13+
IIF(TrainAssign > Training OR (TrainAssign IS NOT NULL AND Training IS NULL), 'Assigned',
14+
IIF(Training IS NULL OR Training < GETDATE(), 'Invalid', 'Valid')
15+
) as training
16+
INTO #summary
17+
FROM #status s;
18+
19+
SELECT COUNT(*) as count,
20+
status as [BackgroundCheck],
21+
employee,
22+
training as [Training]
23+
FROM #summary
24+
GROUP BY status, employee, training
25+
26+
""".format(mainQuery)
27+
28+
29+
d = []
30+
for row in model.SqlListDynamicData(sql):
31+
d.append({
32+
"count": row.count,
33+
"Employee": row.employee,
34+
"Status": row.BackgroundCheck,
35+
"Training": row.Training,
36+
})
37+
38+
39+
print """
40+
<div>
41+
<label for="employeeFilter">Filter by Employment: </label>
42+
<select id="employeeFilter">
43+
<option value="all">All</option>
44+
</select>
45+
</div>
46+
<div id="chart_div"></div>
47+
"""
48+
49+
print """
50+
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
51+
<script type="text/javascript">
52+
53+
let data = {0};""".format(model.JsonSerialize(d))
54+
55+
print """
56+
57+
// Load the Google Charts package
58+
google.charts.load('current', { packages: ['corechart', 'bar'] });
59+
google.charts.setOnLoadCallback(drawChart);
60+
61+
let employeeFilter = document.getElementById('employeeFilter');
62+
63+
// Populate employee filter dropdown
64+
let employees = [...new Set(data.map(item => item.Employee))]; // Unique employees
65+
employees.forEach(emp => {
66+
let option = document.createElement('option');
67+
option.value = emp;
68+
option.text = emp;
69+
employeeFilter.add(option);
70+
});
71+
72+
// Function to filter and aggregate data
73+
function filterData(employee) {
74+
let filteredData = employee === 'all' ? data : data.filter(d => d.Employee === employee);
75+
76+
// Aggregate data for status and training
77+
let statusCounts = {};
78+
let trainingCounts = {};
79+
80+
for (let i = 0; i < filteredData.length; ++i) {
81+
d = filteredData[i];
82+
83+
// Aggregate by status
84+
if (!statusCounts[d.Status]) {
85+
statusCounts[d.Status] = 0;
86+
}
87+
statusCounts[d.Status] += d.count;
88+
89+
// Aggregate by training
90+
if (!trainingCounts[d.Training]) {
91+
trainingCounts[d.Training] = 0;
92+
}
93+
trainingCounts[d.Training] += d.count;
94+
};
95+
96+
// Prepare data for Google Charts
97+
let chartData = [['Category', 'Valid', 'Invalid', 'Assigned']],
98+
classes = {
99+
'BackgroundChecks': statusCounts,
100+
'Training': trainingCounts
101+
}
102+
maxValue = 0;
103+
for (const cl in classes) {
104+
chartData.push([cl, classes[cl]['Valid'] || 0, classes[cl]['Invalid'] || 0, classes[cl]['Assigned'] || 0])
105+
let sum = (classes[cl]['Valid'] || 0) + (classes[cl]['Invalid'] || 0) + (classes[cl]['Assigned'] || 0);
106+
if (sum > maxValue) { maxValue = sum; }
107+
}
108+
109+
return [chartData, maxValue];
110+
}
111+
112+
// Function to draw the chart
113+
function drawChart(employee = 'all') {
114+
let [chartData, maxValue] = filterData(employee);
115+
116+
117+
let data = google.visualization.arrayToDataTable(chartData);
118+
119+
let options = {
120+
chartArea: { width: '50%' },
121+
hAxis: {
122+
title: 'Total Count',
123+
viewWindow: {
124+
min: 0,
125+
max: maxValue
126+
}
127+
},
128+
vAxis: {
129+
title: 'Category',
130+
},
131+
isStacked: true,
132+
legend: { position: 'top', maxLines: 3 },
133+
colors: ['#49917b', '#ff0000', '#d95f02'] // Colors for status and training
134+
};
135+
136+
let chart = new google.visualization.BarChart(document.getElementById('chart_div'));
137+
chart.draw(data, options);
138+
}
139+
140+
// Event listener for employee filter
141+
employeeFilter.addEventListener('change', function () {
142+
drawChart(this.value);
143+
});
144+
145+
// Initial chart rendering (for all employees)
146+
google.charts.setOnLoadCallback(() => drawChart('all'));
147+
148+
149+
</script>
150+
151+
"""

0 commit comments

Comments
 (0)