mirror of
https://github.com/django-components/django-components.git
synced 2025-11-23 23:56:41 +00:00
Deployed 06d08a7c to 0.143.0 with MkDocs 1.6.1 and mike 2.1.3
This commit is contained in:
parent
720aceb939
commit
14ae96e591
460 changed files with 47230 additions and 3 deletions
64
0.143.0/examples/analytics/component.py
Normal file
64
0.143.0/examples/analytics/component.py
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
from typing import Dict, List
|
||||
|
||||
from django_components import Component, register, types
|
||||
|
||||
DESCRIPTION = "Track component errors or success rates to send them to Sentry or other services."
|
||||
|
||||
# A mock analytics service
|
||||
analytics_events: List[Dict] = []
|
||||
error_rate = {
|
||||
"error": 0,
|
||||
"success": 0,
|
||||
}
|
||||
|
||||
|
||||
@register("api_widget")
|
||||
class ApiWidget(Component):
|
||||
class Kwargs:
|
||||
simulate_error: bool = False
|
||||
|
||||
def get_template_data(self, args, kwargs: Kwargs, slots, context):
|
||||
if kwargs.simulate_error:
|
||||
raise ConnectionError("API call failed")
|
||||
return {"data": "Mock API response data"}
|
||||
|
||||
template: types.django_html = """
|
||||
<div class="p-4 border rounded-lg bg-gray-50">
|
||||
<h4 class="font-bold text-gray-800">API Widget</h4>
|
||||
<p class="text-gray-600">Data: {{ data }}</p>
|
||||
</div>
|
||||
"""
|
||||
|
||||
|
||||
@register("sentry_error_tracker")
|
||||
class SentryErrorTracker(Component):
|
||||
def on_render_after(self, context, template, result, error):
|
||||
if error:
|
||||
event = {
|
||||
"type": "error",
|
||||
"component": self.registered_name,
|
||||
"error": error,
|
||||
}
|
||||
analytics_events.append(event)
|
||||
print(f"SENTRY: Captured error in component {self.registered_name}: {error}")
|
||||
|
||||
template: types.django_html = """
|
||||
{% load component_tags %}
|
||||
{% slot "default" / %}
|
||||
"""
|
||||
|
||||
|
||||
@register("success_rate_tracker")
|
||||
class SuccessRateTracker(Component):
|
||||
def on_render_after(self, context, template, result, error):
|
||||
# Track error
|
||||
if error:
|
||||
error_rate["error"] += 1
|
||||
# Track success
|
||||
else:
|
||||
error_rate["success"] += 1
|
||||
|
||||
template: types.django_html = """
|
||||
{% load component_tags %}
|
||||
{% slot "default" / %}
|
||||
"""
|
||||
Loading…
Add table
Add a link
Reference in a new issue