-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata-migration.js
222 lines (203 loc) · 6.44 KB
/
data-migration.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
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
216
217
218
219
220
221
222
"use strict";
// Loading a few environment variables.
require("dotenv").config();
// Begin importing the dependencies.
const db = require("./models").Geolocation;
const winston = require("winston");
const dynamics = require("./controller/src/dynamics");
class dynamicsMigration {
constructor() {
this.count = 1;
this.mappedData = {}; // Data container for fetching and processing data.
this.dataNextLink = ""; // The next link for recursive API calls(For the next 5000 dataset batch).
// Log info so we know what rows failed imports.
this.logger = winston.createLogger({
level: "info",
format: winston.format.json(),
defaultMeta: { service: "user-service" },
transports: [
new winston.transports.File({
filename: "./logs/migrations.log",
level: "error",
}),
],
});
// Safety and additional checks if in production - gotta be careful.
if (process.env.NODE_ENV !== "production") {
this.logger.add(
new winston.transports.Console({
format: winston.format.simple(),
})
);
}
}
getSingleTonDynamicsInstance() {
if (
undefined === typeof this.dynamicsInstance ||
"object" !== typeof this.dynamicsInstance
) {
try {
this.dynamicsInstance = new dynamics();
return this.dynamicsInstance;
} catch (e) {
this.quitOnException(e);
}
} else {
return this.dynamicsInstance;
}
}
async fetchData(url = "") {
let dynamicsInstance = this.getSingleTonDynamicsInstance();
let urlEndpoint = "" === url ? `cr4f2_agentsandrealtors` : url;
this.mappedData = await dynamicsInstance.get(urlEndpoint);
return this.mappedData;
}
async processData(data = undefined) {
let dataToInsert = [];
let localDataTuple = data || this.mappedData.data.value || [];
localDataTuple.forEach((dataTuple) => {
dataToInsert.push({
id: dataTuple.cr4f2_agentsandrealtorid,
name: dataTuple.cr4f2_fullname,
email: dataTuple.new_email,
phone: dataTuple.new_phone,
company: dataTuple.new_companyname,
address: dataTuple.new_address,
latitude: dataTuple.new_latitude,
longitude: dataTuple.new_longitude,
createdAt: dataTuple.createdon,
updatedAt: dataTuple.modifiedon,
});
});
// Let sequelize handle duplicates, bulk create the fresh ones.
try {
await db.bulkCreate(dataToInsert, {
fields: [
"id",
"name",
"email",
"phone",
"company",
"address",
"latitude",
"longitude",
"createdAt",
"updatedAt",
],
updateOnDuplicate: ["id"],
});
} catch(e) {
this.logger.log({
level: "error",
message: `[ERROR] Pull failed on: ${this.dataNextLink}`,
});
}
console.log(
`\x1b[34m[IMPORT] Importing ${
this.count * dataToInsert.length
} records.\x1b[0m`
);
// Free memory.
return delete this.mappedData;
}
async init(param) {
try {
// Declarations for the migration start.
await this.fetchData(param);
// Actual migration happens here.
try {
if (
"undefined" !== typeof this.mappedData.data &&
"undefined" !== typeof this.mappedData.data["@odata.context"]
) {
do {
try {
this.dataNextLink = this.mappedData.data["@odata.nextLink"];
} catch (e) {
this.dataNextLink = false;
}
await this.processData();
await this.processNextLink(this.dataNextLink);
if (false !== this.dataNextLink) {
console.log(
`\x1b[34m[FETCH] Importing data from endpoint: cr4f2_agentsandrealtors for another 5000 records.\x1b[0m`
);
await this.fetchData(this.dataNextLink);
}
} while (false !== this.dataNextLink);
this.quitOnSuccess();
} else {
throw "The server couldn't be reached for polling data.";
}
} catch (e) {
throw e;
}
} catch (e) {
// Failure in fetching, bail quickly.
this.quitOnException(e);
}
await this.finalizeMigration();
}
async processNextLink() {
try {
if (this.dataNextLink.match(/cr4f2_agentsandrealtors.*/gi).length) {
this.dataNextLink = this.dataNextLink.match(
/cr4f2_agentsandrealtors.*/gi
)[0]; // Cherry-pick the endpoint only.
} else {
this.dataNextLink = false;
}
} catch (e) {
this.dataNextLink = false;
}
}
// Recomputes all the geolocation into
// the point form used in Harvesine formula.
async finalizeMigration() {
console.log(`\x1b[34m[PRUNING] Pruning database, please wait.\x1b[0m`);
await db.sequelize.query(
`DELETE FROM
geolocation
WHERE
latitude=NULL
OR latitude=''
OR
longitude=''
OR longitude=NULL`
);
console.log(
`\x1b[34m[FINALIZING] Recomputing all point vectors for geolocation, this might take a while.\x1b[0m`
);
await db.sequelize.query(
`UPDATE
geolocation
SET coordinates=POINT(longitude,latitude);`
);
}
quitOnException(e = "") {
let message =
"" === e
? `\x1b[31m[SIGTERM] Reason: Migration failed. Unknown Exception occured.\x1b[0m`
: `\x1b[31m[SIGTERM] Reason: Migration failed. You can check the logs generated on : migrations.log\x1b[0m\n\nException: ${e}`;
this.logger.log({
level: "error",
message: `[ERROR] Import failed due to exception resulting in SIGTERM: ${e}`,
});
this.logger.log({
level: "error",
message: `[Failed at] : ${this.dataNextLink}`,
});
console.log(message);
}
quitOnSuccess() {
this.logger.log({
level: "info",
message: `[SUCCESS] Imported data successfully. Please check migrations log for more information.`,
});
console.log(
`\x1b[32m[SUCCESS] Migrations were successful. Logs generated on : migrations.log\x1b[0m`
);
}
}
let runMigrations = new dynamicsMigration();
runMigrations.init(process.argv.slice(2).pop() || "cr4f2_agentsandrealtors");