-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
208 lines (177 loc) · 7.3 KB
/
main.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
import random
import psycopg2
import requests
import simplejson as json
from confluent_kafka import SerializingProducer
BASE_URL = 'https://randomuser.me/api/?nat=gb'
PARTIES = ["Management Party", "Tech-Republic Party", "Savior Party"]
random.seed(42)
def generate_voter_data():
response = requests.get(BASE_URL)
if response.status_code == 200:
user_data = response.json()['results'][0]
return {
"voter_id": user_data['login']['uuid'],
"voter_name": f"{user_data['name']['first']} {user_data['name']['last']}",
"date_of_birth": user_data['dob']['date'],
"gender": user_data['gender'],
"nationality": user_data['nat'],
"registration_number": user_data['login']['username'],
"address": {
"street": f"{user_data['location']['street']['number']} {user_data['location']['street']['name']}",
"city": user_data['location']['city'],
"state": user_data['location']['state'],
"country": user_data['location']['country'],
"postcode": user_data['location']['postcode']
},
"email": user_data['email'],
"phone_number": user_data['phone'],
"cell_number": user_data['cell'],
"picture": user_data['picture']['large'],
"registered_age": user_data['registered']['age']
}
else:
return "Error fetching data"
def generate_candidate_data(candidate_number, total_parties):
response = requests.get(BASE_URL + '&gender=' + ('female' if candidate_number % 2 == 1 else 'male'))
if response.status_code == 200:
user_data = response.json()['results'][0]
return {
"candidate_id": user_data['login']['uuid'],
"candidate_name": f"{user_data['name']['first']} {user_data['name']['last']}",
"party_affiliation": PARTIES[candidate_number % total_parties],
"biography": "A brief bio of the candidate.",
"campaign_platform": "Key campaign promises or platform.",
"photo_url": user_data['picture']['large']
}
else:
return "Error fetching data"
def delivery_report(err, msg):
if err is not None:
print(f'Message delivery failed: {err}')
else:
print(f'Message delivered to {msg.topic()} [{msg.partition()}]')
# Kafka Topics
voters_topic = 'voters_topic'
candidates_topic = 'candidates_topic'
def create_tables(conn, cur):
cur.execute("""
CREATE TABLE IF NOT EXISTS candidates (
candidate_id VARCHAR(255) PRIMARY KEY,
candidate_name VARCHAR(255),
party_affiliation VARCHAR(255),
biography TEXT,
campaign_platform TEXT,
photo_url TEXT
)
""")
cur.execute("""
CREATE TABLE IF NOT EXISTS voters (
voter_id VARCHAR(255) PRIMARY KEY,
voter_name VARCHAR(255),
date_of_birth VARCHAR(255),
gender VARCHAR(255),
nationality VARCHAR(255),
registration_number VARCHAR(255),
address_street VARCHAR(255),
address_city VARCHAR(255),
address_state VARCHAR(255),
address_country VARCHAR(255),
address_postcode VARCHAR(255),
email VARCHAR(255),
phone_number VARCHAR(255),
cell_number VARCHAR(255),
picture TEXT,
registered_age INTEGER
)
""")
cur.execute("""
CREATE TABLE IF NOT EXISTS votes (
voter_id VARCHAR(255) UNIQUE,
candidate_id VARCHAR(255),
voting_time TIMESTAMP,
vote int DEFAULT 1,
PRIMARY KEY (voter_id, candidate_id)
)
""")
conn.commit()
def insert_voters(conn, cur, voter):
cur.execute("""
INSERT INTO voters (voter_id, voter_name, date_of_birth, gender, nationality, registration_number, address_street, address_city, address_state, address_country, address_postcode, email, phone_number, cell_number, picture, registered_age)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s,%s,%s,%s,%s,%s,%s)
""",
(voter["voter_id"], voter['voter_name'], voter['date_of_birth'], voter['gender'],
voter['nationality'], voter['registration_number'], voter['address']['street'],
voter['address']['city'], voter['address']['state'], voter['address']['country'],
voter['address']['postcode'], voter['email'], voter['phone_number'],
voter['cell_number'], voter['picture'], voter['registered_age'])
)
conn.commit()
## Check referential integrity between votes and voters
def check_referential_integrity(conn, cur):
cur.execute("""
SELECT COUNT(*) FROM votes v
LEFT JOIN voters vt ON v.voter_id = vt.voter_id
WHERE vt.voter_id IS NULL
""")
result = cur.fetchone()
if result[0] > 0:
print(f"Referential integrity check failed: {result[0]} votes have no corresponding voter.")
def check_sanity_row_counts(conn, cur):
# Check sanity row count for voters
cur.execute("""
SELECT COUNT(*) FROM voters
""")
voters_count = cur.fetchone()[0]
# Check sanity row count for candidates
cur.execute("""
SELECT COUNT(*) FROM candidates
""")
candidates_count = cur.fetchone()[0]
# Check sanity row count for votes
cur.execute("""
SELECT COUNT(*) FROM votes
""")
votes_count = cur.fetchone()[0]
# Output the results
print(f"Sanity row count check for voters: {voters_count} rows")
print(f"Sanity row count check for candidates: {candidates_count} rows")
print(f"Sanity row count check for votes: {votes_count} rows")
if __name__ == "__main__":
conn = psycopg2.connect("host=localhost dbname=voting user=postgres password=postgres")
cur = conn.cursor()
producer = SerializingProducer({'bootstrap.servers': 'localhost:9092', })
create_tables(conn, cur)
# get candidates from db
cur.execute("""
SELECT * FROM candidates
""")
candidates = cur.fetchall()
print(candidates)
if len(candidates) == 0:
for i in range(3):
candidate = generate_candidate_data(i, 3)
print(candidate)
cur.execute("""
INSERT INTO candidates (candidate_id, candidate_name, party_affiliation, biography, campaign_platform, photo_url)
VALUES (%s, %s, %s, %s, %s, %s)
""", (
candidate['candidate_id'], candidate['candidate_name'], candidate['party_affiliation'], candidate['biography'],
candidate['campaign_platform'], candidate['photo_url']))
conn.commit()
for i in range(1000):
voter_data = generate_voter_data()
insert_voters(conn, cur, voter_data)
producer.produce(
voters_topic,
key=voter_data["voter_id"],
value=json.dumps(voter_data),
on_delivery=delivery_report
)
print('Produced voter {}, data: {}'.format(i, voter_data))
producer.flush()
# Perform data quality checks
check_referential_integrity(conn, cur)
check_sanity_row_counts(conn, cur)
# Close the connection after checks
conn.close()