-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathv2.go
263 lines (219 loc) · 5.81 KB
/
v2.go
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
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/labstack/echo/v4"
)
type Number float64
func (n Number) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("%.2f", n)), nil
}
func dbPool() *pgxpool.Pool {
connStr := fmt.Sprintf("user=%s dbname=%s pool_min_conns=5 pool_max_conns=15", os.Getenv("PG_USER"), os.Getenv("PG_DB"))
ssl_mode := os.Getenv("PG_SSLMODE")
password := os.Getenv("PG_PASSWORD")
hostname := os.Getenv("PG_HOSTNAME")
if ssl_mode != "" {
connStr = connStr + fmt.Sprintf(" sslmode=%s", ssl_mode)
}
if password != "" {
connStr = connStr + fmt.Sprintf(" password=%s", password)
}
if hostname != "" {
connStr = connStr + fmt.Sprintf(" host=%s", hostname)
}
pgConfig, err := pgxpool.ParseConfig(connStr)
if err != nil {
log.Fatal(err)
}
pool, err := pgxpool.ConnectConfig(context.Background(), pgConfig)
if err != nil {
log.Fatal(err)
}
return pool
}
type MonedaValues struct {
Value_avg Number `json:"value_avg"`
Value_sell Number `json:"value_sell"`
Value_buy Number `json:"value_buy"`
}
type Dolares struct {
Oficial MonedaValues `json:"oficial"`
Blue MonedaValues `json:"blue"`
Oficial_euro MonedaValues `json:"oficial_euro"`
Blue_euro MonedaValues `json:"blue_euro"`
Last_update time.Time `json:"last_update"`
}
func getDolarData(db *pgxpool.Pool) Dolares {
var res Dolares
rows, err := db.Query(context.Background(), `
select tipo, updated_at, value_buy, value_sell, euro_buy, euro_sell
from dolar_latest
`)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
for rows.Next() {
var name string
var updated_at time.Time
var dolar_buy Number
var dolar_sell Number
var euro_buy Number
var euro_sell Number
if err := rows.Scan(&name, &updated_at, &dolar_buy, &dolar_sell, &euro_buy, &euro_sell); err != nil {
log.Fatal(err)
}
if name == "oficial" {
res.Oficial.Value_buy = dolar_buy
res.Oficial.Value_sell = dolar_sell
res.Oficial.Value_avg = (dolar_sell + dolar_buy) / 2
res.Oficial_euro.Value_buy = euro_buy
res.Oficial_euro.Value_sell = euro_sell
res.Oficial_euro.Value_avg = (euro_sell + euro_buy) / 2
} else {
tz, err := time.LoadLocation("America/Buenos_Aires")
if err != nil {
log.Fatal(err)
}
res.Last_update = updated_at.In(tz)
res.Blue.Value_buy = dolar_buy
res.Blue.Value_sell = dolar_sell
res.Blue.Value_avg = (dolar_sell + dolar_buy) / 2
res.Blue_euro.Value_buy = euro_buy
res.Blue_euro.Value_sell = euro_sell
res.Blue_euro.Value_avg = (euro_sell + euro_buy) / 2
}
}
return res
}
func v2_latest(c echo.Context, db *pgxpool.Pool) error {
dolar := getDolarData(db)
callback := c.QueryParam("callback")
if callback != "" {
return c.JSONP(http.StatusOK, callback, dolar)
} else {
return c.JSON(http.StatusOK, dolar)
}
}
type Alertas struct {
Id uint32 `json:"alert_id"`
Alert_at time.Time `json:"alert_at"`
Current MonedaValues `json:"current"`
Previous MonedaValues `json:"previous"`
}
func getAlertsData(db *pgxpool.Pool) Alertas {
var res Alertas
rows, err := db.Query(context.Background(), `
with ranked as (
select
id,
alert_at,
value_buy,
value_sell,
(value_buy+value_sell)/2 as value_avg,
lag(value_buy) over (order by alert_at) as last_value_sell,
lag(value_sell) over (order by alert_at) as last_value_buy,
lag((value_buy+value_sell)/2) over (order by alert_at) as last_value_avg,
row_number() over(order by alert_at desc) rnk
from
alertas_dolar
)
select id, alert_at, value_buy, value_sell, value_avg, last_value_sell, last_value_buy, last_value_avg
from ranked
where rnk = 1
`)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
for rows.Next() {
err := rows.Scan(
&res.Id,
&res.Alert_at,
&res.Current.Value_buy,
&res.Current.Value_sell,
&res.Current.Value_avg,
&res.Previous.Value_buy,
&res.Previous.Value_sell,
&res.Previous.Value_avg)
if err != nil {
log.Fatal(err)
}
}
return res
}
func v2_alerts(c echo.Context, db *pgxpool.Pool) error {
alert := getAlertsData(db)
return c.JSON(http.StatusOK, alert)
}
// Dolares historical
type DolaresHistorical struct {
Oficial MonedaValues `json:"oficial"`
Blue MonedaValues `json:"blue"`
}
type ErrorJson struct {
Error string `json:"error"`
}
func getHistoricalData(day string, db *pgxpool.Pool) (DolaresHistorical, error) {
var res DolaresHistorical
date, err := time.Parse("2006-01-02", day)
if err != nil {
return res, fmt.Errorf("parsing day parameter: use YYYY-MM-DD")
}
if date.Weekday() == time.Saturday {
// Use friday's data
date = date.AddDate(0, 0, -1)
}
if date.Weekday() == time.Sunday {
// Use friday's data
date = date.AddDate(0, 0, -2)
}
rows, err := db.Query(context.Background(), `
select
lower(tipo) as tipo, value_buy, value_sell
from
dolar_evolution
where dttm = $1
`, date)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
for rows.Next() {
var name string
var dolar_buy Number
var dolar_sell Number
if err := rows.Scan(&name, &dolar_buy, &dolar_sell); err != nil {
log.Fatal(err)
}
if name == "oficial" {
res.Oficial.Value_buy = dolar_buy
res.Oficial.Value_sell = dolar_sell
res.Oficial.Value_avg = (dolar_sell + dolar_buy) / 2
} else {
res.Blue.Value_buy = dolar_buy
res.Blue.Value_sell = dolar_sell
res.Blue.Value_avg = (dolar_sell + dolar_buy) / 2
}
}
if res.Blue.Value_sell == 0 {
return res, fmt.Errorf("day not found")
}
return res, nil
}
func v2_historical(c echo.Context, db *pgxpool.Pool) error {
day := c.QueryParam("day")
dolarhistorico, err := getHistoricalData(day, db)
if err != nil {
var errjson ErrorJson
errjson.Error = fmt.Sprintf("Error: %s", err.Error())
return c.JSON(http.StatusNotFound, errjson)
}
return c.JSON(http.StatusOK, dolarhistorico)
}