Skip to content

Commit c0b882a

Browse files
committed
refactored retrieval of machines
1 parent 147577d commit c0b882a

File tree

4 files changed

+95
-87
lines changed

4 files changed

+95
-87
lines changed
Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,22 @@
11
package filter
22

33
import (
4-
"encoding/json"
5-
64
"github.com/gofiber/fiber/v2"
75
"github.com/typomedia/patchouli/app/store/boltdb"
8-
"github.com/typomedia/patchouli/app/structs"
96
)
107

118
func Operator(c *fiber.Ctx) error {
12-
id := c.Params("id")
9+
operatorId := c.Params("id")
1310
db := boltdb.New()
1411

15-
// set bucket
16-
err := db.SetBucket("machine")
12+
machines, err := db.GetAllMachinesByOperatorId(operatorId)
1713
if err != nil {
1814
return err
1915
}
2016

21-
machines, _ := db.GetAllByOperatorId(id, "machine")
22-
23-
Machines := structs.Machines{}
24-
25-
for _, v := range machines {
26-
machine := structs.Machine{}
27-
err = json.Unmarshal(v, &machine)
28-
if err != nil {
29-
return err
30-
}
31-
Machines = append(Machines, machine)
32-
33-
}
34-
3517
defer db.Close()
3618

3719
return c.Render("app/views/machine/list", fiber.Map{
38-
"Machines": Machines,
20+
"Machines": machines,
3921
})
4022
}

app/handler/machine/list.go

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package machine
22

