Convert check_mhz into sourcestats, using --exec-send, doing multi-source statistics
This commit is contained in:
69
sinks/sourcestats/.gitignore
vendored
Normal file
69
sinks/sourcestats/.gitignore
vendored
Normal file
@@ -0,0 +1,69 @@
|
||||
# Byte-compiled / optimized / DLL files
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
*$py.class
|
||||
|
||||
# C extensions
|
||||
*.so
|
||||
|
||||
# Distribution / packaging
|
||||
.Python
|
||||
env/
|
||||
build/
|
||||
develop-eggs/
|
||||
dist/
|
||||
downloads/
|
||||
eggs/
|
||||
.eggs/
|
||||
lib/
|
||||
lib64/
|
||||
parts/
|
||||
sdist/
|
||||
var/
|
||||
*.egg-info/
|
||||
.installed.cfg
|
||||
*.egg
|
||||
|
||||
# PyInstaller
|
||||
# Usually these files are written by a python script from a template
|
||||
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
||||
*.manifest
|
||||
*.spec
|
||||
|
||||
# Installer logs
|
||||
pip-log.txt
|
||||
pip-delete-this-directory.txt
|
||||
|
||||
# Unit test / coverage reports
|
||||
htmlcov/
|
||||
.tox/
|
||||
.coverage
|
||||
.coverage.*
|
||||
.cache
|
||||
nosetests.xml
|
||||
coverage.xml
|
||||
*,cover
|
||||
.hypothesis/
|
||||
|
||||
# Translations
|
||||
*.mo
|
||||
*.pot
|
||||
|
||||
# Django stuff:
|
||||
*.log
|
||||
local_settings.py
|
||||
|
||||
# Flask instance folder
|
||||
instance/
|
||||
|
||||
# Sphinx documentation
|
||||
docs/_build/
|
||||
|
||||
# PyBuilder
|
||||
target/
|
||||
|
||||
# IPython Notebook
|
||||
.ipynb_checkpoints
|
||||
|
||||
# pyenv
|
||||
.python-version
|
||||
91
sinks/sourcestats/sourcestats.py
Executable file
91
sinks/sourcestats/sourcestats.py
Executable file
@@ -0,0 +1,91 @@
|
||||
#!/usr/bin/python3 -u
|
||||
|
||||
import collections
|
||||
import fileinput
|
||||
import json
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
|
||||
|
||||
def Log(s):
|
||||
print('{sourcestats} %s' % s, file=sys.stderr, flush=True)
|
||||
|
||||
|
||||
class Source(object):
|
||||
_first_event = None
|
||||
|
||||
def __init__(self):
|
||||
self._count_by_type = collections.defaultdict(int)
|
||||
|
||||
def HandleEvent(self, event):
|
||||
now = time.clock_gettime(time.CLOCK_MONOTONIC)
|
||||
if not self._first_event:
|
||||
self._first_event = event
|
||||
self._first_event_timestamp = now
|
||||
self._last_event = event
|
||||
self._last_event_timestamp = now
|
||||
self._count_by_type[event['type']] += 1
|
||||
|
||||
def _GetDuration(self):
|
||||
return self._last_event_timestamp - self._first_event_timestamp
|
||||
|
||||
def GetTypeRate(self, t):
|
||||
return self._count_by_type[t] / self._GetDuration()
|
||||
|
||||
def GetHz(self):
|
||||
ticks = self._last_event['mlat_timestamp'] - self._first_event['mlat_timestamp']
|
||||
return ticks / self._GetDuration()
|
||||
|
||||
|
||||
class Reader(object):
|
||||
def __init__(self):
|
||||
self._sources = collections.defaultdict(Source)
|
||||
self._input = fileinput.input()
|
||||
self._LogStats()
|
||||
|
||||
def _GetEvents(self):
|
||||
for line in self._input:
|
||||
yield json.loads(line)
|
||||
|
||||
def _HandleHeader(self, event):
|
||||
self._config = event
|
||||
|
||||
def _LogStats(self):
|
||||
data = [
|
||||
('Source ID', 'Mode-AC', 'Mode-S short', 'Mode-S long', 'Clock rate'),
|
||||
]
|
||||
|
||||
for source_id, source in self._sources.items():
|
||||
data.append((
|
||||
source_id,
|
||||
'%.02f/s' % source.GetTypeRate('Mode-AC'),
|
||||
'%.02f/s' % source.GetTypeRate('Mode-S short'),
|
||||
'%.02f/s' % source.GetTypeRate('Mode-S long'),
|
||||
'%.04f' % (source.GetHz() / (self._config['mlat_timestamp_mhz'] * 1000000)),
|
||||
))
|
||||
|
||||
col_width = [max(len(row[i]) for row in data) + 2 for i in range(len(data[0]))]
|
||||
for row in data:
|
||||
Log(''.join(word.ljust(col_width[i]) for i, word in enumerate(row)))
|
||||
Log('')
|
||||
|
||||
self._timer = threading.Timer(60.0, self._LogStats)
|
||||
self._timer.start()
|
||||
|
||||
def Read(self):
|
||||
for event in self._GetEvents():
|
||||
if event['type'] == 'header':
|
||||
self._HandleHeader(event)
|
||||
continue
|
||||
source = self._sources[event['source_id']]
|
||||
source.HandleEvent(event)
|
||||
|
||||
|
||||
Log('Runtime data:')
|
||||
Log('\tpython_version: %s' % sys.version.replace('\n', ''))
|
||||
Log('')
|
||||
|
||||
|
||||
reader = Reader()
|
||||
reader.Read()
|
||||
Reference in New Issue
Block a user