-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
130cf82
commit 56c63ac
Showing
9 changed files
with
100 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package calculator | ||
|
||
import "time" | ||
|
||
type RecordStatsData struct { | ||
Type string `json:"-"` | ||
Stats interface{} | ||
Timestamp time.Time | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package calculator | ||
|
||
import ( | ||
"context" | ||
|
||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
) | ||
|
||
func CountAggregate(collection *mongo.Collection, aggregateKey string) map[string]int { | ||
countMap := map[string]int{} | ||
|
||
aggregation := mongo.Pipeline{ | ||
bson.D{ | ||
{Key: "$group", | ||
Value: bson.D{ | ||
{Key: "_id", Value: aggregateKey}, | ||
{Key: "count", Value: bson.D{{Key: "$sum", Value: 1}}}, | ||
}, | ||
}, | ||
}, | ||
} | ||
var result []bson.M | ||
cursor, _ := collection.Aggregate(context.Background(), aggregation) | ||
cursor.All(context.Background(), &result) | ||
|
||
for _, record := range result { | ||
countMap[record["_id"].(string)] = int(record["count"].(int32)) | ||
} | ||
|
||
return countMap | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package routes | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/gofiber/fiber/v2" | ||
"github.com/travigo/travigo/pkg/database" | ||
"go.mongodb.org/mongo-driver/bson" | ||
) | ||
|
||
func CalculatedRoute(c *fiber.Ctx) error { | ||
collection := database.GetCollection("stats") | ||
|
||
var statsRecords []bson.M | ||
cursor, _ := collection.Find(context.Background(), bson.M{}) | ||
cursor.All(context.Background(), &statsRecords) | ||
|
||
statsRecordsMap := map[string]bson.M{} | ||
for _, statsRecord := range statsRecords { | ||
statsRecordsMap[statsRecord["type"].(string)] = statsRecord | ||
} | ||
|
||
return c.JSON(statsRecordsMap) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters