Merge pull request #11 from saravanan30erd/download_report

Add download report feature
This commit is contained in:
adriaaah 2019-12-11 21:35:14 +01:00 committed by GitHub
commit ce5979b58f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 78 additions and 15 deletions

1
.gitignore vendored
View file

@ -1,3 +1,4 @@
*.pyc *.pyc
.*.swp .*.swp
conf/servers.json conf/servers.json
*.xlsx

View file

@ -37,6 +37,7 @@ thanks to the built-in web server, it is displayed in a single HTML page.
#### Python libaries #### Python libaries
- `yum install python-requests python-xmltodict python-simplejson` - `yum install python-requests python-xmltodict python-simplejson`
- `pip install xlsxwriter`
## Requisites ## Requisites

View file

@ -9,9 +9,11 @@ import sys
import datetime import datetime
from collections import OrderedDict from collections import OrderedDict
from operator import itemgetter from operator import itemgetter
import utils
urls = ('/', 'index', urls = ('/', 'index',
'/help', 'help' '/help', 'help',
'/download', 'download'
) )
app = web.application(urls, globals()) app = web.application(urls, globals())
@ -64,35 +66,44 @@ def getMonit():
checks[name] = status[name] checks[name] = status[name]
sorted_checks = OrderedDict() sorted_checks = OrderedDict()
sorted_checks = OrderedDict(sorted(checks.iteritems(), key=itemgetter(1), reverse=True)) sorted_checks = OrderedDict(sorted(checks.iteritems(),
key=itemgetter(1), reverse=True))
count = calculate_count(sorted_checks) count = calculate_count(sorted_checks)
server = dict(name=site, url=s['url'], result=sorted_checks, s_rate=count) server = dict(name=site, url=s['url'],
result=sorted_checks, s_rate=count)
output.append(server) output.append(server)
print(datetime.datetime.now()) print(datetime.datetime.now())
return(output) return(output)
# Classes # Classes
class monitDashboard(web.application): class monitDashboard(web.application):
def run(self, port=8080, *middleware): def run(self, port=8080, *middleware):
func = self.wsgifunc(*middleware) func = self.wsgifunc(*middleware)
return web.httpserver.runsimple(func, ('0.0.0.0', port)) return web.httpserver.runsimple(func, ('0.0.0.0', port))
class index(object): class index(object):
def GET(self): def GET(self):
return render.index(output=getMonit(), now=datetime.datetime.now()) return render.index(output=getMonit(),
now=datetime.datetime.now())
class help(object): class help(object):
def GET(self): def GET(self):
return render.help() return render.help()
class download(object):
def GET(self):
filename = 'health_report.xlsx'
output = getMonit()
utils.generate_report_excel(output, filename)
web.header('Content-Disposition',
'attachment; filename="health_report.xlsx"')
web.header('Content-type','application/octet-stream')
web.header('Cache-Control','no-cache')
return open(filename, 'rb').read()
# Main # Main
if __name__ == "__main__": if __name__ == "__main__":
app = monitDashboard(urls, globals()) app = monitDashboard(urls, globals())

26
bin/utils.py Normal file
View file

@ -0,0 +1,26 @@
import xlsxwriter
import os
def generate_report_excel(output, filename):
if os.path.exists(filename):
os.remove(filename)
workbook = xlsxwriter.Workbook(filename)
bold = workbook.add_format({'bold': 1})
for server in range(len(output)):
worksheet = workbook.add_worksheet(output[server]['name'])
worksheet.set_column('A:A', 40)
worksheet.set_column('B:B', 15)
worksheet.write('A1', 'Components', bold)
worksheet.write('B1', 'Status', bold)
row = 1
col = 0
for key, value in output[server]['result'].items():
worksheet.write_string(row, col, key)
if value == 0:
status = 'OK'
else:
status = 'Error'
worksheet.write_string(row, col + 1, key)
worksheet.write_string(row, col + 1, status)
row += 1
workbook.close()

View file

@ -33,7 +33,7 @@ a {
} }
button.accordion { button.accordion {
color: #444; color: #FFF;
cursor: pointer; cursor: pointer;
padding: 18px; padding: 18px;
width: 100%; width: 100%;
@ -41,7 +41,28 @@ button.accordion {
border: none; border: none;
outline: none; outline: none;
transition: 0.4s; transition: 0.4s;
margin: 0.6rem; margin: 0.6rem 0;
}
button.download-button {
color: white;
background-color: #00a8ff;
cursor: pointer;
padding: 18px;
text-align: center;
border: none;
outline: none;
transition: 0.4s;
float: right;
margin: auto 0;
}
div.download-header {
height: 7%;
background-color: #273c75;
display: flex;
justify-content: flex-end;
padding-right: 20px;
} }
div.panel { div.panel {
@ -73,7 +94,7 @@ div.panel.show {
} }
.canvas-container { .canvas-container {
height: 40%; height: 35%;
margin: auto; margin: auto;
width: 24%; width: 24%;
} }

View file

@ -19,7 +19,7 @@ function draw(rate) {
var red = (percentage[1]*2)/100; var red = (percentage[1]*2)/100;
var canvas = document.getElementById("canvas"); var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d"); var ctx = canvas.getContext("2d");
var colors = ['#40ff00', '#ff0000']; var colors = ['#00a90e','#ff9a02'];
var angles = [Math.PI * green, Math.PI * red]; var angles = [Math.PI * green, Math.PI * red];
var offset = 0; var offset = 0;
var beginAngle = 0; var beginAngle = 0;
@ -41,7 +41,7 @@ function draw(rate) {
ctx.moveTo(200 + offsetX, 200 + offsetY); ctx.moveTo(200 + offsetX, 200 + offsetY);
ctx.arc(200 + offsetX, 200 + offsetY, 120, beginAngle, endAngle); ctx.arc(200 + offsetX, 200 + offsetY, 120, beginAngle, endAngle);
ctx.lineTo(200 + offsetX, 200 + offsetY); ctx.lineTo(200 + offsetX, 200 + offsetY);
ctx.stroke(); // ctx.stroke();
ctx.fill(); ctx.fill();
ctx.rect(canvas.width - 129, i * 20 + 10, 10, 10); ctx.rect(canvas.width - 129, i * 20 + 10, 10, 10);

View file

@ -4,7 +4,10 @@ $def with (output, now)
$ errors = 0 $ errors = 0
$ color = "green" $ color = "green"
<div> <div>
<div style="height: 40%; overflow-y: scroll; overflow-x: hidden;"> <div class="download-header">
<button class="download-button" onclick="window.location.href = 'download';">Download Report</button>
</div>
<div style="height: 38%; overflow-y: scroll; overflow-x: hidden;">
$for server in range(len(output)): $for server in range(len(output)):
$code: $code:
errors = 0 errors = 0