[feature] Avoid sending duplicate usage metrics
⚠️ Not suitable to beginners
Is your feature request related to a problem? Please describe. Currently, the metric usage data may be sent multiple times per day if the Django project is configured incorrectly. i
Describe the solution you'd like I suggest that metric usage should be sent only once per day. To achieve this, we can calculate and store a hash of the payload along with the current date. On any given date, if the same payload is generated again, the hash should be checked before sending. If an identical hash (for the same date) already exists, skip sending to avoid duplication. This ensures each unique payload is sent at most once per day.
can i work on it I'll use this approach. 1.Identify the ProblemCurrently, usage metrics can be sent multiple times per day if the system is misconfigured.
This can cause duplicate metric submissions and inaccurate usage reporting.
- Proposed Solution
Ensure that each unique metrics payload is sent at most once per day.
Achieve this by computing a hash of the payload and storing it along with the date.
- Implementation Steps
Step 3.1 – Create a Model to Track Sent Metrics
Add a Django model MetricSent with fields:
category – type of metric
metrics_hash – SHA-256 hash of the payload
date – date the metric was sent
created – timestamp of record creation
Add a unique constraint on (category, metrics_hash, date) to prevent duplicates.
Step 3.2 – Compute Hash Before Sending
In OpenwispVersion.send_usage_metrics, compute a deterministic hash of the metrics payload:
metrics_hash = sha256(json.dumps(metrics, sort_keys=True).encode()).hexdigest()
Step 3.3 – Check for Existing Metric
Before sending, check if a record with the same category, metrics_hash, and date already exists.
If it exists → skip sending.
If not → send metrics and create a MetricSent record.
Step 3.4 – Testing
Write unit tests to ensure:
Same payload is not sent twice in the same day.
Same payload can be sent on different days.
Category is considered in uniqueness checks.
Step 3.5 – Acceptance Criteria
No duplicate metrics sent for the same payload and category on the same date.
Unit tests validate the behavior.
Database migration added for MetricSent.
Documentation updated to reflect the new behavior.
Step 3.6 – Code Snippet Example
metrics_hash = sha256(json.dumps(metrics, sort_keys=True).encode("utf-8")).hexdigest()
if MetricSent.objects.filter(category=category, metrics_hash=metrics_hash, date=date.today()).exists(): logger.info("Skipping duplicate metrics for today") else: post_metrics(metrics) MetricSent.objects.create(category=category, metrics_hash=metrics_hash, date=date.today())
✅ Summary
“We are preventing duplicate metric submissions by storing a hash of each payload per day. Before sending, we check if it has already been sent today. This ensures accurate reporting while allowing the same payload on different days.”