33
import (
4-
"encoding/json"
5-
"github.com/typomedia/patchouli/app"
6-
74
"github.com/gofiber/fiber/v2"
85
"github.com/typomedia/patchouli/app/store/boltdb"
96
"github.com/typomedia/patchouli/app/structs"
@@ -12,37 +9,20 @@ import (
129
func List(c *fiber.Ctx) error {
1310
db := boltdb.New()
1411

15-
config := app.GetApp().Config
16-
17-
// set bucket
18-
err := db.SetBucket("machine")
19-
if err != nil {
20-
return err
21-
}
22-
23-
machines, _ := db.GetAll("machine")
12+
machines, _ := db.GetAllMachines(false)
2413

25-
Machines := structs.Machines{}
26-
inactiveMachines := structs.Machines{}
14+
var Machines, active, inactive structs.Machines
2715

28-
for _, v := range machines {
29-
machine := structs.Machine{}
30-
err = json.Unmarshal(v, &machine)
31-
if err != nil {
32-
return err
33-
}
34-
if machine.Interval == 0 {
35-
machine.Interval = config.General.Interval
36-
}
16+
for _, machine := range machines {
3717
if machine.Inactive {
38-
inactiveMachines = append(inactiveMachines, machine)
18+
inactive = append(inactive, machine)
3919
} else {
40-
Machines = append(Machines, machine)
20+
active = append(active, machine)
4121

4222
}
4323
}
4424

45-
Machines = append(Machines, inactiveMachines...)
25+
Machines = append(active, inactive...)
4626

4727
defer db.Close()
4828

app/handler/update/list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func List(c *fiber.Ctx) error {
2222
log.Error(err)
2323
}
2424

25-
days, _ := db.GetAllByName(id, "history")
25+
days, _ := db.GetAllUpdatesByMachineId(id)
2626

2727
Updates := structs.Updates{}
2828

app/store/boltdb/boltdb.go

Lines changed: 85 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,10 @@ func (bolt *Bolt) GetAll(bucket string) ([][]byte, error) {
144144
return result, err
145145
}
146146

147-
func (bolt *Bolt) GetAllByName(id string, bucket string) ([][]byte, error) {
147+
func (bolt *Bolt) GetAllUpdatesByMachineId(id string) ([][]byte, error) {
148148
var result [][]byte
149149
err := bolt.db.View(func(tx *bbolt.Tx) error {
150-
bucket := tx.Bucket([]byte(bucket))
150+
bucket := tx.Bucket([]byte("history"))
151151
cursor := bucket.Cursor()
152152
for k, v := cursor.First(); k != nil; k, v = cursor.Next() {
153153
var update structs.Update
@@ -164,54 +164,64 @@ func (bolt *Bolt) GetAllByName(id string, bucket string) ([][]byte, error) {
164164
return result, err
165165
}
166166

167-
func (bolt *Bolt) GetAllByOperatorId(id string, bucket string) ([][]byte, error) {
168-
var result [][]byte
169-
err := bolt.db.View(func(tx *bbolt.Tx) error {
170-
bucket := tx.Bucket([]byte(bucket))
171-
cursor := bucket.Cursor()
172-
for k, v := cursor.First(); k != nil; k, v = cursor.Next() {
173-
var machine structs.Machine
174-
err := json.Unmarshal(v, &machine)
175-
if err != nil {
176-
return err
177-
}
178-
if machine.Operator.Id == id {
179-
result = append(result, v)
180-
}
167+
func (bolt *Bolt) GetAllMachinesByOperatorId(id string) (structs.Machines, error) {
168+
var result structs.Machines
169+
machines, err := bolt.GetAllMachines(false)
170+
if err != nil {
171+
return nil, err
172+
}
173+
for _, m := range machines {
174+
if m.Operator.Id != id {
175+
continue
181176
}
182-
return nil
183-
})
184-
return result, err
177+
result = append(result, m)
178+
}
179+
return result, nil
185180
}
186181

187-
func (bolt *Bolt) GetActiveMachines() (structs.Machines, error) {
188-
machines, _ := bolt.GetAll("machine")
182+
func (bolt *Bolt) GetAllMachinesBySystemId(systemId string) (structs.Machines, error) {
183+
var result structs.Machines
184+
machines, err := bolt.GetAllMachines(false)
185+
186+
for _, m := range machines {
187+
if m.System.Id != systemId {
188+
continue
189+
}
190+
result = append(result, m)
191+
}
192+
return result, err
193+
}
189194

190-
Machines := structs.Machines{}
195+
func (bolt *Bolt) GetAllMachines(onlyActive bool) (structs.Machines, error) {
196+
var result structs.Machines
197+
config, err := bolt.GetConfig()
198+
machines, err := bolt.GetAll("machine")
199+
if err != nil {
200+
return nil, err
201+
}
191202
for _, v := range machines {
192-
machine := structs.Machine{}
193-
err := json.Unmarshal(v, &machine)
203+
m := structs.Machine{}
204+
err = json.Unmarshal(v, &m)
194205
if err != nil {
195206
return nil, err
196207
}
197-
198-
lastUpdate, _ := bolt.GetLastByName(machine.Id, "history")
199-
200-
if lastUpdate != nil {
201-
update := structs.Update{}
202-
err = json.Unmarshal(lastUpdate, &update)
203-
if err != nil {
204-
return nil, err
205-
}
206-
machine.Update = update
208+
if m.Inactive && onlyActive {
209+
continue
207210
}
208-
209-
if !machine.Inactive {
210-
Machines = append(Machines, machine)
211+
if m.Interval == 0 {
212+
m.Interval = config.General.Interval
211213
}
214+
result = append(result, m)
215+
}
216+
217+
return result, nil
218+
}
219+
220+
func (bolt *Bolt) GetActiveMachines() (structs.Machines, error) {
221+
Machines, err := bolt.GetAllMachines(true)
222+
if err != nil {
223+
return nil, err
212224
}
213-
// sort machines by oldest update first
214-
sort.Sort(structs.ByDate(Machines))
215225

216226
config, err := bolt.GetConfig()
217227
if err != nil {
@@ -221,6 +231,17 @@ func (bolt *Bolt) GetActiveMachines() (structs.Machines, error) {
221231
for i := range Machines {
222232
currentDate := time.Now()
223233

234+
lastUpdate, _ := bolt.GetLastByName(Machines[i].Id, "history")
235+
236+
if lastUpdate != nil {
237+
update := structs.Update{}
238+
err = json.Unmarshal(lastUpdate, &update)
239+
if err != nil {
240+
return nil, err
241+
}
242+
Machines[i].Update = update
243+
}
244+
224245
if Machines[i].Update.Date == "" {
225246
Machines[i].Update.Date = "0000-00-00"
226247
Machines[i].Status = "danger"
@@ -254,6 +275,11 @@ func (bolt *Bolt) GetActiveMachines() (structs.Machines, error) {
254275

255276
}
256277

278+
// sort machines by oldest update first
279+
sort.Slice(Machines, func(i, j int) bool {
280+
return Machines[i].Update.Date < Machines[j].Update.Date
281+
})
282+
257283
return Machines, nil
258284
}
259285

@@ -287,6 +313,26 @@ func (bolt *Bolt) GetOperatorById(id string) (structs.Operator, error) {
287313
return operator, nil
288314
}
289315

316+
func (bolt *Bolt) GetMachinesBySystem(systemId string) (structs.Machines, error) {
317+
machines, err := bolt.GetAll("machine")
318+
if err != nil {
319+
return nil, err
320+
}
321+
machinesOfSystem := structs.Machines{}
322+
for _, v := range machines {
323+
machine := structs.Machine{}
324+
err := json.Unmarshal(v, &machine)
325+
if err != nil {
326+
return nil, err
327+
}
328+
if machine.System.Id == systemId {
329+
machinesOfSystem = append(machinesOfSystem, machine)
330+
}
331+
}
332+
333+
return machinesOfSystem, nil
334+
}
335+
290336
func (bolt *Bolt) Close() error {
291337
return bolt.db.Close()
292338
}

0 commit comments

Comments
 (0)