-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpubsub_dataflow.py
188 lines (162 loc) · 6.01 KB
/
pubsub_dataflow.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
# For Data Simulation
from faker import Faker
from faker.providers import DynamicProvider
import random
import numpy as np
import pandas as pd
import datetime
import time
from json import dumps
from google.cloud import pubsub_v1
import string
from concurrent.futures import TimeoutError
from google.cloud import pubsub_v1
data = []
def fraud_data_generation(n):
fake = Faker()
# User ID
orig = np.random.randint(10, 999999, n)
dest = np.random.randint(10, 999999, n)
# Amounts
amounts = np.random.randint(100, 100000, n)
# oldDest = np.random.randint(0, 10000, n)
# newDest = np.random.randint(0, 10000, round(n / 4))
for i in range(0, n):
type_provider = DynamicProvider(
provider_name = "type",
elements = ["CASH_IN", "CASH_OUT", "DEBIT", "PAYMENT", "TRANSFER"]
)
fake.add_provider(type_provider)
type_trans = fake.type()
amount = amounts[i]
# Old Balance Origin
oldAmount = random.randint(100, 100000)
if amounts[i] > oldAmount:
oldAmount = random.sample(range(amounts[i], 100000), 1)[0]
oldbalanceOrg = oldAmount
else:
oldbalanceOrg = oldAmount
# New Balance Origin
# newAmount = oldOrg[i] - amounts[i]
# newOrg.append(newAmount)
if type_trans == "CASH_IN":
newbalanceOrig = amount + oldbalanceOrg
oldbalanceDest = 0
else:
newbalanceOrig = oldbalanceOrg - amounts[i]
oldbalanceDest = random.randint(0, 10000)
# oldbalanceDest = oldDest[i]
# New Balance Destination
if(type_trans != "CASH_IN"):
bal_provider = DynamicProvider(
provider_name = "newbalanceDest",
elements = [0, random.randint(0, 10000), oldbalanceDest + amount, oldbalanceDest]
)
fake.add_provider(bal_provider)
newBal = fake.newbalanceDest()
else:
newBal = 0
nameOrig = f"C{orig[i]}"
nameDest = f"M{dest[i]}"
# Source Bank
type_provider = DynamicProvider(
provider_name = "bank_source",
elements = ["JPMC", "BOA", "UBS", "MS", "GS", "AE"]
)
fake.add_provider(type_provider)
bank_source = fake.bank_source()
# Source Bank City
type_provider = DynamicProvider(
provider_name = "bank_source_city",
elements = ["Los Angeles", "Chicago", "New York", "San Francisco", "Atlanta"]
)
fake.add_provider(type_provider)
bank_source_city = fake.bank_source_city()
# Destination Bank
type_provider = DynamicProvider(
provider_name = "bank_destination",
elements = ["JPMC", "BOA", "UBS", "MS", "GS", "AE"]
)
fake.add_provider(type_provider)
bank_destination = fake.bank_destination()
# Destination Bank City
type_provider = DynamicProvider(
provider_name = "bank_destination_city",
elements = ["Los Angeles", "Chicago", "New York", "San Francisco", "Atlanta"]
)
fake.add_provider(type_provider)
bank_destination_city = fake.bank_destination_city()
# Data payload
tmp = {
"timestamp": str(datetime.datetime.now()),
"transaction_id": "T"
+ str(''.join(random.choices(string.ascii_uppercase, k = 3)))
+ str(datetime.datetime.now().timestamp())[0: 4]
+ str(''.join(random.choices(string.ascii_uppercase, k = 2)))
+ str(datetime.datetime.now().timestamp())[4: 10],
"step": random.randint(1, 800),
"type": type_trans,
"amount": int(amount),
"source_name": nameOrig,
"source_bank": bank_source,
"source_bank_city": bank_source_city,
"oldbalance_source": int(oldbalanceOrg),
"newbalance_source": int(newbalanceOrig),
"destination_name": nameDest,
"destination_bank": bank_destination,
"destination_bank_city": bank_destination_city,
"oldbalance_destination": int(oldbalanceDest),
"newbalance_destination": int(newBal),
"isFlaggedFraud": 0
}
# source & destination bank name, source & destination location, timestamp
data.append(tmp)
return data
def publish_pubsub(message):
project_id = "PROJECT-ID"
topic_id = "TOPIC-ID"
publisher = pubsub_v1.PublisherClient()
topic_path = publisher.topic_path(project_id, topic_id)
data_str = dumps(message)
data_message = data_str.encode("utf-8")
future = publisher.publish(topic_path, data_message)
# print(data_message)
print(future.result())
def recieve_pubsub():
project_id = "PROJECT-ID"
subscription_id = "SUBSCRIPTION-ID"
subscriber = pubsub_v1.SubscriberClient()
subscription_path = subscriber.subscription_path(project_id, subscription_id)
streaming_pull_future = subscriber.subscribe(subscription_path, callback = callback)
print(f"Listening for messages ...\n")
# 'with' block to automatically call close() when done
with subscriber:
try:
streaming_pull_future.result(timeout = 3.0)
except TimeoutError:
streaming_pull_future.cancel() # Trigger the shutdown.
streaming_pull_future.result()
def callback(message: pubsub_v1.subscriber.message.Message) -> None:
print(f"Received {message}.")
message.ack()
def main():
print("Generating dummy data")
m = fraud_data_generation(100)
print("Publishing pubsub message")
publish_pubsub(m)
print(m)
# for i in m:
# publish_pubsub(i)
# time.sleep(2)
recieve_pubsub()
if __name__ == '__main__':
main()
# import pandas as pd
# import json
# list = []
# f = open("demo.txt", "r")
# for x in f:
# list.append(json.loads(x.strip()))
# print(list)
# df = pd.DataFrame.from_dict(list)
# print(df)