Skip to content

Commit

Permalink
Removing general metrics visualization; Fixing code to pass codes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ederporto committed Feb 2, 2024
1 parent 0a6556c commit 9505cef
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 354 deletions.
158 changes: 3 additions & 155 deletions metrics/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from report.models import Report, Editor
from users.models import User, UserProfile, TeamArea
from strategy.models import StrategicAxis
from .views import get_metrics_and_aggregate_per_project, get_aggregated_metrics_data, get_aggregated_metrics_data_done, get_activities, get_chart_data, get_chart_data_many_to_many
from .views import get_metrics_and_aggregate_per_project, get_aggregated_metrics_data, get_aggregated_metrics_data_done
from django.urls import reverse
from django.contrib.auth.models import Permission
from datetime import datetime, timedelta
Expand Down Expand Up @@ -73,7 +73,7 @@ class ProjectModelTests(TestCase):
def setUp(self):
self.text = "text"
self.status = True
self.project = Project.objects.create(text=self.text, status=self.status)
self.project = Project.objects.create(text=self.text, active=self.status)

def test_project_str_returns_text(self):
self.assertEqual(str(self.project), self.text)
Expand Down Expand Up @@ -138,33 +138,10 @@ def test_about_view(self):
self.assertTemplateUsed(response, "metrics/about.html")

def test_show_activities_plan_view(self):
project = Project.objects.create(text="Plano de atividades")
area = Area.objects.create(text="Area")
area.project.add(project)
Activity.objects.create(text="Activity", area=area)
url = reverse("metrics:show_activities")
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "metrics/activities_plan.html")
self.assertContains(response, "Activity")
self.assertContains(response, "Area")

def test_show_metrics_is_only_visible_for_users_with_permission(self):
self.user.user_permissions.remove(self.view_metrics_permission)
self.client.login(username=self.username, password=self.password)
url = reverse("metrics:metrics")
response = self.client.get(url)

self.assertEqual(response.status_code, 302)
self.assertRedirects(response, f"{reverse('login')}?next={reverse('metrics:metrics')}")

def test_show_metrics_get_is_shown(self):
self.client.login(username=self.username, password=self.password)
url = reverse("metrics:metrics")
response = self.client.get(url)

self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "metrics/list_metrics.html")
self.assertEqual(response.url, "https://meta.wikimedia.org/wiki/Wiki_Movement_Brazil_User_Group/Plan_of_Activities")

def test_show_metrics_per_project(self):
self.client.login(username=self.username, password=self.password)
Expand All @@ -174,53 +151,6 @@ def test_show_metrics_per_project(self):
response = self.client.get(url)
self.assertIn("dataset", str(response.context))

def test_show_metrics_shows_metrics_charts(self):
self.client.login(username=self.username, password=self.password)

area = Area.objects.create(text="Area")
activity = Activity.objects.create(text="Activity", area=area)
metric = Metric.objects.create(text="Metric 1", activity=activity, wikipedia_created=4)

url = reverse("metrics:metrics")
response = self.client.get(url)

self.assertIn("wikipedia_created", str(response.context["total_sum"]))
self.assertIn("<canvas id=\"wikipedia_pie_chart\">", str(response.content))
self.assertIn("<canvas id=\"wikipedia_timeline_chart\">", str(response.content))
self.assertNotIn("<canvas id=\"commons_pie_chart\">", str(response.content))

def test_show_metrics_shows_metrics_charts_with_more_than_one_activity(self):
self.client.login(username=self.username, password=self.password)

area = Area.objects.create(text="Area")
activity_1 = Activity.objects.create(text="Activity 1", area=area)
activity_2 = Activity.objects.create(text="Activity 2", area=area)
metric = Metric.objects.create(text="Metric 1", activity=activity_2, wikipedia_created=4)

url = reverse("metrics:metrics")
response = self.client.get(url)

self.assertIn("wikipedia_created", str(response.context["total_sum"]))
self.assertIn("<canvas id=\"wikipedia_pie_chart\">", str(response.content))
self.assertIn("<canvas id=\"wikipedia_timeline_chart\">", str(response.content))
self.assertNotIn("<canvas id=\"commons_pie_chart\">", str(response.content))

