File size: 3,714 Bytes
3cecacc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from sqlalchemy import Column, Integer, Float, String, DateTime, Boolean, JSON, ForeignKey, Enum as SQLEnum
from enum import Enum
from sqlalchemy import Enum as SQLAlchemyEnum
from sqlalchemy.orm import relationship, backref
from datetime import datetime
from sqlalchemy.ext.declarative import declarative_base
from database import Base




class AnalysisStatus(str, Enum):
    PENDING = "pending"        # ๋ถ„์„ ๋Œ€๊ธฐ ์ค‘
    PROCESSING = "processing"  # ๋ถ„์„ ์ง„ํ–‰ ์ค‘
    COMPARING = "comparing"    # ๋ถ„์„ ๊ฒฐ๊ณผ ๋น„๊ต ์ค‘
    COMPLETED = "completed"    # ๋ถ„์„ ์™„๋ฃŒ
    FAILED = "failed"         # ๋ถ„์„ ์‹คํŒจ

class Music_Analysis(Base):  # Music ๊ฐ„ ๋ถ„์„ ๊ด€๊ณ„๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ํ…Œ์ด๋ธ”
    __tablename__ = "music_analysis"

    id = Column(Integer, primary_key=True, index=True)
    user_id = Column(Integer, ForeignKey("users.id"))
    user = relationship("User", back_populates="music_analysis")
    music1_id = Column(Integer, ForeignKey("musics.id"), nullable=False)  # ์ฒซ ๋ฒˆ์งธ Music
    music2_id = Column(Integer, ForeignKey("musics.id"), nullable=False)  # ๋‘ ๋ฒˆ์งธ Music

    analysis_results = relationship("Analysis_Result", back_populates="music_analysis", cascade="all, delete-orphan")
    compare_results = relationship("Compare_Result", back_populates="music_analysis", cascade="all, delete-orphan")
    music1 = relationship("Music", foreign_keys=[music1_id])  # ๊ด€๊ณ„ ์ •์˜
    music2 = relationship("Music", foreign_keys=[music2_id])  # ๊ด€๊ณ„ ์ •์˜
    requested_at = Column(DateTime, default=datetime.utcnow)  # ์š”์ฒญ ์‹œ๊ฐ„
    status = Column(SQLAlchemyEnum(AnalysisStatus), default=AnalysisStatus.COMPLETED)
    progress = Column(Float, default=0.0)  # 1๋Œ€1 ๋ถ„์„ ์ง„ํ–‰๋ฅ . ์•ผ๋งค๋กœ ๋„ฃ์Šต๋‹ˆ๋‹ค
    deleted = Column(Boolean, default=False)  # ์‚ญ์ œ ์—ฌ๋ถ€


    def __repr__(self):
        return f"<MusicAnalysis(id={self.id}, music1_id={self.music1_id}, music2_id={self.music2_id}')>"
    
    @property
    def user_name(self):
        return self.user.user_info.name if self.user and self.user.user_info else None

    @property
    def music1_title(self):
        return self.music1.title if self.music1 else None
    
    @property
    def music2_title(self):
        return self.music2.title if self.music2 else None
    
    def __repr__(self):
        return f"<MusicAnalysis(id={self.id}, music1_id={self.music1_id}, music2_id={self.music2_id}')>"

class Analysis_Result(Base):
    __tablename__ = "analysis_results"

    id = Column(Integer, primary_key=True, index=True)
    analysis_id = Column(Integer, ForeignKey("music_analysis.id"), nullable=False)  # Music_Analysis์™€์˜ ์™ธ๋ž˜ ํ‚ค ๊ด€๊ณ„
    music_analysis = relationship("Music_Analysis", back_populates="analysis_results")

    requested_at = Column(DateTime, default=datetime.utcnow)  # ์š”์ฒญ ์‹œ๊ฐ„
    music1_json_path = Column(String)  # ์ฒซ ๋ฒˆ์งธ Music JSON ํŒŒ์ผ ๊ฒฝ๋กœ
    music2_json_path = Column(String)  # ๋‘ ๋ฒˆ์งธ Music JSON ํŒŒ์ผ ๊ฒฝ๋กœ
    

    def __repr__(self):
        return f"<AnalysisResult(id={self.id}, analysis_id={self.analysis_id})>"

class Compare_Result(Base):
    __tablename__ = "compare_results"

    id = Column(Integer, primary_key=True, index=True)
    analysis_id = Column(Integer, ForeignKey("music_analysis.id"), nullable=False)  # Music_Analysis์™€์˜ ์™ธ๋ž˜ ํ‚ค ๊ด€๊ณ„
    music_analysis = relationship("Music_Analysis", back_populates="compare_results")  # ์—ฌ๊ธฐ๋ฅผ ์ˆ˜์ •

    requested_at = Column(DateTime, default=datetime.utcnow)  # ์š”์ฒญ ์‹œ๊ฐ„
    compare_result_path = Column(String)  # ๋ถ„์„ ๊ฒฐ๊ณผ ํŒŒ์ผ ๊ฒฝ๋กœ

    def __repr__(self):
        return f"<CompareResult(id={self.id}, music_analysis_id={self.analysis_id})>"