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 stateFinder = regexp.MustCompile(`[A-Za-z]+`)
|
||||||
|
var zipFinder = regexp.MustCompile(`[0-9]{5}[0-9]*`)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
if err := _main(); err != nil {
|
if err := _main(); err != nil {
|
||||||
|
|
@ -81,7 +82,7 @@ func matrixrecv() error {
|
||||||
if !printed {
|
if !printed {
|
||||||
if _, err := db.Get(key); err == storage.ErrNotFound {
|
if _, err := db.Get(key); err == storage.ErrNotFound {
|
||||||
logtr.Debugf("sending help")
|
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 {
|
if err := sender.Send(help); err != nil {
|
||||||
logtr.Errorf("failed to send help: %v", err)
|
logtr.Errorf("failed to send help: %v", err)
|
||||||
} else {
|
} 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() {
|
func() {
|
||||||
logtr.Debugf("looking for states")
|
logtr.Debugf("looking for states")
|
||||||
db := config.Get().DB()
|
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{}) {
|
func setNewStates(states map[string]map[config.State]struct{}) {
|
||||||
if len(states) == 0 {
|
if len(states) == 0 {
|
||||||
return
|
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 {
|
func parseOutStates(b []byte) []config.State {
|
||||||
var states []config.State
|
var states []config.State
|
||||||
var state config.State
|
var state config.State
|
||||||
|
|
@ -540,9 +612,14 @@ func sendJob(job broker.Job) (bool, error) {
|
||||||
return true, nil
|
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 {
|
func sendNewStates(client string, states []config.State) error {
|
||||||
sender := message.NewMatrix()
|
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 {
|
func sendNewPause(client string, pause time.Time) error {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue