This repository was archived by the owner on Nov 8, 2022. It is now read-only.
forked from GoogleCloudPlatform/professional-services
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatic_data.py
More file actions
215 lines (182 loc) · 6.55 KB
/
static_data.py
File metadata and controls
215 lines (182 loc) · 6.55 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
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
# Copyright 2020 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
import os
import random
from typing import Any, Dict, Iterable, List, Tuple
import apache_beam as beam
from apache_beam.transforms import window
@beam.typehints.with_input_types(str)
@beam.typehints.with_output_types(Iterable[Dict[str, Any]])
class ReadStaticSideInput(beam.PTransform):
"""Helper Dofn function that generates static side input data and can be used in to validate the functionality
instead of reading side input data from persistent storage such as GCS/LocalFileSystem
Attributes:
products: Static list of products that matches with product names from the main input
"""
def __init__(self, coder=None):
self.products: List[str] = [
"Product 2", "Product 2 XL", "Product 3", "Product 3 XL",
"Product 4", "Product 4 XL", "Product 5", "Product 5 XL"
]
#print("I am in ReadStaticSideInput")
def _get_lookup_values(self,
sideinput_filepath: str) -> List[Dict[str, Any]]:
"""Creates static lookup values based on side input type
Args:
sideinput_filepath: Side input file path with leaf name representing side input type
Returns:
List of look up values
"""
sideinput_type = os.path.basename(os.path.dirname(sideinput_filepath))
if sideinput_type == "bonuspoints":
lookup_values = [122, 245, 332, 564, 341, 654, 109, 265]
elif sideinput_type == "discountpct":
lookup_values = [0.23, 0.56, 0.17, 0.15, 0.06, 0.28, 0.32, 0.18]
else:
lookup_values = [
"tables", "chairs", "couch", "shoes", "electronics", "mobile",
"printer", "laptop"
]
return [{
"productname": product,
sideinput_type: lookup_values[index]
} for index, product in enumerate(self.products)]
def expand(self, pcol):
"""Fetch files based on the file pattern, read the contents from matching files
Args:
pcol: PCollection containing list of file patterns
Returns:
PCollection of type Json objects that represents data read from the files
"""
# yapf: disable
return (pcol
| "Static Side Input Values" >> beam.FlatMap(self._get_lookup_values)
)
# yapf: enable
def get_events() -> List[Dict]:
"""Represents set of sales events"""
return [{
"Txid": 1,
"productname": "Product 2",
"qty": 2,
"sales": 489.5
}, {
"Txid": 2,
"productname": "Product 3 XL",
"qty": 2,
"sales": 411.8
}, {
"Txid": 3,
"productname": "Product 4",
"qty": 2,
"sales": 56.15
}, {
"Txid": 4,
"productname": "Product 4 XL",
"qty": 5,
"sales": 197.7
}, {
"Txid": 5,
"productname": "Product 3",
"qty": 7,
"sales": 222.3
}]
def get_maininput_events() -> List[bytes]:
"""Returns sales events encoded as bytes"""
return list(map(lambda x: json.dumps(x).encode(), get_events()))
def get_expected_enriched_events() -> List[Dict]:
"""Returns enriched sales events with appropriate look up values"""
return [{
'Txid': 1,
'productname': 'Product 2',
'qty': 2,
'sales': 489.5,
'bonuspoints': 122,
'discountpct': 0.23,
'category': 'tables'
}, {
'Txid': 2,
'productname': 'Product 3 XL',
'qty': 2,
'sales': 411.8,
'bonuspoints': 564,
'discountpct': 0.15,
'category': 'shoes'
}, {
'Txid': 3,
'productname': "Product 4",
"qty": 2,
"sales": 56.15,
'bonuspoints': 341,
'discountpct': 0.06,
'category': 'electronics'
}, {
'Txid': 4,
'productname': "Product 4 XL",
"qty": 5,
"sales": 197.7,
'bonuspoints': 654,
'discountpct': 0.28,
'category': 'mobile'
}, {
'Txid': 5,
'productname': "Product 3",
"qty": 7,
"sales": 222.3,
'bonuspoints': 332,
'discountpct': 0.17,
'category': 'couch'
}]
def get_windowedevents(
window_intervals: Tuple[window.IntervalWindow, window.IntervalWindow]
) -> List[window.TimestampedValue]:
"""Split the total events into 2 windows (1st window contains 2 events and second window with 3 events) and
attach the timestamp to the elements withing that range.
Args:
window_intervals: Two sample intervals to distribute events
Returns:
List of Timestamped events distributed between two window intervals
"""
events = get_maininput_events()
windowed_events = []
event_startindex = 0
for window_interval, event_endindex in zip(window_intervals,
(2, len(events))):
windowed_events.extend([
beam.window.TimestampedValue(
event,
random.randrange(window_interval.start, window_interval.end))
for event in events[event_startindex:event_endindex]
])
event_startindex = event_endindex
return windowed_events
def get_expected_enriched_windowevents(
window_intervals: Tuple[window.IntervalWindow, window.IntervalWindow]):
"""Split the enriched events into 2 windows (1st window contains 2 events and second window with 3 events)
Args:
window_intervals: Two sample intervals to distribute events
Returns:
List of enriched events distributed between two window intervals
"""
events = get_expected_enriched_events()
windowed_enriched_events = {}
event_startindex = 0
for window_interval, event_endindex in zip(window_intervals,
(2, len(events))):
windowed_enriched_events[window_interval] = [
events[event_startindex:event_endindex]
]
event_startindex = event_endindex
return windowed_enriched_events