def server_create_view(request): INIFileTemplate = _loadServerINITemplate() INIFileTemplate['server']["name"] =["name",""] INIFileDict = __fillINIwithTemplate(INIFileTemplate,{}) INIFileDict['server']["tests"] =[] INIFileDict['server']["outlets"] =[] INIFileDict['server']["controls"] =[] INIFileDict['server']["dependencies"] =[] multiListChoices = _makeMultichoice("server","tests",lambda: {},INIFileDict) multiListChoices = _makeMultichoice("server","outlets",lambda: {},INIFileDict,multiListChoices) multiListChoices = _makeMultichoice("server","controls",lambda: {},INIFileDict,multiListChoices) multiListChoices = _makeMultichoice("server","dependencies",lambda: {},INIFileDict,multiListChoices) return {"layout": site_layout(), "config_sidebar_head" : config_sidebar_head(), "config_sidebar_body" : config_sidebar_body(), "INI_InputArea_head" : INI_InputArea_head(), "INI_InputArea_body" : INI_InputArea_body(), "INIFileDict" : INIFileDict, "INIFileTemplate" : INIFileTemplate, "multiListChoices" : multiListChoices, "OBJnameSection" : "server", "configPathPrefix": getServerFolder() + "/", "existingOBJCallback" : "checkExistingServers" , "page_title": "Add new Server" }
def index_view(request): ''' View of the server network ''' #get Dot data dot= getServerTree() #Add generate an xdot file from the dot we got from the server gv = pgv.AGraph(string=dot,weights=False) #TODO: fix this ugly escape character, ie add a javascript variable wrapper #gv.node_attr.update(href="javascript:void(click_node(\\'\\\N\\'))") gv.node_attr.update(href="server/\\\N") gv.node_attr.update(title="\\\N") gv.node_attr.update(tooltip="\\\N") gv.node_attr.update(shape="ellipse") gv.node_attr.update(style="filled") gv.node_attr.update(fillcolor="#CBE6FF") gv.node_attr.update(fontname="Arial") gv.graph_attr.update(bgcolor="#F4F4F4") for node in gv.nodes(): print node.get_name() gv.layout(prog='dot') #get autocontrol stuff autoControlStatus = getAutoControlStatus()["status"] return {"layout": site_layout(), "page_title": "Server Network View", "autoControlStatus" : autoControlStatus, "xdottree" : gv.draw(format="xdot").replace('\n','\\n\\\n')}
def server_edit_view(request): serverName = request.matchdict['serverName'] configPath= _getServerConfigPath(serverName) INIFileDict = _loadServerConfig(serverName) #testerType = INIFileDict["tester"]["type"] INIFileTemplate = _loadServerINITemplate() INIFileDict = __fillINIwithTemplate(INIFileTemplate,INIFileDict) multiListChoices = _makeMultichoice("server","tests",lambda: getAvailableServerTesters(serverName),INIFileDict) multiListChoices = _makeMultichoice("server","outlets",lambda: getAvailableServerOutlets(serverName),INIFileDict,multiListChoices) multiListChoices = _makeMultichoice("server","controls",lambda: getAvailableServerControls(serverName),INIFileDict,multiListChoices) multiListChoices = _makeMultichoice("server","dependencies",lambda: getServerAvilableDependencies(serverName),INIFileDict,multiListChoices) #multiListChoices = _makeSelectMulitChoice(testerType,"tester",getAvailableTestersList) return {"layout": site_layout(), "config_sidebar_head" : config_sidebar_head(), "config_sidebar_body" : config_sidebar_body(), "INI_InputArea_head" : INI_InputArea_head(), "INI_InputArea_body" : INI_InputArea_body(), "INIFileDict" : INIFileDict, "INIFileTemplate" : INIFileTemplate, "configPath" : configPath, "serverName" : serverName, "multiListChoices" : multiListChoices, "deleteCallback" : "server", "objectName" : str(serverName), "redirectURL" : "/", "page_title": "Server Edit: " + str(serverName)}
def _server_obj_create(request,obj,objGenerator,objGeneratorConfigCallback,objGeneratorTemplateCallback,getObjGeneratorsCallback,objNameExistJavascriptCallback): serverName = request.matchdict['serverName'] PDU = request.matchdict[objGenerator] tmpName = obj +"Params" #serverName pduType = objGeneratorConfigCallback(PDU)[objGenerator.lower()]["type"] tmpINIFileTemplate = objGeneratorTemplateCallback(pduType) #Here we move the template dict to the right name INIFileTemplate = {} INIFileTemplate[tmpName] = {} try: INIFileTemplate[tmpName] = tmpINIFileTemplate[obj +"Params"] except KeyError: pass INIFileTemplate[tmpName]["name"] =["name",""] INIFileTemplate[tmpName][objGenerator.lower()] =["select",True] #Remove the pdu params if exist, we handle them in the server section try: INIFileTemplate.pop(objGenerator.lower()) except KeyError: pass INIFileDict = __fillINIwithTemplate(INIFileTemplate) try: INIFileDict[tmpName][objGenerator.lower()] = PDU except KeyError: INIFileDict[tmpName] = {} INIFileDict[tmpName][objGenerator.lower()] = PDU multiListChoices= _makeSelectMulitChoice(PDU,tmpName,objGenerator.lower(),getObjGeneratorsCallback) configPath= os.path.join(getServerFolder() , serverName + '.ini') return {"layout": site_layout(), "config_sidebar_head" : config_sidebar_head(), "config_sidebar_body" : config_sidebar_body(), "INI_InputArea_head" : INI_InputArea_head(), "INI_InputArea_body" : INI_InputArea_body(), "INIFileDict" : INIFileDict, "INIFileTemplate" : INIFileTemplate, "multiListChoices" : multiListChoices, "OBJnameSection" : tmpName, "matchdict" : json.dumps(request.matchdict), "changeSavePathToName" : 'false', "configPath": configPath, "existingOBJCallback" : objNameExistJavascriptCallback, "page_title": serverName + ": Add new " + obj +" using " + PDU }
def _obj_add_list_view(request,objGenerator,TypeList): return {"layout": site_layout(), "config_sidebar_head" : config_sidebar_head(), "config_sidebar_body" : config_sidebar_body(), "INI_InputArea_head" : INI_InputArea_head(), "TypeList" : TypeList, "typeCreatePath" : objGenerator.lower() + "Create", "TypeCreateName" : "type of " + objGenerator +" to create:", "page_title": "Add new " + objGenerator +" - Select type from list" }
def servers_view(request): return {"layout": site_layout(), "config_sidebar_head" : config_sidebar_head(), "config_sidebar_body" : config_sidebar_body(), "ObjectList" : getServerDict(), "ObjectName" : "server", "ObjectClassName" : "server", "AddURL" : "/server_add", "ObjectURLCallback" : _objectEditUrl, "page_title": "Servers"}
def controllers_view(request): return {"layout": site_layout(), "config_sidebar_head" : config_sidebar_head(), "config_sidebar_body" : config_sidebar_body(), "ObjectList" : getControllerDict(), "ObjectName" : "controller", "ObjectClassName" : "controller", "AddURL" : "/controllers_add_list", "ObjectURLCallback" : _objectEditUrl, "page_title": "Controllers"}
def pdus_view(request): return {"layout": site_layout(), "config_sidebar_head" : config_sidebar_head(), "config_sidebar_body" : config_sidebar_body(), "ObjectList" : getPDUDict(), "ObjectName" : "PDU", "ObjectClassName" : "pdu", "AddURL" : "/pdus_add_list", "ObjectURLCallback" : _objectEditUrl, "page_title": "PDUs"}
def _servers_obj_add_list_view(request,obj,objDict,objGenerator): serverName = request.matchdict['serverName'] for key in objDict.keys(): objDict[key] = str(objDict[key][objGenerator]["comment"]) return {"layout": site_layout(), "config_sidebar_head" : config_sidebar_head(), "config_sidebar_body" : config_sidebar_body(), "INI_InputArea_head" : INI_InputArea_head(), "TypeList" : objDict, "typeCreatePath" : "server/" + serverName + "/" + obj + "Create", "TypeCreateName" : "from which " + obj + " the outlet is located", "page_title": serverName + ": Add new Server "+ obj +" - Select " + objGenerator +" from list" }
def _objGenerator_edit_view(request,objName,objGenerator,objURLName,getObjGenertorFolderCallback,objGeneratorConfigCallback,objGeneratorTemplateCallback,getAvilableObjGenetorsList): ''' Make an edit view for an object generator (like a PDU or tester) :param request: The request from pyramid :param objName: the object name :param objGenerator: The name of the object generator :param objURLName: The url name that holds the name of the current object we are editing :param getObjGenertorFolderCallback: A function that returns the path of the object generator folder :param objGeneratorConfigCallback: A function the returns the object generator config :param objGeneratorTemplateCallback: A function that returns the template of the current object generator :param getAvilableObjGenetorsList: A callback that returns the current available object generators :return: A ready to render dict for the edit page ''' PDUName = request.matchdict[objURLName] configPath= os.path.join(getObjGenertorFolderCallback() , PDUName + '.ini') INIFileDict = objGeneratorConfigCallback(PDUName) outletType = INIFileDict[objGenerator.lower()]["type"] INIFileTemplate = objGeneratorTemplateCallback(outletType) INIFileDict = __fillINIwithTemplate(INIFileTemplate,INIFileDict) #Remove the obj params if exist, we handle them in the server section try: INIFileTemplate.pop(objName + "Params") except: pass multiListChoices = _makeSelectMulitChoice(outletType,objGenerator.lower(),"type",getAvilableObjGenetorsList) return {"layout": site_layout(), "config_sidebar_head" : config_sidebar_head(), "config_sidebar_body" : config_sidebar_body(), "INI_InputArea_head" : INI_InputArea_head(), "INI_InputArea_body" : INI_InputArea_body(), "INIFileDict" : INIFileDict, "INIFileTemplate" : INIFileTemplate, "configPath" : configPath, "multiListChoices" : multiListChoices, "page_title": objGenerator + " Edit: " + str(PDUName), #delete-variables "deleteCallback" : objGenerator.lower(), "objectName" : PDUName, "redirectURL" : "/" + objGenerator.lower() +"s"}
def _objGenerator_create(request,objName,objGenerator,loadObjGeneratorTemplate,getAvailableObjGeneratorsList,getObjGeneretorFolder): ''' Create a dict for an object Generator page :param objName: The name of the Server Object :param objGenerator: The name of the Server Object Generator :param loadObjGeneratorTemplate: a callback to get the template of the object Generator :param getAvailableObjGeneratorsList: Get a list of the object generators :param getObjGeneretorFolder: Get the folder of the object generator :return: a dict ready to be rendered ''' PDUType = request.matchdict[objGenerator.lower() + 'Type'] INIFileTemplate = loadObjGeneratorTemplate(PDUType) #Remove the outlet params if exist, we handle them in the server section try: INIFileTemplate.pop(objName +"Params") except: pass INIFileTemplate[objGenerator.lower()]["name"] =["name",""] INIFileDict = __fillINIwithTemplate(INIFileTemplate,{}) INIFileDict[objGenerator.lower()]["type"] = PDUType multiListChoices = _makeSelectMulitChoice(PDUType,objGenerator.lower(),"type",getAvailableObjGeneratorsList) return {"layout": site_layout(), "config_sidebar_head" : config_sidebar_head(), "config_sidebar_body" : config_sidebar_body(), "INI_InputArea_head" : INI_InputArea_head(), "INI_InputArea_body" : INI_InputArea_body(), "INIFileDict" : INIFileDict, "INIFileTemplate" : INIFileTemplate, "multiListChoices" : multiListChoices, "OBJnameSection" : objGenerator.lower(), "configPathPrefix": getObjGeneretorFolder() + "/", "existingOBJCallback" : "checkExisting"+ objGenerator +"s" , "page_title": "Add new "+ objGenerator + ": " + PDUType }
def config_view(request): pluginList = getINIFolderTemplate("plugins") configPath = "config.ini" INIFileDict = loadINIFileConfig(configPath) iniTemplate = loadINIFileTemplate([configPath] + pluginList) INIFileDict = __fillINIwithTemplate(iniTemplate,INIFileDict) multiListChoices = _makeMultichoice("plugins","pluginlist",getAvailablePluginsList,INIFileDict) return {"layout": site_layout(), "config_sidebar_head" : config_sidebar_head(), "config_sidebar_body" : config_sidebar_body(), "INI_InputArea_head" : INI_InputArea_head(), "INI_InputArea_body" : INI_InputArea_body(), "page_title": "General", "INIFileDict" : INIFileDict, "INIFileTemplate" : iniTemplate, "configPath" : configPath, "multiListChoices" : multiListChoices}
def server_info_view(request): ''' Server View page ''' def unix2javascript(time): ''' Convert a unix timestamp to a javascript timestamp :param time: Unix timestamp :return: Javascript timestamp ''' return time*1000.0 def javascript2unix(time): ''' Convert a javascript timestamp to a unix timestamp :param time: Javascript timestamp :return: Unix timestamp ''' return time/1000 def getMinMaxListofLists(l,key): ''' Get the min and max of a list of lists, takes a list and the key value :param l: a list of lists, or list of dicts :param key: the key number of the list :return: tuple of the min and max values ''' try: Min = l[0][key] Max = l[0][key] except KeyError: return 0 for element in l: if Max < element[key]: Max = element[key] if Min > element[key]: Min = element[key] return Min,Max serverName = request.matchdict['serverName'] #fix strange bug that servers with spaces get " symbols on the canviz tree if serverName.startswith('"'): serverName = serverName[1:-1] serverDict = getServerView(serverName) if type(serverDict) == dict: #Set the on/of switch serverDict["Switch"]="" onStates = [str(ServerNodeOpState.OK),str(ServerNodeOpState.SwitchingOff),str(ServerNodeOpState.forcedOn)] if serverDict["OpState"] in onStates: serverDict["Switch"]="on" else: serverDict["Switch"]="off" else: #return an empty dict if we encountered some error serverDict={} serverDict["Switch"]="off" ## Build data for the statistics display ## dataLog=[] serverLog = getServerStatistics(serverName,time.time() - STATISTICS_WINDOW,time.time()+1) #get the last data entry dataDictHead = {} if not serverLog.keys() == []: dataEntry = serverLog[serverLog.keys()[len(serverLog)-1]] dataDictHead=json.loads(dataEntry["dataDict"])#parse the dict from the db string #list of variables we are going to fill, generating the plots #plotsTicks=[] plotTitle=[] plotXLabel="Time [Hr:Min]" plotYFormat=[] plotYLabel=[] plotsData=[] minTick=[] maxTick=[] plotNumber=0 def generatePlot(keySlicePrefix,plotTitle,plotYFormat,plotYLabel,plotsData,minTick,maxTick,plotNumber): for outletKey in slicedict(dataDictHead,keySlicePrefix).keys(): for dataKey in dataDictHead[outletKey].keys(): if dataKey != "name": #Init all the plot labels and lists #TODO time pulling can be done at O(n) not O(n^2) plotTitle.insert(plotNumber, dataKey + " graph for " + dataDictHead[outletKey]["name"]) plotYLabel.insert(plotNumber, dataKey + " " + DATA_NAME_TO_UNITS_NAME[dataKey]) plotYFormat.insert(plotNumber, DATA_NAME_TO_UNITS[dataKey]) plotsData.insert(plotNumber, []) #plotsTicks.insert(plotNumber, []) #Retrieve data for this plot for key in serverLog.keys(): #now we scan all keys #parse the database entry dataDict = json.loads(serverLog[key]["dataDict"]) #save data point dataPointTime=serverLog[key]["time"] try: dataPoint=dataDict[outletKey][dataKey] except KeyError: dataPoint=0 plotsData[plotNumber].append([unix2javascript(float(dataPointTime)),float(dataPoint)]) #Build ticks minTime,maxTime = getMinMaxListofLists(plotsData[plotNumber],0) minTime= javascript2unix(minTime) maxTime = javascript2unix(maxTime) minTick.insert(plotNumber,datetime.fromtimestamp(minTime).strftime("%Y-%m-%d %H:%M")) maxTick.insert(plotNumber,datetime.fromtimestamp(maxTime).strftime("%Y-%m-%d %H:%M")) #for timestamp in range(int(minTime),int(maxTime),PLOT_STEP): # plotsTicks[plotNumber].append(datetime.fromtimestamp(timestamp).strftime("%d %H:%M")) plotNumber=plotNumber+1 generatePlot("outlet",plotTitle,plotYFormat,plotYLabel,plotsData,minTick,maxTick,plotNumber) generatePlot("control",plotTitle,plotYFormat,plotYLabel,plotsData,minTick,maxTick,plotNumber) def buildServerObjDict(objs): #Build outlet switches dict outlets={} outletsServerDict = json.loads(serverDict[objs]) for outlet in outletsServerDict: outlets[outlet] ={} outlets[outlet]["name"] = outletsServerDict[outlet]["name"] outlets[outlet]["OpState"] = outletsServerDict[outlet]["OpState"] onOpStates = [OpState.OK,OpState.SwitchingOff,OpState.forcedOn] if outletsServerDict[outlet]["OpState"] in onOpStates: outlets[outlet]["Switch"] = "on" else: outlets[outlet]["Switch"]="off" return outlets outlets = buildServerObjDict("outlets") controls = buildServerObjDict("controls") tests = json.loads(serverDict["tests"]) return {"layout": site_layout(), "xdottree" : "", "server_dict" : serverDict, "page_title" : "Server View: " + str(serverName), "ServerLog" : str(serverLog), #Plot data "plotTitle" : plotTitle, "plotYLabel" : plotYLabel, "plotYFormat" : plotYFormat, "plotsData" : plotsData, #"plotsTicks" : plotsTicks, "plotXLabel" : plotXLabel, "minTick" : minTick, "maxTick" : maxTick, #outlets data "outletsDict" : json.dumps(outlets), "outlets" : outlets, #controls data "controls": controls, "controlsDict": json.dumps(controls), #tests Data "tests": tests, "testsDict": json.dumps(tests), }
def _server_obj_edit_view(request,obj,objGenerator,objGeneratorConfigCallback,getObjGeneratorsCallback,objGeneratorTemplateCallback): ''' Get the edit dict of a server object (like outlet, tester, etc) :param obj: the name of the object in the server (eg. outlet) :param objName: The name of the object :param objGenerator: the name of the object generator (eg. pdu) :param objGeneratorConfigCallback: a callback the returns the object's config dict :param getObjGeneratorsCallback: A callback that gets the available object generators :param objGeneratorTemplateCallback: A callback that gets the template dict of the object generator :param existingOBJJavascriptCallback: The ockle javascript command to check if :return: dict for the edit view ''' serverName = request.matchdict['serverName'] objName = request.matchdict[obj] objParam = obj + "Params" configPath= _getServerConfigPath(serverName) tmpINIFileDict = _loadServerConfig(serverName) INIFileDict = OrderedDict() INIFileDict[objName] = tmpINIFileDict[objName] objGeneratorName = INIFileDict[objName][objGenerator.lower()] objType = objGeneratorConfigCallback(objGeneratorName)[objGenerator.lower()]["type"] INIFileTemplate = objGeneratorTemplateCallback(objType) #pop the main dict INIFileTemplate.pop(objGenerator.lower()) if not objParam in INIFileTemplate: INIFileTemplate[objParam] = {} #add the type name INIFileTemplate[objParam][objGenerator.lower()] =["select",True] #Move outlet Params to the right name INIFileTemplate[objName] = INIFileTemplate[objParam] INIFileTemplate.pop(objParam) multiListChoices= _makeSelectMulitChoice(objGeneratorName,objName,objGenerator.lower(),getObjGeneratorsCallback) INIFileDict = __fillINIwithTemplate(INIFileTemplate,INIFileDict) return {"layout": site_layout(), "config_sidebar_head" : config_sidebar_head(), "config_sidebar_body" : config_sidebar_body(), "INI_InputArea_head" : INI_InputArea_head(), "INI_InputArea_body" : INI_InputArea_body(), "INIFileDict" : INIFileDict, "INIFileTemplate" : INIFileTemplate, "multiListChoices" : multiListChoices, "OBJnameSection" : objName, "matchdict" : json.dumps(request.matchdict), "changeSavePathToName" : 'false', #Name field variables "configPath": configPath, #"existingOBJCallback" : existingOBJJavascriptCallback, #delete variables "deleteCallback" : obj, "objectName" : objName, "redirectURL" : "/server/" + serverName + "/edit" , #section delete stuff "sectionDelete" : json.dumps({"sectionDelete" : objName}), #we are deleting a section only "page_title": serverName + ": Edit " + obj +" " + objName }
def about_view(request): return {"layout": site_layout(), "page_title": "About"}