/
administration.go
115 lines (101 loc) · 3.23 KB
/
administration.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package db
import (
"context"
"fmt"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
)
//var collections []string{"users", "repositories", "stats"}
type collections struct {
Names []string
Users *options.CreateCollectionOptions
Repos *options.CreateCollectionOptions
Stats *options.CreateCollectionOptions
}
//GetCollectionOptions is the only way I have figured for there to be a struct that contains the options and be used.
// This cannot be the best way to do this.
func (c collections) GetCollectionOptions(name string) *options.CreateCollectionOptions {
switch name {
case "users":
return c.Users
case "repositories":
return c.Repos
case "stats":
return c.Stats
default:
return nil
}
}
//TODO: Clean these functions up
//TODO: Make it so that the TimeSeries collections have different parameters
//ConfigureCollections to be run on startup to check that a number of different Collections exist
//Creates the collections if not already exist
func (db *DB) ConfigureCollections() error {
collectionReferences := collections{
Names: []string{"users", "repositories", "stats", "organisation", "timeSeries"},
Users: &options.CreateCollectionOptions{},
Repos: &options.CreateCollectionOptions{},
Stats: &options.CreateCollectionOptions{},
}
for _, name := range collectionReferences.Names {
exists, err := db.CheckIfCollectionExists(name)
if err != nil {
return err
}
if !exists {
fmt.Println("Creating collection: " + name)
options := options.CreateCollectionOptions{}
options.SetMaxDocuments(1000)
options.SetCapped(true)
options.SetSizeInBytes(100000000)
if err = db.CreateCollection(name, &options); err != nil {
return err
}
}
}
return db.ConfigureTimeSeriesCollections()
}
//CheckIfCollectionExists is used to check if a further DB initialisation is required.
//Returns true if a Collection with that name exists
func (db *DB) CheckIfCollectionExists(collectionName string) (bool, error) {
//Bloody Pointers
names, err := db.Client.Database(db.DatabaseName).ListCollectionNames(context.TODO(), bson.D{})
if err != nil {
return false, err
}
for _, name := range names {
if name == collectionName {
return true, nil
}
}
return false, nil
}
//CreateCollection creates a collection with a specified name and with creationParameters
//It does NOT check if the collection already exists and will return an error for sure if it does.
func (db *DB) CreateCollection(collectionName string, collectionCreationParams ...*options.CreateCollectionOptions) error {
return db.Client.Database(db.DatabaseName).CreateCollection(db.BasicContext, collectionName, collectionCreationParams...)
}
func (db *DB) ConfigureTimeSeriesCollections() error {
exists, err := db.CheckIfCollectionExists("timeSeries")
if err != nil {
return err
}
if !exists {
fmt.Println("Creating collection: timeSeries")
timeField := "timestamp"
metaField := "meta"
granularity := "seconds"
options := options.CreateCollectionOptions{
Capped: nil,
TimeSeriesOptions: &options.TimeSeriesOptions{
TimeField: timeField,
MetaField: &metaField,
Granularity: &granularity,
},
}
if err = db.CreateCollection("timeSeries", &options); err != nil {
return err
}
}
return nil
}