-
Notifications
You must be signed in to change notification settings - Fork 0
/
generateSampleData.js
190 lines (175 loc) · 5.4 KB
/
generateSampleData.js
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
require('dotenv').config();
const uuidv1 = require('uuid/v1');
const AWS = require('aws-sdk');
const { networkRequest } = require('./utils');
const awsResourcesCreated = require('./awsResourcesCreated.json');
const googleMapsClient = require('@google/maps').createClient({
key: process.env.GOOGLE_MAPS_API_KEY,
Promise: Promise
});
const DDB = new AWS.DynamoDB();
const { marshall } = AWS.DynamoDB.Converter; // To Convert a JavaScript object into a DynamoDB record
const usMajorCities = [
"new york city",
"miami",
"seattle",
"portland",
"san francisco",
"los angeles",
"phoenix",
"san diego",
"salt lake city",
"denver",
"dallas",
"austin",
"houston",
"minneapolis",
"chicago",
"detroit",
"baltimore",
"washington dc",
]
// NHL, NFL, NBA, MLS, MLB
const leagueIds = [4380, 4391, 4387, 4346, 4424];
const fetchLeagueTeams = async (leagueId) => {
try {
let { teams } = await networkRequest(`https://www.thesportsdb.com/api/v1/json/1/lookup_all_teams.php?id=${leagueId}`);
return teams
} catch (err) {
throw err;
}
}
const mapLeaguesToTeams = async () => {
let map = {}
for (let i = 0; i < leagueIds.length; i++) {
let teams;
try {
teams = await fetchLeagueTeams(leagueIds[i]);
map[leagueIds[i].toString()] = teams;
} catch (err) {
console.log('[Error] => ', err)
}
}
return map;
}
const createEstablishmentObj = (place) => {
const establishment = {
id: uuidv1(),
managerUsername: Math.floor(Math.random() * 6) === 3 ? 'AnEstablishmentManagerUser' : 'SampleManager' ,
googlePlaceId: place.place_id,
name: place.name.toLowerCase(),
displayName: place.name,
address: place.formatted_address,
phone: place.formatted_phone_number || '(123) 456-7890',
location: {
lat: place.geometry.location.lat.toString(),
lon: place.geometry.location.lng.toString(),
}
}
return establishment;
}
const fetchEstablishmentsForCity = async (city) => {
let establishments = [];
try {
let { json } = await googleMapsClient.places({ query: `sports bars in ${city}` }).asPromise()
establishments = json.results.map(place => createEstablishmentObj(place))
return (establishments);
} catch (err) {
throw err
}
}
const insertItemToTable = (item, table) => {
const params = {
TableName: table,
Item: marshall(item),
ReturnConsumedCapacity: 'TOTAL'
}
return new Promise((resolve, reject) => {
DDB.putItem(params, (err, data) => {
if (err) reject(err);
else resolve(data);
})
})
}
const generateRandomDate = () => {
let start = new Date();
let aMonthInMilliseconds = 1000 * 60 * 60 * 24 * 30;
let end = new Date(start.getTime() + aMonthInMilliseconds);
return new Date(start.getTime() + (Math.random() * (end.getTime() - start.getTime())));
}
const pickRandomTeams = (teams) => {
let i = Math.floor(Math.random() * teams.length);
let j = Math.floor(Math.random() * teams.length);
if (i != j) {
return {
homeTeam: teams[i],
awayTeam: teams[j]
}
}
return pickRandomTeams(teams);
}
const generateRandomEvent = (atEstablishmentId, leagueId, teams) => {
let { homeTeam, awayTeam } = pickRandomTeams(teams);
let event = {
atEstablishmentId,
leagueId,
homeTeam: homeTeam.strTeam,
awayTeam: awayTeam.strTeam,
homeTeamBadge: homeTeam.strTeamBadge,
awayTeamBadge: awayTeam.strTeamBadge,
startTime: generateRandomDate().toISOString(),
coverCharge: false,
description: '[Sample event], description of event/show or additional notes.'
}
return event;
}
const seedEventsForEstablishments = async (allEstablishments) => {
try {
const leaguesAndTeams = await mapLeaguesToTeams();
const leagues = Object.keys(leaguesAndTeams);
const eventsTable = awsResourcesCreated.dynamoDBTables[1];
for (let i = 0; i < allEstablishments.length; i++) {
const est = allEstablishments[i];
console.log('=> ', est.displayName);
// Generate 3 random events per establishment
for (let i = 0; i < 3; i++) {
const leagueId = leagues[Math.floor(Math.random() * leagues.length)];
const teams = leaguesAndTeams[leagueId];
const event = generateRandomEvent(est.id, leagueId, teams);
console.log(event.awayTeam, 'vs', event.homeTeam);
await insertItemToTable(event, eventsTable);
}
}
} catch (err) {
throw err;
}
}
const seedEstablishments = async (establishments) => {
let estTable = awsResourcesCreated.dynamoDBTables[0];
for (let i = 0; i < establishments.length; i++) {
try {
// Inserting items could happen concurrently, but I'm afraid that could
// throttle the database and exceed my 5 WCU provisioned on the table
await insertItemToTable(establishments[i], estTable);
} catch (err) {
throw err;
}
}
}
const main = async () => {
try {
let allEstablishments = [];
for (let i = 0; i < usMajorCities.length; i++) {
const city = usMajorCities[i];
let establishments = await fetchEstablishmentsForCity(city);
await seedEstablishments(establishments);
allEstablishments = allEstablishments.concat(establishments);
console.log(`Generated ${establishments.length} establishments for ${city}.`);
}
await seedEventsForEstablishments(allEstablishments);
console.log('=== Generated sample data. Success... ===');
} catch (err) {
console.log('error =>', err);
}
};
main();