Skip to content

Commit

Permalink
Add apps and models (#33)
Browse files Browse the repository at this point in the history
* Add alert app and models

* Add dashboard app

* Add analysis

* Add layers models

* Add base app
  • Loading branch information
dimasciput authored Nov 7, 2024
1 parent 3299f49 commit b66391f
Show file tree
Hide file tree
Showing 44 changed files with 1,306 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ max-line-length = 79
# E12x continuation line indentation
# E251 no spaces around keyword / parameter equals
# E303 too many blank lines (3)
ignore = E125,E126,E251,E303,W504,W60,F405
ignore = E125,E126,E251,E303,W504,W60,F405,D100,D101,D401,D105,D106,D200
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ dev:
@echo "------------------------------------------------------------------"
@echo "Running in dev mode"
@echo "------------------------------------------------------------------"
@docker compose ${ARGS} up --no-recreate -d dev redis worker
@docker compose ${ARGS} up -d dev redis worker

test:
@echo
Expand Down
Empty file.
45 changes: 45 additions & 0 deletions django_project/alerts/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from django.contrib import admin
from .models import AlertSetting, IndicatorAlertHistory, Indicator


@admin.register(AlertSetting)
class AlertSettingAdmin(admin.ModelAdmin):
list_display = ('name', 'enable_alert',
'email_alert', 'in_app_alert', 'last_alert')
list_filter = ('enable_alert', 'email_alert',
'in_app_alert', 'anomaly_detection_alert')
search_fields = ('name',)
readonly_fields = ('created_at', 'updated_at')
fieldsets = (
(None, {
'fields': ('name', 'enable_alert', 'last_alert')
}),
('Threshold Settings', {
'fields': ('threshold_comparison',
'threshold_value', 'anomaly_detection_alert')
}),
('Notification Settings', {
'fields': ('email_alert', 'in_app_alert')
}),
('Timestamps', {
'fields': ('created_at', 'updated_at')
}),
('User', {
'fields': ('user',)
}),
)


@admin.register(IndicatorAlertHistory)
class IndicatorAlertHistoryAdmin(admin.ModelAdmin):
list_display = ('alert_setting', 'created_at', 'text')
list_filter = ('created_at',)
search_fields = ('alert_setting__name', 'text')
readonly_fields = ('created_at',)
date_hierarchy = 'created_at'


@admin.register(Indicator)
class IndicatorAdmin(admin.ModelAdmin):
list_display = ('name',)
search_fields = ('name',)
6 changes: 6 additions & 0 deletions django_project/alerts/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class AlertsConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'alerts'
74 changes: 74 additions & 0 deletions django_project/alerts/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Generated by Django 4.2.7 on 2024-11-05 16:38

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

initial = True

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name='AlertSetting',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(help_text='The name of the alert setting.', max_length=100)),
('enable_alert', models.BooleanField(default=False, help_text='Indicates if the alert is enabled.')),
('last_alert', models.DateTimeField(blank=True, help_text='Timestamp of the last triggered alert.', null=True)),
('threshold_comparison', models.FloatField(default=0, help_text='Value for comparison to trigger an alert if exceeded.')),
('threshold_value', models.FloatField(default=0, help_text='Threshold value to trigger the alert.')),
('anomaly_detection_alert', models.BooleanField(default=False, help_text='Enable to trigger alerts for detected anomalies.')),
('email_alert', models.BooleanField(default=False, help_text='Send alert notifications via email.')),
('in_app_alert', models.BooleanField(default=False, help_text='Enable in-app alert notifications.')),
('created_at', models.DateTimeField(auto_now_add=True, help_text='The date and time when this alert setting was created.')),
('updated_at', models.DateTimeField(auto_now=True, help_text='The date and time when this alert setting was last updated.')),
],
options={
'verbose_name': 'Alert Setting',
'verbose_name_plural': 'Alert Settings',
'ordering': ['name'],
},
),
migrations.CreateModel(
name='Indicator',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(help_text='The name of the indicator.', max_length=200, unique=True)),
],
options={
'verbose_name': 'Indicator',
'verbose_name_plural': 'Indicators',
'ordering': ['name'],
},
),
migrations.CreateModel(
name='IndicatorAlertHistory',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('text', models.TextField(blank=True, help_text='The text of the indicator alert history.')),
('created_at', models.DateTimeField(auto_now_add=True, help_text='The date and time when this alert history entry was created.')),
('alert_setting', models.ForeignKey(help_text='The alert setting associated with this alert history.', on_delete=django.db.models.deletion.CASCADE, related_name='alert_histories', to='alerts.alertsetting')),
],
options={
'verbose_name': 'Indicator Alert History',
'verbose_name_plural': 'Indicator Alert Histories',
'ordering': ['-created_at'],
},
),
migrations.AddField(
model_name='alertsetting',
name='indicator',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='alerts.indicator'),
),
migrations.AddField(
model_name='alertsetting',
name='user',
field=models.ForeignKey(help_text='The user associated with this alert setting.', on_delete=django.db.models.deletion.CASCADE, related_name='alert_settings', to=settings.AUTH_USER_MODEL),
),
]
Empty file.
132 changes: 132 additions & 0 deletions django_project/alerts/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
from django.db import models
from django.contrib.auth.models import User