def test_show_metrics_shows_projects_metrics_as_charts(self):
self.client.login(username=self.username, password=self.password)

area = Area.objects.create(text="Area")
activity = Activity.objects.create(text="Activity", area=area)
project_1 = Project.objects.create(text="Project 1")
project_2 = Project.objects.create(text="Project 2")
metric = Metric.objects.create(text="Metric 1", activity=activity, wikipedia_created=4)
metric.project.add(project_2)
metric.save()

url = reverse("metrics:metrics")
response = self.client.get(url)

self.assertIn(project_2.text, str(response.context["projects_metrics"]))

def test_update_metrics(self):
self.client.login(username=self.username, password=self.password)
area = Area.objects.create(text="Area")
Expand Down Expand Up @@ -507,88 +437,6 @@ def test_get_metrics_and_aggregate_per_project_without_data(self):
aggregated_metrics = get_metrics_and_aggregate_per_project()
self.assertEqual(aggregated_metrics, {})

def test_get_activities_with_data(self):
self.report_1.wikipedia_created = 1
self.report_2.editors.add(self.editor_1)
self.report_1.save()
self.report_2.save()

chart_data = get_activities()
self.assertEqual(chart_data, {"wikipedia": [{"x": str(datetime.now().date() + timedelta(days=1)),
"y": self.report_1.wikipedia_created,
"label": self.report_1.description}],
"commons": [],
"wikidata": [],
"wikiversity": [],
"wikibooks": [],
"wikisource": [],
"wikinews": [],
"wikiquote": [],
"wiktionary": [],
"wikivoyage": [],
"wikispecies": [],
"metawiki": [],
"mediawiki": [],
"participants": [],
"resources": [],
"feedbacks": [],
"editors": [{"x": str(datetime.now().date() + timedelta(days=2)),
"y": self.report_2.editors.count(),
"label": self.report_2.description}],
"organizers": [],
"partnerships": []})

def test_get_activities_without_data(self):
chart_data = get_activities()
self.assertEqual(chart_data, {"wikipedia": [], "commons": [], "wikidata": [], "wikiversity": [], "wikibooks": [], "wikisource": [], "wikinews": [], "wikiquote": [], "wiktionary": [], "wikivoyage": [], "wikispecies": [], "metawiki": [], "mediawiki": [], "participants": [], "resources": [], "feedbacks": [], "editors": [], "organizers": [], "partnerships": []})

def test_get_chart_data_with_data(self):
self.report_1.wikipedia_created = 1
self.report_2.wikipedia_edited = 2
self.report_3.wikipedia_created = 3
self.report_3.wikipedia_edited = 4
self.report_1.save()
self.report_2.save()
self.report_3.save()

activities = Report.objects.all().order_by("end_date")
chart_data = get_chart_data(activities, "wikipedia_created", "wikipedia_edited")
self.assertEqual(chart_data, [{"x": str(datetime.now().date() + timedelta(days=1)),
"y": self.report_1.wikipedia_created + self.report_1.wikipedia_edited,
"label": self.report_1.description},
{"x": str(datetime.now().date() + timedelta(days=2)),
"y": self.report_1.wikipedia_created + self.report_1.wikipedia_edited + self.report_2.wikipedia_created + self.report_2.wikipedia_edited,
"label": self.report_2.description},
{"x": str(datetime.now().date() + timedelta(days=2)),
"y": self.report_1.wikipedia_created + self.report_1.wikipedia_edited + self.report_2.wikipedia_created + self.report_2.wikipedia_edited + self.report_3.wikipedia_created + self.report_3.wikipedia_edited,
"label": self.report_3.description}])

def test_get_chart_data_without_data(self):
activities = Report.objects.all().order_by("end_date")
chart_data = get_chart_data(activities, "wikipedia_created", "wikipedia_edited")
self.assertEqual(chart_data, [])

def test_get_chart_data_many_to_many_with_data(self):
self.report_1.editors.add(self.editor_1)
self.report_1.editors.add(self.editor_2)
self.report_2.editors.add(self.editor_3)
self.report_1.save()
self.report_2.save()

