FreeDATA/tnc/stats.py

64 lines
2.1 KiB
Python
Raw Normal View History

2023-02-02 17:57:01 +00: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
from global_instances import ARQ, AudioParam, Beacon, Channel, Daemon, HamlibParam, ModemParam, Station, TCIParam, TNC
2023-02-02 17:57:01 +00:00
log = structlog.get_logger("stats")
class stats():
def __init__(self):
self.explorer_url = "https://api.freedata.app/stats.php"
2023-02-03 12:30:42 +00:00
2023-02-03 15:48:37 +00:00
def push(self, frame_nack_counter, status, duration):
2023-02-17 20:17:38 +00:00
crcerror = status in ["crc_error", "wrong_crc"]
2023-02-03 15:48:37 +00:00
# get avg snr
try:
2023-04-27 19:43:56 +00:00
snr_raw = [item["snr"] for item in ARQ.speed_list]
2023-02-03 15:48:37 +00:00
avg_snr = round(sum(snr_raw) / len(snr_raw), 2 )
except Exception:
avg_snr = 0
2023-02-02 17:57:01 +00:00
headers = {"Content-Type": "application/json"}
station_data = {
2023-04-27 19:43:56 +00:00
'callsign': str(Station.mycallsign, "utf-8"),
'dxcallsign': str(Station.dxcallsign, "utf-8"),
'gridsquare': str(Station.mygrid, "utf-8"),
'dxgridsquare': str(Station.dxgrid, "utf-8"),
'frequency': 0 if HamlibParam.hamlib_frequency is None else HamlibParam.hamlib_frequency,
2023-02-02 17:57:01 +00:00
'avgstrength': 0,
2023-02-03 15:48:37 +00:00
'avgsnr': avg_snr,
2023-04-27 19:43:56 +00:00
'bytesperminute': ARQ.bytes_per_minute,
'filesize': ARQ.total_bytes,
'compressionfactor': ARQ.arq_compression_factor,
2023-02-03 15:48:37 +00:00
'nacks': frame_nack_counter,
'crcerror': crcerror,
'duration': duration,
2023-04-27 19:43:56 +00:00
'percentage': ARQ.arq_transmission_percent,
2023-02-06 16:42:58 +00:00
'status': status,
2023-04-27 19:43:56 +00:00
'version': TNC.version
2023-02-02 17:57:01 +00:00
}
station_data = json.dumps(station_data)
2023-02-03 12:30:42 +00:00
print(station_data)
2023-02-02 17:57:01 +00: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")