Skip to content

Commit 88e810c

Browse files
Database changes
1 parent f823bd4 commit 88e810c

File tree

6 files changed

+642
-50
lines changed

6 files changed

+642
-50
lines changed

server/go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ require (
66
github.com/go-sql-driver/mysql v1.6.0
77
google.golang.org/grpc v1.39.0
88
google.golang.org/protobuf v1.27.1
9+
github.com/spf13/viper v1.8.1
910
)

server/go.sum

+477
Large diffs are not rendered by default.

server/logClient/main.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@ import (
88

99
"google.golang.org/grpc"
1010
pb "github.com/Matias-Correia/go-test_server/server/protologs"
11+
timestamppb "google.golang.org/protobuf/types/known/timestamppb"
1112
)
1213

1314
const (
14-
address = "localhost:50051"
15+
address = "localhost:50051"
1516
defaultName = "world"
17+
localpeer = "Matias"
18+
remotepeer = "Test"
1619
)
1720

1821
func main() {
@@ -31,7 +34,7 @@ func main() {
3134
}
3235
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
3336
defer cancel()
34-
_, err = c.SendLogs(ctx, &pb.Log{BlockID: name, RequestDelay: 100, BlockDelay:200, BlockDelivered: true})
37+
_, err = c.SendLogs(ctx, &pb.Log{BlockID: name, Localpeer: localpeer, Remotepeer:remotepeer, SentAt: timestamppb.Now(), ReceivedAt:timestamppb.Now(), BlockRequestedAt:timestamppb.Now(), Duplicate: false})
3538
if err != nil {
3639
log.Fatalf("could not greet: %v", err)
3740
}

server/logServer/main.go

+71-6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"google.golang.org/grpc"
1010
pb "github.com/Matias-Correia/go-test_server/server/protologs"
11+
timestamppb "google.golang.org/protobuf/types/known/timestamppb"
1112

1213
"database/sql"
1314
_ "github.com/go-sql-driver/mysql"
@@ -26,18 +27,82 @@ type server struct {
2627
// LogTestData implements protologs.LogTestData
2728
func (s *server) SendLogs(ctx context.Context, in *pb.Log) (*pb.Empty, error) {
2829
log.Printf("Received: %v", in.GetBlockID())
29-
log.Printf("Was delivered?: %v", in.GetBlockDelivered())
30-
log.Printf("Request Delay: %v", in.GetRequestDelay())
31-
log.Printf("Block Delay: %v", in.GetBlockDelay())
30+
log.Printf("Sent By: %v", in.GetLocalpeer())
31+
log.Printf("Received By: %v", in.GetRemotepeer())
32+
log.Printf("SentAt: %v", in.GetSentAt())
33+
log.Printf("ReceivedAt: %v", in.GetReceivedAt())
34+
log.Printf("BlockRequestedAt: %v", in.GetBlockRequestedAt())
35+
log.Printf("Duplicate: %v", in.GetDuplicate())
3236

33-
recordReceivedLogs(s.database, in.GetBlockID(), in.GetRequestDelay(), in.GetBlockDelay(), in.GetBlockDelivered())
37+
/* var sentAt *timestamppb.Timestamp
38+
var receivedAt *timestamppb.Timestamp
39+
var blockRequestedAt *timestamppb.Timestamp */
40+
//var duplicate int64
41+
42+
/* if in.GetSentAt() != nil {
43+
sentAt = in.GetSentAt()
44+
}
45+
46+
if in.GetReceivedAt() != nil {
47+
receivedAt = in.GetReceivedAt()
48+
}
49+
50+
if in.GetBlockRequestedAt() != nil {
51+
blockRequestedAt = in.GetBlockRequestedAt()
52+
}
53+
*/
54+
/* if in.GetDuplicate() != true{
55+
duplicate = 0
56+
}else{
57+
duplicate = 1
58+
} */
59+
60+
recordReceivedLogs(s.database, in.GetBlockID(), in.GetLocalpeer(), in.GetRemotepeer(), in.GetSentAt(), in.GetReceivedAt(), in.GetBlockRequestedAt(), in.GetDuplicate())
3461

3562
return &(pb.Empty{}), nil
3663
}
3764

3865
// Save info in the BD
39-
func recordReceivedLogs( dbCon sql.DB, blockId string, requestDelay int64, blockDelay int64, blockDelivered bool){
40-
query := fmt.Sprintf("INSERT INTO test_logs (BlcokID,RequestDelay,BlockDelay,BlockDelivered) VALUES('%s',%v,%v,%t)",blockId,requestDelay,blockDelay,blockDelivered)
66+
func recordReceivedLogs( dbCon sql.DB, blockId string, localpeer string, remotepeer string, sentAt *timestamppb.Timestamp, receivedAt *timestamppb.Timestamp, blockRequestedAt *timestamppb.Timestamp, duplicate bool){
67+
68+
sentAtAsTime := fmt.Sprintf("%v", sentAt.AsTime().Format("2006-01-02 15:04:05.000"))
69+
receivedAtAsTime := fmt.Sprintf("%v", receivedAt.AsTime().Format("2006-01-02 15:04:05.000"))
70+
blockRequestedAtAsTime := fmt.Sprintf("%v", blockRequestedAt.AsTime().Format("2006-01-02 15:04:05.000"))
71+
72+
73+
query := fmt.Sprintf("INSERT INTO test_logs (BlockID,LocalPeer,RemotePeer,SentAt,ReceivedAt,BlockRequestedAt,Duplicate) VALUES('%s','%s','%s','%v','%v','%v',%t)",blockId,localpeer,remotepeer,sentAtAsTime,receivedAt,blockRequestedAt,duplicate)
74+
75+
if sentAt == nil{
76+
if receivedAt == nil{
77+
if blockRequestedAt == nil{
78+
query = fmt.Sprintf("INSERT INTO test_logs (BlockID,LocalPeer,RemotePeer,Duplicate) VALUES('%s','%s','%s','%v','%v','%v',%t)",blockId,localpeer,remotepeer,duplicate)
79+
}else{
80+
query = fmt.Sprintf("INSERT INTO test_logs (BlockID,LocalPeer,RemotePeer,BlockRequestedAt,Duplicate) VALUES('%s','%s','%s','%v','%v','%v',%t)",blockId,localpeer,remotepeer,blockRequestedAtAsTime,duplicate)
81+
}
82+
}else{
83+
if blockRequestedAt == nil{
84+
query = fmt.Sprintf("INSERT INTO test_logs (BlockID,LocalPeer,RemotePeer,ReceivedAt,Duplicate) VALUES('%s','%s','%s','%v','%v','%v',%t)",blockId,localpeer,remotepeer,receivedAtAsTime,duplicate)
85+
}else{
86+
query = fmt.Sprintf("INSERT INTO test_logs (BlockID,LocalPeer,RemotePeer,ReceivedAt,BlockRequestedAt,Duplicate) VALUES('%s','%s','%s','%v','%v','%v',%t)",blockId,localpeer,remotepeer,receivedAtAsTime,blockRequestedAtAsTime,duplicate)
87+
}
88+
}
89+
}else{
90+
if receivedAt == nil{
91+
if blockRequestedAt == nil{
92+
query = fmt.Sprintf("INSERT INTO test_logs (BlockID,LocalPeer,RemotePeer,SentAt,Duplicate) VALUES('%s','%s','%s','%v','%v','%v',%t)",blockId,localpeer,remotepeer,sentAtAsTime,duplicate)
93+
}else{
94+
query = fmt.Sprintf("INSERT INTO test_logs (BlockID,LocalPeer,RemotePeer,SentAt,BlockRequestedAt,Duplicate) VALUES('%s','%s','%s','%v','%v','%v',%t)",blockId,localpeer,remotepeer,sentAtAsTime,blockRequestedAtAsTime,duplicate)
95+
}
96+
}else{
97+
if blockRequestedAt == nil{
98+
query = fmt.Sprintf("INSERT INTO test_logs (BlockID,LocalPeer,RemotePeer,SentAt,ReceivedAt,Duplicate) VALUES('%s','%s','%s','%v','%v','%v',%t)",blockId,localpeer,remotepeer,sentAtAsTime,receivedAtAsTime,duplicate)
99+
}else{
100+
query = fmt.Sprintf("INSERT INTO test_logs (BlockID,LocalPeer,RemotePeer,SentAt,ReceivedAt,BlockRequestedAt,Duplicate) VALUES('%s','%s','%s','%v','%v','%v',%t)",blockId,localpeer,remotepeer,sentAtAsTime,receivedAtAsTime,blockRequestedAtAsTime,duplicate)
101+
}
102+
}
103+
}
104+
105+
41106
fmt.Printf(query)
42107
insert, err := dbCon.Query(query)
43108

server/protologs/logs.pb.go

+80-39
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/protologs/logs.proto

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
syntax = "proto3";
22

3+
import "google/protobuf/timestamp.proto";
4+
35
option go_package = "github.com/Matias-Correia/go-test_server/protologs";
46

57

@@ -16,7 +18,10 @@ message Empty{}
1618
// Log definition
1719
message Log{
1820
string blockID = 1;
19-
int64 request_delay = 2;
20-
int64 block_delay = 3;
21-
bool blockDelivered = 4;
21+
string localpeer = 2;
22+
string remotepeer = 3;
23+
google.protobuf.Timestamp sentAt = 4;
24+
google.protobuf.Timestamp receivedAt = 5;
25+
google.protobuf.Timestamp blockRequestedAt = 6;
26+
bool duplicate = 7;
2227
}

0 commit comments

Comments
 (0)