annotate calsync/mongoclient/events_collection.go @ 56:635ff76f867c

WIP: rewrite: process load+sync in parallel between cals; simplify a lot
author drewp@bigasterisk.com
date Thu, 05 Sep 2024 13:50:40 -0700
parents a9b720445bcf
children 6c7151126a0b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
50
dade5bbd02e3 refactor
drewp@bigasterisk.com
parents:
diff changeset
1 package mongoclient
dade5bbd02e3 refactor
drewp@bigasterisk.com
parents:
diff changeset
2
dade5bbd02e3 refactor
drewp@bigasterisk.com
parents:
diff changeset
3 import (
dade5bbd02e3 refactor
drewp@bigasterisk.com
parents:
diff changeset
4 "log"
51
a9b720445bcf now roughly syncs cals and events to mongodb, one time
drewp@bigasterisk.com
parents: 50
diff changeset
5 "time"
50
dade5bbd02e3 refactor
drewp@bigasterisk.com
parents:
diff changeset
6
dade5bbd02e3 refactor
drewp@bigasterisk.com
parents:
diff changeset
7 "go.mongodb.org/mongo-driver/bson"
dade5bbd02e3 refactor
drewp@bigasterisk.com
parents:
diff changeset
8 "go.mongodb.org/mongo-driver/mongo/options"
dade5bbd02e3 refactor
drewp@bigasterisk.com
parents:
diff changeset
9 )
dade5bbd02e3 refactor
drewp@bigasterisk.com
parents:
diff changeset
10
56
635ff76f867c WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents: 51
diff changeset
11 func LogWithCal(cal MongoCal, msg ...interface{}) {
635ff76f867c WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents: 51
diff changeset
12 log.Println("cal:", (cal.GoogleId+"........")[:8], msg)
635ff76f867c WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents: 51
diff changeset
13 }
635ff76f867c WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents: 51
diff changeset
14
50
dade5bbd02e3 refactor
drewp@bigasterisk.com
parents:
diff changeset
15 func (c *MongoClient) UpsertOneEvent(ev MongoEvent) error {
dade5bbd02e3 refactor
drewp@bigasterisk.com
parents:
diff changeset
16 filter := bson.M{"_id": ev.Url}
51
a9b720445bcf now roughly syncs cals and events to mongodb, one time
drewp@bigasterisk.com
parents: 50
diff changeset
17 setFields := ev
50
dade5bbd02e3 refactor
drewp@bigasterisk.com
parents:
diff changeset
18 update := bson.M{"$set": setFields}
dade5bbd02e3 refactor
drewp@bigasterisk.com
parents:
diff changeset
19 _, err := c.eventsCollection.UpdateOne(c.ctx, filter, update, options.Update().SetUpsert(true))
dade5bbd02e3 refactor
drewp@bigasterisk.com
parents:
diff changeset
20 if err != nil {
dade5bbd02e3 refactor
drewp@bigasterisk.com
parents:
diff changeset
21 return err
dade5bbd02e3 refactor
drewp@bigasterisk.com
parents:
diff changeset
22 }
dade5bbd02e3 refactor
drewp@bigasterisk.com
parents:
diff changeset
23 return nil
dade5bbd02e3 refactor
drewp@bigasterisk.com
parents:
diff changeset
24 }
dade5bbd02e3 refactor
drewp@bigasterisk.com
parents:
diff changeset
25
56
635ff76f867c WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents: 51
diff changeset
26 func (c *MongoClient) DeleteEventsUpdatedBefore(cal MongoCal, t time.Time) error {
635ff76f867c WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents: 51
diff changeset
27 res, err := c.eventsCollection.DeleteMany(
635ff76f867c WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents: 51
diff changeset
28 c.ctx,
635ff76f867c WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents: 51
diff changeset
29 bson.M{"calendarUrl": cal.Url,
635ff76f867c WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents: 51
diff changeset
30 "lastUpdated": bson.M{"$lt": t}})
635ff76f867c WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents: 51
diff changeset
31 LogWithCal(cal, "deleted", res.DeletedCount, "events updated before", t)
51
a9b720445bcf now roughly syncs cals and events to mongodb, one time
drewp@bigasterisk.com
parents: 50
diff changeset
32 if err != nil {
a9b720445bcf now roughly syncs cals and events to mongodb, one time
drewp@bigasterisk.com
parents: 50
diff changeset
33 return err
a9b720445bcf now roughly syncs cals and events to mongodb, one time
drewp@bigasterisk.com
parents: 50
diff changeset
34 }
a9b720445bcf now roughly syncs cals and events to mongodb, one time
drewp@bigasterisk.com
parents: 50
diff changeset
35 return nil
a9b720445bcf now roughly syncs cals and events to mongodb, one time
drewp@bigasterisk.com
parents: 50
diff changeset
36 }