Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat sql query tuning #73

Merged
merged 10 commits into from
Aug 1, 2024
Binary file modified requirements.txt
Binary file not shown.
Empty file.
95 changes: 95 additions & 0 deletions wtnt/team/management/commands/create_team.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
from django.core.management.base import BaseCommand, CommandParser, CommandError
from team.models import Team, TeamUser, TeamTech
from user.models import CustomUser
from django_seed import Seed
from faker import Faker

from itertools import cycle
import random
import uuid


class Command(BaseCommand):
def add_arguments(self, parser: CommandParser) -> None:
parser.add_argument("--number", default=1, type=int, help="추가하려고 하는 팀의 수, 최대 100개까지")

def handle(self, *args, **options):
faker = Faker("ko_KR")
number = options.get("number")
if number > 100:
raise CommandError("number의 최대 인풋은 100입니다.")
seeder = Seed.seeder()
genres = ["웹", "앱", "게임"]
leader_ids = [CustomUser.objects.order_by("?").first() for _ in range(number)]
leader_cycle = cycle(leader_ids)

seeder.add_entity(
Team,
number,
{
"leader": lambda x: next(leader_cycle),
"title": lambda x: faker.catch_phrase(),
"explain": lambda x: faker.catch_phrase().encode(),
"uuid": lambda x: uuid.uuid4(),
"genre": lambda x: random.choice(genres),
"like": 0,
"view": 0,
"version": 0,
"url": lambda x: faker.url().encode(),
"is_approved": True,
"is_accomplished": False,
"image": "https://wtnt-bucket.s3.ap-northeast-2.amazonaws.com/default/",
},
)

team_ids = seeder.execute()[Team]
team_objects = Team.objects.filter(id__in=team_ids)
team_cycle = cycle(team_objects)

seeder.add_entity(
TeamUser,
number,
{
"user": lambda x: next(leader_cycle),
"team": lambda x: next(team_cycle),
"tech": "팀장",
},
)

seeder.execute()

categories = [
"웹",
"IOS",
"안드로이드",
"크로스플랫폼",
"자바",
"파이썬",
"노드",
"UI/UX 기획",
"게임 기획",
"컨텐츠 기획",
"프로젝트 매니저",
"유니티",
"언리얼",
"딥러닝",
"머신러닝",
"데이터 엔지니어",
"게임 그래픽 디자인",
"UI/UX 디자인",
]

seeder.add_entity(
TeamTech,
number,
{
"current_num": 0,
"need_num": lambda x: random.randint(1, 10),
"team": lambda x: next(team_cycle),
"tech": lambda x: random.choice(categories),
},
)

seeder.execute()

self.stdout.write(self.style.SUCCESS(f"{number}개의 더미 팀 생성 완료. 팀 ID: {team_ids}"))
45 changes: 45 additions & 0 deletions wtnt/team/management/commands/create_team_apply.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from django.core.management.base import BaseCommand, CommandParser, CommandError
from django.db import IntegrityError
from team.models import TeamApply, Team, TeamTech
from user.models import CustomUser
from django_seed import Seed

import random


class Command(BaseCommand):
def add_arguments(self, parser: CommandParser) -> None:
parser.add_argument("--number", default=1, type=int, help="추가하려고 하는 가입 신청의 수, 최대 100개까지")

def handle(self, *args, **options):
number = options.get("number")
if number > 100:
raise CommandError("number의 최대 인풋은 100입니다.")
seeder = Seed.seeder()

for _ in range(number):
team = Team.objects.order_by("?").first()
user = CustomUser.objects.order_by("?").first()
tech_list = TeamTech.objects.filter(team_id=team.id).values_list("tech", flat=True)
seeder.add_entity(
TeamApply,
1,
{
"team": team,
"user": user,
"bio": "열심히 하겠습니다",
"is_approved": False,
"tech": random.choice(tech_list),
},
)

try:
seeder.execute()
except IntegrityError:
print(f"중복된 데이터 발생, 예상 데이터 주입 개수 {number-1}개")
number -= 1
continue
if number:
self.stdout.write(self.style.SUCCESS(f"{number}개의 더미 팀 가입신청 생성 완료"))
else:
self.stdout.write(self.style.ERROR("데이터 주입 실패"))
44 changes: 44 additions & 0 deletions wtnt/team/management/commands/create_team_like.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from django.core.management.base import BaseCommand, CommandParser, CommandError
from django.db import IntegrityError
from team.models import Likes, Team
from user.models import CustomUser
from django_seed import Seed


class Command(BaseCommand):
def add_arguments(self, parser: CommandParser) -> None:
parser.add_argument("--number", default=1, type=int, help="추가하려고 하는 좋아요의 수, 최대 100개까지")

def handle(self, *args, **options):
number = options.get("number")
if number > 100:
raise CommandError("number의 최대 인풋은 100입니다.")
seeder = Seed.seeder()

for _ in range(number):
team = Team.objects.order_by("?").first()
user = CustomUser.objects.order_by("?").first()

seeder.add_entity(
Likes,
1,
{
"team": team,
"user": user,
},
)

try:
seeder.execute()
team.like += 1
team.version += 1
team.save()

