mirror of
https://github.com/ae-utbm/sith.git
synced 2025-07-09 19:40:19 +00:00
Add galaxy (#562)
* style.scss: lint * style.scss: add 'th' padding * core: populate: add much more data for development * Add galaxy
This commit is contained in:
0
galaxy/__init__.py
Normal file
0
galaxy/__init__.py
Normal file
30
galaxy/apps.py
Normal file
30
galaxy/apps.py
Normal file
@ -0,0 +1,30 @@
|
||||
# -*- coding:utf-8 -*
|
||||
#
|
||||
# Copyright 2023
|
||||
# - Skia <skia@hya.sk>
|
||||
#
|
||||
# Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM,
|
||||
# http://ae.utbm.fr.
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify it under
|
||||
# the terms of the GNU General Public License a published by the Free Software
|
||||
# Foundation; either version 3 of the License, or (at your option) any later
|
||||
# version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||||
# details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License along with
|
||||
# this program; if not, write to the Free Sofware Foundation, Inc., 59 Temple
|
||||
# Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
#
|
||||
#
|
||||
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class GalaxyConfig(AppConfig):
|
||||
default_auto_field = "django.db.models.BigAutoField"
|
||||
name = "galaxy"
|
61
galaxy/management/commands/rule_galaxy.py
Normal file
61
galaxy/management/commands/rule_galaxy.py
Normal file
@ -0,0 +1,61 @@
|
||||
# -*- coding:utf-8 -*
|
||||
#
|
||||
# Copyright 2023
|
||||
# - Skia <skia@hya.sk>
|
||||
#
|
||||
# Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM,
|
||||
# http://ae.utbm.fr.
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify it under
|
||||
# the terms of the GNU General Public License a published by the Free Software
|
||||
# Foundation; either version 3 of the License, or (at your option) any later
|
||||
# version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||||
# details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License along with
|
||||
# this program; if not, write to the Free Sofware Foundation, Inc., 59 Temple
|
||||
# Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
#
|
||||
#
|
||||
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.db import connection
|
||||
|
||||
from galaxy.models import Galaxy
|
||||
|
||||
import logging
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = (
|
||||
"Rule the Galaxy! "
|
||||
"Reset the whole galaxy and compute once again all the relation scores of all users. "
|
||||
"As the sith's users are rather numerous, this command might be quite expensive in memory "
|
||||
"and CPU time. Please keep this fact in mind when scheduling calls to this command in a production "
|
||||
"environment."
|
||||
)
|
||||
|
||||
def handle(self, *args, **options):
|
||||
logger = logging.getLogger("main")
|
||||
if options["verbosity"] > 1:
|
||||
logger.setLevel(logging.DEBUG)
|
||||
elif options["verbosity"] > 0:
|
||||
logger.setLevel(logging.INFO)
|
||||
else:
|
||||
logger.setLevel(logging.NOTSET)
|
||||
|
||||
logger.info("The Galaxy is being ruled by the Sith.")
|
||||
Galaxy.rule()
|
||||
logger.info(
|
||||
"Caching current Galaxy state for a quicker display of the Empire's power."
|
||||
)
|
||||
Galaxy.make_state()
|
||||
|
||||
logger.info("Ruled the galaxy in {} queries.".format(len(connection.queries)))
|
||||
if options["verbosity"] > 2:
|
||||
for q in connection.queries:
|
||||
logger.debug(q)
|
113
galaxy/migrations/0001_initial.py
Normal file
113
galaxy/migrations/0001_initial.py
Normal file
@ -0,0 +1,113 @@
|
||||
# Generated by Django 3.2.16 on 2023-02-03 10:31
|
||||
|
||||
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="Galaxy",
|
||||
fields=[
|
||||
(
|
||||
"id",
|
||||
models.BigAutoField(
|
||||
auto_created=True,
|
||||
primary_key=True,
|
||||
serialize=False,
|
||||
verbose_name="ID",
|
||||
),
|
||||
),
|
||||
("state", models.JSONField(verbose_name="current state")),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name="GalaxyStar",
|
||||
fields=[
|
||||
(
|
||||
"id",
|
||||
models.BigAutoField(
|
||||
auto_created=True,
|
||||
primary_key=True,
|
||||
serialize=False,
|
||||
verbose_name="ID",
|
||||
),
|
||||
),
|
||||
(
|
||||
"mass",
|
||||
models.PositiveIntegerField(default=0, verbose_name="star mass"),
|
||||
),
|
||||
(
|
||||
"owner",
|
||||
models.OneToOneField(
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
related_name="galaxy_user",
|
||||
to=settings.AUTH_USER_MODEL,
|
||||
verbose_name="galaxy user",
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name="GalaxyLane",
|
||||
fields=[
|
||||
(
|
||||
"id",
|
||||
models.BigAutoField(
|
||||
auto_created=True,
|
||||
primary_key=True,
|
||||
serialize=False,
|
||||
verbose_name="ID",
|
||||
),
|
||||
),
|
||||
(
|
||||
"distance",
|
||||
models.PositiveIntegerField(
|
||||
default=0,
|
||||
help_text="Distance separating star1 and star2",
|
||||
verbose_name="distance",
|
||||
),
|
||||
),
|
||||
(
|
||||
"family",
|
||||
models.PositiveIntegerField(default=0, verbose_name="family score"),
|
||||
),
|
||||
(
|
||||
"pictures",
|
||||
models.PositiveIntegerField(
|
||||
default=0, verbose_name="pictures score"
|
||||
),
|
||||
),
|
||||
(
|
||||
"clubs",
|
||||
models.PositiveIntegerField(default=0, verbose_name="clubs score"),
|
||||
),
|
||||
(
|
||||
"star1",
|
||||
models.ForeignKey(
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
related_name="lanes1",
|
||||
to="galaxy.galaxystar",
|
||||
verbose_name="galaxy lanes 1",
|
||||
),
|
||||
),
|
||||
(
|
||||
"star2",
|
||||
models.ForeignKey(
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
related_name="lanes2",
|
||||
to="galaxy.galaxystar",
|
||||
verbose_name="galaxy lanes 2",
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
]
|
0
galaxy/migrations/__init__.py
Normal file
0
galaxy/migrations/__init__.py
Normal file
377
galaxy/models.py
Normal file
377
galaxy/models.py
Normal file
@ -0,0 +1,377 @@
|
||||
# -*- coding:utf-8 -*
|
||||
#
|
||||
# Copyright 2023
|
||||
# - Skia <skia@hya.sk>
|
||||
#
|
||||
# Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM,
|
||||
# http://ae.utbm.fr.
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify it under
|
||||
# the terms of the GNU General Public License a published by the Free Software
|
||||
# Foundation; either version 3 of the License, or (at your option) any later
|
||||
# version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||||
# details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License along with
|
||||
# this program; if not, write to the Free Sofware Foundation, Inc., 59 Temple
|
||||
# Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
#
|
||||
#
|
||||
|
||||
import math
|
||||
import logging
|
||||
|
||||
from django.db import models
|
||||
from django.db.models import Q, Case, F, Value, When, Count
|
||||
from django.db.models.functions import Concat
|
||||
from django.utils import timezone
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from typing import List, TypedDict
|
||||
|
||||
from core.models import User
|
||||
from club.models import Club
|
||||
from sas.models import Picture
|
||||
|
||||
|
||||
class GalaxyStar(models.Model):
|
||||
"""
|
||||
This class defines a star (vertex -> user) in the galaxy graph, storing a reference to its owner citizen, and being
|
||||
referenced by GalaxyLane.
|
||||
|
||||
It also stores the individual mass of this star, used to push it towards the center of the galaxy.
|
||||
"""
|
||||
|
||||
owner = models.OneToOneField(
|
||||
User,
|
||||
verbose_name=_("galaxy user"),
|
||||
related_name="galaxy_user",
|
||||
on_delete=models.CASCADE,
|
||||
)
|
||||
mass = models.PositiveIntegerField(
|
||||
_("star mass"),
|
||||
default=0,
|
||||
)
|
||||
|
||||
def __str__(self):
|
||||
return str(self.owner)
|
||||
|
||||
|
||||
class GalaxyLane(models.Model):
|
||||
"""
|
||||
This class defines a lane (edge -> link between galaxy citizen) in the galaxy map, storing a reference to both its
|
||||
ends and the distance it covers.
|
||||
Score details between citizen owning the stars is also stored here.
|
||||
"""
|
||||
|
||||
star1 = models.ForeignKey(
|
||||
GalaxyStar,
|
||||
verbose_name=_("galaxy lanes 1"),
|
||||
related_name="lanes1",
|
||||
on_delete=models.CASCADE,
|
||||
)
|
||||
star2 = models.ForeignKey(
|
||||
GalaxyStar,
|
||||
verbose_name=_("galaxy lanes 2"),
|
||||
related_name="lanes2",
|
||||
on_delete=models.CASCADE,
|
||||
)
|
||||
distance = models.PositiveIntegerField(
|
||||
_("distance"),
|
||||
default=0,
|
||||
help_text=_("Distance separating star1 and star2"),
|
||||
)
|
||||
family = models.PositiveIntegerField(
|
||||
_("family score"),
|
||||
default=0,
|
||||
)
|
||||
pictures = models.PositiveIntegerField(
|
||||
_("pictures score"),
|
||||
default=0,
|
||||
)
|
||||
clubs = models.PositiveIntegerField(
|
||||
_("clubs score"),
|
||||
default=0,
|
||||
)
|
||||
|
||||
|
||||
class StarDict(TypedDict):
|
||||
id: int
|
||||
name: str
|
||||
mass: int
|
||||
|
||||
|
||||
class GalaxyDict(TypedDict):
|
||||
nodes: List[StarDict]
|
||||
links: List
|
||||
|
||||
|
||||
class Galaxy(models.Model):
|
||||
logger = logging.getLogger("main")
|
||||
|
||||
GALAXY_SCALE_FACTOR = 2_000
|
||||
FAMILY_LINK_POINTS = 366 # Equivalent to a leap year together in a club, because.
|
||||
PICTURE_POINTS = 2 # Equivalent to two days as random members of a club.
|
||||
CLUBS_POINTS = 1 # One day together as random members in a club is one point.
|
||||
|
||||
state = models.JSONField("current state")
|
||||
|
||||
@staticmethod
|
||||
def make_state() -> GalaxyDict:
|
||||
"""
|
||||
Compute JSON structure to send to 3d-force-graph: https://github.com/vasturiano/3d-force-graph/
|
||||
"""
|
||||
without_nickname = Concat(
|
||||
F("owner__first_name"), Value(" "), F("owner__last_name")
|
||||
)
|
||||
with_nickname = Concat(
|
||||
F("owner__first_name"),
|
||||
Value(" "),
|
||||
F("owner__last_name"),
|
||||
Value(" ("),
|
||||
F("owner__nick_name"),
|
||||
Value(")"),
|
||||
)
|
||||
stars = GalaxyStar.objects.annotate(
|
||||
owner_name=Case(
|
||||
When(owner__nick_name=None, then=without_nickname),
|
||||
default=with_nickname,
|
||||
)
|
||||
)
|
||||
lanes = GalaxyLane.objects.annotate(
|
||||
star1_owner=F("star1__owner__id"),
|
||||
star2_owner=F("star2__owner__id"),
|
||||
)
|
||||
json = GalaxyDict(
|
||||
nodes=[
|
||||
StarDict(id=star.owner_id, name=star.owner_name, mass=star.mass)
|
||||
for star in stars
|
||||
],
|
||||
links=[],
|
||||
)
|
||||
# Make bidirectional links
|
||||
# TODO: see if this impacts performance with a big graph
|
||||
for path in lanes:
|
||||
json["links"].append(
|
||||
{
|
||||
"source": path.star1_owner,
|
||||
"target": path.star2_owner,
|
||||
"value": path.distance,
|
||||
}
|
||||
)
|
||||
json["links"].append(
|
||||
{
|
||||
"source": path.star2_owner,
|
||||
"target": path.star1_owner,
|
||||
"value": path.distance,
|
||||
}
|
||||
)
|
||||
Galaxy.objects.all().delete()
|
||||
Galaxy(state=json).save()
|
||||
|
||||
###################
|
||||
# User self score #
|
||||
###################
|
||||
|
||||
@classmethod
|
||||
def compute_user_score(cls, user):
|
||||
"""
|
||||
This compute an individual score for each citizen. It will later be used by the graph algorithm to push
|
||||
higher scores towards the center of the galaxy.
|
||||
|
||||
Idea: This could be added to the computation:
|
||||
- Forum posts
|
||||
- Picture count
|
||||
- Counter consumption
|
||||
- Barman time
|
||||
- ...
|
||||
"""
|
||||
user_score = 1
|
||||
user_score += cls.query_user_score(user)
|
||||
|
||||
# TODO:
|
||||
# Scale that value with some magic number to accommodate to typical data
|
||||
# Really active galaxy citizen after 5 years typically have a score of about XXX
|
||||
# Citizen that were seen regularly without taking much part in organizations typically have a score of about XXX
|
||||
# Citizen that only went to a few events typically score about XXX
|
||||
user_score = int(math.log2(user_score))
|
||||
|
||||
return user_score
|
||||
|
||||
@classmethod
|
||||
def query_user_score(cls, user):
|
||||
score_query = (
|
||||
User.objects.filter(id=user.id)
|
||||
.annotate(
|
||||
godchildren_count=Count("godchildren", distinct=True)
|
||||
* cls.FAMILY_LINK_POINTS,
|
||||
godfathers_count=Count("godfathers", distinct=True)
|
||||
* cls.FAMILY_LINK_POINTS,
|
||||
pictures_score=Count("pictures", distinct=True) * cls.PICTURE_POINTS,
|
||||
clubs_score=Count("memberships", distinct=True) * cls.CLUBS_POINTS,
|
||||
)
|
||||
.aggregate(
|
||||
score=models.Sum(
|
||||
F("godchildren_count")
|
||||
+ F("godfathers_count")
|
||||
+ F("pictures_score")
|
||||
+ F("clubs_score")
|
||||
)
|
||||
)
|
||||
)
|
||||
return score_query.get("score")
|
||||
|
||||
####################
|
||||
# Inter-user score #
|
||||
####################
|
||||
|
||||
@classmethod
|
||||
def compute_users_score(cls, user1, user2):
|
||||
family = cls.compute_users_family_score(user1, user2)
|
||||
pictures = cls.compute_users_pictures_score(user1, user2)
|
||||
clubs = cls.compute_users_clubs_score(user1, user2)
|
||||
score = family + pictures + clubs
|
||||
return score, family, pictures, clubs
|
||||
|
||||
@classmethod
|
||||
def compute_users_family_score(cls, user1, user2):
|
||||
link_count = User.objects.filter(
|
||||
Q(id=user1.id, godfathers=user2) | Q(id=user2.id, godfathers=user1)
|
||||
).count()
|
||||
if link_count:
|
||||
cls.logger.debug(
|
||||
f"\t\t- '{user1}' and '{user2}' have {link_count} direct family link"
|
||||
)
|
||||
return link_count * cls.FAMILY_LINK_POINTS
|
||||
|
||||
@classmethod
|
||||
def compute_users_pictures_score(cls, user1, user2):
|
||||
picture_count = (
|
||||
Picture.objects.filter(people__user__in=(user1,))
|
||||
.filter(people__user__in=(user2,))
|
||||
.count()
|
||||
)
|
||||
if picture_count:
|
||||
cls.logger.debug(
|
||||
f"\t\t- '{user1}' was pictured with '{user2}' {picture_count} times"
|
||||
)
|
||||
return picture_count * cls.PICTURE_POINTS
|
||||
|
||||
@classmethod
|
||||
def compute_users_clubs_score(cls, user1, user2):
|
||||
common_clubs = Club.objects.filter(members__in=user1.memberships.all()).filter(
|
||||
members__in=user2.memberships.all()
|
||||
)
|
||||
user1_memberships = user1.memberships.filter(club__in=common_clubs)
|
||||
user2_memberships = user2.memberships.filter(club__in=common_clubs)
|
||||
|
||||
score = 0
|
||||
for user1_membership in user1_memberships:
|
||||
if user1_membership.end_date is None:
|
||||
user1_membership.end_date = timezone.now().date()
|
||||
query = Q( # start2 <= start1 <= end2
|
||||
start_date__lte=user1_membership.start_date,
|
||||
end_date__gte=user1_membership.start_date,
|
||||
)
|
||||
query |= Q( # start2 <= start1 <= now
|
||||
start_date__lte=user1_membership.start_date, end_date=None
|
||||
)
|
||||
query |= Q( # start1 <= start2 <= end2
|
||||
start_date__gte=user1_membership.start_date,
|
||||
start_date__lte=user1_membership.end_date,
|
||||
)
|
||||
for user2_membership in user2_memberships.filter(
|
||||
query, club=user1_membership.club
|
||||
):
|
||||
if user2_membership.end_date is None:
|
||||
user2_membership.end_date = timezone.now().date()
|
||||
latest_start = max(
|
||||
user1_membership.start_date, user2_membership.start_date
|
||||
)
|
||||
earliest_end = min(user1_membership.end_date, user2_membership.end_date)
|
||||
cls.logger.debug(
|
||||
"\t\t- '%s' was with '%s' in %s starting on %s until %s (%s days)"
|
||||
% (
|
||||
user1,
|
||||
user2,
|
||||
user2_membership.club,
|
||||
latest_start,
|
||||
earliest_end,
|
||||
(earliest_end - latest_start).days,
|
||||
)
|
||||
)
|
||||
score += cls.CLUBS_POINTS * (earliest_end - latest_start).days
|
||||
return score
|
||||
|
||||
###################
|
||||
# Rule the galaxy #
|
||||
###################
|
||||
|
||||
@classmethod
|
||||
def rule(cls):
|
||||
GalaxyStar.objects.all().delete()
|
||||
# The following is a no-op thanks to cascading, but in case that changes in the future, better keep it anyway.
|
||||
GalaxyLane.objects.all().delete()
|
||||
rulable_users = (
|
||||
User.objects.filter(subscriptions__isnull=False)
|
||||
.filter(
|
||||
Q(godchildren__isnull=False)
|
||||
| Q(godfathers__isnull=False)
|
||||
| Q(pictures__isnull=False)
|
||||
| Q(memberships__isnull=False)
|
||||
)
|
||||
.distinct()
|
||||
)
|
||||
# force fetch of the whole query to make sure there won't
|
||||
# be any more db hits
|
||||
# this is memory expensive but prevents a lot of db hits, therefore
|
||||
# is far more time efficient
|
||||
rulable_users = list(rulable_users)
|
||||
while len(rulable_users) > 0:
|
||||
user1 = rulable_users.pop()
|
||||
for user2 in rulable_users:
|
||||
cls.logger.debug("")
|
||||
cls.logger.debug(f"\t> Ruling '{user1}' against '{user2}'")
|
||||
star1, _ = GalaxyStar.objects.get_or_create(owner=user1)
|
||||
star2, _ = GalaxyStar.objects.get_or_create(owner=user2)
|
||||
if star1.mass == 0:
|
||||
star1.mass = cls.compute_user_score(user1)
|
||||
star1.save()
|
||||
if star2.mass == 0:
|
||||
star2.mass = cls.compute_user_score(user2)
|
||||
star2.save()
|
||||
users_score, family, pictures, clubs = cls.compute_users_score(
|
||||
user1, user2
|
||||
)
|
||||
if users_score > 0:
|
||||
GalaxyLane(
|
||||
star1=star1,
|
||||
star2=star2,
|
||||
distance=cls.scale_distance(users_score),
|
||||
family=family,
|
||||
pictures=pictures,
|
||||
clubs=clubs,
|
||||
).save()
|
||||
|
||||
@classmethod
|
||||
def scale_distance(cls, value):
|
||||
# TODO: this will need adjustements with the real, typical data on Taiste
|
||||
|
||||
cls.logger.debug(f"\t\t> Score: {value}")
|
||||
# Invert score to draw close users together
|
||||
value = 1 / value # Cannot be 0
|
||||
value += 2 # We use log2 just below and need to stay above 1
|
||||
value = ( # Let's get something in the range ]0; log2(3)-1≈0.58[ that we can multiply later
|
||||
math.log2(value) - 1
|
||||
)
|
||||
value *= ( # Scale that value with a magic number to accommodate to typical data
|
||||
# Really close galaxy citizen after 5 years typically have a score of about XXX
|
||||
# Citizen that were in the same year without being really friends typically have a score of about XXX
|
||||
# Citizen that have met once or twice only have a couple of pictures together typically score about XXX
|
||||
cls.GALAXY_SCALE_FACTOR
|
||||
)
|
||||
cls.logger.debug(f"\t\t> Scaled distance: {value}")
|
||||
return int(value)
|
5
galaxy/static/galaxy/js/3d-force-graph.min.js
vendored
Normal file
5
galaxy/static/galaxy/js/3d-force-graph.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
2
galaxy/static/galaxy/js/d3-force-3d.min.js
vendored
Normal file
2
galaxy/static/galaxy/js/d3-force-3d.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
2
galaxy/static/galaxy/js/three-spritetext.min.js
vendored
Normal file
2
galaxy/static/galaxy/js/three-spritetext.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
6
galaxy/static/galaxy/js/three.min.js
vendored
Normal file
6
galaxy/static/galaxy/js/three.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
108
galaxy/templates/galaxy/user.jinja
Normal file
108
galaxy/templates/galaxy/user.jinja
Normal file
@ -0,0 +1,108 @@
|
||||
{% extends "core/base.jinja" %}
|
||||
|
||||
{% block title %}
|
||||
{% trans user_name=user.get_display_name() %}{{ user_name }}'s Galaxy{% endtrans %}
|
||||
{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
{% if object.galaxy_user %}
|
||||
<p><a onclick="focus_node(get_node_from_id({{ object.id }}))">Reset on {{ object.get_display_name() }}</a></p>
|
||||
<p>Self score: {{ object.galaxy_user.mass }}</p>
|
||||
<table style="width: initial;">
|
||||
<tr>
|
||||
<th></th>
|
||||
<th>Citizen</th>
|
||||
<th>Score</th>
|
||||
<th>Distance</th>
|
||||
<th>Family</th>
|
||||
<th>Pictures</th>
|
||||
<th>Clubs</th>
|
||||
</tr>
|
||||
{% for lane in lanes %}
|
||||
<tr>
|
||||
<td><a onclick="focus_node(get_node_from_id({{ lane.other_star_id }}))">Locate</a></td>
|
||||
<td><a href="{{ url("galaxy:user", user_id=lane.other_star_id) }}">{{ lane.other_star_name }}</a></td>
|
||||
<td>{{ lane.other_star_mass }}</td>
|
||||
<td>{{ lane.distance }}</td>
|
||||
<td>{{ lane.family }}</td>
|
||||
<td>{{ lane.pictures }}</td>
|
||||
<td>{{ lane.clubs }}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</table>
|
||||
|
||||
<div id="3d-graph" style="margin: 1em;"></div>
|
||||
{% else %}
|
||||
<p>This citizen has not yet joined the galaxy</p>
|
||||
{% endif %}
|
||||
|
||||
{% endblock %}
|
||||
|
||||
{% block script %}
|
||||
{{ super() }}
|
||||
|
||||
<script src="{{ static('galaxy/js/three.min.js') }}" defer></script>
|
||||
<script src="{{ static('galaxy/js/three-spritetext.min.js') }}" defer></script>
|
||||
<script src="{{ static('galaxy/js/3d-force-graph.min.js') }}" defer></script>
|
||||
<script src="{{ static('galaxy/js/d3-force-3d.min.js') }}" defer></script>
|
||||
|
||||
<script>
|
||||
var Graph;
|
||||
|
||||
function get_node_from_id(id) {
|
||||
return Graph.graphData().nodes.find(n => n.id === id);
|
||||
}
|
||||
|
||||
function focus_node(node) {
|
||||
// Aim at node from outside it
|
||||
const distance = 200;
|
||||
const distRatio = 1 + distance/Math.hypot(node.x, node.y, node.z);
|
||||
|
||||
const newPos = node.x || node.y || node.z
|
||||
? { x: node.x * distRatio, y: node.y * distRatio, z: node.z * distRatio }
|
||||
: { x: 0, y: 0, z: distance }; // special case if node is in (0,0,0)
|
||||
|
||||
Graph.cameraPosition(
|
||||
newPos, // new position
|
||||
node, // lookAt ({ x, y, z })
|
||||
3000 // ms transition duration
|
||||
);
|
||||
}
|
||||
|
||||
document.addEventListener("DOMContentLoaded", () => {
|
||||
Graph = ForceGraph3D();
|
||||
Graph(document.getElementById('3d-graph'));
|
||||
Graph
|
||||
.jsonUrl('{{ url("galaxy:data") }}')
|
||||
.width(1000)
|
||||
.height(700)
|
||||
.nodeAutoColorBy('id')
|
||||
.nodeLabel(node => `${node.name}`)
|
||||
.onNodeClick(node => focus_node(node))
|
||||
.linkDirectionalParticles(3)
|
||||
.linkDirectionalParticleWidth(0.8)
|
||||
.linkDirectionalParticleSpeed(0.006)
|
||||
.nodeThreeObject(node => {
|
||||
const sprite = new SpriteText(node.name);
|
||||
sprite.material.depthWrite = false; // make sprite background transparent
|
||||
sprite.color = node.color;
|
||||
sprite.textHeight = 5;
|
||||
return sprite;
|
||||
});
|
||||
|
||||
// Set distance between stars
|
||||
Graph.d3Force('link').distance(link => link.value);
|
||||
|
||||
// Set high masses nearer the center of the galaxy
|
||||
// TODO: quick and dirty strength computation, this will need tuning.
|
||||
Graph.d3Force('positionX', d3.forceX().strength(node => { return 1 - (1 / node.mass); }));
|
||||
Graph.d3Force('positionY', d3.forceY().strength(node => { return 1 - (1 / node.mass); }));
|
||||
Graph.d3Force('positionZ', d3.forceZ().strength(node => { return 1 - (1 / node.mass); }));
|
||||
|
||||
// Focus current user
|
||||
setTimeout(() => focus_node(get_node_from_id({{ object.id }})), 1000);
|
||||
})
|
||||
</script>
|
||||
{% endblock %}
|
||||
|
||||
|
129
galaxy/tests.py
Normal file
129
galaxy/tests.py
Normal file
@ -0,0 +1,129 @@
|
||||
# -*- coding:utf-8 -*
|
||||
#
|
||||
# Copyright 2023
|
||||
# - Skia <skia@hya.sk>
|
||||
#
|
||||
# Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM,
|
||||
# http://ae.utbm.fr.
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify it under
|
||||
# the terms of the GNU General Public License a published by the Free Software
|
||||
# Foundation; either version 3 of the License, or (at your option) any later
|
||||
# version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||||
# details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License along with
|
||||
# this program; if not, write to the Free Sofware Foundation, Inc., 59 Temple
|
||||
# Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
#
|
||||
#
|
||||
|
||||
from django.test import TestCase
|
||||
from django.core.management import call_command
|
||||
|
||||
from core.models import User
|
||||
from galaxy.models import Galaxy
|
||||
|
||||
|
||||
class GalaxyTest(TestCase):
|
||||
def setUp(self):
|
||||
call_command("populate")
|
||||
self.root = User.objects.get(username="root")
|
||||
self.skia = User.objects.get(username="skia")
|
||||
self.sli = User.objects.get(username="sli")
|
||||
self.krophil = User.objects.get(username="krophil")
|
||||
self.richard = User.objects.get(username="rbatsbak")
|
||||
self.subscriber = User.objects.get(username="subscriber")
|
||||
self.public = User.objects.get(username="public")
|
||||
self.com = User.objects.get(username="comunity")
|
||||
|
||||
def test_user_self_score(self):
|
||||
with self.assertNumQueries(8):
|
||||
self.assertEqual(Galaxy.compute_user_score(self.root), 9)
|
||||
self.assertEqual(Galaxy.compute_user_score(self.skia), 10)
|
||||
self.assertEqual(Galaxy.compute_user_score(self.sli), 8)
|
||||
self.assertEqual(Galaxy.compute_user_score(self.krophil), 2)
|
||||
self.assertEqual(Galaxy.compute_user_score(self.richard), 10)
|
||||
self.assertEqual(Galaxy.compute_user_score(self.subscriber), 8)
|
||||
self.assertEqual(Galaxy.compute_user_score(self.public), 8)
|
||||
self.assertEqual(Galaxy.compute_user_score(self.com), 1)
|
||||
|
||||
def test_users_score(self):
|
||||
expected_scores = {
|
||||
"krophil": {
|
||||
"comunity": {"clubs": 0, "family": 0, "pictures": 0, "score": 0},
|
||||
"public": {"clubs": 0, "family": 0, "pictures": 0, "score": 0},
|
||||
"rbatsbak": {"clubs": 100, "family": 0, "pictures": 0, "score": 100},
|
||||
"subscriber": {"clubs": 0, "family": 0, "pictures": 0, "score": 0},
|
||||
},
|
||||
"public": {
|
||||
"comunity": {"clubs": 0, "family": 0, "pictures": 0, "score": 0}
|
||||
},
|
||||
"rbatsbak": {
|
||||
"comunity": {"clubs": 0, "family": 0, "pictures": 0, "score": 0},
|
||||
"public": {"clubs": 0, "family": 366, "pictures": 0, "score": 366},
|
||||
"subscriber": {"clubs": 0, "family": 366, "pictures": 0, "score": 366},
|
||||
},
|
||||
"root": {
|
||||
"comunity": {"clubs": 0, "family": 0, "pictures": 0, "score": 0},
|
||||
"krophil": {"clubs": 0, "family": 0, "pictures": 0, "score": 0},
|
||||
"public": {"clubs": 0, "family": 0, "pictures": 0, "score": 0},
|
||||
"rbatsbak": {"clubs": 0, "family": 0, "pictures": 0, "score": 0},
|
||||
"skia": {"clubs": 0, "family": 732, "pictures": 0, "score": 732},
|
||||
"sli": {"clubs": 0, "family": 0, "pictures": 0, "score": 0},
|
||||
"subscriber": {"clubs": 0, "family": 0, "pictures": 0, "score": 0},
|
||||
},
|
||||
"skia": {
|
||||
"comunity": {"clubs": 0, "family": 0, "pictures": 0, "score": 0},
|
||||
"krophil": {"clubs": 114, "family": 0, "pictures": 2, "score": 116},
|
||||
"public": {"clubs": 0, "family": 0, "pictures": 0, "score": 0},
|
||||
"rbatsbak": {"clubs": 100, "family": 0, "pictures": 0, "score": 100},
|
||||
"sli": {"clubs": 0, "family": 366, "pictures": 4, "score": 370},
|
||||
"subscriber": {"clubs": 0, "family": 0, "pictures": 0, "score": 0},
|
||||
},
|
||||
"sli": {
|
||||
"comunity": {"clubs": 0, "family": 0, "pictures": 0, "score": 0},
|
||||
"krophil": {"clubs": 17, "family": 0, "pictures": 2, "score": 19},
|
||||
"public": {"clubs": 0, "family": 0, "pictures": 0, "score": 0},
|
||||
"rbatsbak": {"clubs": 0, "family": 0, "pictures": 0, "score": 0},
|
||||
"subscriber": {"clubs": 0, "family": 0, "pictures": 0, "score": 0},
|
||||
},
|
||||
"subscriber": {
|
||||
"comunity": {"clubs": 0, "family": 0, "pictures": 0, "score": 0},
|
||||
"public": {"clubs": 0, "family": 0, "pictures": 0, "score": 0},
|
||||
},
|
||||
}
|
||||
computed_scores = {}
|
||||
users = [
|
||||
self.root,
|
||||
self.skia,
|
||||
self.sli,
|
||||
self.krophil,
|
||||
self.richard,
|
||||
self.subscriber,
|
||||
self.public,
|
||||
self.com,
|
||||
]
|
||||
|
||||
with self.assertNumQueries(100):
|
||||
while len(users) > 0:
|
||||
user1 = users.pop(0)
|
||||
for user2 in users:
|
||||
score, family, pictures, clubs = Galaxy.compute_users_score(
|
||||
user1, user2
|
||||
)
|
||||
u1 = computed_scores.get(user1.username, {})
|
||||
u1[user2.username] = {
|
||||
"score": score,
|
||||
"family": family,
|
||||
"pictures": pictures,
|
||||
"clubs": clubs,
|
||||
}
|
||||
computed_scores[user1.username] = u1
|
||||
|
||||
self.maxDiff = None # Yes, we want to see the diff if any
|
||||
self.assertDictEqual(expected_scores, computed_scores)
|
40
galaxy/urls.py
Normal file
40
galaxy/urls.py
Normal file
@ -0,0 +1,40 @@
|
||||
# -*- coding:utf-8 -*
|
||||
#
|
||||
# Copyright 2023
|
||||
# - Skia <skia@hya.sk>
|
||||
#
|
||||
# Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM,
|
||||
# http://ae.utbm.fr.
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify it under
|
||||
# the terms of the GNU General Public License a published by the Free Software
|
||||
# Foundation; either version 3 of the License, or (at your option) any later
|
||||
# version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||||
# details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License along with
|
||||
# this program; if not, write to the Free Sofware Foundation, Inc., 59 Temple
|
||||
# Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
#
|
||||
#
|
||||
|
||||
from django.urls import path
|
||||
|
||||
from galaxy.views import *
|
||||
|
||||
urlpatterns = [
|
||||
path(
|
||||
"<int:user_id>/",
|
||||
GalaxyUserView.as_view(),
|
||||
name="user",
|
||||
),
|
||||
path(
|
||||
"data.json",
|
||||
GalaxyDataView.as_view(),
|
||||
name="data",
|
||||
),
|
||||
]
|
97
galaxy/views.py
Normal file
97
galaxy/views.py
Normal file
@ -0,0 +1,97 @@
|
||||
# -*- coding:utf-8 -*
|
||||
#
|
||||
# Copyright 2023
|
||||
# - Skia <skia@hya.sk>
|
||||
#
|
||||
# Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM,
|
||||
# http://ae.utbm.fr.
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify it under
|
||||
# the terms of the GNU General Public License a published by the Free Software
|
||||
# Foundation; either version 3 of the License, or (at your option) any later
|
||||
# version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||||
# details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License along with
|
||||
# this program; if not, write to the Free Sofware Foundation, Inc., 59 Temple
|
||||
# Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
#
|
||||
#
|
||||
|
||||
from django.views.generic import DetailView, View
|
||||
from django.http import JsonResponse
|
||||
from django.db.models import Q, Case, F, When, Value
|
||||
from django.db.models.functions import Concat
|
||||
|
||||
from core.views import (
|
||||
CanViewMixin,
|
||||
FormerSubscriberMixin,
|
||||
)
|
||||
from core.models import User
|
||||
from core.views import UserTabsMixin
|
||||
from galaxy.models import Galaxy, GalaxyLane
|
||||
|
||||
|
||||
class GalaxyUserView(CanViewMixin, UserTabsMixin, DetailView):
|
||||
model = User
|
||||
pk_url_kwarg = "user_id"
|
||||
template_name = "galaxy/user.jinja"
|
||||
current_tab = "galaxy"
|
||||
|
||||
def get_queryset(self):
|
||||
return super(GalaxyUserView, self).get_queryset().select_related("galaxy_user")
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
kwargs = super(GalaxyUserView, self).get_context_data(**kwargs)
|
||||
kwargs["lanes"] = (
|
||||
GalaxyLane.objects.filter(
|
||||
Q(star1=self.object.galaxy_user) | Q(star2=self.object.galaxy_user)
|
||||
)
|
||||
.order_by("distance")
|
||||
.annotate(
|
||||
other_star_id=Case(
|
||||
When(star1=self.object.galaxy_user, then=F("star2__owner__id")),
|
||||
default=F("star1__owner__id"),
|
||||
),
|
||||
other_star_mass=Case(
|
||||
When(star1=self.object.galaxy_user, then=F("star2__mass")),
|
||||
default=F("star1__mass"),
|
||||
),
|
||||
other_star_name=Case(
|
||||
When(
|
||||
star1=self.object.galaxy_user,
|
||||
then=Case(
|
||||
When(
|
||||
star2__owner__nick_name=None,
|
||||
then=Concat(
|
||||
F("star2__owner__first_name"),
|
||||
Value(" "),
|
||||
F("star2__owner__last_name"),
|
||||
),
|
||||
)
|
||||
),
|
||||
),
|
||||
default=Case(
|
||||
When(
|
||||
star1__owner__nick_name=None,
|
||||
then=Concat(
|
||||
F("star1__owner__first_name"),
|
||||
Value(" "),
|
||||
F("star1__owner__last_name"),
|
||||
),
|
||||
)
|
||||
),
|
||||
),
|
||||
)
|
||||
.select_related("star1", "star2")
|
||||
)
|
||||
return kwargs
|
||||
|
||||
|
||||
class GalaxyDataView(FormerSubscriberMixin, View):
|
||||
def get(self, request, *args, **kwargs):
|
||||
return JsonResponse(Galaxy.objects.first().state)
|
Reference in New Issue
Block a user