FreeDATA/tnc/stats.py

64 lines
2 KiB
Python
Raw Normal View History

2023-02-02 18:57:01 +01:00
# -*- coding: UTF-8 -*-
"""
Created on 05.11.23
@author: DJ2LS
"""
# pylint: disable=invalid-name, line-too-long, c-extension-no-member
# pylint: disable=import-outside-toplevel, attribute-defined-outside-init
import requests
import threading
import time
import ujson as json
import structlog
import static
log = structlog.get_logger("stats")
class stats():
def __init__(self):
self.explorer_url = "https://api.freedata.app/stats.php"
2023-02-03 13:30:42 +01:00
2023-02-03 16:48:37 +01:00
def push(self, frame_nack_counter, status, duration):
2023-02-09 13:26:25 +01:00
crcerror = status in ["crc_error"]
2023-02-03 16:48:37 +01:00
# get avg snr
try:
2023-02-09 13:26:25 +01:00
snr_raw = [item["snr"] for item in static.SPEED_LIST]
2023-02-03 16:48:37 +01:00
avg_snr = round(sum(snr_raw) / len(snr_raw), 2 )
except Exception:
avg_snr = 0
2023-02-02 18:57:01 +01:00
headers = {"Content-Type": "application/json"}
station_data = {
2023-02-03 16:48:37 +01:00
'callsign': str(static.MYCALLSIGN, "utf-8"),
'dxcallsign': str(static.DXCALLSIGN, "utf-8"),
2023-02-02 18:57:01 +01:00
'gridsquare': str(static.MYGRID, "utf-8"),
'dxgridsquare': str(static.DXGRID, "utf-8"),
'frequency': 0 if static.HAMLIB_FREQUENCY is None else static.HAMLIB_FREQUENCY,
'avgstrength': 0,
2023-02-03 16:48:37 +01:00
'avgsnr': avg_snr,
2023-02-03 13:30:42 +01:00
'bytesperminute': static.ARQ_BYTES_PER_MINUTE,
'filesize': static.TOTAL_BYTES,
'compressionfactor': static.ARQ_COMPRESSION_FACTOR,
2023-02-03 16:48:37 +01:00
'nacks': frame_nack_counter,
'crcerror': crcerror,
'duration': duration,
'percentage': static.ARQ_TRANSMISSION_PERCENT,
2023-02-06 17:42:58 +01:00
'status': status,
2023-02-03 13:30:42 +01:00
'version': static.VERSION
2023-02-02 18:57:01 +01:00
}
station_data = json.dumps(station_data)
2023-02-03 13:30:42 +01:00
print(station_data)
2023-02-02 18:57:01 +01:00
try:
response = requests.post(self.explorer_url, json=station_data, headers=headers)
log.info("[STATS] push", code=response.status_code)
# print(response.status_code)
# print(response.content)
except Exception as e:
log.warning("[EXPLORER] connection lost")