📄 history.py
字号:
# -*- coding: UTF-8 -*-# $Id: history.py 4232 2007-10-01 10:41:16Z mortenv $## Copyright 2003, 2004 Norwegian University of Science and Technology# Copyright 2007 UNINETT AS## This file is part of Network Administration Visualized (NAV)## NAV is free software; you can redistribute it and/or modify# it under the terms of the GNU General Public License as published by# the Free Software Foundation; either version 2 of the License, or# (at your option) any later version.## NAV is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU General Public License for more details.## You should have received a copy of the GNU General Public License# along with NAV; if not, write to the Free Software# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA### Authors: Hans J酶rgen Hoel <hansjorg@orakel.ntnu.no># Stein Magnus Jodal <stein.magnus.jodal@uninett.no#"""History page with helper classes of Device Management"""### Importsimport calendarimport forgetSQLimport mx.DateTimeimport reimport nav.dbimport nav.db.managefrom nav.web import urlbuilderfrom nav.web.templates.deviceManagementTemplate import deviceManagementTemplatefrom nav.web.devicemanagement.constants import *from nav.web.devicemanagement.common import *from nav.web.devicemanagement import db as dtTablesfrom nav.web.devicemanagement.deviceevent import DeviceEventfrom nav.web.devicemanagement.page import Pagefrom nav.web.devicemanagement.widget import Widget### Functionsdef history(req,deviceorderid=None): page = Page() form = req.form page.name = 'history' page.title = 'Device history' page.description = 'Select and view device history for a location, ' + \ 'room, box or module. Use quicksearch to search for '+\ 'a (partial) serialnumber, hostname, IP or room.' page.widgets = {} dbconn = nav.db.getConnection('devicemanagement', 'manage') db = dbconn.cursor() # Get year of first entry in alerthist date_options = {} sql = """ SELECT min(start_time) IS NOT NULL AS exists, min(start_time) FROM alerthist """ db.execute(sql) row = db.dictfetchall()[0] if row['exists']: date_options['startyear'] = row['min'].year # Get filter values if (form.has_key('startday') and form['startday'].isdigit() and form.has_key('startmonth') and form['startmonth'].isdigit() and form.has_key('startyear') and form['startyear'].isdigit()): startyear = int(form['startyear']) startmonth = int(form['startmonth']) startday = int(form['startday']) startdaymax = calendar.monthrange(startyear, startmonth)[1] if startday > startdaymax: startday = startdaymax startdate_value = [str(startyear), str(startmonth), str(startday)] startTime = mx.DateTime.Date(startyear, startmonth, startday) else: weekago = mx.DateTime.now() - mx.DateTime.oneWeek startdate_value = [str(weekago.year), str(weekago.month), str(weekago.day)] startTime = weekago if (form.has_key('endday') and form['endday'].isdigit() and form.has_key('endmonth') and form['endmonth'].isdigit() and form.has_key('endyear') and form['endyear'].isdigit()): endyear = int(form['endyear']) endmonth = int(form['endmonth']) endday = int(form['endday']) enddaymax = calendar.monthrange(endyear, endmonth)[1] if endday > enddaymax: endday = enddaymax enddate_value = [str(endyear), str(endmonth), str(endday)] endTime = mx.DateTime.Date(endyear, endmonth, endday, 23, 59, 59) else: now = mx.DateTime.now() enddate_value = [str(now.year), str(now.month), str(now.day)] endTime = now if form.has_key('type') and form['type'] != 'All': type_value = form['type'] else: type_value = 'All' if type_value.startswith('e_'): eventtype_filter = [type_value[2:]] else: eventtype_filter = [] if type_value.startswith('a_'): alerttype_filter = [type_value[2:]] else: alerttype_filter = [] type_options = {'options': [('opt', 'All', 'All', False)]} for eventtype in nav.db.manage.Eventtype.getAllIDs(): if type_value.startswith('e_') and type_value.endswith(eventtype): selected = True else: selected = False optgroup = [('opt', 'e_%s' % eventtype, 'All %s' % eventtype, selected)] for alerttype in nav.db.manage.Alerttype.getAllIDs( where="eventtypeid='%s'" % eventtype, orderBy='alerttype'): if type_value.startswith('a_') and type_value.endswith(alerttype): selected = True else: selected = False optgroup.append(('opt', 'a_%s' % alerttype, alerttype, selected)) type_options['options'].append(('grp', eventtype, eventtype, optgroup)) # Create filter form widgets page.widgets['filter_startdate'] = Widget(['startday', 'startmonth', 'startyear'], 'date', name='Start date', value=startdate_value, options=date_options) page.widgets['filter_enddate'] = Widget(['endday', 'endmonth', 'endyear'], 'date', name='End date', value=enddate_value, options=date_options) page.widgets['filter_eventtype'] = Widget('type', 'selectoptgroup', name='Type', options=type_options) page.widgets['filter_submit'] = Widget('history', 'submit', 'Filter') # Add data from treeselect to hidden fields in the filter form page.filterform = {} if form.has_key('location'): page.filterform['location'] = form['location'] else: page.filterform['location'] = '' if form.has_key('room'): page.filterform['room'] = form['room'] else: page.filterform['room'] = '' if form.has_key('box'): page.filterform['box'] = form['box'] else: page.filterform['box'] = '' if form.has_key('module'): page.filterform['module'] = form['module'] else: page.filterform['module'] = '' # FIXME: Vidar, what are these links supposed to point to? #submenu = [('Browse devices','Browse or search for devices', # BASEPATH), # ('Show active devices','Show all devices in operation', # BASEPATH), # ('Show devices with registered errors', # 'Show all devices with registered errors', # BASEPATH)] submenu = [] if deviceorderid: submenu.append(('Order history','Go back to order history', BASEPATH+'order/history')) page.submenu = submenu # Set menu page.menu = makeMainMenu(selected=0) page.action = '' page.subname = '' showHistory = False if form.has_key('history'): # History mode historyType = None if form.has_key(CN_MODULE): historyType = CN_MODULE elif form.has_key(CN_BOX): historyType = CN_BOX elif form.has_key(CN_ROOM): historyType = CN_ROOM elif form.has_key(CN_LOCATION): historyType = CN_LOCATION elif form.has_key(CN_DEVICE): historyType = CN_DEVICE if historyType: showHistory = True unitList = form[historyType] if not type(unitList) is list: unitList = [unitList] page.boxList = makeHistory(form, historyType, unitList, startTime, endTime, eventtype_filter, alerttype_filter) page.searchbox = None page.subname = 'history' else: page.errors.append('No unit selected') elif deviceorderid: sql = "SELECT deviceid FROM device WHERE " +\ "deviceorderid='%s'" % (deviceorderid,) result = executeSQL(sql,fetch=True) if result: historyType = CN_DEVICE unitList = [] for row in result: unitList.append(row[0]) page.boxList = makeHistory(form, historyType, unitList, startTime, endTime, eventtype_filter, alerttype_filter) page.searchbox = None page.subname = 'history' else: page.errors.append('Could not find any devices for this order') if not showHistory: # Browse mode, make treeselect page.searchbox,page.treeselect = makeTreeSelect(req,serialSearch=True) page.formname = page.treeselect.formName #validSubmit = False #if form.has_key(CN_LOCATION): # # If a location has been selected, allow submit # validSubmit = True page.submit = {'control': 'history', 'value': 'View history', 'enabled': True} nameSpace = {'page': page} template = deviceManagementTemplate(searchList=[nameSpace]) template.path = CURRENT_PATH return template.respond()def makeHistory(form, historyType, unitList, startTime, endTime, eventtypes, alerttypes): boxList = [] for unitid in unitList: if historyType == CN_MODULE: boxList.append(ModuleHistoryBox(unitid, startTime, endTime, eventtypes, alerttypes)) elif historyType == CN_BOX: boxList.append(NetboxHistoryBox(unitid, startTime, endTime, eventtypes, alerttypes)) elif historyType == CN_ROOM: boxList.append(RoomHistoryBox(unitid, startTime, endTime, eventtypes, alerttypes)) elif historyType == CN_LOCATION: boxList.append(LocationHistoryBox(unitid, startTime, endTime, eventtypes, alerttypes)) elif historyType == CN_DEVICE: where = "deviceid='%s'" % (unitid,) box = nav.db.manage.Netbox.getAll(where=where) module = nav.db.manage.Module.getAll(where=where) if box: box = box[0] boxList.append(NetboxHistoryBox(box.netboxid, startTime, endTime, eventtypes, alerttypes)) elif module: module = module[0] boxList.append(ModuleHistoryBox(module.moduleid, startTime, endTime, eventtypes, alerttypes)) else: boxList.append(DeviceHistoryBox(unitid, startTime, endTime, eventtypes, alerttypes)) return boxList### Classesclass HistoryBox: # Format string tokens tokenVars = {DN_E_USERNAME: ['username',DeviceEvent.STATE_NONE], DN_E_COMMENT: ['comment',DeviceEvent.STATE_NONE], DN_E_UNITTYPE: ['unittype',DeviceEvent.STATE_NONE], DN_E_LOCATIONID: ['locationid',DeviceEvent.STATE_NONE], DN_E_ROOMID: ['roomid',DeviceEvent.STATE_NONE]} globalVars = {G_EVENTTYPE: 'eventtypeid', G_ALERTTYPE: 'alerttype', G_ALLVARS: 'allvars', G_ALLMSGS: 'allmsgs'} headings = [('Start',''), ('End',''), ('Description','')]
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -