support logging zip changes
parent
64f1488a91
commit
2b420252d5
81
main.go
81
main.go
|
|
@ -19,6 +19,7 @@ import (
|
|||
)
|
||||
|
||||
var stateFinder = regexp.MustCompile(`[A-Za-z]+`)
|
||||
var zipFinder = regexp.MustCompile(`[0-9]{5}[0-9]*`)
|
||||
|
||||
func main() {
|
||||
if err := _main(); err != nil {
|
||||
|
|
@ -81,7 +82,7 @@ func matrixrecv() error {
|
|||
if !printed {
|
||||
if _, err := db.Get(key); err == storage.ErrNotFound {
|
||||
logtr.Debugf("sending help")
|
||||
help := fmt.Sprintf("commands:\n...`!help` print this help\n...`!state nc NC nC Nc` set states for self\n...`!available 2022-12-31` set date self is available for work\n\nrun a command for someone else: `!state ga @caleb`")
|
||||
help := fmt.Sprintf("commands:\n...`!help` print this help\n...`!zip 27006 84058` to set zip codes for self\n...`!state nc NC nC Nc` set states for self\n...`!available 2022-12-31` set date self is available for work\n\nrun a command for someone else: `!state ga @caleb`")
|
||||
if err := sender.Send(help); err != nil {
|
||||
logtr.Errorf("failed to send help: %v", err)
|
||||
} else {
|
||||
|
|
@ -98,6 +99,30 @@ func matrixrecv() error {
|
|||
}
|
||||
}
|
||||
}()
|
||||
func() {
|
||||
logtr.Debugf("looking for zips")
|
||||
db := config.Get().DB()
|
||||
zips := map[string]map[string]struct{}{}
|
||||
for _, msg := range messages {
|
||||
key := fmt.Sprintf("zips_%d", msg.Timestamp.Unix())
|
||||
if !strings.HasPrefix(msg.Content, "!zip") {
|
||||
continue
|
||||
}
|
||||
if _, ok := zips[msg.Sender]; ok {
|
||||
continue
|
||||
}
|
||||
if _, err := db.Get(key); err == storage.ErrNotFound {
|
||||
zips[msg.Sender] = map[string]struct{}{}
|
||||
for _, zip := range parseOutZips([]byte(msg.Content)) {
|
||||
zips[msg.Sender][zip] = struct{}{}
|
||||
}
|
||||
}
|
||||
if err := db.Set(key, []byte{'k'}); err != nil {
|
||||
logtr.Errorf("failed to mark state gathered @%s: %v", key, err)
|
||||
}
|
||||
}
|
||||
setNewZips(zips)
|
||||
}()
|
||||
func() {
|
||||
logtr.Debugf("looking for states")
|
||||
db := config.Get().DB()
|
||||
|
|
@ -182,6 +207,41 @@ func setNewPauses(pauses map[string]time.Time) {
|
|||
}
|
||||
}
|
||||
|
||||
func setNewZips(zips map[string]map[string]struct{}) {
|
||||
if len(zips) == 0 {
|
||||
return
|
||||
}
|
||||
conf := *config.Get()
|
||||
changed := map[string][]string{}
|
||||
for client, clientZips := range zips {
|
||||
newzips := []string{}
|
||||
for k := range clientZips {
|
||||
newzips = append(newzips, k)
|
||||
}
|
||||
sort.Slice(newzips, func(i, j int) bool {
|
||||
return newzips[i] < newzips[j]
|
||||
})
|
||||
clientconf := conf.Clients[client]
|
||||
if fmt.Sprint(newzips) == fmt.Sprint(clientconf.Zips) {
|
||||
message.NewMatrix().Send(fmt.Sprintf("%s: still searching for %+v", client, newzips))
|
||||
continue
|
||||
}
|
||||
clientconf.Zips = newzips
|
||||
conf.Clients[client] = clientconf
|
||||
changed[client] = newzips
|
||||
}
|
||||
if len(changed) == 0 {
|
||||
return
|
||||
}
|
||||
logtr.Infof("updating config new zips: %+v", conf)
|
||||
config.Set(conf)
|
||||
for client, zips := range changed {
|
||||
if err := sendNewZips(client, zips); err != nil {
|
||||
logtr.Errorf("failed to send new zips %s/%+v: %v", client, zips, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func setNewStates(states map[string]map[config.State]struct{}) {
|
||||
if len(states) == 0 {
|
||||
return
|
||||
|
|
@ -217,6 +277,18 @@ func setNewStates(states map[string]map[config.State]struct{}) {
|
|||
}
|
||||
}
|
||||
|
||||
func parseOutZips(b []byte) []string {
|
||||
var zips []string
|
||||
candidates := zipFinder.FindAll(b, -1)
|
||||
for _, candidate := range candidates {
|
||||
if len(candidate) != 5 {
|
||||
continue
|
||||
}
|
||||
zips = append(zips, string(candidate))
|
||||
}
|
||||
return zips
|
||||
}
|
||||
|
||||
func parseOutStates(b []byte) []config.State {
|
||||
var states []config.State
|
||||
var state config.State
|
||||
|
|
@ -540,9 +612,14 @@ func sendJob(job broker.Job) (bool, error) {
|
|||
return true, nil
|
||||
}
|
||||
|
||||
func sendNewZips(client string, zips []string) error {
|
||||
sender := message.NewMatrix()
|
||||
return sender.Send(fmt.Sprintf("%s: now searching for loads from zip codes: %+v", client, zips))
|
||||
}
|
||||
|
||||
func sendNewStates(client string, states []config.State) error {
|
||||
sender := message.NewMatrix()
|
||||
return sender.Send(fmt.Sprintf("%s: now searching for loads from: %+v", client, states))
|
||||
return sender.Send(fmt.Sprintf("%s: now searching for loads from states: %+v", client, states))
|
||||
}
|
||||
|
||||
func sendNewPause(client string, pause time.Time) error {
|
||||
|
|
|
|||
Loading…
Reference in New Issue