give stats module more specific name

This commit is contained in:
Kevin Froman 2020-01-27 02:20:09 -06:00
parent b1a7641db9
commit 11d97a12d0
8 changed files with 6 additions and 6 deletions

View file

@ -0,0 +1 @@
from .transports import tor

View file

@ -0,0 +1,52 @@
"""
Onionr - Private P2P Communication
This module serializes various data pieces for use in other modules, in particular the web api
"""
"""
This program 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 3 of the License, or
(at your option) any later version.
This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
"""
import json # noqa
import time # noqa
from coredb import blockmetadb # noqa
import communicator # noqa
class SerializedData:
def __init__(self):
"""
Serialized data is in JSON format:
{
'success': bool,
'foo': 'bar',
etc
}
"""
def get_stats(self):
"""Return statistics about our node"""
stats = {}
try:
self._too_many
except AttributeError:
time.sleep(1)
comm_inst = self._too_many.get(communicator.OnionrCommunicatorDaemon, args=(self._too_many,))
connected = []
[connected.append(x) for x in comm_inst.onlinePeers if x not in connected]
stats['uptime'] = comm_inst.getUptime()
stats['connectedNodes'] = '\n'.join(connected)
stats['blockCount'] = len(blockmetadb.get_block_list())
stats['blockQueueCount'] = len(comm_inst.blockQueue)
return json.dumps(stats)

View file

@ -0,0 +1 @@
from . import tor

View file

@ -0,0 +1,60 @@
"""Onionr - Private P2P Communication.
"""
import json
from gevent import sleep
from stem import CircStatus
from netcontroller.torcontrol.torcontroller import get_controller
"""
This program 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 3 of the License, or
(at your option) any later version.
This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
"""
class TorStats:
def __init__(self):
self.circuits = {}
self.json_data = ""
def get_json(self):
"""Refresh circuits then serialize them into form:
"nodes": list of tuples containing fingerprint and nickname strings"
"purpose": https://stem.torproject.org/api/control.html#stem.CircPurpose
"""
self.get_circuits()
json_serialized = {}
for circuit in self.circuits.keys():
json_serialized[circuit] = {
"nodes": [],
"purpose": self.circuits[circuit][1]
}
for entry in self.circuits[circuit][0]:
json_serialized[circuit]["nodes"].append({'finger': entry[0],
'nick': entry[1]})
self.json_data = json.dumps(json_serialized)
return self.json_data
def get_circuits(self):
"""Update the circuit dictionary"""
circuits = {}
for circ in list(sorted(get_controller().get_circuits())):
if circ.status != CircStatus.BUILT:
continue
circuits[circ.id] = (circ.path, circ.purpose)
self.circuits = circuits