activities = Report.objects.all().order_by("end_date")
chart_data = get_chart_data_many_to_many(activities, "editors")
self.assertEqual(chart_data, [{"x": str(datetime.now().date() + timedelta(days=1)),
"y": self.report_1.editors.count(),
"label": self.report_1.description},
{"x": str(datetime.now().date() + timedelta(days=2)),
"y": self.report_1.editors.count() + self.report_2.editors.count(),
"label": self.report_2.description}])

def test_get_chart_data_many_to_many_without_data(self):
activities = Report.objects.all().order_by("end_date")
chart_data = get_chart_data_many_to_many(activities, "editors")
self.assertEqual(chart_data, [])


class TagsTests(TestCase):
def test_categorize_for_0(self):
Expand Down
1 change: 0 additions & 1 deletion metrics/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
path('', views.index, name='index'),
path('about', views.about, name='about'),
path('activities_plan', views.show_activities_plan, name='show_activities'),
path('metrics', views.show_metrics, name='metrics'),
path('metrics_per_project', views.show_metrics_per_project, name='per_project'),
path('update_metrics', views.update_metrics_relations, name='update_metrics'),
path('metrics_reports/<int:metric_id>', views.metrics_reports, name='metrics_reports'),
Expand Down
77 changes: 4 additions & 73 deletions metrics/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,6 @@ def show_activities_plan(request):
return redirect(settings.POA_URL)


@login_required
@permission_required("metrics.view_metric")
def show_metrics(request):
total_sum = get_aggregated_metrics_data()
total_done = get_aggregated_metrics_data_done()
timeline_activites = get_activities()

projects = Project.objects.filter(pk__in=list(Metric.objects.filter(project__isnull=False).values_list("project__pk", flat=True))).exclude(pk=1)
projects_metrics_sum = []

if projects.count():
for project in projects:
projects_metrics_sum.append({"funding": project.text, "total_sum": get_aggregated_metrics_data(project=project)})

context = {"total_sum": total_sum, "total_done": total_done, "timeline": timeline_activites, "projects_metrics": projects_metrics_sum, "title": _("Show general metrics")}
return render(request, "metrics/list_metrics.html", context)


@login_required
@permission_required("metrics.view_metric")
def show_metrics_per_project(request):
Expand Down Expand Up @@ -150,7 +132,7 @@ def get_done_for_report(reports):
"MetaWiki": reports.aggregate(total=Sum(F("metawiki_created") + F("metawiki_edited")))["total"] or 0,
"MediaWiki": reports.aggregate(total=Sum(F("mediawiki_created") + F("mediawiki_edited")))["total"] or 0,
"Number of participants": reports.aggregate(total=Sum("participants"))["total"] or 0,
"Number of resources": reports.aggregate(total=Sum("resources"))["total"] or 0,
# "Number of resources": reports.aggregate(total=Sum("resources"))["total"] or 0,
"Number of feedbacks": reports.aggregate(total=Sum("feedbacks"))["total"] or 0,
"Number of events": reports.count() or 0,
"Number of editors": Editor.objects.filter(editors__in=reports).distinct().count() or 0,
Expand All @@ -159,7 +141,7 @@ def get_done_for_report(reports):
"Number of partnerships activated": Partner.objects.filter(partners__in=reports).distinct().count() or 0,
"Number of organizers": Organizer.objects.filter(organizers__in=reports).distinct().count() or 0,
"Number of organizers retained": Organizer.objects.filter(retained=True, organizers__in=reports).distinct().count() or 0,
"Number of people reached through social media": reports.aggregate(total=Sum(F("number_of_people_reached_through_social_media")))["total"] or 0,
# "Number of people reached through social media": reports.aggregate(total=Sum(F("number_of_people_reached_through_social_media")))["total"] or 0,
"Occurence": reports.filter(metrics_related__boolean_type=True).exists() or False,
}

Expand Down Expand Up @@ -282,7 +264,7 @@ def get_aggregated_metrics_data_done():
mediawiki_edited = Report.objects.aggregate(Sum('mediawiki_edited'))['mediawiki_edited__sum']

