-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.coffee
135 lines (111 loc) · 3.55 KB
/
server.coffee
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
_ = require 'lodash'
bodyParser = require 'body-parser'
express = require 'express'
moment = require 'moment'
engine = require 'ejs-locals'
cp = require 'child_process'
fs = require 'fs'
app = express()
app.engine 'ejs', engine
app.set 'views', "#{__dirname}/views"
app.set 'view engine', 'ejs'
app.use bodyParser.json
limit: 104900000
# Prettify item title
app.locals.TITLES =
'smartphone': 'Line Base Charge'
'iphone': 'Line Base Charge'
'access': 'Line Discount'
'international': 'International Roaming'
'shared': 'Shared Plan Charges'
'equipment': 'Equipment Charges'
'surcharges': 'Surcharges & Fees'
'sharedSurcharges': 'Shared Surcharges'
'overage': 'Overage Charge'
'government': 'Government Fees & Taxes'
'30gb': 'Plan Base Charge'
'national': 'Plan Discount'
bill = {}
updatedAt = null
app.get '/', (req, res, next) ->
try
content = fs.readFileSync './data.json', 'utf8'
data = JSON.parse(content)
catch e
# Render loading page if data.json not exist
return res.render 'loading'
bill =
plan: {}
surcharges: {}
updatedAt: updatedAt
lineCount: 0
lines: {}
overage: {amount: 0, total: 0, lines: {}}
for number, detail of data.lines
bill.lineCount += 1
bill.lines[number] = {}
for title, amount of detail
if title in ['30gb', 'national']
# Add to plan charges
bill.plan[title] ||= 0
bill.plan[title] += amount
else if title in ['data']
# Add to overage chrages
bill.overage.amount += amount
else if title in ['surcharges', 'government']
# Add to surcharges
bill.surcharges[title] ||= 0
bill.surcharges[title] += amount
else
# Line's charges
bill.lines[number][title] = amount
# Compute total of surcharges and plan charges
bill.surcharges.total = _.sum _.values(bill.surcharges)
bill.plan.total = _.sum _.values(bill.plan)
# Compute overage usages
for number, usage of data.usages
if usage > (30 / bill.lineCount) # Over average usage
bill.overage.total += usage - (30 / bill.lineCount)
bill.overage.lines[number] = usage - (30 / bill.lineCount)
total = 0
# Distribute overage charges
for number, line of bill.lines
line.shared = bill.plan.total / bill.lineCount
if bill.overage.lines[number]
line.overage = bill.overage.amount * (bill.overage.lines[number] / bill.overage.total)
# Calculate tmp total for distributing surcharges
line.total = _.sum _.values(line)
total += line.total
# Distribute surcharges
for number, line of bill.lines
line.sharedSurcharges = bill.surcharges.total * (line.total / total)
line.total = _.sum _.values(_.omit(line, 'total'))
# Compute grand total for sanity check
bill.total = _.sumBy _.values(bill.lines), 'total'
# Render report page
res.render 'index', {bill}
app.get '/data', (req, res, next) ->
try
content = fs.readFileSync './data.json', 'utf8'
data = JSON.parse(content)
res.send data
catch e
res.send {status: 'not ready'}
app.post '/data', (req, res, next) ->
updatedAt = moment().format('MM-DD-YYYY')
fs.writeFileSync './data.json', JSON.stringify(req.body, null, 2)
res.status(200).end()
app.get '/update', (req, res, next) ->
try
# Delete data.json
fs.unlinkSync './data.json'
catch
# Ignore error for file not exists
# Start scraping script
cp.exec 'phantomjs phantom.js', (err) ->
throw err if err
console.log "Finish scraping"
# Redirect to the loading page
res.redirect '/'
app.listen 3500, ->
console.log "Listening on port 3500"