backend readability and performance improvements
+ add some typing * optimized imports to only import if necessary * change xmlrpc connection to with statement * change rest connection to with statement * replace format string to f-string * pep8 changes
This commit is contained in:
		
							parent
							
								
									9b6d0c0b97
								
							
						
					
					
						commit
						0145458961
					
				| 
						 | 
					@ -1,21 +1,19 @@
 | 
				
			||||||
#!/usr/bin/env python3
 | 
					#!/usr/bin/env python3
 | 
				
			||||||
# -*- coding: utf-8 -*-
 | 
					# -*- coding: utf-8 -*-
 | 
				
			||||||
from xmlrpc import client
 | 
					 | 
				
			||||||
import requests
 | 
					 | 
				
			||||||
import ipaddress
 | 
					import ipaddress
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# rfc6052: IPv6 Addressing of IPv4/IPv6 Translators
 | 
					# rfc6052: IPv6 Addressing of IPv4/IPv6 Translators
 | 
				
			||||||
nat64 = ipaddress.ip_network("64:ff9b::/96")
 | 
					nat64 = ipaddress.ip_network("64:ff9b::/96")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class EjabberdMetrics():
 | 
					class EjabberdMetrics:
 | 
				
			||||||
    """
 | 
					    """
 | 
				
			||||||
    class to fetch metrics per xmlrpc
 | 
					    class to fetch metrics per xmlrpc
 | 
				
			||||||
    """
 | 
					    """
 | 
				
			||||||
    def __init__(self, url, login=None, api="rpc"):
 | 
					    def __init__(self, url, login=None, api="rpc"):
 | 
				
			||||||
        self._login = login
 | 
					        self._login = login
 | 
				
			||||||
        if api == "rpc":
 | 
					        if api == "rpc":
 | 
				
			||||||
            self._server = client.ServerProxy(url)
 | 
					            self.url = url
 | 
				
			||||||
            self._cmd = self._rpc
 | 
					            self._cmd = self._rpc
 | 
				
			||||||
        else:
 | 
					        else:
 | 
				
			||||||
            self._url = url
 | 
					            self._url = url
 | 
				
			||||||
| 
						 | 
					@ -24,18 +22,24 @@ class EjabberdMetrics():
 | 
				
			||||||
    @property
 | 
					    @property
 | 
				
			||||||
    def _auth(self):
 | 
					    def _auth(self):
 | 
				
			||||||
        if self._login is not None:
 | 
					        if self._login is not None:
 | 
				
			||||||
            return ( "%s@%s" % (self._login['user'], self._login['server']),
 | 
					            return f"{self._login['user']}@{self._login['server']}", self._login['password']
 | 
				
			||||||
                self._login['password'])
 | 
					 | 
				
			||||||
        return None
 | 
					        return None
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def _rest(self, command, data):
 | 
					    def _rest(self, command: str, data):
 | 
				
			||||||
        r = requests.post("%s/%s" % (self._url, command), auth=self._auth, json=data)
 | 
					        import requests
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        with requests.Session() as s:
 | 
				
			||||||
 | 
					            r = s.post(f'{self._url}/{command}', auth=self._auth, json=data)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if r.status_code == 200:
 | 
					            if r.status_code == 200:
 | 
				
			||||||
                return r.json()
 | 
					                return r.json()
 | 
				
			||||||
            return{}
 | 
					            return{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def _rpc(self, command, data):
 | 
					    def _rpc(self, command: str, data):
 | 
				
			||||||
        fn = getattr(self._server, command)
 | 
					        from xmlrpc import client
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        with client.ServerProxy(self.url) as server:
 | 
				
			||||||
 | 
					            fn = getattr(server, command)
 | 
				
			||||||
            try:
 | 
					            try:
 | 
				
			||||||
                if self._login is not None:
 | 
					                if self._login is not None:
 | 
				
			||||||
                    return fn(self._login, data)
 | 
					                    return fn(self._login, data)
 | 
				
			||||||
| 
						 | 
					@ -147,22 +151,28 @@ class EjabberdMetrics():
 | 
				
			||||||
    def update(self):
 | 
					    def update(self):
 | 
				
			||||||
        # nodes
 | 
					        # nodes
 | 
				
			||||||
        self._nodes = self.fetch_nodes()
 | 
					        self._nodes = self.fetch_nodes()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # vhosts
 | 
					        # vhosts
 | 
				
			||||||
        self._vhosts = self.fetch_vhosts()
 | 
					        self._vhosts = self.fetch_vhosts()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # registered
 | 
					        # registered
 | 
				
			||||||
        if not hasattr(self, "_registered"):
 | 
					        if not hasattr(self, "_registered"):
 | 
				
			||||||
            self._registered = {}
 | 
					            self._registered = {}
 | 
				
			||||||
        self._registered[None] = self.fetch_registered()
 | 
					        self._registered[None] = self.fetch_registered()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # muc
 | 
					        # muc
 | 
				
			||||||
        if not hasattr(self, "_muc"):
 | 
					        if not hasattr(self, "_muc"):
 | 
				
			||||||
            self._muc = {}
 | 
					            self._muc = {}
 | 
				
			||||||
        self._muc[None] = self.fetch_muc()
 | 
					        self._muc[None] = self.fetch_muc()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # registered + muc
 | 
					        # registered + muc
 | 
				
			||||||
        for vhost in self._vhosts:
 | 
					        for vhost in self._vhosts:
 | 
				
			||||||
            self._registered[vhost] = self.fetch_registered(vhost)
 | 
					            self._registered[vhost] = self.fetch_registered(vhost)
 | 
				
			||||||
            self._muc[vhost] = self.fetch_muc(vhost)
 | 
					            self._muc[vhost] = self.fetch_muc(vhost)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # online user
 | 
					        # online user
 | 
				
			||||||
        self._onlineuser = self.fetch_onlineuser()
 | 
					        self._onlineuser = self.fetch_onlineuser()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # s2s
 | 
					        # s2s
 | 
				
			||||||
        self._s2s_in = self.fetch_s2s_in()
 | 
					        self._s2s_in = self.fetch_s2s_in()
 | 
				
			||||||
        self._s2s_out = self.fetch_s2s_out()
 | 
					        self._s2s_out = self.fetch_s2s_out()
 | 
				
			||||||
| 
						 | 
					@ -323,6 +333,7 @@ class EjabberdMetrics():
 | 
				
			||||||
if __name__ == "__main__":
 | 
					if __name__ == "__main__":
 | 
				
			||||||
    import os
 | 
					    import os
 | 
				
			||||||
    import json
 | 
					    import json
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # load config
 | 
					    # load config
 | 
				
			||||||
    path = os.path.dirname(__file__)
 | 
					    path = os.path.dirname(__file__)
 | 
				
			||||||
    with open("/".join([path, "config.json"]), "r", encoding="utf-8") as f:
 | 
					    with open("/".join([path, "config.json"]), "r", encoding="utf-8") as f:
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue