Fakultas Ilmu Komputer UI

Commit f9774e93 authored by I Gusti Putu Agastya Indrayana's avatar I Gusti Putu Agastya Indrayana
Browse files

Addes api for generating dummy data.

parent 59d87955
from django.urls import path
from administration.views import VerificationView, DetailVerificationView, VerificationSettingView, CategorySettingView, KelolaKontributorView, ProfileContributorAdminView, ProfileAdminAdministrationView, KelolaAdminView, RegistrasiAdminView, EditVerificationView, delete_admin, delete_contributor, delete_verification, StatisticsView, StatisticApiView, EditCategoryView, delete_category
from administration.views import VerificationView, DetailVerificationView, VerificationSettingView, CategorySettingView, KelolaKontributorView, ProfileContributorAdminView, ProfileAdminAdministrationView, KelolaAdminView, RegistrasiAdminView, EditVerificationView, delete_admin, delete_contributor, delete_verification, StatisticsView, StatisticApiView, EditCategoryView, delete_category, generatedummy
app_name = "administration"
......@@ -24,4 +24,5 @@ urlpatterns = [
path("kelola-admin/tambah/", RegistrasiAdminView.as_view()),
path("hapus-admin/<int:pk>/", delete_admin),
path("hapus-kontributor/<int:pk>/", delete_contributor),
path("generate-dummy", generatedummy),
]
......@@ -17,6 +17,8 @@ from datetime import datetime
from administration.utils import generate_time_step
from django.core import management
def get_start_end_date(period):
if period == 'ALL_TIME':
......@@ -578,3 +580,17 @@ def delete_category(request, *args, **kwargs):
messages.success(request, "Kategori " +
category_name + " berhasil dihapus")
return HttpResponseRedirect("/administration/setting/category/")
def generatedummy(request):
if request.user.is_authenticated is False or request.user.is_admin is False:
return PermissionDenied(request)
materi = request.GET.get("materi", None)
visitor = request.GET.get("visitor", None)
if materi is None or visitor is None:
return JsonResponse({"success":False})
materi = int(materi)
visitor = int(visitor)
# RUn management command
management.call_command('generatedummy', materi)
management.call_command('generatetraffic', visitor)
return JsonResponse({"success":True,"materi":materi,"visitor":visitor})
\ No newline at end of file
......@@ -76,7 +76,7 @@ class Command(BaseCommand):
user = User(email=email, name=name, is_admin=True)
user.set_password(name)
user.date_joined = get_time_before(
timezone.now(), (365*SECONDS_IN_DAY))
timezone.now(), (180*SECONDS_IN_DAY))
user.save()
dummy_user.append(user)
return dummy_user
......
......@@ -64,16 +64,16 @@ class Command(BaseCommand):
s_date = materi_published_date[0]
s_date = s_date.replace(day=s_date.day+1, hour=0,
minute=0, second=0, microsecond=0)
reports = []
# reports = []
while (s_date < today):
report = {
"s_date": s_date,
"visit": 0,
"view": 0,
"download": 0,
"like": 0,
"comment": 0,
}
# report = {
# "s_date": s_date,
# "visit": 0,
# "view": 0,
# "download": 0,
# "like": 0,
# "comment": 0,
# }
today_materi = [i for i in materi if i.published_date < s_date]
visiting_user = options["baseline"]
visiting_user += int(options["coef_time"] * r_day) + \
......@@ -82,8 +82,8 @@ class Command(BaseCommand):
(1 + uniform(-options["coef_visit_range"],
options["coef_visit_range"])))
active_user = int(visiting_user * options["coef_read"])
report["visit"] = visiting_user
report["view"] = active_user
# report["visit"] = visiting_user
# report["view"] = active_user
times = generate_list_of_random_datetime(
s_date, s_date+timedelta(days=1), active_user)
for timestamp in times:
......@@ -91,23 +91,23 @@ class Command(BaseCommand):
self._view_materi(timestamp, selected_materi)
if random() < options["coef_download"]:
self._download_materi(timestamp, selected_materi)
report["download"] += 1
# report["download"] += 1
if random() < options["coef_like"]:
self._like_materi(timestamp, selected_materi)
report["like"] += 1
# report["like"] += 1
if random() < options["coef_comment"]:
self._comment_materi(timestamp, selected_materi)
report["comment"] += 1
# report["comment"] += 1
s_date = s_date + timedelta(days=1)
r_day += 1
reports.append(report)
# reports.append(report)
# for i in range
for i in reports:
self.stdout.write(self.style.SUCCESS(f"Today is {i['s_date']}"))
self.stdout.write(self.style.SUCCESS(f"User visit {i['visit']}"))
self.stdout.write(self.style.SUCCESS(f"User view {i['view']}"))
self.stdout.write(self.style.SUCCESS(
f"User download {i['download']}"))
self.stdout.write(self.style.SUCCESS(f"User like {i['like']}"))
self.stdout.write(self.style.SUCCESS(
f"User comment {i['comment']}"))
# for i in reports:
# self.stdout.write(self.style.SUCCESS(f"Today is {i['s_date']}"))
# self.stdout.write(self.style.SUCCESS(f"User visit {i['visit']}"))
# self.stdout.write(self.style.SUCCESS(f"User view {i['view']}"))
# self.stdout.write(self.style.SUCCESS(
# f"User download {i['download']}"))
# self.stdout.write(self.style.SUCCESS(f"User like {i['like']}"))
# self.stdout.write(self.style.SUCCESS(
# f"User comment {i['comment']}"))
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment