summaryrefslogtreecommitdiffstats
path: root/app/messages/message.py
blob: e940054a8feffb773d7f459516b2edc8d3c7d8d7 (plain)
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
###############################################################################
# Copyright (c) 2017 Koren Lev (Cisco Systems), Yaron Yogev (Cisco Systems)   #
# and others                                                                  #
#                                                                             #
# All rights reserved. This program and the accompanying materials            #
# are made available under the terms of the Apache License, Version 2.0       #
# which accompanies this distribution, and is available at                    #
# http://www.apache.org/licenses/LICENSE-2.0                                  #
###############################################################################
import datetime
from typing import Union

from bson import ObjectId


class Message:

    LEVELS = ['info', 'warn', 'error']
    DEFAULT_LEVEL = LEVELS[0]

    def __init__(self,
                 msg_id: str,
                 msg: dict,
                 source: str,
                 env: str = None,
                 object_id: Union[str, ObjectId] = None,
                 display_context: Union[str, ObjectId] = None,
                 level: str = DEFAULT_LEVEL,
                 object_type: str = None,
                 ts: datetime = None,
                 received_ts: datetime = None,
                 finished_ts: datetime = None):
        super().__init__()

        if level and level.lower() in self.LEVELS:
            self.level = level.lower()
        else:
            self.level = self.DEFAULT_LEVEL

        self.id = msg_id
        self.environment = env
        self.source_system = source
        self.related_object = object_id
        self.related_object_type = object_type
        self.display_context = display_context
        self.message = msg
        self.timestamp = ts if ts else received_ts
        self.received_timestamp = received_ts
        self.finished_timestamp = finished_ts
        self.viewed = False

    def get(self):
        return {
            "id": self.id,
            "environment": self.environment,
            "source_system": self.source_system,
            "related_object": self.related_object,
            "related_object_type": self.related_object_type,
            "display_context": self.display_context,
            "level": self.level,
            "message": self.message,
            "timestamp": self.timestamp,
            "received_timestamp": self.received_timestamp,
            "finished_timestamp": self.finished_timestamp,
            "viewed": self.viewed
        }