class Indicator(models.Model):
"""Model to represent an indicator used in analyses and alerts."""

name = models.CharField(
max_length=200,
unique=True,
help_text='The name of the indicator.'
)

class Meta:
"""Meta class for the Indicator model."""

verbose_name = "Indicator"
verbose_name_plural = "Indicators"
ordering = ['name']

def __str__(self):
"""String representation of the Indicator model."""
return self.name


class AlertSetting(models.Model):
"""Model to define settings for alerts."""

name = models.CharField(
max_length=100,
help_text="The name of the alert setting."
)

indicator = models.ForeignKey(
'Indicator',
on_delete=models.CASCADE,
)

enable_alert = models.BooleanField(
default=False,
help_text="Indicates if the alert is enabled."
)

last_alert = models.DateTimeField(
null=True,
blank=True,
help_text="Timestamp of the last triggered alert."
)

threshold_comparison = models.FloatField(
default=0,
help_text="Value for comparison to trigger an alert if exceeded."
)

threshold_value = models.FloatField(
default=0,
help_text="Threshold value to trigger the alert."
)

anomaly_detection_alert = models.BooleanField(
default=False,
help_text="Enable to trigger alerts for detected anomalies."
)

email_alert = models.BooleanField(
default=False,
help_text="Send alert notifications via email."
)

in_app_alert = models.BooleanField(
default=False,
help_text="Enable in-app alert notifications."
)

created_at = models.DateTimeField(
auto_now_add=True,
help_text="The date and time when this alert setting was created."
)

updated_at = models.DateTimeField(
auto_now=True,
help_text="The date and time when this alert setting was last updated."
)

user = models.ForeignKey(
User,
on_delete=models.CASCADE,
related_name="alert_settings",
help_text="The user associated with this alert setting."
)

class Meta:
"""Meta class for the AlertSetting model."""

verbose_name = "Alert Setting"
verbose_name_plural = "Alert Settings"
ordering = ['name']

def __str__(self):
"""String representation of the AlertSetting model."""
return self.name


class IndicatorAlertHistory(models.Model):
"""Model to store the history of alerts for a specific indicator."""

text = models.TextField(
blank=True,
help_text='The text of the indicator alert history.'
)

alert_setting = models.ForeignKey(
'AlertSetting',
on_delete=models.CASCADE,
related_name='alert_histories',
help_text='The alert setting associated with this alert history.'
)

created_at = models.DateTimeField(
auto_now_add=True,
help_text=(
'The date and time when this alert '
'history entry was created.'
)
)

class Meta:
"""Meta class for the IndicatorAlertHistory model."""

verbose_name = "Indicator Alert History"
verbose_name_plural = "Indicator Alert Histories"
ordering = ['-created_at']
3 changes: 3 additions & 0 deletions django_project/alerts/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase # noqa: F401

# Create your tests here.
1 change: 1 addition & 0 deletions django_project/alerts/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Create your views here.
Empty file.
66 changes: 66 additions & 0 deletions django_project/analysis/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from django.contrib import admin
from django.contrib.gis.admin import OSMGeoAdmin
from .models import Analysis, InterventionArea


@admin.register(Analysis)
class AnalysisAdmin(OSMGeoAdmin):
list_display = (
'uuid', 'analysis_type', 'indicator', 'intervention_area',
'temporal_resolution', 'created_at', 'updated_at')
list_filter = (
'analysis_type', 'temporal_resolution', 'created_at', 'updated_at')
search_fields = ('uuid', 'indicator__name', 'intervention_area__name')
readonly_fields = ('created_at', 'updated_at')

fieldsets = (
(None, {
'fields': ('analysis_type', 'indicator', 'intervention_area')
}),
('Resolution & Period', {
'fields': (
'temporal_resolution', 'reference_period_start',
'reference_period_end')
}),
('Spatial Data', {
'fields': ('geom',),
'classes': ('collapse',),
}),
('User Information', {
'fields': ('created_by',),
}),
('Timestamps', {
'fields': ('created_at', 'updated_at')
}),
)

map_template = 'gis/admin/osm.html'
default_lon = 0
default_lat = 0
default_zoom = 2


@admin.register(InterventionArea)
class InterventionAreaAdmin(OSMGeoAdmin):
list_display = ('name', 'created_at', 'updated_at')
search_fields = ('name',)
readonly_fields = ('created_at', 'updated_at')
list_filter = ('created_at', 'updated_at')

fieldsets = (
(None, {
'fields': ('name',)
}),
('Spatial Data', {
'fields': ('geom',),
'classes': ('collapse',),
}),
('Timestamps', {
'fields': ('created_at', 'updated_at')
}),
)

map_template = 'gis/admin/osm.html'
default_lon = 0
default_lat = 0
default_zoom = 2
6 changes: 6 additions & 0 deletions django_project/analysis/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class AnalysisConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'analysis'
Loading

0 comments on commit b66391f

Please sign in to comment.