except IntegrityError:
print(f"중복된 데이터 발생, 예상 데이터 주입 개수 {number-1}개")
number -= 1
continue
if number:
self.stdout.write(self.style.SUCCESS(f"{number}개의 더미 좋아요 생성 완료"))
else:
self.stdout.write(self.style.ERROR("데이터 주입 실패"))
75 changes: 75 additions & 0 deletions wtnt/team/management/commands/create_team_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
from django.core.management.base import BaseCommand, CommandParser, CommandError
from django.db import IntegrityError
from team.models import TeamApply, Team, TeamTech, TeamUser
from user.models import CustomUser
from django_seed import Seed

import random


class Command(BaseCommand):
def add_arguments(self, parser: CommandParser) -> None:
parser.add_argument("--number", default=1, type=int, help="추가하려고 하는 팀원의 수, 최대 100개까지")

def handle(self, *args, **options):
number = options.get("number")
if number > 100:
raise CommandError("number의 최대 인풋은 100입니다.")
seeder = Seed.seeder()

for _ in range(number):
team = Team.objects.order_by("?").first()
user = CustomUser.objects.order_by("?").first()
try:
team_user = TeamUser.objects.get(team_id=team.id, user_id=user.id)
if team_user is not None and team_user.tech == "팀장":
print(f"중복된 데이터 발생, 예상 데이터 주입 개수 {number-1}개")
number -= 1
continue
except TeamUser.DoesNotExist:
pass

tech_list = TeamTech.objects.filter(team_id=team.id).values_list("tech", flat=True)
tech = random.choice(tech_list)
team_tech = TeamTech.objects.get(team_id=team.id, tech=tech)

seeder.add_entity(
TeamApply,
1,
{
"team": team,
"user": user,
"bio": "열심히 하겠습니다",
"is_approved": True,
"tech": tech,
},
)

seeder.add_entity(
TeamUser,
1,
{
"team": team,
"user": user,
"tech": tech,
},
)

try:
if team_tech.need_num <= team_tech.current_num:
print(f"중복된 데이터 발생, 예상 데이터 주입 개수 {number-1}개")
number -= 1
continue
seeder.execute()
team_tech.current_num += 1
team_tech.save()

except IntegrityError:
print(f"중복된 데이터 발생, 예상 데이터 주입 개수 {number-1}개")
number -= 1
continue

if number:
self.stdout.write(self.style.SUCCESS(f"{number}개의 더미 팀원 생성 완료"))
else:
self.stdout.write(self.style.ERROR("데이터 주입 실패"))
3 changes: 3 additions & 0 deletions wtnt/team/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ class Likes(TimestampedModel):
user = models.ForeignKey(CustomUser, on_delete=models.CASCADE)
objects = LikesManager()

class Meta:
constraints = [models.UniqueConstraint(fields=["user", "team"], name="teamlike_unique")]


class TeamUser(models.Model):
team = models.ForeignKey(Team, on_delete=models.CASCADE)
Expand Down
Empty file.
41 changes: 41 additions & 0 deletions wtnt/user/management/commands/create_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from django.core.management.base import BaseCommand, CommandParser
from user.models import CustomUser
from django_seed import Seed
from faker import Faker
import random


class Command(BaseCommand):
def add_arguments(self, parser: CommandParser) -> None:
parser.add_argument("--number", default=1, type=int, help="추가하려고 하는 유저의 수")

def handle(self, *args, **options):
faker = Faker("ko_KR")
number = options.get("number")
seeder = Seed.seeder()
positions = ["백엔드", "프론트엔드", "AI", "디자이너"]

seeder.add_entity(
CustomUser,
number,
{
"email": lambda x: faker.unique.email(),
"social_id": lambda x: faker.unique.random_number(digits=9, fix_len=True),
"name": lambda x: faker.name(),
"student_num": lambda x: faker.unique.random_number(digits=9, fix_len=True),
"position": lambda x: random.choice(positions),
"explain": "열심히 하겠습니다!",
"university": "부경대학교",
"club": "WAP",
"password": "password",
"is_superuser": False,
"is_approved": True,
"is_active": True,
"is_staff": False,
"image": "default/",
},
)

seeder.execute()

self.stdout.write(self.style.SUCCESS(f"{number}명의 더미 유저 생성 완료."))
4 changes: 4 additions & 0 deletions wtnt/wtnt/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_AUTHENTICATION_METHOD = "email"

INTERNAL_IPS = ["127.0.0.1", "localhost"]

# Application definition

Expand All @@ -112,6 +113,8 @@
"celery",
"django_celery_beat",
"django_celery_results",
"debug_toolbar",
"django_seed",
]

MIDDLEWARE = [
Expand All @@ -126,6 +129,7 @@
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"allauth.account.middleware.AccountMiddleware",
"core.middleware.AttachJWTFromHeaderToCookieMiddleware",
"debug_toolbar.middleware.DebugToolbarMiddleware",
]

ROOT_URLCONF = "wtnt.urls"
Expand Down
8 changes: 8 additions & 0 deletions wtnt/wtnt/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.conf import settings
from django.contrib import admin
from django.urls import path, include, re_path

Expand Down Expand Up @@ -41,3 +42,10 @@
path("api/", include("admin.urls")),
path("admin/", admin.site.urls),
]

if settings.DEBUG:
import debug_toolbar

urlpatterns.append(
re_path(r"^__debug__/", include(debug_toolbar.urls)),
)