-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.rb
69 lines (65 loc) · 1.79 KB
/
web.rb
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
require 'sinatra'
require 'json'
require 'fileutils'
require 'find'
require 'iconv'
require './classes.rb'
require './methods.rb'
set :environment, :development
set :public_folder, File.dirname(__FILE__) + '/assets'
set :logging, :true
get '/' do
erb :start
end
put '/upload' do
Dir.mkdir('uploads') unless File.exists?('uploads')
if env['HTTP_X_FILENAME'] =~ /.+.lgil/
File.open('uploads/intervention.lgil', "w") do |f|
f.write(request.body.read)
end
else
File.open('uploads/' + env['HTTP_X_FILENAME'], "w") do |f|
f.write(request.body.read)
end
end
end
get '/processFiles' do
nodes, links = [], []
lgil_file = Iconv.new("UTF-8//IGNORE", "UTF-8").iconv(File.open('uploads/' + 'intervention.lgil', "r").read).split(/\r\n/)
clean_lgil(lgil_file)
create_nodes(nodes, links)
find_sections(nodes)
parse_lgil
parse_xml
links.uniq!
FileUtils.rm_rf('uploads/.')
{
:nodes => nodes,
:links => links
}.to_json
end
post '/processSessions/?' do
sessions = {}
session_arr = []
duration = 0
JSON.parse(request.env["rack.input"].read)["session_data"].each do |session|
session["visited_nodes"].each_with_index do |node, index|
duration += node["duration"]
unless index == session["visited_nodes"].size-1
current_name = node["node_name"]
next_name = session["visited_nodes"][index+1]["node_name"]
if sessions["lineid_"+current_name+next_name]
sessions["lineid_"+current_name+next_name] += 1
else
sessions["lineid_"+current_name+next_name] = 1
end
end
end
session_arr.push({ :id => session["id"], :location => session["location"], :duration => readable_time(duration) })
duration = 0
end
{
:sessions => sessions,
:tablular_data => session_arr
}.to_json
end