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 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 dbh = None
cur = None cur = None
xls = None xls = None
@ -48,55 +81,49 @@ SELECT d.description, d.address,
secondsPerWeek = datetime.timedelta(7).total_seconds() secondsPerWeek = datetime.timedelta(7).total_seconds()
xls = xlwt.Workbook() xls = xlwt.Workbook()
sheet = xls.add_sheet('Sheet 1') mySheet = MySheet(xls.add_sheet('Sheet 1'))
rowCnt = 0 mySheet.writeFirstFirst('Name')
sheet.row(rowCnt).write(0, 'Name') mySheet.writeSameNext('Address')
sheet.row(rowCnt).write(1, 'Address') mySheet.writeSameNext('First Sample Date')
sheet.row(rowCnt).write(2, 'First Sample Date') mySheet.writeSameNext('First Sample Value')
sheet.row(rowCnt).write(3, 'First Sample Value') mySheet.writeSameNext('Last Sample Date')
sheet.row(rowCnt).write(4, 'Last Sample Date') mySheet.writeSameNext('Last Sample Value')
sheet.row(rowCnt).write(5, 'Last Sample Value') mySheet.writeSameNext('Diff in SampleBox')
sheet.row(rowCnt).write(6, 'Diff in SampleBox') mySheet.writeSameNext('MonthFactor')
sheet.row(rowCnt).write(7, 'MonthFactor') mySheet.writeSameNext('WeekFactor')
sheet.row(rowCnt).write(8, 'WeekFactor') mySheet.writeSameNext('Month Average/Forecast')
sheet.row(rowCnt).write(9, 'Month Average/Forecast') mySheet.writeSameNext('Week Average/Forecast')
sheet.row(rowCnt).write(10, '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(): for printer in printers.values():
rowCnt += 1
print("Printer: %s, %s" % (printer['name'], printer['address'])) print("Printer: %s, %s" % (printer['name'], printer['address']))
sheet.row(rowCnt).write(0, printer['name']) mySheet.writeNextFirst(printer['name'])
sheet.row(rowCnt).write(1, printer['address']) mySheet.writeSameNext(printer['address'])
print(" First: %s, %s" % (printer['first']['timestamp'], printer['first']['value'])) print(" First: %s, %s" % (printer['first']['timestamp'], printer['first']['value']))
sheet.row(rowCnt).write(2, printer['first']['timestamp'], dateStyle) mySheet.writeSameNext(printer['first']['timestamp'])
sheet.row(rowCnt).write(3, printer['first']['value']) mySheet.writeSameNext(printer['first']['value'])
print(" Last: %s, %s" % (printer['last']['timestamp'], printer['last']['value'])) print(" Last: %s, %s" % (printer['last']['timestamp'], printer['last']['value']))
sheet.row(rowCnt).write(4, printer['last']['timestamp'], dateStyle) mySheet.writeSameNext(printer['last']['timestamp'])
sheet.row(rowCnt).write(5, printer['last']['value']) mySheet.writeSameNext(printer['last']['value'])
deltaTime = printer['last']['timestamp'] - printer['first']['timestamp'] deltaTime = printer['last']['timestamp'] - printer['first']['timestamp']
deltaValue = printer['last']['value'] - printer['first']['value'] deltaValue = printer['last']['value'] - printer['first']['value']
print(" Diff: %s, %s" % (deltaTime, deltaValue)) print(" Diff: %s, %s" % (deltaTime, deltaValue))
sheet.row(rowCnt).write(6, deltaValue) mySheet.writeSameNext(deltaValue)
factorMonth = secondsPerMonth / deltaTime.total_seconds() factorMonth = secondsPerMonth / deltaTime.total_seconds()
sheet.row(rowCnt).write(7, factorMonth) mySheet.writeSameNext(factorMonth)
factorWeek = secondsPerWeek / deltaTime.total_seconds() factorWeek = secondsPerWeek / deltaTime.total_seconds()
sheet.row(rowCnt).write(8, factorWeek) mySheet.writeSameNext(factorWeek)
print(" Month average/forecast: %s" % (int(deltaValue * factorMonth))) 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))) 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: except mysql.connector.Error as err:

View File

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

View File

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