Skip to content

Commit

Permalink
further improvements related to webui and degendb
Browse files Browse the repository at this point in the history
  • Loading branch information
benleb committed Jul 12, 2023
1 parent 3dfd35e commit 9721113
Show file tree
Hide file tree
Showing 18 changed files with 307 additions and 188 deletions.
7 changes: 6 additions & 1 deletion cmd/live.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/benleb/gloomberg/internal"
"github.com/benleb/gloomberg/internal/collections"
"github.com/benleb/gloomberg/internal/config"
"github.com/benleb/gloomberg/internal/degendb"
"github.com/benleb/gloomberg/internal/degendb/degendata"
"github.com/benleb/gloomberg/internal/gbl"
"github.com/benleb/gloomberg/internal/nemo/gloomberg"
Expand Down Expand Up @@ -74,11 +75,15 @@ func runGloomberg(_ *cobra.Command, _ []string) {
// gbl.Log.Infof("listings from opensea: %v", viper.GetBool("listings.enabled"))
// }

// gb := gloomberg.New()
// initialize
gb.CollectionDB = collections.New()
gb.OwnWallets = &wallet.Wallets{}
gb.Watcher = &watch.Watcher{}

go func() {
gb.DegenDB = degendb.NewDegenDB()
}()

//
// start central terminal printer
go func() {
Expand Down
9 changes: 9 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,12 @@ func initConfig() {

fmt.Print(rootCmd.CalledAs())
}

func GracefulShutdown() {
if gb.DegenDB != nil {
err := gb.DegenDB.Disconnect()
if err != nil {
gbl.Log.Error(err)
}
}
}
4 changes: 3 additions & 1 deletion internal/degendb/degen.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ type Degen struct {
Accounts Accounts `bson:"accounts,omitempty" json:"accounts"`

// Wallets is a list of wallet addresses associated with this degen
Wallets []Wallet `bson:"wallets,omitempty" json:"wallets"`
Wallets []primitive.ObjectID `bson:"wallets,omitempty" json:"wallets"`
// RawWallets is a list of wallet addresses associated with this degen
RawWallets []Wallet `bson:"raw_wallets,omitempty" json:"raw_wallets"`

// Tags is a list of tags associated with this degen
Tags []Tag `bson:"tags,omitempty" json:"tags"`
Expand Down
205 changes: 114 additions & 91 deletions internal/degendb/degendb.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ package degendb

import (
"context"
"time"

"github.com/charmbracelet/log"
"github.com/ethereum/go-ethereum/common"
"github.com/spf13/viper"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
Expand All @@ -17,98 +16,153 @@ const (
collCollections = "collections"
collDegens = "degens"
collTokens = "tokens"
collWalllets = "wallets"
)

type DegenDB struct {
uri string
mongo *mongo.Client
}

func NewDegenDB() *DegenDB {
mongoClient := connectToMongo()
if mongoClient == nil {
log.Errorf("❌ could not connect to MongoDB at: %s", viper.GetString("mongodb.uri"))
ddb := &DegenDB{
uri: viper.GetString("mongodb.uri"),
}

mongoClient, err := ddb.connect()
if err != nil {
log.Errorf("❌ could not connect to mongoDB at %s: %s", ddb.uri, err)

return nil
}

// defer func() {
// if err := mongoClient.Disconnect(context.TODO()); err != nil {
// panic(err)
// }
// }()
log.Infof("✅ connected to mongoDB at %s", ddb.uri)

ddb := &DegenDB{
mongo: mongoClient,
ddb.mongo = mongoClient

if viper.GetBool("mongodb.initialize") {
// cleanup & initialize
collectionsColl := ddb.mongo.Database(mongoDB).Collection(collCollections)
degensColl := ddb.mongo.Database(mongoDB).Collection(collDegens)
tokensColl := ddb.mongo.Database(mongoDB).Collection(collTokens)
walletsColl := ddb.mongo.Database(mongoDB).Collection(collWalllets)

err := collectionsColl.Drop(context.Background())
if err != nil {
log.Error(err)
}

err = degensColl.Drop(context.Background())
if err != nil {
log.Error(err)
}

err = tokensColl.Drop(context.Background())
if err != nil {
log.Error(err)
}

err = walletsColl.Drop(context.Background())
if err != nil {
log.Error(err)
}

// initialize og degens
ddb.initializeDegensCollection()

// check
cursor, err := degensColl.Find(context.Background(), bson.D{})
if err != nil {
log.Error(err)
}

// query
var mongoDegen []Degen
if err = cursor.All(context.TODO(), &mongoDegen); err != nil {
log.Error(err)
}

// print
for _, dgn := range mongoDegen {
log.Printf("mongoDegen: %+v", dgn)
}
}

// cleanup & initialize
collectionsColl := ddb.mongo.Database(mongoDB).Collection(collCollections)
degensColl := ddb.mongo.Database(mongoDB).Collection(collDegens)
tokensColl := ddb.mongo.Database(mongoDB).Collection(collTokens)
return ddb
}

err := collectionsColl.Drop(context.Background())
if err != nil {
log.Error(err)
func (ddb *DegenDB) connect() (*mongo.Client, error) {
if ddb.uri == "" {
log.Error("You must set your 'MONGODB_URI' environmental variable. See https://www.mongodb.com/docs/drivers/go/current/usage-examples/#environment-variable")
}

err = degensColl.Drop(context.Background())
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(ddb.uri))
if err != nil {
log.Error(err)
return nil, err
}

err = tokensColl.Drop(context.Background())
if err != nil {
log.Error(err)
// Send a ping to confirm a successful connection
var result bson.M
if err := client.Database("admin").RunCommand(context.TODO(), bson.D{{Key: "ping", Value: 1}}).Decode(&result); err != nil {
return nil, err
}

ddb.initializeDegensCollection()
log.Print("mongodb ping successful")

// check
cursor, err := degensColl.Find(context.Background(), bson.D{})
if err != nil {
panic(err)
}
return client, nil
}

var mongoDegen []Degen
if err = cursor.All(context.TODO(), &mongoDegen); err != nil {
panic(err)
}
for _, dgn := range mongoDegen {
log.Printf("mongoDegen: %+v", dgn)
}
func (ddb *DegenDB) Disconnect() error {
err := ddb.mongo.Disconnect(context.TODO())

return ddb
return err
}

func (ddb *DegenDB) initializeDegensCollection() {
degensColl := ddb.mongo.Database(mongoDB).Collection(collDegens)
walletsColl := ddb.mongo.Database(mongoDB).Collection(collWalllets)

ogDegens := []interface{}{}

for _, degen := range ogDegens {
degen, ok := degen.(*Degen)
if !ok {
log.Printf("type asserting degen error")

continue
}
// og := ogDegens[degenID].(Degen)

wallets := make([]interface{}, 0)

ogDegens := []interface{}{
Degen{
Name: "Ben",
Accounts: Accounts{Twitter: "ben_leb", Telegram: "benleb", TelegramChatID: -1001808788625},
Wallets: []Wallet{
{Address: common.HexToAddress("0xCd8aF79Ba3974404e37f126a8E355690351Da8bD")},
},
Tags: []Tag{"dakma"},
CreatedAt: time.Now(),
},
Degen{
Name: "Luke",
Accounts: Accounts{Twitter: "0xlugges", Telegram: "luke_take_profits", TelegramChatID: 1337},
Wallets: []Wallet{{Address: common.HexToAddress("0x0DB54CC560Ae7832898e82E5E607E8142e519891")}},
Tags: []Tag{"dakma"},
CreatedAt: time.Now(),
},
for _, wallet := range degen.RawWallets {
// wallets[degen.Name] = append(wallets[degen.Name], wallet.ID)
wallets = append(wallets, wallet)
}

walletsResult, err := walletsColl.InsertMany(context.TODO(), wallets)
if err != nil {
log.Error(err)
}

for _, walletID := range walletsResult.InsertedIDs {
wID, ok := walletID.(primitive.ObjectID)
if !ok {
log.Printf("type asserting walletID error")

continue
}

degen.Wallets = append(degen.Wallets, wID)
}
}

// result, err := degensColl.UpdateMany(context.TODO(), bson.D{{}}, ogDegens, &options.UpdateOptions{
result, err := degensColl.InsertMany(context.TODO(), ogDegens)
degensResult, err := degensColl.InsertMany(context.TODO(), ogDegens)
if err != nil {
panic(err)
log.Error(err)
}

log.Printf("result: %+v", result)
log.Printf("result: %+v", degensResult)
}

func (ddb *DegenDB) AddCollectionToken(collections interface{}, tokens interface{}) {
Expand All @@ -119,7 +173,6 @@ func (ddb *DegenDB) AddCollectionToken(collections interface{}, tokens interface
collectionList, ok := collections.([]Collection)
if !ok {
log.Printf("type asserting collections error")
// panic(err)

return
}
Expand All @@ -131,7 +184,6 @@ func (ddb *DegenDB) AddCollectionToken(collections interface{}, tokens interface
result, err := collectionsColl.InsertMany(context.TODO(), mongoCollections)
if err != nil {
log.Printf("add collections error: %+v", err)
// panic(err)

return
}
Expand All @@ -143,7 +195,6 @@ func (ddb *DegenDB) AddCollectionToken(collections interface{}, tokens interface
tokenList, ok := tokens.([]Token)
if !ok {
log.Printf("ttype asserting tokens error: %+v", err)
// panic(err)

return
}
Expand All @@ -156,36 +207,8 @@ func (ddb *DegenDB) AddCollectionToken(collections interface{}, tokens interface
// result, err := degensColl.UpdateMany(context.TODO(), bson.D{{}}, ogDegens, &options.UpdateOptions{
result, err = tokensColl.InsertMany(context.TODO(), mongoTokens)
if err != nil {
panic(err)
log.Error(err)
}

log.Printf("add tokens result: %+v", result)
}

func connectToMongo() *mongo.Client {
uri := viper.GetString("mongodb.uri")
if uri == "" {
log.Fatal("You must set your 'MONGODB_URI' environmental variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/usage-examples/#environment-variable")
}

client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))
if err != nil {
panic(err)
}

// defer func() {
// if err := client.Disconnect(context.TODO()); err != nil {
// panic(err)
// }
// }()

// Send a ping to confirm a successful connection
var result bson.M
if err := client.Database("admin").RunCommand(context.TODO(), bson.D{{Key: "ping", Value: 1}}).Decode(&result); err != nil {
panic(err)
}

log.Print("Pinged your deployment. You successfully connected to MongoDB!")

return client
}

0 comments on commit 9721113

Please sign in to comment.