This commit is contained in:
Wolfgang Hottgenroth 2015-10-17 22:40:25 +02:00
parent 0b094b8df2
commit 7345073bb0
3 changed files with 59 additions and 37 deletions

View File

@ -3,7 +3,40 @@ import datetime
import xlwt
class MySheet:
def __init__(self, sheet):
self.sheet = sheet
self.rowCnt = 0
self.colCnt = 0
self.widthFactor = 268
self.dateStyle = xlwt.XFStyle()
self.dateStyle.num_format_str = 'DD.MM.YYYY hh:mm:ss'
def write(self, txt, style=None):
if isinstance(txt, datetime.datetime):
self.sheet.row(self.rowCnt).write(self.colCnt, txt, self.dateStyle)
else:
self.sheet.row(self.rowCnt).write(self.colCnt, txt)
l = len(unicode(txt)) * self.widthFactor
if l > self.sheet.col(self.colCnt).width:
self.sheet.col(self.colCnt).width = l
def writeFirstFirst(self, txt, style=None):
self.rowCnt = 0
self.colCnt = 0
self.write(txt, style)
def writeNextFirst(self, txt, style=None):
self.rowCnt += 1
self.colCnt = 0
self.write(txt, style)
def writeSameNext(self, txt, style=None):
self.colCnt += 1
self.write(txt, style)
dbh = None
cur = None
xls = None
@ -48,55 +81,49 @@ SELECT d.description, d.address,
secondsPerWeek = datetime.timedelta(7).total_seconds()
xls = xlwt.Workbook()
sheet = xls.add_sheet('Sheet 1')
rowCnt = 0
sheet.row(rowCnt).write(0, 'Name')
sheet.row(rowCnt).write(1, 'Address')
sheet.row(rowCnt).write(2, 'First Sample Date')
sheet.row(rowCnt).write(3, 'First Sample Value')
sheet.row(rowCnt).write(4, 'Last Sample Date')
sheet.row(rowCnt).write(5, 'Last Sample Value')
sheet.row(rowCnt).write(6, 'Diff in SampleBox')
sheet.row(rowCnt).write(7, 'MonthFactor')
sheet.row(rowCnt).write(8, 'WeekFactor')
sheet.row(rowCnt).write(9, 'Month Average/Forecast')
sheet.row(rowCnt).write(10, 'Week Average/Forecast')
mySheet = MySheet(xls.add_sheet('Sheet 1'))
mySheet.writeFirstFirst('Name')
mySheet.writeSameNext('Address')
mySheet.writeSameNext('First Sample Date')
mySheet.writeSameNext('First Sample Value')
mySheet.writeSameNext('Last Sample Date')
mySheet.writeSameNext('Last Sample Value')
mySheet.writeSameNext('Diff in SampleBox')
mySheet.writeSameNext('MonthFactor')
mySheet.writeSameNext('WeekFactor')
mySheet.writeSameNext('Month Average/Forecast')
mySheet.writeSameNext('Week Average/Forecast')
sheet.col(2).width = 256 * 20
sheet.col(4).width = 256 * 20
dateStyle = xlwt.XFStyle()
dateStyle.num_format_str = 'DD.MM.YYYY hh:mm:ss'
for printer in printers.values():
rowCnt += 1
print("Printer: %s, %s" % (printer['name'], printer['address']))
sheet.row(rowCnt).write(0, printer['name'])
sheet.row(rowCnt).write(1, printer['address'])
mySheet.writeNextFirst(printer['name'])
mySheet.writeSameNext(printer['address'])
print(" First: %s, %s" % (printer['first']['timestamp'], printer['first']['value']))
sheet.row(rowCnt).write(2, printer['first']['timestamp'], dateStyle)
sheet.row(rowCnt).write(3, printer['first']['value'])
mySheet.writeSameNext(printer['first']['timestamp'])
mySheet.writeSameNext(printer['first']['value'])
print(" Last: %s, %s" % (printer['last']['timestamp'], printer['last']['value']))
sheet.row(rowCnt).write(4, printer['last']['timestamp'], dateStyle)
sheet.row(rowCnt).write(5, printer['last']['value'])
mySheet.writeSameNext(printer['last']['timestamp'])
mySheet.writeSameNext(printer['last']['value'])
deltaTime = printer['last']['timestamp'] - printer['first']['timestamp']
deltaValue = printer['last']['value'] - printer['first']['value']
print(" Diff: %s, %s" % (deltaTime, deltaValue))
sheet.row(rowCnt).write(6, deltaValue)
mySheet.writeSameNext(deltaValue)
factorMonth = secondsPerMonth / deltaTime.total_seconds()
sheet.row(rowCnt).write(7, factorMonth)
mySheet.writeSameNext(factorMonth)
factorWeek = secondsPerWeek / deltaTime.total_seconds()
sheet.row(rowCnt).write(8, factorWeek)
mySheet.writeSameNext(factorWeek)
print(" Month average/forecast: %s" % (int(deltaValue * factorMonth)))
sheet.row(rowCnt).write(9, int(deltaValue * factorMonth))
mySheet.writeSameNext(int(deltaValue * factorMonth))
print(" Week average/forecast: %s" % (int(deltaValue * factorWeek)))
sheet.row(rowCnt).write(10, int(deltaValue * factorWeek))
mySheet.writeSameNext(int(deltaValue * factorWeek))
except mysql.connector.Error as err:

View File

@ -1,16 +1,10 @@
<Properties StartupItem="Snmp2Mysql/Snmp2Mysql.csproj">
<MonoDevelop.Ide.Workspace ActiveConfiguration="Release" />
<<<<<<< local
<MonoDevelop.Ide.Workbench />
=======
<MonoDevelop.Ide.Workbench ActiveDocument="Snmp2Mysql/Program.cs">
<Files>
<File FileName="Snmp2Mysql/DatabaseLink.cs" Line="1" Column="1" />
<File FileName="Snmp2Mysql/Program.cs" Line="20" Column="60" />
<File FileName="Snmp2Mysql/SnmpGetter.cs" Line="1" Column="1" />
<File FileName="Snmp2Mysql/Program.cs" Line="16" Column="2" />
</Files>
</MonoDevelop.Ide.Workbench>
>>>>>>> other
<MonoDevelop.Ide.DebuggingService.Breakpoints>
<BreakpointStore />
</MonoDevelop.Ide.DebuggingService.Breakpoints>

View File

@ -33,6 +33,7 @@ namespace Snmp2Mysql
if (verbose)
Console.WriteLine ("Database connection to {0}", dbConnStr);
DatabaseLink dbLink = new DatabaseLink(dbConnStr);
using (DataCollector dc = dbLink.DataCollector)
using (DeviceProvider dp = dbLink.DeviceProvider)