-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.lua
362 lines (298 loc) · 10.9 KB
/
init.lua
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#!/usr/bin/env tarantool
local fio = require('fio')
local connectors = require('connectors')
local xml_parser = require('xml_parse')
local log = require('log')
local json = require('json')
-- local date = require('icu-date')
box.cfg({
log = 'log.txt',
log_level = 6
})
local _ = require('schema')
--[[
Add person/flight to DB
0. Transorm data
1. Try to find person in DB (create if needed)
2. Add new data or verify
* update profile
* add flights:
** try to find flight (update or create)
** relate with person
]]
--[[ Pattern search
List all persons:
....Pass all flights through filter: (
time gap between flights,
routes (length, specified countries..),
loyalty cards,
flight class)
....
--]]
local persons = box.space.PERSON
local flights = box.space.FLIGHTS
local DEFAULT_PERSON_FILTER = function(person)
if person.first_name then
person.first_name = person.first_name:gsub("'", "")
end
if person.second_name then
person.second_name = person.second_name:gsub("'", "")
end
if person.last_name then
person.last_name = person.last_name:gsub("'", "")
end
end
local function map_record(mapping, record)
local result = {}
for k,v in pairs(mapping) do
if type(v) == 'table' then
local temp = record[v[1]]
for i = 2, #v do
if temp ~= nil then
temp = temp[v[i]]
end
end
result[k] = temp
else
result[k] = record[v]
end
end
return result
end
local function __find_person_by_flights(person_brief, flights_history)
local function __find_max(any_kv_table)
local max = { val = -1, key = {} }
for k, v in pairs(any_kv_table) do
if v >= max.val then
max.val = v
if v > max.val then
max.key = {}
end
table.insert(max.key, k)
end
end
return max.key
end
local persons_match = {}
for _, current_flight in ipairs(flights_history) do
log.info("Flight: %s", json.encode(current_flight))
local res = flights.index.date_flight:select({current_flight['code'], current_flight['date']}, { iterator = "EQ" })
for _, matched_flight in ipairs(res) do
matched_flight = matched_flight:tomap({ names_only = true })
log.info("matched: %s", json.encode(matched_flight))
if matched_flight.person_id ~= nil then
if not persons_match[matched_flight.person_id] then
persons_match[matched_flight.person_id] = 1
else
persons_match[matched_flight.person_id] = persons_match[matched_flight.person_id] + 1
end
end
end
end
local function __concat_name(first_name, last_name)
first_name = first_name or ''
last_name = last_name or ''
return first_name .. last_name
end
local result = __find_max(persons_match)
-- log.info("RESOLVING BY FLIGHTS %s", json.encode(result))
local results_with_name = {}
for _, person_id in ipairs(result) do
local passenger = persons.index.id:select(person_id)[1]:tomap({ names_only = true })
local passenger_name = __concat_name(passenger.first_name, passenger.last_name)
-- TODO: Support last name with 'G.'
if passenger_name == __concat_name(person_brief.first_name, person_brief.last_name) then
table.insert(results_with_name, person_id)
end
end
if #results_with_name > 0 then
result = results_with_name
end
return result[1] and persons.index.id:select(result[1])
end
local function find_person(record, flights_history)
local res
if record['document'] then
res = persons.index.document:select(record['document'], { iterator = "EQ" })
else
if record['first_name'] and record['last_name'] then
res = persons.index.name:select({record['first_name'], record['last_name']}, { iterator = "EQ" })
end
if not res or #res > 1 then
-- Not determined yet
res = __find_person_by_flights(record, flights_history)
if res and #res >= 1 then
log.info("find via flights: %s", json.encode(record))
end
end
end
return res and res[1]
end
local function create_person(record)
record.id = box.sequence.seq_person_id:next()
local person = persons:insert(persons:frommap(record))
return person
end
local function update_person(person, new_record, filters)
for k,v in pairs(new_record) do
-- Apply filters
for _, func in ipairs(filters) do
func(person, new_record, k)
end
if not person[k] then
person[k] = v
end
end
persons:replace(persons:frommap(person))
end
local function find_flight(person, flight_record)
local res = flights.index.same_flight:select({person['id'], flight_record['code'], flight_record['date']}, { iterator = "EQ" })
assert(#res < 2, ("More than one flight: %s"):format(json.encode(record)))
return res[1]
end
local function create_flight(person, flight_record)
flight_record.id = box.sequence.seq_flight_id:next()
flight_record.person_id = person.id
local flight = flights:insert(flights:frommap(flight_record))
return flight
end
local function update_flight(flight, flight_record, filters)
for k,v in pairs(flight_record) do
for _, func in ipairs(filters) do
func(flight, flight_record, k)
end
if not flight[k] then
flight[k] = v
end
end
flights:replace(flights:frommap(flight))
end
local function process_csv()
local source1 = connectors.csv(fio.pathjoin('data', 'BoardingData.csv'), ';')
for row, val in ipairs(source1) do
local record = map_record({
first_name = 'PassengerFirstName',
second_name = 'PassengerSecondName',
last_name = 'PassengerLastName',
sex = 'PassengerSex',
birth_date = 'PassengerBirthDate',
document = 'PassengerDocument',
}, val)
DEFAULT_PERSON_FILTER(record)
local person = find_person(record)
if not person then
person = create_person(record)
end
person = person:tomap({ names_only = true })
update_person(person, record, {
function(_, new_data, key)
if type(new_data[key]) == 'string' then
new_data[key] = new_data[key]:upper()
end
end,
function (old_data, new_data, key)
if type(old_data[key]) == 'string' then
if old_data[key] and old_data[key]:match('^[A-Z].$') then
if new_data[key] and not new_data[key]:match('^[A-Z].$') then
old_data[key] = new_data[key]
end
end
end
end
})
local flight_record = map_record({
arrival = 'Destination',
code = 'FlightNumber',
flight_time = 'FlightTime',
date = 'FlightDate',
ticket_number = 'TicketNumber',
booking_code = 'BookingCode'
}, val)
local flight = find_flight(person, flight_record)
if not flight then
flight = create_flight(person, flight_record)
end
flight = flight:tomap({ names_only = true })
update_flight(flight, flight_record, {
function(_, new_data, key)
if type(new_data[key]) == 'string' then
new_data[key] = new_data[key]:upper()
end
end
})
end
end
local function process_xml()
local loyalty_source = connectors.xml(fio.pathjoin('data', 'PointzAggregator-AirlinesData.xml'))
local loyalty_data = xml_parser.parse(loyalty_source)
for _, user in ipairs(loyalty_data) do
local all_flights_by_loyalty = {}
local all_loyalty_cards = {}
for _, loyalty in ipairs(user.loyalty) do
for _, flight in ipairs(loyalty.flights) do
table.insert(all_flights_by_loyalty, flight)
end
table.insert(all_loyalty_cards, loyalty.name)
end
local person_brief = { first_name = user.first_name, last_name = user.last_name, loyalty = all_loyalty_cards }
DEFAULT_PERSON_FILTER(person_brief)
local person = find_person(person_brief, all_flights_by_loyalty)
if not person then
person = create_person(person_brief)
end
person = person:tomap({ names_only = true })
for _, flight in ipairs(all_flights_by_loyalty) do
flight = map_record({
date = 'date',
code = 'code',
departure = 'departure',
arrival = 'arrival',
}, flight)
local flight_from_db = find_flight(person, flight)
if not flight_from_db then
flight_from_db = create_flight(person, flight)
-- flight_from_db = flight_from_db:tomap({ names_only = true })
end
-- update_flight()
end
end
end
local function process_json()
local source2 = connectors.json(fio.pathjoin('data', 'FrequentFlyerForum-Profiles.json'))
for id, user in ipairs(source2['Forum Profiles']) do
local user_flights = {}
for _, flight in ipairs(user["Registered Flights"]) do
local flight_record = map_record({
arrival = {'Arrival', 'Airport'},
departure = {'Departure', 'Airport'},
code = 'Flight',
date = 'Date',
}, flight)
table.insert(user_flights, flight_record)
end
local person_brief = user["Real Name"]
person_brief['Sex'] = user['Sex']
-- Too little info about person in this source -> try to find person with flights scope
local person_record = map_record({
first_name = {'Real Name', 'First Name'},
second_name = {'Real Name', 'Last Name'},
sex = 'Sex',
}, person_brief)
DEFAULT_PERSON_FILTER(person_record)
local loyalties = {}
for _, loyalty in ipairs(person_brief['Loyality Programm']) do
table.insert(loyalties, loyalty["programm"] .. loyalty["Number"])
end
person_record.loyalty = loyalties
local person = find_person(person_record, user_flights)
if not person then
person = create_person(person_record)
end
-- TODO: Update loyalties
-- update_person()
end
end
process_csv()
-- TODO: add Male
process_xml() -- Have FIRST/LAST name for each loyalty information
process_json() -- Called after XML for matching by loyalty