aboutsummaryrefslogtreecommitdiff
path: root/jb/flow/monitoring.py
blob: 28f7271dd1b79372a1a901512b01c6b0ef01a8d7 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import socket
from typing import Optional

from mypy_boto3_mturk.literals import EventTypeType

from jb.config import settings
from jb.decorators import influx_client
from generalresearchutils.currency import USDCent
from jb.models.definitions import HitStatus, AssignmentStatus


def write_hit_gauge(status: HitStatus, amt_hit_type_id: str, cnt: int) -> None:

    tags = {
        "host": socket.gethostname(),  # could be "amt-jb-0"
        "service": "amt-jb",
        "status": status.value,
        "amt_hit_type_id": amt_hit_type_id,
        "debug": settings.debug,
    }
    point = {
        "measurement": "amt_jb.hits",
        "tags": tags,
        "fields": {"count": cnt},
    }
    if influx_client:
        influx_client.write_points(points=[point])

    return None


def write_assignment_gauge(
    status: AssignmentStatus, amt_hit_type_id: str, cnt: int
) -> None:
    tags = {
        "host": socket.gethostname(),
        "service": "amt-jb",
        "status": status.value,
        "amt_hit_type_id": amt_hit_type_id,
        "debug": settings.debug,
    }
    point = {
        "measurement": "amt_jb.assignments",
        "tags": tags,
        "fields": {"count": cnt},
    }
    if influx_client:
        influx_client.write_points(points=[point])

    return None


def emit_hit_event(
    status: HitStatus, amt_hit_type_id: str, reason: Optional[str] = None
) -> None:
    """
    e.g. a HIT was created, Reviewable, etc. We don't have a "created"
    HitStatus, so it would just be when status=='Assignable'
    """
    tags = {
        "host": socket.gethostname(),
        "service": "amt-jb",
        "status": status.value,
        "amt_hit_type_id": amt_hit_type_id,
        "debug": settings.debug,
    }

    if reason:
        tags["reason"] = reason

    point = {
        "measurement": "amt_jb.hit_events",
        "tags": tags,
        "fields": {"value": 1},
    }

    if influx_client:
        influx_client.write_points([point])

    return None


def emit_assignment_event(
    status: AssignmentStatus, amt_hit_type_id: str, reason: Optional[str] = None
) -> None:
    """
    e.g. an Assignment was accepted/approved/reject
    """
    tags = {
        "host": socket.gethostname(),
        "service": "amt-jb",
        "status": status.value,
        "amt_hit_type_id": amt_hit_type_id,
        "debug": settings.debug,
    }

    if reason:
        tags["reason"] = reason

    point = {
        "measurement": "amt_jb.assignment_events",
        "tags": tags,
        "fields": {"value": 1},
    }

    if influx_client:
        influx_client.write_points([point])

    return None


def emit_mturk_notification_event(
    event_type: EventTypeType, amt_hit_type_id: str
) -> None:
    """
    e.g. a Mturk notification was received. We just put it in redis, we
    haven't processed it yet.
    """
    tags = {
        "host": socket.gethostname(),
        "service": "amt-jb",
        "event_type": event_type,
        "amt_hit_type_id": amt_hit_type_id,
        "debug": settings.debug,
    }

    point = {
        "measurement": "amt_jb.mturk_notification_events",
        "tags": tags,
        "fields": {"value": 1},
    }

    if influx_client:
        influx_client.write_points([point])

    return None


def emit_error_event(event_type: str, amt_hit_type_id: str) -> None:
    """
    e.g. todo: structure the error_types
    """
    tags = {
        "host": socket.gethostname(),
        "service": "amt-jb",
        "event_type": event_type,
        "amt_hit_type_id": amt_hit_type_id,
        "debug": settings.debug,
    }

    point = {
        "measurement": "amt_jb.error_events",
        "tags": tags,
        "fields": {"value": 1},
    }

    if influx_client:
        influx_client.write_points([point])

    return None


def emit_bonus_event(amount: USDCent, amt_hit_type_id: str) -> None:
    """
    An AMT bonus was awarded
    """
    tags = {
        "host": socket.gethostname(),
        "service": "amt-jb",
        "amt_hit_type_id": amt_hit_type_id,
        "debug": settings.debug,
    }

    point = {
        "measurement": "amt_jb.bonus_events",
        "tags": tags,
        "fields": {"value": 1, "amount": int(amount)},
    }

    if influx_client:
        influx_client.write_points([point])

    return None