Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cron job for anonymous analytics #3547

Merged
merged 1 commit into from Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion Dockerfile
Expand Up @@ -25,8 +25,9 @@ COPY ./dist/venv/ /venv

COPY ./manage.py /Kiwi/
# create directories so we can properly set ownership for them
RUN mkdir /Kiwi/ssl /Kiwi/static /Kiwi/uploads /Kiwi/etc
RUN mkdir -p /Kiwi/ssl /Kiwi/static /Kiwi/uploads /Kiwi/etc/cron.jobs
COPY ./etc/*.conf /Kiwi/etc/
COPY ./etc/cron.jobs/* /Kiwi/etc/cron.jobs/

# generate self-signed SSL certificate
RUN /usr/bin/sscg -v -f \
Expand Down
7 changes: 7 additions & 0 deletions docs/source/modules/tcms.cron.anonymous_analytics.rst
@@ -0,0 +1,7 @@
tcms.cron.anonymous\_analytics module
=====================================

.. automodule:: tcms.cron.anonymous_analytics
:members:
:undoc-members:
:show-inheritance:
15 changes: 15 additions & 0 deletions docs/source/modules/tcms.cron.rst
@@ -0,0 +1,15 @@
tcms.cron package
=================

.. automodule:: tcms.cron
:members:
:undoc-members:
:show-inheritance:

Submodules
----------

.. toctree::
:maxdepth: 4

tcms.cron.anonymous_analytics
1 change: 1 addition & 0 deletions docs/source/modules/tcms.rst
Expand Up @@ -14,6 +14,7 @@ Subpackages

tcms.bugs
tcms.core
tcms.cron
tcms.issuetracker
tcms.kiwi_attachments
tcms.kiwi_auth
Expand Down
8 changes: 8 additions & 0 deletions etc/cron.jobs/anonymous_analytics.sh
@@ -0,0 +1,8 @@
#!/bin/bash

# make sure Python is enabled
export VIRTUAL_ENV=/venv
export PATH=/venv/bin:${PATH}

# execute the actual Python script
cat /venv/lib64/python3.11/site-packages/tcms/cron/anonymous_analytics.py | /Kiwi/manage.py shell
2 changes: 2 additions & 0 deletions etc/uwsgi.conf
Expand Up @@ -10,6 +10,8 @@ harakiri = 30
max-requests = 1000
vacuum = true
home = /venv
; anonymous analytics every 5 days
cron2 = day=-5,harakiri=10 /Kiwi/etc/cron.jobs/anonymous_analytics.sh

; override the standard configuration
if-file = /Kiwi/etc/uwsgi.override
Expand Down
Empty file added tcms/cron/__init__.py
Empty file.
49 changes: 49 additions & 0 deletions tcms/cron/anonymous_analytics.py
@@ -0,0 +1,49 @@
# Copyright (c) 2024 Alexander Todorov <atodorov@otb.bg>
#
# Licensed under the GPL 2.0: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html

"""
Send a custom pageview event to Plausible.io so that we can count
how many/which versions of Kiwi TCMS are out there in the wild!

WARNING: this file is meant to be executed as a wsgi cron job,
see etc/wsgi.conf for more details.
"""
import sys

import requests
from django.conf import settings


def post_analytics():
response = requests.post(
"https://plausible.io/api/event",
json={
"name": "pageview",
"url": f"app://{settings.PLAUSIBLE_DOMAIN}/deployed-versions",
"domain": settings.PLAUSIBLE_DOMAIN,
"props": {
"version": settings.KIWI_VERSION,
},
},
headers={
"User-Agent": f"kiwi-tcms/{settings.KIWI_VERSION}",
"Content-Type": "application/json",
},
timeout=10,
)

print(response.status_code)
print(response.headers)
print(response.text)


if not settings.ANONYMOUS_ANALYTICS:
print("Anonymous analytics are explicitly disabled. Exiting")
elif "manage.py" in sys.argv and "shell" in sys.argv:
# execute only when piped to `./manage.py shell`
post_analytics()
else:
print(
"WARNING: pipe the content of this file to ./manage.py shell in order to execute it"
)