-
Notifications
You must be signed in to change notification settings - Fork 0
/
ad.go
141 lines (120 loc) · 4.13 KB
/
ad.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
package ad
import (
"context"
"fmt"
"net/http"
"time"
"github.com/pkg/errors"
dec "github.com/shopspring/decimal"
"github.com/sku4/ad-parser/model"
client "github.com/sku4/ad-parser/pkg/ad"
clientModel "github.com/sku4/ad-parser/pkg/ad/model"
"github.com/sku4/ad-parser/pkg/logger"
"github.com/tarantool/go-tarantool/v2"
"github.com/tarantool/go-tarantool/v2/datetime"
"github.com/tarantool/go-tarantool/v2/decimal"
"github.com/tarantool/go-tarantool/v2/pool"
)
const (
roundPlaces = 2
)
type Ad struct {
conn pool.Pooler
client *client.Client
}
func NewAd(conn pool.Pooler) *Ad {
return &Ad{
conn: conn,
client: client.NewClient(conn),
}
}
func (ad *Ad) Put(ctx context.Context, modelAd *model.Ad, profileID uint16) error {
var adsTnt []clientModel.AdTnt
extIDSelect := tarantool.NewSelectRequest(clientModel.SpaceAd).
Index(clientModel.IndexExt).
Limit(1).
Iterator(tarantool.IterEq).
Key(tarantool.UintKey{I: uint(modelAd.ExtID)})
err := ad.conn.Do(extIDSelect, pool.PreferRW).GetTyped(&adsTnt)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("put: ext_id select %d", modelAd.ExtID))
}
updated, err := datetime.NewDatetime(time.Now().UTC())
if err != nil {
return errors.Wrap(err, "put: time convert to datetime")
}
// get street id
if modelAd.Street != nil && *modelAd.Street != "" {
streetGetIDTnt, errStreet := ad.client.StreetGetID(ctx, *modelAd.Street)
if errStreet != nil {
return errors.Wrap(errStreet, "put: street.get_id")
}
if streetGetIDTnt.Status == http.StatusOK {
modelAd.StreetID = &streetGetIDTnt.ID
}
}
modelAd.Updated = updated
modelAd.Profile = profileID
if modelAd.Price != nil && modelAd.M2Main != nil && *modelAd.M2Main > 0 {
m2Main := dec.NewFromFloat(*modelAd.M2Main)
modelAd.PriceM2 = decimal.NewDecimal(modelAd.Price.Div(m2Main).Round(roundPlaces))
}
if len(adsTnt) > 0 {
// if ad exists - update u_time
operations := tarantool.NewOperations().
Assign(clientModel.SpaceAdFieldUTime, updated).
Assign(clientModel.SpaceAdFieldLocLat, modelAd.LocLat).
Assign(clientModel.SpaceAdFieldLocLong, modelAd.LocLong).
Assign(clientModel.SpaceAdFieldHouse, modelAd.House).
Assign(clientModel.SpaceAdFieldPrice, modelAd.Price).
Assign(clientModel.SpaceAdFieldPriceM2, modelAd.PriceM2).
Assign(clientModel.SpaceAdFieldRooms, modelAd.Rooms).
Assign(clientModel.SpaceAdFieldFloor, modelAd.Floor).
Assign(clientModel.SpaceAdFieldFloors, modelAd.Floors).
Assign(clientModel.SpaceAdFieldYear, modelAd.Year).
Assign(clientModel.SpaceAdFieldPhotos, modelAd.Photos).
Assign(clientModel.SpaceAdFieldM2Main, modelAd.M2Main).
Assign(clientModel.SpaceAdFieldM2Living, modelAd.M2Living).
Assign(clientModel.SpaceAdFieldM2Kitchen, modelAd.M2Kitchen).
Assign(clientModel.SpaceAdFieldBathroom, modelAd.Bathroom)
if modelAd.StreetID == nil {
operations.Assign(clientModel.SpaceAdFieldStreetID, nil)
} else {
operations.Assign(clientModel.SpaceAdFieldStreetID, uint(*modelAd.StreetID))
}
timeUpdate := tarantool.NewUpdateRequest(clientModel.SpaceAd).
Index(clientModel.IndexExt).
Key(tarantool.UintKey{I: uint(modelAd.ExtID)}).
Operations(operations)
_, errUpd := ad.conn.Do(timeUpdate, pool.RW).Get()
if errUpd != nil {
return errors.Wrap(errUpd, "put: update")
}
return nil
}
// put ad
adTuple, err := modelAd.ConvertToTuple()
if err != nil {
return errors.Wrap(err, "put")
}
callPut := tarantool.NewCallRequest("box.space.ad:put").Args([]interface{}{adTuple})
_, err = ad.conn.Do(callPut, pool.RW).Get()
if err != nil {
return errors.Wrap(err, "put: call")
}
// send broadcast event as put new ad
ad.conn.Do(tarantool.NewBroadcastRequest(clientModel.EventNewAd).Value(true), pool.RO)
return nil
}
func (ad *Ad) Clean(ctx context.Context, timeTo time.Time, profileID uint16) error {
log := logger.Get()
// clean ads
cntClean, err := ad.client.AdsClean(ctx, timeTo, profileID)
if err != nil {
return errors.Wrap(err, "clean: call")
}
profileCode := ad.client.ProfileGetByID(ctx, profileID)
log.Infof("Clean %d tuples before time %s for '%s' profile",
cntClean, timeTo.Format(time.DateTime), profileCode)
return nil
}