Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…into fix/disparity
  • Loading branch information
Veivel committed Jul 5, 2024
2 parents 6537bde + f49fe6f commit 60b3a85
Show file tree
Hide file tree
Showing 27 changed files with 611 additions and 43 deletions.
42 changes: 21 additions & 21 deletions .ebextensions/django.config → .ebextensions/02_django.config
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
option_settings:
aws:elasticbeanstalk:application:environment:
DJANGO_SETTINGS_MODULE: "UlasKelas.settings"
POSTGRES_DB: ""
POSTGRES_USER: ""
POSTGRES_PASSWORD: ""
POSTGRES_HOST: ""
SENTRY_DSN: ""
aws:elasticbeanstalk:container:python:
WSGIPath: "UlasKelas.wsgi:application"
aws:elasticbeanstalk:environment:proxy:staticfiles:
/static: static
aws:elasticbeanstalk:environment:process:default:
HealthCheckPath: "/ping"
MatcherHTTPCode: "200-499"
commands:
01_postgres_activate:
command: sudo amazon-linux-extras enable postgresql11
02_postgres_install:
command: sudo yum install -y postgresql-devel
option_settings:
aws:elasticbeanstalk:application:environment:
DJANGO_SETTINGS_MODULE: "UlasKelas.settings"
POSTGRES_DB: ""
POSTGRES_USER: ""
POSTGRES_PASSWORD: ""
POSTGRES_HOST: ""
SENTRY_DSN: ""
aws:elasticbeanstalk:container:python:
WSGIPath: "UlasKelas.wsgi:application"
aws:elasticbeanstalk:environment:proxy:staticfiles:
/static: static
aws:elasticbeanstalk:environment:process:default:
HealthCheckPath: "/ping"
MatcherHTTPCode: "200-499"

commands:
01_postgres_activate:
command: sudo amazon-linux-extras enable postgresql11
02_postgres_install:
command: sudo yum install -y postgresql-devel
3 changes: 3 additions & 0 deletions .ebextensions/99_setup_swap.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
container_commands:
01setup_swap:
command: "sudo bash .ebextensions/setup_swap.sh"
15 changes: 15 additions & 0 deletions .ebextensions/setup_swap.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

SWAPFILE=/var/swapfile
SWAP_MEGABYTES=768

if [ -f $SWAPFILE ]; then
echo "Swapfile $SWAPFILE found, assuming already setup"
exit;
fi

/bin/dd if=/dev/zero of=$SWAPFILE bs=1M count=$SWAP_MEGABYTES
/bin/chmod 600 $SWAPFILE
/sbin/mkswap $SWAPFILE
/sbin/swapon $SWAPFILE
sysctl vm.swappiness=15
Empty file added .github/workflows/dev.yml
Empty file.
1 change: 1 addition & 0 deletions Buildfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
release: bash deployment.sh
1 change: 0 additions & 1 deletion Procfile
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
release: bash deployment.sh
web: gunicorn UlasKelas.wsgi
2 changes: 2 additions & 0 deletions UlasKelas/.env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ POSTGRES_HOST=localhost
DEBUG=true
# prod : POSTGRES_HOST=postgres

# old secret key
SECRET_KEY=-zrv7c0@3$c6#7e#ll!z94oy0=-2-e0eqvy4%so=!z3zw6k=da

SENTRY_DSN=
3 changes: 2 additions & 1 deletion UlasKelas/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '-zrv7c0@3$c6#7e#ll!z94oy0=-2-e0eqvy4%so=!z3zw6k=da'
SECRET_KEY = env("SECRET_KEY")

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
Expand Down Expand Up @@ -80,6 +80,7 @@
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.IsAuthenticated'
],
'DEFAULT_VERSIONING_CLASS': 'rest_framework.versioning.NamespaceVersioning',
}

ROOT_URLCONF = 'UlasKelas.urls'
Expand Down
5 changes: 4 additions & 1 deletion UlasKelas/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@
urlpatterns = [
path('admin/', admin.site.urls),
path('update-course/', views.update_course),
path('update-leaderboard/', views.update_leaderboard),
path('ping', views.ping),
path('health-check', views.health_check),
path('login/', views.login),
path('api/', include("main.urls")),
path('api/', include(("main.urls", "api"), namespace="v1")),
path('api/v1/', include(("main.urls", "api"), namespace="v1")),
path('api/v2/', include(("main.urls", "api"), namespace="v2")),
path('api-auth-token/', views_token.obtain_auth_token),
path('token/', views.token, name='token'),
path('logout/', views.logout),
Expand Down
6 changes: 4 additions & 2 deletions UlasKelas/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
application = get_wsgi_application()

# Scheduler update course
from courseUpdater import updater
updater.start()
from courseUpdater import updater as course_updater
from leaderboard_updater import updater as leaderboard_updater
course_updater.start()
leaderboard_updater.start()

5 changes: 3 additions & 2 deletions courseUpdater/courseApi.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ def update_courses():
if json is not None:
courses_json = json['courses']
for course_json in courses_json:
course = getCourse(course_json)
course.save()
if course_json['code']:
course = getCourse(course_json)
course.save()

def _get_courses_json():
url = django_settings.SUNJAD_BASE_URL + 'susunjadwal/api/courses'
Expand Down
Empty file added leaderboard_updater/__init__.py
Empty file.
29 changes: 29 additions & 0 deletions leaderboard_updater/updater.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from datetime import datetime
from django.db import transaction
from django.db.models import Count, Sum
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.triggers.interval import IntervalTrigger

from main.models import Profile, Review

def update_leaderboard():
users = Profile.objects.all()

for user in users:
likes_count = Review.objects.filter(user=user).filter(is_active=True).annotate(likes_count=Count('reviewlike')).aggregate(Sum('likes_count'))['likes_count__sum']
user.likes_count = likes_count

with transaction.atomic():
for user in users:
user.save()

def start():
scheduler = BackgroundScheduler()
scheduler.add_job(
update_leaderboard,
trigger=IntervalTrigger(minutes=5), # Every 5 minutes
id="update_courses",
max_instances=1,
replace_existing=True,
)
scheduler.start()
4 changes: 3 additions & 1 deletion main/admin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.contrib import admin
from .models import Course, ReviewLike, Tag, Profile, Review, ReviewTag
from .models import Calculator, Course, ReviewLike, ScoreComponent, Tag, Profile, Review, ReviewTag

# Register your models here.
admin.site.register(Course)
Expand All @@ -8,3 +8,5 @@
admin.site.register(Review)
admin.site.register(ReviewLike)
admin.site.register(ReviewTag)
admin.site.register(Calculator)
admin.site.register(ScoreComponent)
18 changes: 18 additions & 0 deletions main/migrations/0003_review_is_reviewed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.1.2 on 2022-07-22 12:12

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('main', '0002_profile_is_blocked'),
]

operations = [
migrations.AddField(
model_name='review',
name='is_reviewed',
field=models.BooleanField(default=False),
),
]
34 changes: 34 additions & 0 deletions main/migrations/0004_calculator_scorecomponent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Generated by Django 3.1.2 on 2022-07-28 07:25

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


class Migration(migrations.Migration):

dependencies = [
('main', '0003_review_is_reviewed'),
]

operations = [
migrations.CreateModel(
name='Calculator',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('total_score', models.FloatField(default=0)),
('total_percentage', models.FloatField(default=0)),
('course', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='main.course')),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='main.profile')),
],
),
migrations.CreateModel(
name='ScoreComponent',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.TextField()),
('weight', models.FloatField()),
('score', models.FloatField()),
('calculator', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='main.calculator')),
],
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Generated by Django 4.0.6 on 2022-07-30 03:56

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('main', '0004_calculator_scorecomponent'),
]

operations = [
migrations.AddField(
model_name='profile',
name='likes_count',
field=models.PositiveIntegerField(default=0),
),
migrations.AddField(
model_name='review',
name='rating_beneficial',
field=models.PositiveSmallIntegerField(default=0),
),
migrations.AddField(
model_name='review',
name='rating_fit_to_credit',
field=models.PositiveSmallIntegerField(default=0),
),
migrations.AddField(
model_name='review',
name='rating_fit_to_study_book',
field=models.PositiveSmallIntegerField(default=0),
),
migrations.AddField(
model_name='review',
name='rating_recommended',
field=models.PositiveSmallIntegerField(default=0),
),
migrations.AddField(
model_name='review',
name='rating_understandable',
field=models.PositiveSmallIntegerField(default=0),
),
]
18 changes: 18 additions & 0 deletions main/migrations/0006_alter_profile_likes_count.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.0.6 on 2022-07-30 06:00

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('main', '0005_profile_likes_count_review_rating_beneficial_and_more'),
]

operations = [
migrations.AlterField(
model_name='profile',
name='likes_count',
field=models.PositiveIntegerField(default=0, null=True),
),
]
38 changes: 38 additions & 0 deletions main/migrations/0007_alter_review_rating_beneficial_and_more.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Generated by Django 4.0.6 on 2022-07-30 07:52

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('main', '0006_alter_profile_likes_count'),
]

operations = [
migrations.AlterField(
model_name='review',
name='rating_beneficial',
field=models.FloatField(default=0, null=True),
),
migrations.AlterField(
model_name='review',
name='rating_fit_to_credit',
field=models.FloatField(default=0, null=True),
),
migrations.AlterField(
model_name='review',
name='rating_fit_to_study_book',
field=models.FloatField(default=0, null=True),
),
migrations.AlterField(
model_name='review',
name='rating_recommended',
field=models.FloatField(default=0, null=True),
),
migrations.AlterField(
model_name='review',
name='rating_understandable',
field=models.FloatField(default=0, null=True),
),
]
26 changes: 25 additions & 1 deletion main/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Profile(models.Model):
role = models.CharField(max_length=63)
org_code = models.CharField(max_length=63)
is_blocked = models.BooleanField(default=False)

likes_count = models.PositiveIntegerField(default=0, null=True)

class Review(models.Model):
"""
Expand All @@ -69,6 +69,12 @@ class HateSpeechStatus(models.TextChoices):
sentimen = models.PositiveSmallIntegerField(null=True)
is_anonym = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
is_reviewed = models.BooleanField(default=False)
rating_understandable = models.FloatField(null=True, default=0)
rating_fit_to_credit = models.FloatField(null=True, default=0)
rating_fit_to_study_book = models.FloatField(null=True, default=0)
rating_beneficial = models.FloatField(null=True, default=0)
rating_recommended = models.FloatField(null=True, default=0)

def save(self, *args, **kwargs):
''' On save, update timestamps '''
Expand All @@ -93,3 +99,21 @@ class Bookmark(models.Model):
user = models.ForeignKey(Profile, on_delete=CASCADE)
course = models.ForeignKey(Course, on_delete=CASCADE)

class Calculator(models.Model):
"""
Calculator for course score
"""
user = models.ForeignKey(Profile, on_delete=CASCADE)
course = models.ForeignKey(Course, on_delete=CASCADE)
total_score = models.FloatField(default=0)
total_percentage = models.FloatField(default=0)

class ScoreComponent(models.Model):
"""
Score component for calculator
"""
calculator = models.ForeignKey(Calculator, on_delete=CASCADE)
name = models.TextField()
weight = models.FloatField()
score = models.FloatField()

Loading

0 comments on commit 60b3a85

Please sign in to comment.