forked from OpenCTI-Platform/client-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopencti_api_work.py
235 lines (219 loc) · 7.44 KB
/
opencti_api_work.py
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import time
from typing import Dict, List
class OpenCTIApiWork:
"""OpenCTIApiJob"""
def __init__(self, api):
self.api = api
def to_received(self, work_id: str, message: str):
if self.api.bundle_send_to_queue:
self.api.app_logger.info(
"Reporting work update_received", {"work_id": work_id}
)
query = """
mutation workToReceived($id: ID!, $message: String) {
workEdit(id: $id) {
toReceived (message: $message)
}
}
"""
self.api.query(query, {"id": work_id, "message": message})
def to_processed(self, work_id: str, message: str, in_error: bool = False):
if self.api.bundle_send_to_queue:
self.api.app_logger.info(
"Reporting work update_processed", {"work_id": work_id}
)
query = """
mutation workToProcessed($id: ID!, $message: String, $inError: Boolean) {
workEdit(id: $id) {
toProcessed (message: $message, inError: $inError)
}
}
"""
self.api.query(
query, {"id": work_id, "message": message, "inError": in_error}
)
def ping(self, work_id: str):
self.api.app_logger.info("Ping work", {"work_id": work_id})
query = """
mutation pingWork($id: ID!) {
workEdit(id: $id) {
ping
}
}
"""
self.api.query(query, {"id": work_id})
def report_expectation(self, work_id: str, error):
if self.api.bundle_send_to_queue:
self.api.app_logger.info("Report expectation", {"work_id": work_id})
query = """
mutation reportExpectation($id: ID!, $error: WorkErrorInput) {
workEdit(id: $id) {
reportExpectation(error: $error)
}
}
"""
try:
self.api.query(query, {"id": work_id, "error": error})
except:
self.api.app_logger.error("Cannot report expectation")
def add_expectations(self, work_id: str, expectations: int):
if self.api.bundle_send_to_queue:
self.api.app_logger.info(
"Update action expectations",
{"work_id": work_id, "expectations": expectations},
)
query = """
mutation addExpectations($id: ID!, $expectations: Int) {
workEdit(id: $id) {
addExpectations(expectations: $expectations)
}
}
"""
try:
self.api.query(query, {"id": work_id, "expectations": expectations})
except:
self.api.app_logger.error("Cannot report expectation")
def initiate_work(self, connector_id: str, friendly_name: str) -> str:
if self.api.bundle_send_to_queue:
self.api.app_logger.info("Initiate work", {"connector_id": connector_id})
query = """
mutation workAdd($connectorId: String!, $friendlyName: String) {
workAdd(connectorId: $connectorId, friendlyName: $friendlyName) {
id
}
}
"""
work = self.api.query(
query, {"connectorId": connector_id, "friendlyName": friendly_name}
)
return work["data"]["workAdd"]["id"]
def delete_work(self, work_id: str):
query = """
mutation ConnectorWorksMutation($workId: ID!) {
workEdit(id: $workId) {
delete
}
}"""
work = self.api.query(
query,
{"workId": work_id},
)
return work["data"]
def wait_for_work_to_finish(self, work_id: str):
status = ""
cnt = 0
while status != "complete":
state = self.get_work(work_id=work_id)
if len(state) > 0:
status = state["status"]
if state["errors"]:
self.api.app_logger.error(
"Unexpected connector error", {"state_errors": state["errors"]}
)
return ""
time.sleep(1)
cnt += 1
def get_work(self, work_id: str) -> Dict:
query = """
query WorkQuery($id: ID!) {
work(id: $id) {
id
name
user {
name
}
timestamp
status
event_source_id
received_time
processed_time
completed_time
tracking {
import_expected_number
import_processed_number
}
messages {
timestamp
message
sequence
source
}
errors {
timestamp
message
sequence
source
}
}
}
"""
result = self.api.query(
query,
{"id": work_id},
)
return result["data"]["work"]
def get_connector_works(self, connector_id: str) -> List[Dict]:
query = """
query ConnectorWorksQuery(
$count: Int
$orderBy: WorksOrdering
$orderMode: OrderingMode
$filters: FilterGroup
) {
works(
first: $count
orderBy: $orderBy
orderMode: $orderMode
filters: $filters
) {
edges {
node {
id
name
user {
name
}
timestamp
status
event_source_id
received_time
processed_time
completed_time
tracking {
import_expected_number
import_processed_number
}
messages {
timestamp
message
sequence
source
}
errors {
timestamp
message
sequence
source
}
}
}
}
}
"""
result = self.api.query(
query,
{
"count": 50,
"filters": {
"mode": "and",
"filters": [{"key": "connector_id", "values": [connector_id]}],
"filterGroups": [],
},
},
)
result = result["data"]["works"]["edges"]
return_value = []
for node in result:
node = node["node"]
return_value.append(node)
return sorted(return_value, key=lambda i: i["timestamp"])