Compare commits

..

18 Commits

Author SHA1 Message Date
bel
db1838b3ab up matrix timeout in http client 2022-02-25 21:59:54 -07:00
bel
ec030676ea update image with not avail over del 2022-02-24 09:26:45 -07:00
Bel LaPointe
3b4dd1ba70 do not delete old jobs text 2022-02-24 09:23:22 -07:00
bel
003ffb1231 bad matrix cert ok 2022-02-02 07:12:18 -07:00
bel
cdabce7a56 SH 2022-01-31 09:35:56 -07:00
bel
0ab534624a NO 2022-01-31 09:31:40 -07:00
bel
b3ce49788b little 2022-01-30 08:43:57 -07:00
bel
bbe839bb88 make working hours, weekdays configurable by impl ntg 2022-01-30 08:34:14 -07:00
bel
60f19968e3 add ntg configurable work hours, weekdays 2022-01-30 08:29:06 -07:00
bel
c62d84b40a cache last ntg result for off hours 2022-01-29 18:59:16 -07:00
bel
1a072fee59 Merge branch 'master' of http://gogs.scratch.com:59515/bel/truckstop 2022-01-29 18:44:21 -07:00
bel
c4213e697d fuck backwards bool 2022-01-29 18:44:13 -07:00
bel
934a306bc9 log 2022-01-29 18:40:13 -07:00
Bel LaPointe
82bdbb1f3b todo 2022-01-27 20:18:13 -07:00
Bel LaPointe
b090cb86a5 fix offset to not need system timezone 2022-01-27 20:04:31 -07:00
Bel LaPointe
e42df54632 only do ntgvision searches 9-4 EST because 417s mean unauth until forced pw reset 2022-01-27 19:53:50 -07:00
Bel LaPointe
0120fdd0e2 accept !radius for miles radius search 2022-01-27 19:21:03 -07:00
Bel LaPointe
eab73aec04 include one very nice log 2022-01-27 19:00:40 -07:00
10 changed files with 172 additions and 22 deletions

View File

