-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBackgroundCheckStats.py
More file actions
151 lines (118 loc) · 4 KB
/
Copy pathBackgroundCheckStats.py
File metadata and controls
151 lines (118 loc) · 4 KB
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
mainQuery = model.SqlContent('BackgroundChecks-Status')
model.Title = "Background Checks Stats"
mainQuery = mainQuery.replace("-- INTO ", "INTO ")
sql = """
{0};
SELECT
IIF(Status = 'Invalid', 'Invalid', 'Valid') as status,
IIF(Employee = 'Employee', 'Employee', 'Volunteer') as employee,
IIF(TrainAssign > Training OR (TrainAssign IS NOT NULL AND Training IS NULL), 'Assigned',
IIF(Training IS NULL OR Training < GETDATE(), 'Invalid', 'Valid')
) as training
INTO #summary
FROM #status s;
SELECT COUNT(*) as count,
status as [BackgroundCheck],
employee,
training as [Training]
FROM #summary
GROUP BY status, employee, training
""".format(mainQuery)
d = []
for row in model.SqlListDynamicData(sql):
d.append({
"count": row.count,
"Employee": row.employee,
"Status": row.BackgroundCheck,
"Training": row.Training,
})
print """
<div>
<label for="employeeFilter">Filter by Employment: </label>
<select id="employeeFilter">
<option value="all">All</option>
</select>
</div>
<div id="chart_div"></div>
"""
print """
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
let data = {0};""".format(model.JsonSerialize(d))
print """
// Load the Google Charts package
google.charts.load('current', { packages: ['corechart', 'bar'] });
google.charts.setOnLoadCallback(drawChart);
let employeeFilter = document.getElementById('employeeFilter');
// Populate employee filter dropdown
let employees = [...new Set(data.map(item => item.Employee))]; // Unique employees
employees.forEach(emp => {
let option = document.createElement('option');
option.value = emp;
option.text = emp;
employeeFilter.add(option);
});
// Function to filter and aggregate data
function filterData(employee) {
let filteredData = employee === 'all' ? data : data.filter(d => d.Employee === employee);
// Aggregate data for status and training
let statusCounts = {};
let trainingCounts = {};
for (let i = 0; i < filteredData.length; ++i) {
d = filteredData[i];
// Aggregate by status
if (!statusCounts[d.Status]) {
statusCounts[d.Status] = 0;
}
statusCounts[d.Status] += d.count;
// Aggregate by training
if (!trainingCounts[d.Training]) {
trainingCounts[d.Training] = 0;
}
trainingCounts[d.Training] += d.count;
};
// Prepare data for Google Charts
let chartData = [['Category', 'Valid', 'Invalid', 'Assigned']],
classes = {
'BackgroundChecks': statusCounts,
'Training': trainingCounts
}
maxValue = 0;
for (const cl in classes) {
chartData.push([cl, classes[cl]['Valid'] || 0, classes[cl]['Invalid'] || 0, classes[cl]['Assigned'] || 0])
let sum = (classes[cl]['Valid'] || 0) + (classes[cl]['Invalid'] || 0) + (classes[cl]['Assigned'] || 0);
if (sum > maxValue) { maxValue = sum; }
}
return [chartData, maxValue];
}
// Function to draw the chart
function drawChart(employee = 'all') {
let [chartData, maxValue] = filterData(employee);
let data = google.visualization.arrayToDataTable(chartData);
let options = {
chartArea: { width: '50%' },
hAxis: {
title: 'Total Count',
viewWindow: {
min: 0,
max: maxValue
}
},
vAxis: {
title: 'Category',
},
isStacked: true,
legend: { position: 'top', maxLines: 3 },
colors: ['#49917b', '#ff0000', '#d95f02'] // Colors for status and training
};
let chart = new google.visualization.BarChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
// Event listener for employee filter
employeeFilter.addEventListener('change', function () {
drawChart(this.value);
});
// Initial chart rendering (for all employees)
google.charts.setOnLoadCallback(() => drawChart('all'));
</script>
"""