Skip to content

Commit

Permalink
Stats - Make a different server for LizCloud, add debug
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustry committed May 24, 2024
1 parent 5098057 commit 0347665
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions lizmap_server/plausible.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@

from qgis.core import Qgis, QgsNetworkAccessManager
from qgis.PyQt.QtCore import QByteArray, QDateTime, QUrl
from qgis.PyQt.QtNetwork import QNetworkRequest
from qgis.PyQt.QtNetwork import QNetworkReply, QNetworkRequest

from lizmap_server.logger import Logger
from lizmap_server.tools import to_bool, version

MIN_SECONDS = 3600
ENV_SKIP_STATS = "3LIZ_SKIP_STATS"

PLAUSIBLE_DOMAIN_LIZCLOUD = "plugin.server.lizcloud"
PLAUSIBLE_DOMAIN_PROD = "plugin.server.lizmap.com"
PLAUSIBLE_URL_PROD = "https://bourbon.3liz.com/api/event"

Expand Down Expand Up @@ -71,7 +73,12 @@ def _send_stat_event() -> bool:
debug = True

plausible_url = PLAUSIBLE_URL_TEST if debug else PLAUSIBLE_URL_PROD
plausible_domain = PLAUSIBLE_DOMAIN_TEST if debug else PLAUSIBLE_DOMAIN_PROD

is_lizcloud = "lizcloud" in os.getenv("QGIS_SERVER_APPLICATION_NAME", "").lower()
if is_lizcloud:
plausible_domain = PLAUSIBLE_DOMAIN_LIZCLOUD
else:
plausible_domain = PLAUSIBLE_DOMAIN_TEST if debug else PLAUSIBLE_DOMAIN_PROD

request = QNetworkRequest()
# noinspection PyArgumentList
Expand Down Expand Up @@ -110,6 +117,17 @@ def _send_stat_event() -> bool:
"url": plausible_url,
"domain": plausible_domain,
}

# noinspection PyArgumentList
QgsNetworkAccessManager.instance().post(request, QByteArray(str.encode(json.dumps(data))))
r: QNetworkReply = QgsNetworkAccessManager.instance().post(request, QByteArray(str.encode(json.dumps(data))))
if not is_lizcloud:
return True

logger = Logger()
message = f"Request sent to '{plausible_url}' with domain '{plausible_domain} : "
if r.error() != QNetworkReply.NoError:
logger.warning(message + r.error())
else:
logger.info(message + "OK")

return True

0 comments on commit 0347665

Please sign in to comment.