-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache_dd.go
191 lines (151 loc) · 5.67 KB
/
cache_dd.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
//go:generate mockgen -source cache_dd.go -destination mocks/cache_dd_mock.go -package mocks
package services
import (
"context"
"encoding/json"
"fmt"
"math/big"
"time"
"github.com/DIMO-Network/device-definitions-api/internal/core/common"
"github.com/DIMO-Network/device-definitions-api/internal/core/models"
repoModel "github.com/DIMO-Network/device-definitions-api/internal/infrastructure/db/models"
"github.com/DIMO-Network/device-definitions-api/internal/infrastructure/db/repositories"
"github.com/DIMO-Network/shared/redis"
)
type DeviceDefinitionCacheService interface {
GetDeviceDefinitionByID(ctx context.Context, id string) (*models.GetDeviceDefinitionQueryResult, error)
GetDeviceDefinitionByMakeModelAndYears(ctx context.Context, make string, model string, year int) (*models.GetDeviceDefinitionQueryResult, error)
DeleteDeviceDefinitionCacheByID(ctx context.Context, id string)
DeleteDeviceDefinitionCacheByMakeModelAndYears(ctx context.Context, make string, model string, year int)
}
type deviceDefinitionCacheService struct {
Cache redis.CacheService
Repository repositories.DeviceDefinitionRepository
}
func NewDeviceDefinitionCacheService(cache redis.CacheService, repository repositories.DeviceDefinitionRepository) DeviceDefinitionCacheService {
return &deviceDefinitionCacheService{Cache: cache, Repository: repository}
}
const (
cacheLenghtHours = 48
cacheDeviceDefinitionKey = "device-definition-by-id-"
cacheDeviceDefinitionMMYKey = "device-definition-by-mmy-"
)
func (c deviceDefinitionCacheService) GetDeviceDefinitionByID(ctx context.Context, id string) (*models.GetDeviceDefinitionQueryResult, error) {
cache := fmt.Sprintf("%s-%s", cacheDeviceDefinitionKey, id)
cacheData := c.Cache.Get(ctx, cache)
rp := &models.GetDeviceDefinitionQueryResult{}
if cacheData != nil {
val, _ := cacheData.Bytes()
if val != nil {
_ = json.Unmarshal(val, rp)
return rp, nil
}
}
dd, err := c.Repository.GetByID(ctx, id)
if err != nil {
return nil, err
}
if dd == nil {
return nil, nil
}
rp = buildDeviceDefinitionResult(dd)
rpJSON, _ := json.Marshal(rp)
_ = c.Cache.Set(ctx, cache, rpJSON, cacheLenghtHours*time.Hour)
return rp, nil
}
func (c deviceDefinitionCacheService) DeleteDeviceDefinitionCacheByID(ctx context.Context, id string) {
cache := fmt.Sprintf("%s-%s", cacheDeviceDefinitionKey, id)
c.Cache.Del(ctx, cache)
}
func (c deviceDefinitionCacheService) DeleteDeviceDefinitionCacheByMakeModelAndYears(ctx context.Context, make string, model string, year int) {
cache := fmt.Sprintf("%s-%s-%s-%d", cacheDeviceDefinitionMMYKey, make, model, year)
c.Cache.Del(ctx, cache)
}
func (c deviceDefinitionCacheService) GetDeviceDefinitionByMakeModelAndYears(ctx context.Context, make string, model string, year int) (*models.GetDeviceDefinitionQueryResult, error) {
cache := fmt.Sprintf("%s-%s-%s-%d", cacheDeviceDefinitionMMYKey, make, model, year)
cacheData := c.Cache.Get(ctx, cache)
rp := &models.GetDeviceDefinitionQueryResult{}
if cacheData != nil {
val, _ := cacheData.Bytes()
if val != nil {
_ = json.Unmarshal(val, rp)
return rp, nil
}
}
dd, err := c.Repository.GetByMakeModelAndYears(ctx, make, model, year, true)
if err != nil {
return nil, err
}
if dd == nil {
return nil, nil
}
rp = buildDeviceDefinitionResult(dd)
rpJSON, _ := json.Marshal(rp)
_ = c.Cache.Set(ctx, cache, rpJSON, cacheLenghtHours*time.Hour)
return rp, nil
}
func buildDeviceDefinitionResult(dd *repoModel.DeviceDefinition) *models.GetDeviceDefinitionQueryResult {
rp := &models.GetDeviceDefinitionQueryResult{
DeviceDefinitionID: dd.ID,
Name: fmt.Sprintf("%d %s %s", dd.Year, dd.R.DeviceMake.Name, dd.Model),
ImageURL: dd.ImageURL.String,
Source: dd.Source.String,
DeviceMake: models.DeviceMake{
ID: dd.R.DeviceMake.ID,
Name: dd.R.DeviceMake.Name,
LogoURL: dd.R.DeviceMake.LogoURL,
OemPlatformName: dd.R.DeviceMake.OemPlatformName,
NameSlug: dd.R.DeviceMake.NameSlug.String,
},
Type: models.DeviceType{
Type: "Vehicle",
Make: dd.R.DeviceMake.Name,
Model: dd.Model,
Year: int(dd.Year),
MakeSlug: dd.R.DeviceMake.NameSlug.String,
ModelSlug: dd.ModelSlug.String,
},
Metadata: string(dd.Metadata.JSON),
Verified: dd.Verified,
}
if !dd.R.DeviceMake.TokenID.IsZero() {
rp.DeviceMake.TokenID = dd.R.DeviceMake.TokenID.Big.Int(new(big.Int))
}
// vehicle info
var vi map[string]models.VehicleInfo
if err := dd.Metadata.Unmarshal(&vi); err == nil {
rp.VehicleInfo = vi["vehicle_info"]
}
if dd.R != nil {
// sub_models
rp.Type.SubModels = common.SubModelsFromStylesDB(dd.R.DeviceStyles)
}
// build object for integrations that have all the info
rp.DeviceIntegrations = []models.DeviceIntegration{}
rp.DeviceStyles = []models.DeviceStyle{}
rp.CompatibleIntegrations = []models.DeviceIntegration{}
if dd.R != nil {
for _, di := range dd.R.DeviceIntegrations {
rp.DeviceIntegrations = append(rp.DeviceIntegrations, models.DeviceIntegration{
ID: di.R.Integration.ID,
Type: di.R.Integration.Type,
Style: di.R.Integration.Style,
Vendor: di.R.Integration.Vendor,
Region: di.Region,
Capabilities: common.JSONOrDefault(di.Capabilities),
})
rp.CompatibleIntegrations = rp.DeviceIntegrations
}
for _, ds := range dd.R.DeviceStyles {
rp.DeviceStyles = append(rp.DeviceStyles, models.DeviceStyle{
ID: ds.ID,
DeviceDefinitionID: ds.DeviceDefinitionID,
ExternalStyleID: ds.ExternalStyleID,
Name: ds.Name,
Source: ds.Source,
SubModel: ds.SubModel,
})
}
}
return rp
}