Skip to content

Commit

Permalink
Merge pull request #312 from nevenas-mit/master
Browse files Browse the repository at this point in the history
New Review Service with the latest DSB version and changed only the necessary files
  • Loading branch information
cdelimitrou authored Apr 9, 2024
2 parents aeb4860 + 6f95420 commit 3695ca1
Show file tree
Hide file tree
Showing 15 changed files with 2,015 additions and 13 deletions.
14 changes: 2 additions & 12 deletions hotelReservation/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,7 @@ COPY tls/ tls/
COPY tracing/ tracing/
COPY tune/ tune/

RUN CGO_ENABLED=0 GOOS=linux GO111MODULE=on go install -ldflags="-s -w" -mod=vendor ./cmd/...

FROM gcr.io/distroless/static:nonroot
COPY config.json config.json

WORKDIR /
RUN CGO_ENABLED=0 GOOS=linux GO111MODULE=on go install -ldflags="-s -w" -mod=vendor ./cmd/...

COPY --from=builder /go/bin/frontend .
COPY --from=builder /go/bin/geo .
COPY --from=builder /go/bin/profile .
COPY --from=builder /go/bin/rate .
COPY --from=builder /go/bin/recommendation .
COPY --from=builder /go/bin/reservation .
COPY --from=builder /go/bin/search .
COPY --from=builder /go/bin/user .
101 changes: 101 additions & 0 deletions hotelReservation/cmd/attractions/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package main

import (
"github.com/rs/zerolog/log"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"fmt"
"context"
)

type Restaurant struct {
RestaurantId string `bson:"restaurantId"`
RLat float64 `bson:"lat"`
RLon float64 `bson:"lon"`
RestaurantName string `bson:"restaurantName"`
Rating float32 `bson:"rating"`
Type string `bson:"type"`
}

type Museum struct {
MuseumId string `bson:"museumId"`
MLat float64 `bson:"lat"`
MLon float64 `bson:"lon"`
MuseumName string `bson:"museumName"`
Type string `bson:"type"`
}

type point struct {
Pid string `bson:"hotelId"`
Plat float64 `bson:"lat"`
Plon float64 `bson:"lon"`
}

func initializeDatabase(url string) (*mongo.Client, func()) {

newPoints := []interface{}{
point{"1", 37.7867, -122.4112},
point{"2", 37.7854, -122.4005},
point{"3", 37.7854, -122.4071},
point{"4", 37.7936, -122.3930},
point{"5", 37.7831, -122.4181},
point{"6", 37.7863, -122.4015},
}

newRestaurants := []interface{}{
&Restaurant{"1", 37.7867, -122.4112, "R1", 3.5, "fusion"},
&Restaurant{"2", 37.7857, -122.4012, "R2", 3.9, "italian"},
&Restaurant{"3", 37.7847, -122.3912, "R3", 4.5, "sushi"},
&Restaurant{"4", 37.7862, -122.4212, "R4", 3.2, "sushi"},
&Restaurant{"5", 37.7839, -122.4052, "R5", 4.9, "fusion"},
&Restaurant{"6", 37.7831, -122.3812, "R6", 4.1, "american"},
}

newMuseums := []interface{}{
&Museum{"1", 35.7867, -122.4112, "M1", "history"},
&Museum{"2", 36.7867, -122.5112, "M2", "history"},
&Museum{"3", 38.7867, -122.4612, "M3", "nature"},
&Museum{"4", 37.7867, -122.4912, "M4", "nature"},
&Museum{"5", 36.9867, -122.4212, "M5", "nature"},
&Museum{"6", 37.3867, -122.5012, "M6", "technology"},
}

uri := fmt.Sprintf("mongodb://%s", url)
log.Info().Msgf("Attempting connection to %v", uri)

opts := options.Client().ApplyURI(uri)
client, err := mongo.Connect(context.TODO(), opts)
if err != nil {
log.Panic().Msg(err.Error())
}
log.Info().Msg("Successfully connected to MongoDB")

collectionH := client.Database("attractions-db").Collection("hotels")
_, err = collectionH.InsertMany(context.TODO(), newPoints)
if err != nil {
log.Fatal().Msg(err.Error())
}
log.Info().Msg("Successfully inserted test data into museum DB")

collectionR := client.Database("attractions-db").Collection("restaurants")
_, err = collectionR.InsertMany(context.TODO(), newRestaurants)
if err != nil {
log.Fatal().Msg(err.Error())
}
log.Info().Msg("Successfully inserted test data into restaurant DB")

collectionM := client.Database("attractions-db").Collection("museums")
_, err = collectionM.InsertMany(context.TODO(), newMuseums)
if err != nil {
log.Fatal().Msg(err.Error())
}
log.Info().Msg("Successfully inserted test data into museum DB")

return client, func() {
if err := client.Disconnect(context.TODO()); err != nil {
log.Fatal().Msg(err.Error())
}
}

}

93 changes: 93 additions & 0 deletions hotelReservation/cmd/attractions/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package main

import (
"encoding/json"
"flag"
"io/ioutil"
"os"
"strconv"

"github.com/delimitrou/DeathStarBench/hotelreservation/registry"
"github.com/delimitrou/DeathStarBench/hotelreservation/services/attractions"
"github.com/delimitrou/DeathStarBench/hotelreservation/tracing"
"github.com/delimitrou/DeathStarBench/hotelreservation/tune"

"github.com/rs/zerolog"
"github.com/rs/zerolog/log"

"time"

// "github.com/bradfitz/gomemcache/memcache"
)

func main() {
tune.Init()
log.Logger = zerolog.New(zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339}).With().Timestamp().Caller().Logger()

