-
Notifications
You must be signed in to change notification settings - Fork 1
/
tools.js
261 lines (237 loc) · 7.55 KB
/
tools.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
const oracledb = require('oracledb');
const dbConfig = require('./configs/dbconfig.js');
const axios = require('axios');
async function addCountries(countries) {
let connection;
try {
connection = await oracledb.getConnection(dbConfig);
const createSql = `insert into countries (name, code) values (:name, :code)`;
const result = await connection.executeMany(createSql, countries, {
autoCommit: true,
});
console.log('Result is:', result);
} catch (err) {
console.error(err);
} finally {
if (connection) {
try {
await connection.close();
} catch (err) {
console.error(err);
}
}
}
}
function getCountries() {
axios
.get('https://api.covid19api.com/countries')
.then(function ({
data
}) {
const countries = data.map((data) => {
const {
Slug,
...newData
} = data;
// change properties for matching with oracle
const {
Country: name,
ISO2: code
} = newData;
return {
name,
code,
}; // Country
});
addCountries(countries);
})
.catch(function (error) {
// handle error
console.log(error);
})
.then(function () {
// always executed
});
}
async function addCases(cases) {
let connection;
try {
connection = await oracledb.getConnection(dbConfig);
const createSql = `insert into cases (country_code, confirmed, deathed, recovered, actived, occured_at) values (:country_code, :confirmed, :deathed, :recovered, :actived, to_date(:occured_at,'YYYY-MM-DD'))`;
const result = await connection.executeMany(createSql, cases, {
autoCommit: true,
});
console.log('Result is:', result);
} catch (err) {
console.error(err);
} finally {
if (connection) {
try {
await connection.close();
} catch (err) {
console.error(err);
}
}
}
}
async function getCases(country) {
axios
.get(
`https://api.covid19api.com/total/country/${country}?from=2020-06-23T00:00:00Z&to=2020-06-24T00:00:00Z`,
)
.then(async function ({
data
}) {
//get id_country, code_country
let connection;
connection = await oracledb.getConnection(dbConfig);
const countryCodeSql = `select code from countries where name = '${data[0].Country}'`;
const result = await connection.execute(countryCodeSql);
const country_code = result.rows[0][0];
const cases = data.map((data) => {
//change properties to match with oracle
// console.log(data.Date.split('T')[0])
const {
Confirmed: confirmed,
Deaths: deathed,
Recovered: recovered,
Active: actived,
Date: occured_at,
} = data;
return {
country_code,
confirmed,
deathed,
recovered,
actived,
occured_at: occured_at.split('T')[0],
}; // Case
});
// console.log(cases);
addCases(cases);
})
.catch(function (error) {
// handle error
console.log(error);
})
.then(function () {
// always executed
});
}
const sleep = (waitTimeInMs) =>
new Promise((resolve) => setTimeout(resolve, waitTimeInMs));
function getCasesAllCountries() {
axios
.get('https://api.covid19api.com/countries')
.then(async function ({
data
}) {
const countries = data.map(({
Slug
}) => Slug); // return slug for all countries
for (let i = 0; i < countries.length; i++) {
getCases(countries[i]);
await sleep(2000);
console.log(countries[i]);
}
})
.catch(function (error) {
// handle error
console.log(error);
})
.then(function () {
// always executed
});
}
function getCotinents() {
axios
.get(
'https://pkgstore.datahub.io/JohnSnowLabs/country-and-continent-codes-list/country-and-continent-codes-list-csv_json/data/c218eebbf2f8545f3db9051ac893d69c/country-and-continent-codes-list-csv_json.json',
)
.then(function ({
data
}) {
console.log(data);
})
.catch(function (error) {
// handle error
console.log(error);
})
.then(function () {
// always executed
});
}
async function todayUpdate(country) {
axios
.get(`https://api.covid19api.com/summary`)
.then(async function ({
data
}) {
console.log(data['Countries']);
//get id_country, code_country
// let connection;
// connection = await oracledb.getConnection(dbConfig);
// const countryCodeSql = `select code from countries where name = '${data[0].Country}'`;
// const result = await connection.execute(countryCodeSql);
// const country_code = result.rows[0][0];
const cases = data['Countries'].map((data) => {
//change properties to match with oracle
// console.log(data.Date.split('T')[0])
const {
CountryCode: country_code,
TotalConfirmed: confirmed,
TotalDeaths: deathed,
TotalRecovered: recovered,
Date: occured_at,
} = data;
const actived = confirmed - deathed - recovered;
return {
country_code,
confirmed,
deathed,
recovered,
actived,
occured_at: occured_at.split('T')[0],
}; // Case
});
// console.log(cases);
addCases(cases);
})
.catch(function (error) {
// handle error
console.log(error);
})
.then(function () {
// always executed
});
}
// getCasesAllCountries();
todayUpdate();
// Test Lap Lich
async function testLapLich() {
let connection;
try {
connection = await oracledb.getConnection(dbConfig);
const createSql = `insert into cases (country_code, confirmed, deathed, recovered, actived, occured_at) values ('VN', 122, 122, 122, 122, to_date('2020-06-06','YYYY-MM-DD'))`;
const result = await connection.execute(
createSql, {}, {
autoCommit: true,
},
);
console.log('Result is:', result);
} catch (err) {
console.error(err);
} finally {
if (connection) {
try {
await connection.close();
} catch (err) {
console.error(err);
}
}
}
}
// console.log(
// 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
// );
// testLapLich();