application.py 2.94 KB
Newer Older
1
from sqlalchemy import Column, Integer, String, PickleType, ForeignKey
2
from sqlalchemy.orm import relationship
3
4
5
6
7
8
9
10
11

from common.state import Base


class Application(Base):
    __tablename__ = 'applications'

    id = Column(Integer, primary_key=True)
    name = Column(String(64))
12
    required_resources = Column(PickleType())  # JSON resource description
13
14
    user_id = Column(Integer, ForeignKey('users.id'))

15
16
    executions = relationship("Execution", order_by="Execution.id", backref="application")

17
18
19
20
21
22
23
    type = Column(String(20))  # Needed by sqlalchemy to manage class inheritance

    __mapper_args__ = {
        'polymorphic_on': type,
        'polymorphic_identity': 'application'
    }

24
25
26
27
28
29
30
31
32
    def to_dict(self) -> dict:
        ret = {
            'id': self.id,
            'name': self.name,
            'required_resources': self.required_resources.__dict__.copy(),
            'user_id': self.user_id
        }
        return ret

33
34
35
36
    def __repr__(self):
        return "<Application(name='%s', id='%s', required_resourced='%s')>" % (
            self.name, self.id, self.required_resources)

37
38
39
40
41
42
43
    def executions_running(self):
        ret = []
        for e in self.executions:
            if e.status == "running":
                ret.append(e)
        return ret

44
45
46
47
48
49
50
51
52
53
    def extract(self):
        ret = PlainApplication()
        ret.id = self.id
        ret.name = self.name
        ret.required_resources = self.required_resources
        ret.user_id = self.user_id
        ret.executions = [x.id for x in self.executions]
        ret.type = self.type
        return ret

54
55
56
57
58
59
60
61

class SparkApplication(Application):
    master_image = Column(String(256))
    worker_image = Column(String(256))

    __mapper_args__ = {
        'polymorphic_identity': 'spark-application'
    }
62
63
64
65
66
67

    def to_dict(self) -> dict:
        ret = super().to_dict()
        ret["master_image"] = self.master_image
        ret["worker_image"] = self.worker_image
        return ret
68

69
70
71
72
73
74
    def extract(self):
        ret = super().extract()
        ret.master_image = self.master_image
        ret.worker_image = self.worker_image
        return ret

75
76
77
78
79
80
81
82
83
84
85
86
87

class SparkNotebookApplication(SparkApplication):
    notebook_image = Column(String(256))

    __mapper_args__ = {
        'polymorphic_identity': 'spark-notebook'
    }

    def to_dict(self) -> dict:
        ret = super().to_dict()
        ret["notebook_image"] = self.notebook_image
        return ret

88
89
90
91
92
    def extract(self):
        ret = super().extract()
        ret.notebook_image = self.notebook_image
        return ret

93
94
95
96
97
98
99
100
101
102
103
104

class SparkSubmitApplication(SparkApplication):
    submit_image = Column(String(256))

    __mapper_args__ = {
        'polymorphic_identity': 'spark-submit'
    }

    def to_dict(self) -> dict:
        ret = super().to_dict()
        ret["submit_image"] = self.submit_image
        return ret
105
106
107
108
109
110
111
112
113

    def extract(self):
        ret = super().extract()
        ret.submit_image = self.submit_image
        return ret


class PlainApplication:
    pass