-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathbase.py
More file actions
184 lines (141 loc) · 6.3 KB
/
base.py
File metadata and controls
184 lines (141 loc) · 6.3 KB
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
import datetime
from functools import reduce
import pandas as pd
from dateutil.relativedelta import relativedelta
from metrics_utility.dataframe_schema import DataframeSchemaMixin
def granularity_cast(date, granularity):
if granularity == 'monthly':
return date.replace(day=1)
elif granularity == 'yearly':
return date.replace(month=1, day=1)
else:
return date
def list_dates(start_date, end_date, granularity):
# Given start date and end date, return list of dates in the given granularity
# e.g. for daily it is a list of days withing the interval, for monthly it is a
# list of months withing the interval, etc.
start_date = granularity_cast(start_date, granularity)
end_date = granularity_cast(end_date, granularity)
dates_arr = []
while start_date < end_date:
dates_arr.append(start_date)
if granularity == 'monthly':
start_date += relativedelta(months=+1)
elif granularity == 'yearly':
start_date += relativedelta(years=+1)
else:
start_date += datetime.timedelta(days=1)
dates_arr.append(end_date)
return dates_arr
# For JSON/dict columns: update one dict with the other (later values overwrite earlier ones)
def combine_json(json1, json2):
merged = {}
if isinstance(json1, dict):
merged.update(json1)
if isinstance(json2, dict):
merged.update(json2)
return merged
# For set columns: take the union of the two sets
def combine_set(set1, set2):
"""
Combine two collections (set or list) into a single set of unique items.
If an input is a list, it is first converted to a set.
If an input is not a list or a set, it is treated as empty.
"""
# Convert to set if input is a list; otherwise, if not a set, default to an empty set.
if isinstance(set1, list):
set1 = set(set1)
elif not isinstance(set1, set):
set1 = set()
if isinstance(set2, list):
set2 = set(set2)
elif not isinstance(set2, set):
set2 = set()
# Return the union of both sets.
return set1.union(set2)
def merge_sets(x):
return set().union(*x)
def merge_setdicts(x):
return reduce(combine_json_values, x, {})
# Helper function to combine two JSON values.
# For each key, it builds a set of non-null, non-empty values from both inputs.
def combine_json_values(val1, val2):
merged = {}
for d in [val1, val2]:
if isinstance(d, dict):
for key, value in d.items():
if value is not None and value != '':
if isinstance(value, set):
merged.setdefault(key, set()).update(value)
else:
merged.setdefault(key, set()).add(value)
return merged
class Base(DataframeSchemaMixin):
def __init__(self, extractor, month, extra_params):
self.extractor = extractor
self.month = month
self.extra_params = extra_params
def build_dataframe(self):
return None
def dates(self):
if self.extra_params.get('since_date') is not None:
beginning_of_the_month = self.extra_params.get('since_date')
end_of_the_month = self.extra_params.get('until_date')
else:
beginning_of_the_month = self.month.replace(day=1)
end_of_the_month = beginning_of_the_month + relativedelta(months=1) - relativedelta(days=1)
dates_list = list_dates(start_date=beginning_of_the_month, end_date=end_of_the_month, granularity='daily')
return dates_list
def cast_dataframe(self, df, types):
levels = []
if len(self.unique_index_columns()) == 1:
# Special behavior if the index is not composite, but only 1 column
# Casting index field to object
df.index = df.index.astype(object)
else:
# Composite index branch
# Casting index field to object
for index, _level in enumerate(df.index.levels):
casted_level = df.index.levels[index].astype(object)
levels.append(casted_level)
df.index = df.index.set_levels(levels)
return df.astype(types)
def summarize_merged_dataframes(self, df, columns, operations={}):
for col in columns:
if operations.get(col) == 'min':
df[col] = df[[f'{col}_x', f'{col}_y']].min(axis=1)
elif operations.get(col) == 'max':
df[col] = df[[f'{col}_x', f'{col}_y']].max(axis=1)
elif operations.get(col) == 'combine_set':
df[col] = df.apply(lambda row, c=col: combine_set(row.get(f'{c}_x'), row.get(f'{c}_y')), axis=1)
elif operations.get(col) == 'combine_json':
df[col] = df.apply(lambda row, c=col: combine_json(row.get(f'{c}_x'), row.get(f'{c}_y')), axis=1)
elif operations.get(col) == 'combine_json_values':
df[col] = df.apply(lambda row, c=col: combine_json_values(row.get(f'{c}_x'), row.get(f'{c}_y')), axis=1)
else:
df[col] = df[[f'{col}_x', f'{col}_y']].sum(axis=1)
del df[f'{col}_x']
del df[f'{col}_y']
return df
def empty(self):
return pd.DataFrame(columns=self.unique_index_columns() + self.data_columns())
# Multipart collection, merge the dataframes and sum counts
def merge(self, rollup, new_group):
if rollup is None:
return new_group
rollup = pd.merge(rollup.loc[:,], new_group.loc[:,], on=self.unique_index_columns(), how='outer', validate='one_to_one')
rollup = self.summarize_merged_dataframes(rollup, self.data_columns(), operations=self.operations())
return self.cast_dataframe(rollup, self.cast_types())
def dedup(self, dataframe, hostname_mapping=None):
if dataframe is None or dataframe.empty:
return self.empty()
if not hostname_mapping:
return dataframe
# map hostnames to canonical value
df = dataframe.copy()
df['host_name'] = df['host_name'].map(hostname_mapping).fillna(df['host_name'])
# multiple rows can now have the same hostname, regroup
df_grouped = self.regroup(df)
# cast types to match the table
df_grouped = self.cast_dataframe(df_grouped, self.cast_types())
return df_grouped.reset_index()