@@ -221,7 +221,43 @@ func (ntg NTGVision) SearchZips(zips []string) ([]Job, error) {
return jobs, nil return jobs, nil
} }
func (ntg NTGVision) workingHours(now time.Time) bool {
// TODO assert M-F 9-4 EST
now = now.In(time.FixedZone("EST", -5*60*60))
working := config.Get().Brokers.NTG.Working
logtr.Debugf("ntg.workingHours: now=%s, weekday=%v, hour=%v (ok=%+v)", now.String(), now.Weekday(), now.Hour(), working)
if ok := func() bool {
for _, hr := range working.Hours {
if now.Hour() == hr {
return true
}
}
return false
}(); !ok {
return false
}
if ok := func() bool {
for _, weekday := range working.Weekdays {
if now.Weekday() == time.Weekday(weekday) {
return true
}
}
return false
}(); !ok {
return false
}
return true
}
func (ntg NTGVision) SearchStates(states []config.State) ([]Job, error) { func (ntg NTGVision) SearchStates(states []config.State) ([]Job, error) {
if !ntg.workingHours(time.Now()) {
lastNtgB, _ := config.Get().DB().Get("ntg_last_search_states")
var jobs []Job
json.Unmarshal(lastNtgB, &jobs)
logtr.Verbosef("ntg.SearchStates: outside of working hours so returning ntg_last_search_states: %+v", jobs)
return jobs, nil
}
rc, err := ntg.searcher.searchStates(states) rc, err := ntg.searcher.searchStates(states)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -237,12 +273,22 @@ func (ntg NTGVision) SearchStates(states []config.State) ([]Job, error) {
var ntgjobs []ntgVisionJob var ntgjobs []ntgVisionJob
err = json.Unmarshal(b, &ntgjobs) err = json.Unmarshal(b, &ntgjobs)
if err != nil {
return nil, err
}
jobs := make([]Job, len(ntgjobs)) jobs := make([]Job, len(ntgjobs))
for i := range jobs { for i := range jobs {
jobs[i] = ntgjobs[i].Job() jobs[i] = ntgjobs[i].Job()
} }
return jobs, err
jobsB, err := json.Marshal(jobs)
if err == nil {
config.Get().DB().Set("ntg_last_search_states", jobsB)
logtr.Verbosef("ntg.SearchStates: in working hours so setting ntg_last_search_states: %+v", jobs)
}
return jobs, nil
} }
func getNTGTokenKey() string { func getNTGTokenKey() string {
@@ -334,7 +380,7 @@ func (ntg NTGVision) _searchStates(states []config.State) (io.ReadCloser, error)
request2, _ := ntg.newRequest(states) request2, _ := ntg.newRequest(states)
requestb, _ := ioutil.ReadAll(request2.Body) requestb, _ := ioutil.ReadAll(request2.Body)
logtr.Debugf("ntg auth bad status: url=%s, status=%v, body=%s, headers=%+v, request=%+v, requestb=%s", request.URL.String(), resp.StatusCode, b, resp.Header, request2, requestb) logtr.Debugf("ntg auth bad status: url=%s, status=%v, body=%s, headers=%+v, request=%+v, requestb=%s", request.URL.String(), resp.StatusCode, b, resp.Header, request2, requestb)
if resp.StatusCode > 400 && resp.StatusCode < 404 { if resp.StatusCode > 400 && resp.StatusCode < 404 || resp.StatusCode == 417 { // TODO wtf is 417 for
logtr.Debugf("ntg auth bad status: err no auth") logtr.Debugf("ntg auth bad status: err no auth")
return nil, ErrNoAuth return nil, ErrNoAuth
} }

22
broker/ntgvision_test.go Normal file
View File

@@ -0,0 +1,22 @@
package broker
import (
"local/truckstop/config"
"os"
"testing"
"time"
)
func TestWorkingHoursNTG(t *testing.T) {
os.Setenv("CONFIG", "../config.json")
if err := config.Refresh(nil); err != nil {
t.Fatal(err)
}
ntg := NTGVision{}
if !ntg.workingHours(time.Date(2022, 1, 27, 12, 0, 0, 0, time.FixedZone("MST", -7*60*60))) {
t.Fatal("noon MST not ok")
}
if ntg.workingHours(time.Date(2022, 1, 27, 23, 0, 0, 0, time.FixedZone("MST", -7*60*60))) {
t.Fatal("midnight MST ok")
}
}

View File

@@ -77,11 +77,15 @@
"Once": false, "Once": false,
"Brokers": { "Brokers": {
"UseZips": true, "UseZips": true,
"RadiusMiles": 300, "RadiusMiles": 200,
"NTG": { "NTG": {
"Enabled": true, "Working": {
"Hours": [6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
"Weekdays": [1, 2, 3, 4, 5]
},
"Enabled": false,
"JobInfo": true, "JobInfo": true,
"Mock": false, "Mock": true,
"LoadPageURIFormat": "https://ntgvision.com/LoadDetails?loadId=%d", "LoadPageURIFormat": "https://ntgvision.com/LoadDetails?loadId=%d",
"LoadPageAPIURIFormat": "https://ntgvision.com/api/v1/load/LoadDetails?loadId=%d", "LoadPageAPIURIFormat": "https://ntgvision.com/api/v1/load/LoadDetails?loadId=%d",
"Username": "noeasyrunstrucking@gmail.com", "Username": "noeasyrunstrucking@gmail.com",
@@ -89,9 +93,9 @@
}, },
"FastExact": { "FastExact": {
"Enabled": true, "Enabled": true,
"Mock": false, "Mock": true,
"Username": "birdman", "Username": "birdman",
"Password": "166647" "Password": "166647"
} }
} }
} }

View File

@@ -83,6 +83,10 @@
"Password": "p" "Password": "p"
}, },
"NTG": { "NTG": {
"Working": {
"Hours": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23],
"Weekdays": [0, 1, 2, 3, 4, 5, 6]
},
"Enabled": true, "Enabled": true,
"JobInfo": true, "JobInfo": true,
"Mock": true, "Mock": true,

View File

@@ -67,6 +67,10 @@ type Config struct {
UseZips bool UseZips bool
RadiusMiles int RadiusMiles int
NTG struct { NTG struct {
Working struct {
Hours []int
Weekdays []int
}
Enabled bool Enabled bool
JobInfo bool JobInfo bool
Mock bool Mock bool

74
main.go
View File

@@ -13,6 +13,7 @@ import (
"net/url" "net/url"
"regexp" "regexp"
"sort" "sort"
"strconv"
"strings" "strings"
"sync" "sync"
"time" "time"
@@ -84,9 +85,9 @@ func matrixrecv() error {
logtr.Debugf("sending help") logtr.Debugf("sending help")
locationHelp := "`!state nc NC nC Nc` set states for self" locationHelp := "`!state nc NC nC Nc` set states for self"
if config.Get().Brokers.UseZips { if config.Get().Brokers.UseZips {
locationHelp = "`!zip 27006 84058` to set zip codes for self" locationHelp = "...`!zip 27006 84058` to set zip codes for self\n...`!radius 100` to set miles radius to 100, 200, or 300"
} }
help := fmt.Sprintf("commands:\n...`!help` print this help\n...%s\n\nrun a command for someone else: `!zip 2022-12-31 @caleb`", locationHelp) help := fmt.Sprintf("commands:\n...`!help` print this help\n%s\n\nrun a command for someone else: `!zip 2022-12-31 @caleb`", locationHelp)
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 {
@@ -125,7 +126,9 @@ func matrixrecv() error {
logtr.Errorf("failed to mark state gathered @%s: %v", key, err) logtr.Errorf("failed to mark state gathered @%s: %v", key, err)
} }
} }
setNewZips(zips) if config.Get().Brokers.UseZips {
setNewZips(zips)
}
}() }()
func() { func() {
logtr.Verbosef("looking for states") logtr.Verbosef("looking for states")
@@ -149,7 +152,34 @@ func matrixrecv() error {
logtr.Errorf("failed to mark state gathered @%s: %v", key, err) logtr.Errorf("failed to mark state gathered @%s: %v", key, err)
} }
} }
setNewStates(states) if !config.Get().Brokers.UseZips {
setNewStates(states)
}
}()
func() {
logtr.Verbosef("looking for radius")
db := config.Get().DB()
var radius *int
for _, msg := range messages {
key := fmt.Sprintf("radius_%d", msg.Timestamp.Unix())
if !strings.HasPrefix(msg.Content, "!radius ") {
continue
}
if _, err := db.Get(key); err == storage.ErrNotFound {
cmd := msg.Content
for strings.HasPrefix(cmd, "!radius ") {
cmd = strings.TrimPrefix(cmd, "!radius ")
}
n, err := strconv.Atoi(strings.TrimSpace(cmd))
if err == nil {
radius = &n
}
}
if err := db.Set(key, []byte{'k'}); err != nil {
logtr.Errorf("failed to mark radius gathered @%s: %v", key, err)
}
}
setNewRadius(radius)
}() }()
func() { func() {
logtr.Verbosef("looking for pauses") logtr.Verbosef("looking for pauses")
@@ -183,6 +213,33 @@ func matrixrecv() error {
return nil return nil
} }
func setNewRadius(radius *int) {
if radius == nil {
return
}
sender := message.NewMatrix()
logtr.Debugf("set new radius: %d", *radius)
acceptable := map[int]struct{}{
100: struct{}{},
200: struct{}{},
300: struct{}{},
}
if _, ok := acceptable[*radius]; !ok {
sender.Send(fmt.Sprintf("radius of %v is not among acceptable %+v", *radius, acceptable))
logtr.Debugf("bad radius, not setting: %d", *radius)
return
}
conf := *config.Get()
if conf.Brokers.RadiusMiles == *radius {
logtr.Debugf("noop radius, not setting: %d", *radius)
return
}
conf.Brokers.RadiusMiles = *radius
logtr.Infof("updating config new pauses: %+v", conf)
config.Set(conf)
sender.Send(fmt.Sprintf("now using radius of %d mi for zip code search", *radius))
}
func setNewPauses(pauses map[string]time.Time) { func setNewPauses(pauses map[string]time.Time) {
if len(pauses) == 0 { if len(pauses) == 0 {
return return
@@ -318,7 +375,7 @@ func __main() error {
logtr.Errorf("failed _main: %v", err) logtr.Errorf("failed _main: %v", err)
} }
if config.Get().Once { if config.Get().Once {
time.Sleep(3 * time.Second) time.Sleep(10 * time.Second)
return err return err
} }
if err != nil { if err != nil {
@@ -469,15 +526,12 @@ func updateDeadJobs(jobs []broker.Job) error {
return err return err
} }
*/ */
if err := message.NewMatrix().Remove(recorded.MatrixID); err != nil {
logtr.Debugf("failed to remove matrix: %v: %v", recorded.MatrixID, err)
}
if err := db.Set(listEntry, nil); err != nil { if err := db.Set(listEntry, nil); err != nil {
logtr.Debugf("failed to remove db: %v: %v", listEntry, err) logtr.Debugf("failed to remove db: %v: %v", listEntry, err)
} }
for _, imageid := range recorded.MatrixImageIDs { for _, imageid := range recorded.MatrixImageIDs {
if err := message.NewMatrix().Remove(imageid); err != nil { if err := message.NewMatrix().Update(imageid, "<job no longer available>"); err != nil {
logtr.Debugf("failed to remove matrix image: %v: %v", imageid, err) logtr.Debugf("failed to update matrix image: %v: %v", imageid, err)
} }
} }
} }

View File

@@ -64,7 +64,7 @@ func uploadImage(b []byte) (string, error) {
request.Header.Set("Authorization", "Bearer "+images.AccessToken) request.Header.Set("Authorization", "Bearer "+images.AccessToken)
request.Header.Set("Content-Type", writer.FormDataContentType()) request.Header.Set("Content-Type", writer.FormDataContentType())
c := &http.Client{Timeout: time.Minute} c := &http.Client{Timeout: time.Minute * 5}
response, err := c.Do(request) response, err := c.Do(request)
if err != nil { if err != nil {
return "", err return "", err

View File

@@ -2,6 +2,7 @@ package message
import ( import (
"bytes" "bytes"
"crypto/tls"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"local/truckstop/config" "local/truckstop/config"
@@ -45,7 +46,19 @@ func newMatrix(conf config.Matrix, cont string) Matrix {
} }
func (m Matrix) getclient() (*gomatrix.Client, error) { func (m Matrix) getclient() (*gomatrix.Client, error) {
return gomatrix.NewClient(m.homeserver, m.username, m.token) client, err := gomatrix.NewClient(m.homeserver, m.username, m.token)
if err != nil {
return nil, err
}
client.Client = &http.Client{
Timeout: time.Minute * 5,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}
return client, nil
} }
func (m Matrix) Continuation() string { func (m Matrix) Continuation() string {

View File

@@ -1,7 +1,4 @@
todo: todo:
- fast exact does not use ID in UID because they spammy
- from states to zip codes w/ range
- modify old items once no longer available; drop stale jobs good candidate but requires new matrix interaction
- more than NTG; blue one - more than NTG; blue one
- !states emits current state - !states emits current state
- test each !command callbacks to matrix - test each !command callbacks to matrix
@@ -14,7 +11,11 @@ todo:
subtasks: subtasks:
- banlist criteria like vendors, brokers, metadata - banlist criteria like vendors, brokers, metadata
- set up copy for caleb, broc - set up copy for caleb, broc
- move from main() and make more functions
done: done:
- modify old items once no longer available; drop stale jobs good candidate but requires new matrix interaction
- from states to zip codes w/ range
- fast exact does not use ID in UID because they spammy
- fast exact is dumb or...? - fast exact is dumb or...?
- try search ntg by autoinc? - try search ntg by autoinc?
- TEST. Just like, refactor and test to shit. - TEST. Just like, refactor and test to shit.

View File

@@ -2,6 +2,7 @@ package zip
import ( import (
_ "embed" _ "embed"
"local/truckstop/logtr"
"math" "math"
"strconv" "strconv"
"strings" "strings"
@@ -44,6 +45,7 @@ func init() {
State: state, State: state,
} }
} }
logtr.Infof("loaded %d zip codes", len(zips))
} }
func alphafy(s string) string { func alphafy(s string) string {