this repo has no description

feat: add plc-activity

Changed files
+102
cmd
plc-activity
+102
cmd/plc-activity/main.go
··· 1 + package main 2 + 3 + import ( 4 + "context" 5 + "encoding/json" 6 + "io" 7 + "log" 8 + "net/http" 9 + "strings" 10 + "time" 11 + 12 + "github.com/bluesky-social/indigo/atproto/syntax" 13 + "github.com/redis/go-redis/v9" 14 + ) 15 + 16 + const PlcExportUrl = `https://plc.directory/export` 17 + const PlcOpsCountKey = `dev.edavis.muninsky.plc_ops` 18 + 19 + func main() { 20 + ctx := context.Background() 21 + client := http.DefaultClient 22 + 23 + rdb := redis.NewClient(&redis.Options{ 24 + Addr: "localhost:6379", 25 + Password: "", 26 + DB: 0, 27 + }) 28 + 29 + req, err := http.NewRequestWithContext(ctx, "GET", PlcExportUrl, nil) 30 + if err != nil { 31 + panic(err) 32 + } 33 + 34 + var lastCursor string 35 + var cursor string 36 + cursor = syntax.DatetimeNow().String() 37 + 38 + q := req.URL.Query() 39 + q.Add("count", "1000") 40 + req.URL.RawQuery = q.Encode() 41 + 42 + for { 43 + q := req.URL.Query() 44 + if cursor != "" { 45 + q.Set("after", cursor) 46 + } 47 + req.URL.RawQuery = q.Encode() 48 + 49 + log.Printf("requesting %s\n", req.URL.String()) 50 + resp, err := client.Do(req) 51 + if err != nil { 52 + log.Printf("error doing PLC request: %v\n", err) 53 + } 54 + if resp.StatusCode != http.StatusOK { 55 + log.Printf("PLC request failed status=%d\n", resp.StatusCode) 56 + } 57 + 58 + respBytes, err := io.ReadAll(resp.Body) 59 + if err != nil { 60 + log.Printf("error reading response body: %v\n", err) 61 + } 62 + 63 + lines := strings.Split(string(respBytes), "\n") 64 + if len(lines) == 0 || (len(lines) == 1 && len(lines[0]) == 0) { 65 + time.Sleep(5 * time.Second) 66 + continue 67 + } 68 + 69 + var opCount int64 70 + for _, l := range lines { 71 + if len(l) < 2 { 72 + break 73 + } 74 + 75 + var op map[string]interface{} 76 + err = json.Unmarshal([]byte(l), &op) 77 + if err != nil { 78 + log.Printf("error decoding JSON: %v\n", err) 79 + } 80 + 81 + var ok bool 82 + cursor, ok = op["createdAt"].(string) 83 + if !ok { 84 + log.Printf("missing createdAt") 85 + } 86 + 87 + if cursor == lastCursor { 88 + continue 89 + } 90 + 91 + opCount += 1 92 + lastCursor = cursor 93 + } 94 + 95 + log.Printf("fetched %d operations", opCount) 96 + if _, err := rdb.IncrBy(ctx, PlcOpsCountKey, opCount).Result(); err != nil { 97 + log.Printf("error incrementing op count in redis: %v\n", err) 98 + } 99 + 100 + time.Sleep(5 * time.Second) 101 + } 102 + }