log.Info().Msg("Reading config...")
jsonFile, err := os.Open("config.json")
if err != nil {
log.Error().Msgf("Got error while reading config: %v", err)
}

defer jsonFile.Close()

byteValue, _ := ioutil.ReadAll(jsonFile)

var result map[string]string
json.Unmarshal([]byte(byteValue), &result)

log.Info().Msgf("Read database URL: %v", result["AttractionsMongoAddress"])
log.Info().Msg("Initializing DB connection...")
mongo_session, mongoClose := initializeDatabase(result["AttractionsMongoAddress"])
defer mongoClose()
log.Info().Msg("Successfull")

// log.Info().Msgf("Read attractions memcashed address: %v", result["AttractionsMemcAddress"])
// log.Info().Msg("Initializing Memcashed client...")
// memc_client := memcache.New(result["AttractionsMemcAddress"])
// memc_client.Timeout = time.Second * 2
// memc_client.MaxIdleConns = 512
// memc_client := tune.NewMemCClient2(result["AttractionsMemcAddress"])
// log.Info().Msg("Successfull")

serv_port, _ := strconv.Atoi(result["AttractionsPort"])
serv_ip := result["AttractionsIP"]
log.Info().Msgf("Read target port: %v", serv_port)
log.Info().Msgf("Read consul address: %v", result["consulAddress"])
log.Info().Msgf("Read jaeger address: %v", result["jaegerAddress"])

var (
// port = flag.Int("port", 8081, "The server port")
jaegeraddr = flag.String("jaegeraddr", result["jaegerAddress"], "Jaeger server addr")
consuladdr = flag.String("consuladdr", result["consulAddress"], "Consul address")
)
flag.Parse()

log.Info().Msgf("Initializing jaeger agent [service name: %v | host: %v]...", "attractions", *jaegeraddr)
tracer, err := tracing.Init("attractions", *jaegeraddr)
if err != nil {
log.Panic().Msgf("Got error while initializing jaeger agent: %v", err)
}
log.Info().Msg("Jaeger agent initialized")

log.Info().Msgf("Initializing consul agent [host: %v]...", *consuladdr)
registry, err := registry.NewClient(*consuladdr)
if err != nil {
log.Panic().Msgf("Got error while initializing consul agent: %v", err)
}
log.Info().Msg("Consul agent initialized")

srv := attractions.Server{
Tracer: tracer,
// Port: *port,
Registry: registry,
Port: serv_port,
IpAddr: serv_ip,
MongoClient: mongo_session,
}

log.Info().Msg("Starting server...")
log.Fatal().Msg(srv.Run().Error())
}

107 changes: 107 additions & 0 deletions hotelReservation/cmd/review/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package main

import (
"github.com/rs/zerolog/log"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"fmt"
"context"
)

type Review struct {
ReviewId string `bson:"reviewId"`
HotelId string `bson:"hotelId"`
Name string `bson:"name"`
Rating float32 `bson:"rating"`
Description string `bson:"description"`
Image *Image `bson:"images"`
}

type Image struct {
Url string `bson:"url"`
Default bool `bson:"default"`
}

func initializeDatabase(url string) (*mongo.Client, func()) {

newReviews := []interface{}{
&Review{
"1",
"1",
"Person 1",
3.4,
"A 6-minute walk from Union Square and 4 minutes from a Muni Metro station, this luxury hotel designed by Philippe Starck features an artsy furniture collection in the lobby, including work by Salvador Dali.",
&Image{
"some url",
false}},
&Review{
"2",
"1",
"Person 2",
4.4,
"A 6-minute walk from Union Square and 4 minutes from a Muni Metro station, this luxury hotel designed by Philippe Starck features an artsy furniture collection in the lobby, including work by Salvador Dali.",
&Image{
"some url",
false}},
&Review{
"3",
"1",
"Person 3",
4.2,
"A 6-minute walk from Union Square and 4 minutes from a Muni Metro station, this luxury hotel designed by Philippe Starck features an artsy furniture collection in the lobby, including work by Salvador Dali.",
&Image{
"some url",
false}},
&Review{
"4",
"1",
"Person 4",
3.9,
"A 6-minute walk from Union Square and 4 minutes from a Muni Metro station, this luxury hotel designed by Philippe Starck features an artsy furniture collection in the lobby, including work by Salvador Dali.",
&Image{
"some url",
false}},
&Review{
"5",
"2",
"Person 5",
4.2,
"A 6-minute walk from Union Square and 4 minutes from a Muni Metro station, this luxury hotel designed by Philippe Starck features an artsy furniture collection in the lobby, including work by Salvador Dali.",
&Image{
"some url",
false}},
&Review{
"6",
"2",
"Person 6",
3.7,
"A 6-minute walk from Union Square and 4 minutes from a Muni Metro station, this luxury hotel designed by Philippe Starck features an artsy furniture collection in the lobby, including work by Salvador Dali.",
&Image{
"some url",
false}},
}

uri := fmt.Sprintf("mongodb://%s", url)
log.Info().Msgf("Attempting connection to %v", uri)

opts := options.Client().ApplyURI(uri)
client, err := mongo.Connect(context.TODO(), opts)
if err != nil {
log.Panic().Msg(err.Error())
}
log.Info().Msg("Successfully connected to MongoDB")

collection := client.Database("review-db").Collection("reviews")
_, err = collection.InsertMany(context.TODO(), newReviews)
if err != nil {
log.Fatal().Msg(err.Error())
}
log.Info().Msg("Successfully inserted test data into rate DB")

return client, func() {
if err := client.Disconnect(context.TODO()); err != nil {
log.Fatal().Msg(err.Error())
}
}

}
Loading

0 comments on commit 3695ca1

Please sign in to comment.