nidus-sync/platform/event/event.go
Eli Ribble 1e071d5ce5
Overhaul publicreport storage layer, create unified tables
This is a huge change. I was getting really sick of the split between
nuisance/water tables when more than half of the data they store is
common. I finally bit off the big work of switching it all.

This creates a single unified table, publicreport.report and copies the
existing report data into it. It also ports existing data from the
original tables into the new table.

Along with all of this I also overhauled the system for handling
asynchronous work to use a LISTEN/NOTIFY connection from the database
and a single cache table to avoid ever losing work.
2026-03-18 15:36:20 +00:00

124 lines
2.3 KiB
Go

package event
import (
"time"
"github.com/Gleipnir-Technology/nidus-sync/config"
)
var chanEvents chan<- Envelope
type Event struct {
Resource string `json:"resource"`
Time time.Time `json:"time"`
Type EventType `json:"type"`
URI string `json:"uri"`
}
type Envelope struct {
OrganizationID int32
Event Event
}
func SetEventChannel(chan_events chan<- Envelope) {
chanEvents = chan_events
}
type EventType int
const (
EventTypeCreated EventType = iota
EventTypeDeleted
EventTypeHeartbeat
EventTypeSudo
EventTypeUnknown
EventTypeUpdated
)
func (et EventType) String() string {
switch et {
case EventTypeCreated:
return "created"
case EventTypeDeleted:
return "deleted"
case EventTypeHeartbeat:
return "heartbeat"
case EventTypeSudo:
return "sudo"
case EventTypeUnknown:
return "unknown"
case EventTypeUpdated:
return "updated"
}
return "unknown"
}
func EventTypeFromString(s string) EventType {
switch s {
case "created":
return EventTypeCreated
case "deleted":
return EventTypeDeleted
case "heartbeat":
return EventTypeHeartbeat
case "sudo":
return EventTypeSudo
case "updated":
return EventTypeUpdated
default:
return EventTypeUnknown
}
}
type ResourceType int
const (
TypeUnknown = iota
TypeRMONuisance
TypeRMOReport
TypeRMOWater
)
func Created(t ResourceType, organization_id int32, uri_id string) {
go Send(Envelope{
Event: Event{
Resource: resourceString(t),
Time: time.Now(),
Type: EventTypeCreated,
URI: makeURI(t, uri_id),
},
OrganizationID: organization_id,
})
}
func Updated(t ResourceType, organization_id int32, uri_id string) {
go Send(Envelope{
Event: Event{
Resource: resourceString(t),
Time: time.Now(),
Type: EventTypeUpdated,
URI: makeURI(t, uri_id),
},
OrganizationID: organization_id,
})
}
func Send(env Envelope) {
chanEvents <- env
}
func resourceString(t ResourceType) string {
switch t {
case TypeRMONuisance:
return "rmo:nuisance"
case TypeRMOWater:
return "rmo:water"
default:
return "unknown"
}
}
func makeURI(t ResourceType, id string) string {
switch t {
case TypeRMONuisance:
return config.MakeURLReport("/report/%s", id)
case TypeRMOWater:
return config.MakeURLReport("/report/%s", id)
default:
return config.MakeURLReport("/unknown")
}
}