Skip to content

Commit d8e82ed

Browse files
Merge branch 'refactor/clean-up'
2 parents 4dae913 + dfe9bb4 commit d8e82ed

File tree

10 files changed

+226
-257
lines changed

10 files changed

+226
-257
lines changed

apiModels.go

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,5 @@
11
package main
22

3-
import (
4-
"time"
5-
6-
"github.com/google/uuid"
7-
"gorm.io/datatypes"
8-
)
9-
10-
type apiHis struct {
11-
PointId uuid.UUID `json:"pointId"`
12-
Ts *time.Time `json:"ts"`
13-
Value *float64 `json:"value"`
14-
}
15-
16-
type apiHisItem struct {
17-
Ts *time.Time `json:"ts"`
18-
Value *float64 `json:"value"`
19-
}
20-
21-
type apiRec struct {
22-
ID uuid.UUID `json:"id"`
23-
Tags datatypes.JSON `json:"tags"`
24-
Dis *string `json:"dis"`
25-
Unit *string `json:"unit"`
26-
}
27-
28-
type apiCurrentInput struct {
29-
Value *float64 `json:"value"`
30-
}
31-
32-
type apiCurrent struct {
33-
Ts *time.Time `json:"ts"`
34-
Value *float64 `json:"value"`
35-
}
36-
373
type clientToken struct {
384
Token string `json:"token"`
395
}

currentController.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func (h currentController) postCurrent(writer http.ResponseWriter, request *http
5353
return
5454
}
5555
decoder := json.NewDecoder(request.Body)
56-
var currentItem apiCurrentInput
56+
var currentItem currentInput
5757
err = decoder.Decode(&currentItem)
5858
if err != nil {
5959
log.Printf("Cannot decode request JSON: %s", err)

currentStore.go

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,52 @@
11
package main
22

33
import (
4+
"sync"
45
"time"
56

67
"github.com/google/uuid"
78
)
89

910
// currentStore is able to store point current values
1011
type currentStore interface {
11-
getCurrent(uuid.UUID) apiCurrent
12-
setCurrent(uuid.UUID, apiCurrentInput)
12+
getCurrent(uuid.UUID) current
13+
setCurrent(uuid.UUID, currentInput)
14+
}
15+
16+
type currentInput struct {
17+
Value *float64 `json:"value"`
18+
}
19+
20+
type current struct {
21+
Ts *time.Time `json:"ts"`
22+
Value *float64 `json:"value"`
1323
}
1424

1525
// inMemoryCurrentStore stores point current values in a local in-memory cache.
1626
// These are not shared between instances.
1727
type inMemoryCurrentStore struct {
18-
cache map[uuid.UUID]apiCurrent
28+
mux *sync.Mutex
29+
cache map[uuid.UUID]current
1930
}
2031

2132
func newInMemoryCurrentStore() inMemoryCurrentStore {
22-
return inMemoryCurrentStore{cache: map[uuid.UUID]apiCurrent{}}
33+
return inMemoryCurrentStore{
34+
mux: &sync.Mutex{},
35+
cache: map[uuid.UUID]current{},
36+
}
2337
}
2438

25-
func (s inMemoryCurrentStore) getCurrent(id uuid.UUID) apiCurrent {
39+
func (s inMemoryCurrentStore) getCurrent(id uuid.UUID) current {
40+
s.mux.Lock()
41+
defer s.mux.Unlock()
2642
return s.cache[id]
2743
}
2844

29-
func (s inMemoryCurrentStore) setCurrent(id uuid.UUID, input apiCurrentInput) {
45+
func (s inMemoryCurrentStore) setCurrent(id uuid.UUID, input currentInput) {
3046
timestamp := time.Now()
31-
s.cache[id] = apiCurrent{
47+
s.mux.Lock()
48+
defer s.mux.Unlock()
49+
s.cache[id] = current{
3250
Ts: &timestamp,
3351
Value: input.Value,
3452
}

dbModels.go

Lines changed: 0 additions & 25 deletions
This file was deleted.

hisController.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func (h hisController) postHis(writer http.ResponseWriter, request *http.Request
8686
return
8787
}
8888
decoder := json.NewDecoder(request.Body)
89-
var hisItem apiHisItem
89+
var hisItem hisItem
9090
err = decoder.Decode(&hisItem)
9191
if err != nil {
9292
log.Printf("Cannot decode request JSON: %s", err)

historyStore.go

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,16 @@ import (
1010

1111
// historyStore is able to store point historical values
1212
type historyStore interface {
13-
readHistory(uuid.UUID, *time.Time, *time.Time) ([]apiHisItem, error)
14-
writeHistory(uuid.UUID, apiHisItem) error
13+
readHistory(uuid.UUID, *time.Time, *time.Time) ([]hisItem, error)
14+
writeHistory(uuid.UUID, hisItem) error
1515
deleteHistory(uuid.UUID, *time.Time, *time.Time) error
1616
}
1717

18+
type hisItem struct {
19+
Ts *time.Time `json:"ts"`
20+
Value *float64 `json:"value"`
21+
}
22+
1823
// gormHistoryStore stores point historical values in a GORM database.
1924
type gormHistoryStore struct {
2025
db *gorm.DB
@@ -28,11 +33,11 @@ func (s gormHistoryStore) readHistory(
2833
pointId uuid.UUID,
2934
start *time.Time,
3035
end *time.Time,
31-
) ([]apiHisItem, error) {
32-
result := []apiHisItem{}
36+
) ([]hisItem, error) {
37+
result := []hisItem{}
3338

34-
var sqlResult []his
35-
query := s.db.Where(&his{PointId: pointId})
39+
var sqlResult []gormHis
40+
query := s.db.Where(&gormHis{PointId: pointId})
3641
if start != nil {
3742
query.Where("ts >= ?", start)
3843
}
@@ -44,16 +49,16 @@ func (s gormHistoryStore) readHistory(
4449
return result, err
4550
}
4651
for _, sqlRow := range sqlResult {
47-
result = append(result, apiHisItem{Ts: sqlRow.Ts, Value: sqlRow.Value})
52+
result = append(result, hisItem{Ts: sqlRow.Ts, Value: sqlRow.Value})
4853
}
4954
return result, nil
5055
}
5156

5257
func (s gormHistoryStore) writeHistory(
5358
pointId uuid.UUID,
54-
hisItem apiHisItem,
59+
hisItem hisItem,
5560
) error {
56-
his := his{
61+
gormHis := gormHis{
5762
PointId: pointId,
5863
Ts: hisItem.Ts,
5964
Value: hisItem.Value,
@@ -62,16 +67,16 @@ func (s gormHistoryStore) writeHistory(
6267
return s.db.Clauses(clause.OnConflict{
6368
Columns: []clause.Column{{Name: "pointId"}, {Name: "ts"}},
6469
DoUpdates: clause.AssignmentColumns([]string{"value"}),
65-
}).Create(&his).Error
70+
}).Create(&gormHis).Error
6671
}
6772

6873
func (s gormHistoryStore) deleteHistory(
6974
pointId uuid.UUID,
7075
start *time.Time,
7176
end *time.Time,
7277
) error {
73-
var sqlResult []his
74-
query := s.db.Where(&his{PointId: pointId})
78+
var sqlResult []gormHis
79+
query := s.db.Where(&gormHis{PointId: pointId})
7580
if start != nil {
7681
query.Where("ts >= ?", start)
7782
}
@@ -80,3 +85,9 @@ func (s gormHistoryStore) deleteHistory(
8085
}
8186
return query.Delete(&sqlResult).Error
8287
}
88+
89+
type gormHis struct {
90+
PointId uuid.UUID `gorm:"column:pointId;type:uuid;primaryKey;index:his_pointId_ts_idx"`
91+
Ts *time.Time `gorm:"primaryKey:pk_his;index:his_pointId_ts_idx,sort:desc;index:his_ts_idx,sort:desc"`
92+
Value *float64 `gorm:"type:double precision"`
93+
}

recController.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,22 @@ func (recController recController) getRecs(w http.ResponseWriter, r *http.Reques
3535
// POST /recs
3636
func (recController recController) postRecs(w http.ResponseWriter, r *http.Request) {
3737
decoder := json.NewDecoder(r.Body)
38-
var apiRec apiRec
39-
err := decoder.Decode(&apiRec)
38+
var rec rec
39+
err := decoder.Decode(&rec)
4040
if err != nil {
4141
log.Printf("Cannot decode request JSON: %s", err)
4242
w.WriteHeader(http.StatusBadRequest)
4343
return
4444
}
4545

46-
err = recController.store.createRec(apiRec)
46+
err = recController.store.createRec(rec)
4747
if err != nil {
4848
log.Printf("Storage Error: %s", err)
4949
w.WriteHeader(http.StatusInternalServerError)
5050
return
5151
}
5252

53-
recJSON, err := json.Marshal(apiRec)
53+
recJSON, err := json.Marshal(rec)
5454
if err != nil {
5555
log.Printf("Cannot encode response JSON")
5656
w.WriteHeader(http.StatusBadRequest)
@@ -63,14 +63,14 @@ func (recController recController) postRecs(w http.ResponseWriter, r *http.Reque
6363

6464
// GET /recs/tag/:tag
6565
func (recController recController) getRecsByTag(w http.ResponseWriter, r *http.Request) {
66-
apiRecs, err := recController.store.readRecs("siteMeter")
66+
recs, err := recController.store.readRecs("siteMeter")
6767
if err != nil {
6868
log.Printf("Storage Error: %s", err)
6969
w.WriteHeader(http.StatusInternalServerError)
7070
return
7171
}
7272

73-
httpJson, err := json.Marshal(apiRecs)
73+
httpJson, err := json.Marshal(recs)
7474
if err != nil {
7575
log.Printf("Cannot encode response JSON")
7676
w.WriteHeader(http.StatusBadRequest)
@@ -90,14 +90,14 @@ func (recController recController) getRec(w http.ResponseWriter, r *http.Request
9090
w.WriteHeader(http.StatusNotFound)
9191
return
9292
}
93-
apiRec, err := recController.store.readRec(id)
93+
rec, err := recController.store.readRec(id)
9494
if err != nil {
9595
log.Printf("Storage Error: %s", err)
9696
w.WriteHeader(http.StatusInternalServerError)
9797
return
9898
}
9999

100-
httpJson, err := json.Marshal(apiRec)
100+
httpJson, err := json.Marshal(rec)
101101
if err != nil {
102102
log.Printf("Cannot encode response JSON")
103103
w.WriteHeader(http.StatusBadRequest)
@@ -119,14 +119,14 @@ func (recController recController) putRec(w http.ResponseWriter, r *http.Request
119119
}
120120

121121
decoder := json.NewDecoder(r.Body)
122-
var apiRec apiRec
123-
err = decoder.Decode(&apiRec)
122+
var rec rec
123+
err = decoder.Decode(&rec)
124124
if err != nil {
125125
log.Printf("Cannot decode request JSON: %s", err)
126126
w.WriteHeader(http.StatusBadRequest)
127127
return
128128
}
129-
err = recController.store.updateRec(id, apiRec)
129+
err = recController.store.updateRec(id, rec)
130130
if err != nil {
131131
log.Printf("Storage Error: %s", id)
132132
w.WriteHeader(http.StatusBadRequest)

0 commit comments

Comments
 (0)