Skip to content

Commit

Permalink
[skip ci][feature] Added optional clean insights measurement collection
Browse files Browse the repository at this point in the history
#360

Closes #360
  • Loading branch information
pandafy committed Nov 29, 2023
1 parent 5db88c1 commit 5724918
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 0 deletions.
Empty file.
7 changes: 7 additions & 0 deletions openwisp_utils/measurements/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.apps import AppConfig


class MeasurementsConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'openwisp_utils.measurements'
app_label = 'openwisp_measurements'
50 changes: 50 additions & 0 deletions openwisp_utils/measurements/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Generated by Django 4.2.7 on 2023-11-29 09:16

from django.db import migrations, models
import django.utils.timezone
import model_utils.fields
import uuid


class Migration(migrations.Migration):

initial = True

dependencies = []

operations = [
migrations.CreateModel(
name="OpenwispVersion",
fields=[
(
"id",
models.UUIDField(
default=uuid.uuid4,
editable=False,
primary_key=True,
serialize=False,
),
),
(
"created",
model_utils.fields.AutoCreatedField(
default=django.utils.timezone.now,
editable=False,
verbose_name="created",
),
),
(
"modified",
model_utils.fields.AutoLastModifiedField(
default=django.utils.timezone.now,
editable=False,
verbose_name="modified",
),
),
("module_version", models.JSONField(blank=True, default=dict)),
],
options={
"abstract": False,
},
),
]
Empty file.
24 changes: 24 additions & 0 deletions openwisp_utils/measurements/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from django.db import models
from openwisp_utils.base import TimeStampedEditableModel
from packaging.version import parse as parse_version


class OpenwispVersion(TimeStampedEditableModel):
module_version = models.JSONField(default=dict, blank=True)

@classmethod
def has_openwisp_upgraded(cls, current_versions):
openwisp_version = cls.objects.first()
if not openwisp_version:
cls.objects.create(module_version=current_versions)
return False
old_versions = openwisp_version.module_version
for module, version in current_versions.items():
if (
module not in old_versions
or parse_version(old_versions[module]) < version
):
openwisp_version.module_version = current_versions
openwisp_version.save()
return True
return False
25 changes: 25 additions & 0 deletions openwisp_utils/measurements/tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from celery import shared_task
from django.utils.html import escape
from openwisp_utils.admin_theme.system_info import (
get_enabled_openwisp_modules,
get_os_details,
)

from .models import OpenwispVersion

CLEAN_INSIGHTS_URL = 'https://analytics.openwisp.io/ci/cleaninsights.php'


@shared_task
def send_clean_insights_measurements():
current_versions = get_enabled_openwisp_modules()
data = current_versions.copy()
data.update(get_os_details())
for key in data.keys():
data[key] = escape(data[key])
if OpenwispVersion.has_openwisp_upgraded(current_versions):
# TODO: Write logic for sending upgrade event
pass
else:
# TODO: Write logic for sending event with module info
pass
Empty file.
Empty file.
1 change: 1 addition & 0 deletions tests/openwisp2/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
# test project
'test_project',
'openwisp_utils.admin_theme',
'openwisp_utils.measurements',
'django.contrib.sites',
# admin
'django.contrib.admin',
Expand Down

0 comments on commit 5724918

Please sign in to comment.