Commit ed0a726a authored by Petter Goksøyr Åsen's avatar Petter Goksøyr Åsen
Browse files

notify on removed items

parent 47d25ef5
......@@ -67,7 +67,7 @@ func (c collector) setup() error {
}
// persistUpdated stores updated records on disk
func (c collector) persistUpdated(newRecords map[uint32]record, timestamp time.Time) (nNew, nUpdated int, deleted []uint32, finalErr error) {
func (c collector) persistUpdated(newRecords map[uint32]record, timestamp time.Time) (nNew, nUpdated int, deleted []uint32, delBarcodes []string, finalErr error) {
finalErr = c.db.Update(func(tx *bolt.Tx) error {
cur := tx.Bucket(bktBiblio).Cursor()
......@@ -84,6 +84,7 @@ func (c collector) persistUpdated(newRecords map[uint32]record, timestamp time.T
delete(newRecords, newRec.Biblionumber)
continue
}
delBarcodes = append(delBarcodes, delItems(oldRec, newRec)...)
// Mark record as to be updated. It's not safe to mutate the key/values
// while iterating.
updates[newRec.Biblionumber] = true
......@@ -237,17 +238,20 @@ func (c collector) run() error {
// Persist all updated records to disk
log.Println("Persisting to disk...")
nNew, nUpdated, deletedIDs, err := c.persistUpdated(newRecords, timestamp)
nNew, nUpdated, deletedBiblios, deletedBarcodes, err := c.persistUpdated(newRecords, timestamp)
if err != nil {
log.Fatal(err)
}
for _, id := range deletedIDs {
c.srv.broadcast <- []byte(fmt.Sprintf(msg, "del", id))
for _, id := range deletedBiblios {
c.srv.broadcast <- []byte(fmt.Sprintf(msg, "delBiblio", id))
}
for _, id := range deletedBarcodes {
c.srv.broadcast <- []byte(fmt.Sprintf(msg, "delBarcode", id))
}
log.Printf("Persisted all changes to disk, with timestamp=%v", timestamp.Format(time.RFC3339))
log.Printf("Stats: updated=%d new=%d deleted=%d unchanged=%d", nUpdated, nNew, len(deletedIDs), totalCount-(nUpdated+nNew))
log.Printf("Stats: updated=%d new=%d deleted=%d unchanged=%d", nUpdated, nNew, len(deletedBiblios), totalCount-(nUpdated+nNew))
firstLoop = false
......@@ -300,6 +304,29 @@ func (r record) sameAs(stored record) bool {
return eq
}
func delItems(a, b record) (res []string) {
storedBarcodes := make(map[string]bool)
newBarcodes := make(map[string]bool)
for _, barcode := range a.Items {
storedBarcodes[barcode] = true
}
for _, barcode := range b.Items {
newBarcodes[barcode] = true
}
for k := range storedBarcodes {
if _, found := newBarcodes[k]; !found {
res = append(res, k)
}
}
for k := range newBarcodes {
if _, found := storedBarcodes[k]; !found {
res = append(res, k)
}
}
log.Println(res)
return res
}
const (
sqlPickupsByBiblio = `
SELECT b.biblionumber,
......
......@@ -131,7 +131,8 @@
console.log("ws connection closed")
}
ws.onmessage = function(e) {
console.log(e)
const msg = JSON.parse(e.data)
console.log(msg)
}
</script>
</body>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment