changeset 60:3b0595c2bf03

logging and small refactors
author drewp@bigasterisk.com
date Fri, 06 Sep 2024 16:41:48 -0700
parents 1f8e66cb0108
children 8aee4f5c4bdd
files calsync/cal_sync.go calsync/event_sync.go calsync/gcalclient/gcalclient.go calsync/go.mod calsync/go.sum calsync/main.go calsync/mongoclienttypes/mongoclienttypes.go
diffstat 7 files changed, 35 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/calsync/cal_sync.go	Fri Sep 06 16:37:04 2024 -0700
+++ b/calsync/cal_sync.go	Fri Sep 06 16:41:48 2024 -0700
@@ -6,6 +6,7 @@
 	"bigasterisk.com/go/gcalendarwatch/convert"
 	"bigasterisk.com/go/gcalendarwatch/gcalclient"
 	"bigasterisk.com/go/gcalendarwatch/mongoclient"
+	M "bigasterisk.com/go/gcalendarwatch/mongoclienttypes"
 )
 
 func updateMongoCalsToMatchGoogleOnce(mc *mongoclient.MongoClient, gc *gcalclient.GCalClient) (err error) {
@@ -18,15 +19,14 @@
 		return err
 	}
 
-	for _, cal := range cals {
-		calUrl := gcalclient.MakeCalUrl(cal.Id)
-		log.Println("syncing", calUrl)
-		seen[calUrl] = true
-		err = mc.UpsertOneCal(convert.MongoCalFromGoogleCal(cal))
+	for _, serviceCal := range cals {
+		cal := convert.MongoCalFromGoogleCal(serviceCal)
+		log.Println(M.Prefix(cal), "syncing calendar doc")
+		seen[cal.Url] = true
+		err = mc.UpsertOneCal(cal)
 		if err != nil {
 			return err
 		}
-		log.Println("syncing", calUrl, "done")
 	}
 
 	err = mc.DeleteCalsNotInSet(seen)
--- a/calsync/event_sync.go	Fri Sep 06 16:37:04 2024 -0700
+++ b/calsync/event_sync.go	Fri Sep 06 16:41:48 2024 -0700
@@ -19,6 +19,7 @@
 	gc *gcalclient.GCalClient,
 	keepHistory time.Duration,
 	syncAhead time.Duration) error {
+	log.Println("starting updateMongoEventsToMatchGoogleForever")
 
 	now := time.Now()
 	syncStart := now.Add(-keepHistory)
@@ -35,10 +36,9 @@
 		rd := newCalEventsReader(mc, gc, cal, syncStart, syncEnd)
 		go rd.updateForever()
 	}
-	return nil // return a mapping of webhook name to calEventsReader?
+	return nil
 }
 
-// i think this should own the webhook watcher
 type calEventsReader struct {
 	mc        *mongoclient.MongoClient
 	gc        *gcalclient.GCalClient
--- a/calsync/gcalclient/gcalclient.go	Fri Sep 06 16:37:04 2024 -0700
+++ b/calsync/gcalclient/gcalclient.go	Fri Sep 06 16:41:48 2024 -0700
@@ -58,7 +58,7 @@
 	}
 
 	// todo: do not submit
-	debugFilterCals(list)
+	// debugFilterCals(list)
 
 	return list.Items, nil
 }
@@ -67,7 +67,7 @@
 	log.Println("filtering cal list")
 	ret := make([]*calendar.CalendarListEntry, 0)
 	for _, cal := range list.Items {
-		if strings.Contains(cal.Id, "drewp") || strings.Contains(cal.Id, "east") {
+		if strings.Contains(cal.Id, "drewp") || strings.Contains(cal.Id, "ast") {
 			ret = append(ret, cal)
 		}
 	}
--- a/calsync/go.mod	Fri Sep 06 16:37:04 2024 -0700
+++ b/calsync/go.mod	Fri Sep 06 16:41:48 2024 -0700
@@ -3,9 +3,18 @@
 go 1.21.1
 
 require (
+	github.com/dchest/uniuri v1.2.0
+	github.com/gorilla/mux v1.8.1
+	go.mongodb.org/mongo-driver v1.16.1
+	golang.org/x/oauth2 v0.22.0
+	google.golang.org/api v0.192.0
+)
+
+require (
 	cloud.google.com/go/auth v0.8.1 // indirect
 	cloud.google.com/go/auth/oauth2adapt v0.2.3 // indirect
 	cloud.google.com/go/compute/metadata v0.5.0 // indirect
+	github.com/aquilax/truncate v1.0.0 // indirect
 	github.com/felixge/httpsnoop v1.0.4 // indirect
 	github.com/go-logr/logr v1.4.2 // indirect
 	github.com/go-logr/stdr v1.2.2 // indirect
@@ -15,14 +24,12 @@
 	github.com/google/uuid v1.6.0 // indirect
 	github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
 	github.com/googleapis/gax-go/v2 v2.13.0 // indirect
-	github.com/gorilla/mux v1.8.1 // indirect
 	github.com/klauspost/compress v1.13.6 // indirect
 	github.com/montanaflynn/stats v0.7.1 // indirect
 	github.com/xdg-go/pbkdf2 v1.0.0 // indirect
 	github.com/xdg-go/scram v1.1.2 // indirect
 	github.com/xdg-go/stringprep v1.0.4 // indirect
 	github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
-	go.mongodb.org/mongo-driver v1.16.1 // indirect
 	go.opencensus.io v0.24.0 // indirect
 	go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect
 	go.opentelemetry.io/otel v1.24.0 // indirect
@@ -30,11 +37,9 @@
 	go.opentelemetry.io/otel/trace v1.24.0 // indirect
 	golang.org/x/crypto v0.25.0 // indirect
 	golang.org/x/net v0.27.0 // indirect
-	golang.org/x/oauth2 v0.22.0 // indirect
 	golang.org/x/sync v0.8.0 // indirect
 	golang.org/x/sys v0.22.0 // indirect
 	golang.org/x/text v0.16.0 // indirect
-	google.golang.org/api v0.192.0 // indirect
 	google.golang.org/genproto/googleapis/rpc v0.0.0-20240730163845-b1a4ccb954bf // indirect
 	google.golang.org/grpc v1.64.1 // indirect
 	google.golang.org/protobuf v1.34.2 // indirect
--- a/calsync/go.sum	Fri Sep 06 16:37:04 2024 -0700
+++ b/calsync/go.sum	Fri Sep 06 16:41:48 2024 -0700
@@ -7,11 +7,15 @@
 cloud.google.com/go/compute/metadata v0.5.0 h1:Zr0eK8JbFv6+Wi4ilXAR8FJ3wyNdpxHKJNPos6LTZOY=
 cloud.google.com/go/compute/metadata v0.5.0/go.mod h1:aHnloV2TPI38yx4s9+wAZhHykWvVCfu7hQbF+9CWoiY=
 github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
+github.com/aquilax/truncate v1.0.0 h1:UgIGS8U/aZ4JyOJ2h3xcF5cSQ06+gGBnjxH2RUHJe0U=
+github.com/aquilax/truncate v1.0.0/go.mod h1:BeMESIDMlvlS3bmg4BVvBbbZUNwWtS8uzYPAKXwwhLw=
 github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
 github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
 github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/dchest/uniuri v1.2.0 h1:koIcOUdrTIivZgSLhHQvKgqdWZq5d7KdMEWF1Ud6+5g=
+github.com/dchest/uniuri v1.2.0/go.mod h1:fSzm4SLHzNZvWLvWJew423PhAzkpNQYq+uNLq4kxhkY=
 github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
 github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
 github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
--- a/calsync/main.go	Fri Sep 06 16:37:04 2024 -0700
+++ b/calsync/main.go	Fri Sep 06 16:41:48 2024 -0700
@@ -28,9 +28,9 @@
 )
 
 func main() {
+	startLogging()
 	ctx := context.Background()
 
-	log.SetFlags(log.LstdFlags | log.Lshortfile)
 	gc, err := gcalclient.New(ctx)
 	if err != nil {
 		log.Fatal(err)
@@ -43,7 +43,8 @@
 	}
 	defer mc.Close()
 
-	// todo: if a cal is deleted, nothing touches its db events ever again.
+	// todo: if a cal is deleted, we don't clean up its events, even upon
+	// restart.
 	err = updateMongoCalsToMatchGoogleOnce(mc, gc)
 	if err != nil {
 		log.Fatal(err)
@@ -68,6 +69,12 @@
 	log.Fatal(http.ListenAndServe(addr, nil))
 }
 
+func startLogging() {
+	log.SetFlags(log.LstdFlags | log.Lshortfile)
+	log.Println(`
+	==================================================
+                    calsync
+					----------------------------------------------`)
 }
 
 func homePage(w http.ResponseWriter, r *http.Request) {
--- a/calsync/mongoclienttypes/mongoclienttypes.go	Fri Sep 06 16:37:04 2024 -0700
+++ b/calsync/mongoclienttypes/mongoclienttypes.go	Fri Sep 06 16:41:48 2024 -0700
@@ -1,5 +1,7 @@
 package mongoclienttypes
 
+import "github.com/aquilax/truncate"
+
 // Docs in calsCollection.
 type MongoCal struct {
 	Url         string `bson:"_id"` // bigasterisk.com/...
@@ -10,5 +12,5 @@
 
 // For logging.
 func Prefix(cal MongoCal) string {
-	return "<cal " + (cal.GoogleId + "........")[:8] + ">"
+	return ("<cal " + truncate.Truncate(cal.Summary, 8, "…", truncate.PositionEnd) + ">")
 }