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 | from __future__ import absolute_import |
||
17 | import hashlib |
||
18 | |||
19 | import mongoengine as me |
||
20 | |||
21 | from st2common.models.db import stormbase |
||
22 | from st2common.fields import ComplexDateTimeField |
||
23 | from st2common.util import date as date_utils |
||
24 | from st2common.constants.types import ResourceType |
||
25 | |||
26 | from st2common.models.db import MongoDBAccess |
||
27 | |||
28 | __all__ = [ |
||
29 | 'TraceDB', |
||
30 | 'TraceComponentDB' |
||
31 | ] |
||
32 | |||
33 | |||
34 | class TraceComponentDB(me.EmbeddedDocument): |
||
0 ignored issues
–
show
Documentation
introduced
by
Loading history...
|
|||
35 | """ |
||
36 | """ |
||
37 | object_id = me.StringField() |
||
38 | ref = me.StringField(default='') |
||
39 | updated_at = ComplexDateTimeField( |
||
40 | default=date_utils.get_datetime_utc_now, |
||
41 | help_text='The timestamp when the TraceComponent was included.') |
||
42 | caused_by = me.DictField(help_text='Causal component.') |
||
43 | |||
44 | def __str__(self): |
||
45 | return 'TraceComponentDB@(object_id:{}, updated_at:{})'.format( |
||
46 | self.object_id, self.updated_at) |
||
47 | |||
48 | |||
49 | class TraceDB(stormbase.StormFoundationDB, stormbase.UIDFieldMixin): |
||
50 | """ |
||
51 | Trace is a collection of all TriggerInstances, Rules and ActionExecutions |
||
52 | that represent an activity which begins with the introduction of a |
||
53 | TriggerInstance or request of an ActionExecution and ends with the |
||
54 | completion of an ActionExecution. Given the closed feedback look sort of |
||
55 | nature of StackStorm this implies a Trace can comprise of multiple |
||
56 | TriggerInstances, Rules and ActionExecutions. |
||
57 | |||
58 | :param trace_tag: A user specified reference to the trace. |
||
59 | |||
60 | :param trigger_instances: TriggerInstances associated with this trace. |
||
61 | |||
62 | :param rules: Rules associated with this trace. |
||
63 | |||
64 | :param action_executions: ActionExecutions associated with this trace. |
||
65 | """ |
||
66 | |||
67 | RESOURCE_TYPE = ResourceType.TRACE |
||
68 | |||
69 | trace_tag = me.StringField(required=True, |
||
70 | help_text='A user specified reference to the trace.') |
||
71 | trigger_instances = me.ListField(field=me.EmbeddedDocumentField(TraceComponentDB), |
||
72 | required=False, |
||
73 | help_text='Associated TriggerInstances.') |
||
74 | rules = me.ListField(field=me.EmbeddedDocumentField(TraceComponentDB), |
||
75 | required=False, |
||
76 | help_text='Associated Rules.') |
||
77 | action_executions = me.ListField(field=me.EmbeddedDocumentField(TraceComponentDB), |
||
78 | required=False, |
||
79 | help_text='Associated ActionExecutions.') |
||
80 | start_timestamp = ComplexDateTimeField(default=date_utils.get_datetime_utc_now, |
||
81 | help_text='The timestamp when the Trace was created.') |
||
82 | |||
83 | meta = { |
||
84 | 'indexes': [ |
||
85 | {'fields': ['trace_tag']}, |
||
86 | {'fields': ['start_timestamp']}, |
||
87 | {'fields': ['action_executions.object_id']}, |
||
88 | {'fields': ['trigger_instances.object_id']}, |
||
89 | {'fields': ['rules.object_id']}, |
||
90 | {'fields': ['-start_timestamp', 'trace_tag']}, |
||
91 | ] |
||
92 | } |
||
93 | |||
94 | def __init__(self, *args, **values): |
||
95 | super(TraceDB, self).__init__(*args, **values) |
||
96 | self.uid = self.get_uid() |
||
97 | |||
98 | def get_uid(self): |
||
99 | parts = [] |
||
100 | parts.append(self.RESOURCE_TYPE) |
||
101 | |||
102 | componenets_hash = hashlib.md5() |
||
103 | componenets_hash.update(str(self.trace_tag).encode()) |
||
104 | componenets_hash.update(str(self.trigger_instances).encode()) |
||
105 | componenets_hash.update(str(self.rules).encode()) |
||
106 | componenets_hash.update(str(self.action_executions).encode()) |
||
107 | componenets_hash.update(str(self.start_timestamp).encode()) |
||
108 | |||
109 | parts.append(componenets_hash.hexdigest()) |
||
110 | |||
111 | uid = self.UID_SEPARATOR.join(parts) |
||
112 | return uid |
||
113 | |||
114 | |||
115 | # specialized access objects |
||
116 | trace_access = MongoDBAccess(TraceDB) |
||
117 | |||
118 | MODELS = [TraceDB] |
||
119 |