Passed
Push — master ( 1043ed...a36ace )
by W
07:49
created

TraceDB   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 64
rs 10
c 1
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __init__() 0 3 1
A get_uid() 0 15 1
1
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
2
# contributor license agreements.  See the NOTICE file distributed with
3
# this work for additional information regarding copyright ownership.
4
# The ASF licenses this file to You under the Apache License, Version 2.0
5
# (the "License"); you may not use this file except in compliance with
6
# the License.  You may obtain a copy of the License at
7
#
8
#     http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15
16
import hashlib
17
18
import mongoengine as me
19
20
from st2common.models.db import stormbase
21
from st2common.fields import ComplexDateTimeField
22
from st2common.util import date as date_utils
23
from st2common.constants.types import ResourceType
24
25
from st2common.models.db import MongoDBAccess
26
27
__all__ = [
28
    'TraceDB',
29
    'TraceComponentDB'
30
]
31
32
33
class TraceComponentDB(me.EmbeddedDocument):
0 ignored issues
show
Documentation introduced by
Empty class docstring
Loading history...
34
    """
35
    """
36
    object_id = me.StringField()
37
    ref = me.StringField(default='')
38
    updated_at = ComplexDateTimeField(
39
        default=date_utils.get_datetime_utc_now,
40
        help_text='The timestamp when the TraceComponent was included.')
41
    caused_by = me.DictField(help_text='Causal component.')
42
43
    def __str__(self):
44
        return 'TraceComponentDB@(object_id:{}, updated_at:{})'.format(
45
            self.object_id, self.updated_at)
46
47
48
class TraceDB(stormbase.StormFoundationDB, stormbase.UIDFieldMixin):
49
    """
50
    Trace is a collection of all TriggerInstances, Rules and ActionExecutions
51
    that represent an activity which begins with the introduction of a
52
    TriggerInstance or request of an ActionExecution and ends with the
53
    completion of an ActionExecution. Given the closed feedback look sort of
54
    nature of StackStorm this implies a Trace can comprise of multiple
55
    TriggerInstances, Rules and ActionExecutions.
56
57
    :param trace_tag: A user specified reference to the trace.
58
59
    :param trigger_instances: TriggerInstances associated with this trace.
60
61
    :param rules: Rules associated with this trace.
62
63
    :param action_executions: ActionExecutions associated with this trace.
64
    """
65
66
    RESOURCE_TYPE = ResourceType.TRACE
67
68
    trace_tag = me.StringField(required=True,
69
                               help_text='A user specified reference to the trace.')
70
    trigger_instances = me.ListField(field=me.EmbeddedDocumentField(TraceComponentDB),
71
                                     required=False,
72
                                     help_text='Associated TriggerInstances.')
73
    rules = me.ListField(field=me.EmbeddedDocumentField(TraceComponentDB),
74
                         required=False,
75
                         help_text='Associated Rules.')
76
    action_executions = me.ListField(field=me.EmbeddedDocumentField(TraceComponentDB),
77
                                     required=False,
78
                                     help_text='Associated ActionExecutions.')
79
    start_timestamp = ComplexDateTimeField(default=date_utils.get_datetime_utc_now,
80
                                           help_text='The timestamp when the Trace was created.')
81
82
    meta = {
83
        'indexes': [
84
            {'fields': ['trace_tag']},
85
            {'fields': ['start_timestamp']},
86
            {'fields': ['action_executions.object_id']},
87
            {'fields': ['trigger_instances.object_id']},
88
            {'fields': ['rules.object_id']},
89
            {'fields': ['-start_timestamp', 'trace_tag']},
90
        ]
91
    }
92
93
    def __init__(self, *args, **values):
94
        super(TraceDB, self).__init__(*args, **values)
95
        self.uid = self.get_uid()
96
97
    def get_uid(self):
98
        parts = []
99
        parts.append(self.RESOURCE_TYPE)
100
101
        componenets_hash = hashlib.md5()
102
        componenets_hash.update(str(self.trace_tag))
103
        componenets_hash.update(str(self.trigger_instances))
104
        componenets_hash.update(str(self.rules))
105
        componenets_hash.update(str(self.action_executions))
106
        componenets_hash.update(str(self.start_timestamp))
107
108
        parts.append(componenets_hash.hexdigest())
109
110
        uid = self.UID_SEPARATOR.join(parts)
111
        return uid
112
113
114
# specialized access objects
115
trace_access = MongoDBAccess(TraceDB)
116
117
MODELS = [TraceDB]
118