number_of_participants = Report.objects.aggregate(Sum('participants'))['participants__sum']
number_of_resources = Report.objects.aggregate(Sum('resources'))['resources__sum']
# number_of_resources = Report.objects.aggregate(Sum('resources'))['resources__sum']
number_of_feedbacks = Report.objects.aggregate(Sum('feedbacks'))['feedbacks__sum']
number_of_editors = Report.objects.annotate(num_editors=Count('editors')).aggregate(Sum('num_editors'))['num_editors__sum']
number_of_organizers = Report.objects.annotate(num_organizers=Count('organizers')).aggregate(Sum('num_organizers'))['num_organizers__sum']
Expand Down Expand Up @@ -321,7 +303,7 @@ def get_aggregated_metrics_data_done():
total_sum["mediawiki_edited"] = mediawiki_edited

total_sum["participants"] = number_of_participants
total_sum["resources"] = number_of_resources
# total_sum["resources"] = number_of_resources
total_sum["feedbacks"] = number_of_feedbacks
total_sum["editors"] = number_of_editors
total_sum["organizers"] = number_of_organizers
Expand All @@ -334,57 +316,6 @@ def get_aggregated_metrics_data_done():
return total_sum


def get_activities():
activities = Report.objects.all().order_by("end_date")
chart_data = {}
chart_data["wikipedia"] = get_chart_data(activities, "wikipedia_created", "wikipedia_edited")
chart_data["commons"] = get_chart_data(activities, "commons_created", "commons_edited")
chart_data["wikidata"] = get_chart_data(activities, "wikidata_created", "wikidata_edited")
chart_data["wikiversity"] = get_chart_data(activities, "wikiversity_created", "wikiversity_edited")
chart_data["wikibooks"] = get_chart_data(activities, "wikibooks_created", "wikibooks_edited")
chart_data["wikisource"] = get_chart_data(activities, "wikisource_created", "wikisource_edited")
chart_data["wikinews"] = get_chart_data(activities, "wikinews_created", "wikinews_edited")
chart_data["wikiquote"] = get_chart_data(activities, "wikiquote_created", "wikiquote_edited")
chart_data["wiktionary"] = get_chart_data(activities, "wiktionary_created", "wiktionary_edited")
chart_data["wikivoyage"] = get_chart_data(activities, "wikivoyage_created", "wikivoyage_edited")
chart_data["wikispecies"] = get_chart_data(activities, "wikispecies_created", "wikispecies_edited")
chart_data["metawiki"] = get_chart_data(activities, "metawiki_created", "metawiki_edited")
chart_data["mediawiki"] = get_chart_data(activities, "mediawiki_created", "mediawiki_edited")

chart_data["participants"] = get_chart_data(activities, "participants")
chart_data["resources"] = get_chart_data(activities, "resources")
chart_data["feedbacks"] = get_chart_data(activities, "feedbacks")
chart_data["editors"] = get_chart_data_many_to_many(activities, "editors")
chart_data["organizers"] = get_chart_data_many_to_many(activities, "organizers")
chart_data["partnerships"] = get_chart_data_many_to_many(activities, "partners_activated")

return chart_data


def get_chart_data(activities, created_field=None, edited_field=None):
created_filter = Q(**{created_field + '__gt': 0}) if created_field else Q()
edited_filter = Q(**{edited_field + '__gt': 0}) if edited_field else Q()
filtered_activities = activities.filter(created_filter | edited_filter).order_by("end_date")
chart_data = []
total_ = 0
for activity in filtered_activities:
total_created = getattr(activity, created_field) if created_field else 0
total_edited = getattr(activity, edited_field) if edited_field else 0
total_ += total_created + total_edited
chart_data.append({"x": activity.end_date.isoformat(), "y": total_, "label": activity.description})
return chart_data


def get_chart_data_many_to_many(activities, field):
filtered_activities = Report.objects.filter(Q(**{field + '__isnull': False})).distinct().order_by("end_date")
chart_data = []
total_ = 0
for activity in filtered_activities:
total_ += getattr(activity, field).count()
chart_data.append({"x": activity.end_date.isoformat(), "y": total_, "label": activity.description})
return chart_data


def update_metrics_relations(request):
main_funding = Project.objects.get(text="Wikimedia Community Fund")
editors_metrics = Metric.objects.filter(project=main_funding).filter(Q(number_of_editors__gt=0) | Q(number_of_editors_retained__gt=0) | Q(number_of_new_editors__gt=0))
Expand Down
Loading

0 comments on commit 9505cef

Please sign in to comment.