-
Notifications
You must be signed in to change notification settings - Fork 73
/
basic_filter.py
202 lines (161 loc) · 6.47 KB
/
basic_filter.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
import datetime as dt
from medallion.utils import common
class BasicFilter(object):
def __init__(self, filter_args):
self.filter_args = filter_args
@staticmethod
def _belongs_in_class(c, obj):
return c[0]["id"] == obj["id"]
@staticmethod
def _equivalence_partition_by_id(initial_results):
classes = []
for o in initial_results: # for each object
# find the class it is in
found = False
for c in classes:
if BasicFilter._belongs_in_class(c, o): # is it equivalent to this class?
c.append(o)
found = True
break
if not found: # it is in a new class
classes.append([o])
return classes
@staticmethod
def filter_by_id(data, id_):
id_ = id_.split(",")
match_objects = []
for obj in data:
if "id" in obj and any(s == obj["id"] for s in id_):
match_objects.append(obj)
return match_objects
@staticmethod
def filter_by_version(data, version):
# There can be more than one filter, using v_filter for looping through
# each filter. For example ?match[version]=first,last
match_objects = []
version_indicators = version.split(",")
if "all" in version_indicators:
# if "all" is in the list, just return everything
return data
actual_dates = [x for x in version_indicators if (x != "first" and x != "last")]
first = last = None
t_first = t_last = None
for obj in data:
time_of_obj = dt.datetime.strptime(obj["modified"], "%Y-%m-%dT%H:%M:%S.%fZ")
if first is None:
first = last = obj
t_first = time_of_obj
t_last = time_of_obj
else:
if time_of_obj < t_first:
first = obj
t_first = time_of_obj
elif time_of_obj > t_last:
last = obj
t_last = time_of_obj
if obj["modified"] in actual_dates:
match_objects.append(obj)
if "first" in version_indicators:
match_objects.append(first)
if "last" in version_indicators:
match_objects.append(last)
return match_objects
@staticmethod
def is_manifest_entry(x):
return "modified" not in x
@staticmethod
def filter_manifest_entries_by_version(data, version):
match_objects = []
version_indicators = version.split(",")
if "all" in version_indicators:
# if "all" is in the list, just return everything
return data
actual_dates = [x for x in version_indicators if (x != "first" and x != "last")]
for obj in data:
versions_returned = []
first = last = None
t_first = t_last = None
for t in obj["versions"]:
timestamp = dt.datetime.strptime(t, "%Y-%m-%dT%H:%M:%S.%fZ")
if first is None:
first = last = t
t_first = timestamp
t_last = timestamp
else:
if timestamp < t_first:
first = t
t_first = timestamp
elif timestamp > t_last:
last = t
t_last = timestamp
if t in actual_dates:
versions_returned.append(t)
if "first" in version_indicators:
versions_returned.append(first)
if "last" in version_indicators:
versions_returned.append(last)
if versions_returned:
obj["versions"] = versions_returned
match_objects.append(obj)
return match_objects
@staticmethod
def filter_by_type(data, type_):
type_ = type_.split(",")
match_objects = []
for obj in data:
if "type" in obj and any(s == obj["type"] for s in type_):
match_objects.append(obj)
elif "id" in obj and any(s == obj["id"].split("--")[0] for s in type_):
match_objects.append(obj)
return match_objects
def process_filter(self, data, allowed, manifest_info):
filtered_by_type = []
filtered_by_id = []
match_type = self.filter_args.get("match[type]")
if match_type and "type" in allowed:
filtered_by_type = self.filter_by_type(data, match_type)
match_id = self.filter_args.get("match[id]")
if match_id and "id" in allowed:
filtered_by_id = self.filter_by_id(data, match_id)
results = []
if filtered_by_type and filtered_by_id:
for type_match in filtered_by_type:
for id_match in filtered_by_id:
if type_match == id_match:
results.append(type_match)
elif match_type:
if filtered_by_type:
results.extend(filtered_by_type)
elif match_id:
if filtered_by_id:
results.extend(filtered_by_id)
else:
results = data
match_version = self.filter_args.get("match[version]")
if "version" in allowed:
if not match_version:
match_version = "last"
if self.is_manifest_entry(data[0]):
results = self.filter_manifest_entries_by_version(results, match_version)
else:
new_results = []
for bucket in BasicFilter._equivalence_partition_by_id(results):
new_results.extend(self.filter_by_version(bucket, match_version))
results = new_results
added_after_date = self.filter_args.get("added_after")
if added_after_date:
added_after_timestamp = common.convert_to_stix_datetime(added_after_date)
new_results = []
for obj in results:
info = None
for item in manifest_info:
if item["id"] == obj["id"]:
info = item
break
if info:
added_date_timestamp = common.convert_to_stix_datetime(info["date_added"])
if added_date_timestamp > added_after_timestamp:
new_results.append(obj)
return new_results
else:
return results