diff --git a/hw12_13_14_15_calendar/api/public/public.proto b/hw12_13_14_15_calendar/api/public/public.proto index 5b0c4dd..4d82ba8 100644 --- a/hw12_13_14_15_calendar/api/public/public.proto +++ b/hw12_13_14_15_calendar/api/public/public.proto @@ -58,6 +58,7 @@ message Event { string Note = 5; int64 UserID = 6; google.protobuf.Duration NotifyTime = 7; + bool notified = 8; } message CreateReq { diff --git a/hw12_13_14_15_calendar/cicd/dc_sender.yml b/hw12_13_14_15_calendar/cicd/dc_sender.yml index 919eaa1..9a0145f 100644 --- a/hw12_13_14_15_calendar/cicd/dc_sender.yml +++ b/hw12_13_14_15_calendar/cicd/dc_sender.yml @@ -15,6 +15,8 @@ services: APP_RABBITMQ_EXCHANGE: calendar APP_RABBITMQ_QUEUE: notifications APP_RABBITMQ_KEY: events + APP_API_ADDRESS: localhost + APP_API_PORT: 50053 APP_LOGGER_FILE: /logs/calendar.log APP_LOGGER_LEVEL: INFO network_mode: host \ No newline at end of file diff --git a/hw12_13_14_15_calendar/cmd/calendar/main_test.go b/hw12_13_14_15_calendar/cmd/calendar/main_test.go index baf13bc..6721274 100644 --- a/hw12_13_14_15_calendar/cmd/calendar/main_test.go +++ b/hw12_13_14_15_calendar/cmd/calendar/main_test.go @@ -148,101 +148,6 @@ func TestPublicGRPCEndpoint(t *testing.T) { wg.Wait() } -func TestPublicAPIEndpoint(t *testing.T) { - wg := sync.WaitGroup{} - ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) - defer cancel() - publicAPI, err := public.NewClient(ctx, "localhost", "50051") - require.NoError(t, err) - - wg.Add(5) - // Реализовать тесты логики приложения: - t.Run("test public GRPC.Create and GRPC.GetById", func(t *testing.T) { - defer wg.Done() - resp1, err := publicAPI.Create(ctx, &testEvent01) - require.NoError(t, err) - require.Greater(t, resp1.ID, int64(0)) - - resp2, err := publicAPI.GetByID(ctx, &public.GetByIDReq{ID: resp1.ID}) - require.NoError(t, err) - require.Equal(t, 1, len(resp2.Events)) - require.Equal(t, testEvent01.Title, resp2.Events[0].Title) - require.Equal(t, testEvent01.UserID, resp2.Events[0].UserID) - require.Equal(t, testEvent01.Date.Seconds, resp2.Events[0].Date.Seconds) - require.Equal(t, testEvent01.Note, resp2.Events[0].Note) - }) - - t.Run("test public GRPC.Create, GRPC.Update and GRPC.GetById", func(t *testing.T) { - defer wg.Done() - resp1, err := publicAPI.Create(ctx, &testEvent01) - require.NoError(t, err) - require.Greater(t, resp1.ID, int64(0)) - _, err = publicAPI.Update(ctx, &public.UpdateReq{ID: resp1.ID, Event: &public.Event{ID: resp1.ID, Title: testEvent02.Title, Date: testEvent02.Date, Latency: testEvent02.Latency, Note: testEvent02.Note, UserID: testEvent02.UserID, NotifyTime: testEvent02.NotifyTime}}) - require.NoError(t, err) - resp2, err := publicAPI.GetByID(ctx, &public.GetByIDReq{ID: resp1.ID}) - require.NoError(t, err) - require.Equal(t, 1, len(resp2.Events)) - require.Equal(t, testEvent02.Title, resp2.Events[0].Title) - require.Equal(t, testEvent02.UserID, resp2.Events[0].UserID) - require.Equal(t, testEvent02.Date.Seconds, resp2.Events[0].Date.Seconds) - require.Equal(t, testEvent02.Note, resp2.Events[0].Note) - }) - - t.Run("test public GRPC.Create, GRPC.Delete and GRPC.GetById", func(t *testing.T) { - defer wg.Done() - resp1, err := publicAPI.Create(ctx, &testEvent01) - require.NoError(t, err) - require.Greater(t, resp1.ID, int64(0)) - _, err = publicAPI.Delete(ctx, &public.DeleteReq{ID: resp1.ID}) - require.NoError(t, err) - resp2, err := publicAPI.GetByID(ctx, &public.GetByIDReq{ID: resp1.ID}) - require.Error(t, err) - require.Nil(t, resp2) - }) - - t.Run("test public GRPC.Create and GRPC.List", func(t *testing.T) { - defer wg.Done() - resp1, err := publicAPI.Create(ctx, &testEvent01) - require.NoError(t, err) - resp2, err := publicAPI.Create(ctx, &testEvent02) - require.NoError(t, err) - require.NotEqual(t, resp1.ID, resp2.ID) - - list, err := publicAPI.List(ctx, &empty.Empty{}) - require.NoError(t, err) - require.GreaterOrEqual(t, len(list.Events), 2) - var e1, e2 bool - for _, v := range list.Events { - if v.ID == resp1.ID { - e1 = true - } - if v.ID == resp2.ID { - e2 = true - } - } - require.True(t, e1) - require.True(t, e2) - }) - - t.Run("test public GRPC.Create and GRPC.GetByDate", func(t *testing.T) { - defer wg.Done() - resp1, err := publicAPI.Create(ctx, &testEvent01) - require.NoError(t, err) - list, err := publicAPI.GetByDate(ctx, &public.GetByDateReq{Date: testEvent01.Date, Range: public.QueryRange_DAY}) - require.NoError(t, err) - require.GreaterOrEqual(t, len(list.Events), 2) - var e1 bool - for _, v := range list.Events { - if v.ID == resp1.ID { - e1 = true - } - } - require.True(t, e1) - }) - - wg.Wait() -} - func time2pbtimestamp(t time.Time) *timestamp.Timestamp { r, err := ptypes.TimestampProto(t) if err != nil { diff --git a/hw12_13_14_15_calendar/configs/calendar.conf b/hw12_13_14_15_calendar/configs/calendar.conf index 3518edc..b1f3a9e 100644 --- a/hw12_13_14_15_calendar/configs/calendar.conf +++ b/hw12_13_14_15_calendar/configs/calendar.conf @@ -15,7 +15,7 @@ File = "./logs/calendar.log" Level = "INFO" [Storage] -inMemory = true +inMemory = false SQLHost = "localhost" SQLPort = "5432" SQLDbase = "calendar" diff --git a/hw12_13_14_15_calendar/configs/scheduler.conf b/hw12_13_14_15_calendar/configs/scheduler.conf index da6ef69..3abe61e 100644 --- a/hw12_13_14_15_calendar/configs/scheduler.conf +++ b/hw12_13_14_15_calendar/configs/scheduler.conf @@ -11,6 +11,6 @@ Key = "events" File = "./logs/calendar.log" Level = "INFO" -[API] +[CalendarAPI] Address = "localhost" Port = "50053" \ No newline at end of file diff --git a/hw12_13_14_15_calendar/configs/sender.conf b/hw12_13_14_15_calendar/configs/sender.conf index 2e6ebcc..da6ef69 100644 --- a/hw12_13_14_15_calendar/configs/sender.conf +++ b/hw12_13_14_15_calendar/configs/sender.conf @@ -9,4 +9,8 @@ Key = "events" [Logger] File = "./logs/calendar.log" -Level = "INFO" \ No newline at end of file +Level = "INFO" + +[API] +Address = "localhost" +Port = "50053" \ No newline at end of file diff --git a/hw12_13_14_15_calendar/internal/sender/app_sender.go b/hw12_13_14_15_calendar/internal/sender/app_sender.go index 6117811..04880d3 100644 --- a/hw12_13_14_15_calendar/internal/sender/app_sender.go +++ b/hw12_13_14_15_calendar/internal/sender/app_sender.go @@ -13,15 +13,17 @@ import ( ) type Sender struct { - Logger logger.Interface - Rabbit *rabbit.Rabbit - Queue string - Stop context.CancelFunc + GRPCAPI private.GrpcClient + Logger logger.Interface + Rabbit *rabbit.Rabbit + Queue string + Stop context.CancelFunc } type Config struct { - Rabbitmq config.Rabbit - Logger config.Logger + CalendarAPI config.Server + Rabbitmq config.Rabbit + Logger config.Logger } func New(conf Config) Sender { @@ -33,7 +35,11 @@ func New(conf Config) Sender { if err != nil { log.Fatalf("failed to connect to RabbitMQ:", err.Error()) } - return Sender{Logger: log, Rabbit: rb, Queue: conf.Rabbitmq.Queue} + cli, err := private.NewClient(conf.CalendarAPI.Address, conf.CalendarAPI.Port) + if err != nil { + log.Fatalf("can't get GRPC client: %w", err.Error()) + } + return Sender{Logger: log, Rabbit: rb, Queue: conf.Rabbitmq.Queue, GRPCAPI: cli} } func (s *Sender) Start() error { @@ -54,6 +60,10 @@ func (s *Sender) Start() error { s.Logger.Errorf("can`t unmarshal data %w", err) } for _, v := range data { + _, err = s.GRPCAPI.SetNotified(ctx, &private.SetReq{ID: v.ID}) + if err != nil { + s.Logger.Errorf("can`t mark event with ID %d as notified data %w", v.ID, err) + } s.Logger.Infof("User %s notified about event %s", v.UserID, v.ID) } case <-ctx.Done(): diff --git a/hw12_13_14_15_calendar/internal/sheduler/app_scheduler.go b/hw12_13_14_15_calendar/internal/sheduler/app_scheduler.go index 8932b72..c6034b0 100644 --- a/hw12_13_14_15_calendar/internal/sheduler/app_scheduler.go +++ b/hw12_13_14_15_calendar/internal/sheduler/app_scheduler.go @@ -39,7 +39,7 @@ func New(conf Config) Scheduler { func (s *Scheduler) Start() error { ctx, cancel := context.WithCancel(context.Background()) s.Stop = cancel - fetcher := riseOnTick(ctx, s.Logger, func() interface{} { return worker(ctx, s.CalendarAPI, s.Rabbit, s.Logger) }, 1*time.Minute) + fetcher := riseOnTick(ctx, s.Logger, func() interface{} { return worker(ctx, s.CalendarAPI, s.Rabbit, s.Logger) }, 10*time.Second) go func() { for { select { diff --git a/hw12_13_14_15_calendar/internal/sheduler/scheduler.go b/hw12_13_14_15_calendar/internal/sheduler/scheduler.go index 75850a7..a00a71a 100644 --- a/hw12_13_14_15_calendar/internal/sheduler/scheduler.go +++ b/hw12_13_14_15_calendar/internal/sheduler/scheduler.go @@ -6,6 +6,7 @@ import ( "fmt" "time" + "github.com/golang/protobuf/ptypes/empty" "github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/internal/api/private" "github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/pkg/config" "github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/pkg/logger" @@ -43,11 +44,11 @@ func worker(ctx context.Context, calendarAPI config.Server, rb *rabbit.Rabbit, l if err != nil { return fmt.Errorf("can't get GRPC client: %w", err) } - resp, err := cli.GetNotifications(ctx, nil) + resp, err := cli.GetNotifications(ctx, &empty.Empty{}) if err != nil { return fmt.Errorf("can't get events from GRPC endpoint: %w", err) } - for event := range resp.Events { + for _, event := range resp.Events { b, err := json.Marshal(event) if err != nil { return fmt.Errorf("can't marshal events into JSON: %w", err) diff --git a/hw12_13_14_15_calendar/pkg/api/public/public.pb.go b/hw12_13_14_15_calendar/pkg/api/public/public.pb.go index e9ecd62..ccd63bc 100644 --- a/hw12_13_14_15_calendar/pkg/api/public/public.pb.go +++ b/hw12_13_14_15_calendar/pkg/api/public/public.pb.go @@ -64,6 +64,7 @@ type Event struct { Note string `protobuf:"bytes,5,opt,name=Note,proto3" json:"Note,omitempty"` UserID int64 `protobuf:"varint,6,opt,name=UserID,proto3" json:"UserID,omitempty"` NotifyTime *duration.Duration `protobuf:"bytes,7,opt,name=NotifyTime,proto3" json:"NotifyTime,omitempty"` + Notified bool `protobuf:"varint,8,opt,name=notified,proto3" json:"notified,omitempty"` XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_unrecognized []byte `json:"-"` XXX_sizecache int32 `json:"-"` @@ -143,6 +144,13 @@ func (m *Event) GetNotifyTime() *duration.Duration { return nil } +func (m *Event) GetNotified() bool { + if m != nil { + return m.Notified + } + return false +} + type CreateReq struct { Title string `protobuf:"bytes,2,opt,name=Title,proto3" json:"Title,omitempty"` Date *timestamp.Timestamp `protobuf:"bytes,3,opt,name=Date,proto3" json:"Date,omitempty"` @@ -567,45 +575,46 @@ func init() { func init() { proto.RegisterFile("public.proto", fileDescriptor_413a91106d7bcce8) } var fileDescriptor_413a91106d7bcce8 = []byte{ - // 606 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe4, 0x95, 0xcd, 0x6e, 0xd3, 0x40, - 0x10, 0xc7, 0x71, 0xe2, 0x38, 0xf5, 0x34, 0x2d, 0xe9, 0x50, 0x8a, 0x71, 0xab, 0x12, 0x2d, 0x42, + // 619 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xdc, 0x95, 0xcd, 0x6e, 0xd3, 0x40, + 0x10, 0xc7, 0xb1, 0xe3, 0x38, 0xf6, 0x34, 0x2d, 0xe9, 0x50, 0x8a, 0x71, 0xab, 0x12, 0x2d, 0x42, 0x8a, 0x72, 0xb0, 0x45, 0x8a, 0x90, 0xe0, 0x44, 0x8b, 0x23, 0x6a, 0x51, 0x8a, 0xb0, 0x5a, 0xf1, - 0x71, 0x73, 0xd2, 0x6d, 0xb0, 0x94, 0xd8, 0x8b, 0xbd, 0x41, 0x8a, 0xaa, 0x5c, 0x78, 0x05, 0xde, - 0x8a, 0x2b, 0xaf, 0xc0, 0x95, 0x07, 0xe0, 0x86,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x00, 0xff, 0xff, 0xef, 0xf0, 0x80, 0xc3, 0x4a, 0x06, 0x00, 0x00, } // Reference imports to suppress errors if they are not otherwise used. diff --git a/hw12_13_14_15_calendar/pkg/storage/sql/sql.go b/hw12_13_14_15_calendar/pkg/storage/sql/sql.go index e65463b..bd178cd 100644 --- a/hw12_13_14_15_calendar/pkg/storage/sql/sql.go +++ b/hw12_13_14_15_calendar/pkg/storage/sql/sql.go @@ -41,14 +41,15 @@ func (s *Storage) Create(ev event.Event) (event.ID, error) { lastInsertID := -1 if err := s.db.QueryRow( `INSERT INTO events - (title, date, latency, note, userID, notifyTime) VALUES - ($1, $2, $3, $4, $5, $6) RETURNING id`, + (title, date, latency, note, userID, notifyTime,notified) VALUES + ($1, $2, $3, $4, $5, $6, $7) RETURNING id`, ev.Title, ev.Date.Format(dateTimeLayout), ev.Latency, ev.Note, ev.UserID, ev.NotifyTime, + false, ).Scan(&lastInsertID); err != nil { return -1, fmt.Errorf("can't create event in SQL DB: %w", err) } @@ -87,7 +88,7 @@ func (s *Storage) Delete(id event.ID) error { func (s *Storage) List() (map[event.ID]event.Event, error) { res := make(map[event.ID]event.Event) - results, err := s.db.Query(`SELECT id,title,date,latency,note,userID,notifyTime from events ORDER BY id`) + results, err := s.db.Query(`SELECT id,title,date,latency,note,userID,notifyTime,notified from events ORDER BY id`) if err != nil { return nil, fmt.Errorf("can't get list of events from SQL DB: %w", err) } @@ -96,7 +97,7 @@ func (s *Storage) List() (map[event.ID]event.Event, error) { var id event.ID var evt event.Event var dateRaw string - err = results.Scan(&id, &evt.Title, &dateRaw, &evt.Latency, &evt.Note, &evt.UserID, &evt.NotifyTime) + err = results.Scan(&id, &evt.Title, &dateRaw, &evt.Latency, &evt.Note, &evt.UserID, &evt.NotifyTime, &evt.Notified) if err != nil { return nil, fmt.Errorf("can't parce list of events getted from SQL DB: %w", err) } @@ -116,7 +117,7 @@ func (s *Storage) GetByID(id event.ID) (event.Event, bool) { var res event.Event var dateRaw string err := s.db.QueryRow( - `SELECT title,date,latency,note,userID,notifyTime from events where id=$1`, id).Scan(&res.Title, &dateRaw, &res.Latency, &res.Note, &res.UserID, &res.NotifyTime) + `SELECT title,date,latency,note,userID,notifyTime,notified from events where id=$1`, id).Scan(&res.Title, &dateRaw, &res.Latency, &res.Note, &res.UserID, &res.NotifyTime, &res.Notified) if err != nil { return res, false } @@ -133,7 +134,7 @@ func (s *Storage) GetByDate(startDate time.Time, rng string) (map[event.ID]event endDate := getEndDate(startDate, rng) results, err := s.db.Query( - `SELECT id,title,date,latency,note,userID,notifyTime + `SELECT id,title,date,latency,note,userID,notifyTime,notified from events where (date>=$1 AND date<=$2) ORDER BY id`, @@ -148,7 +149,7 @@ func (s *Storage) GetByDate(startDate time.Time, rng string) (map[event.ID]event var id event.ID var evt event.Event var dateRaw string - err = results.Scan(&id, &evt.Title, &dateRaw, &evt.Latency, &evt.Note, &evt.UserID, &evt.NotifyTime) + err = results.Scan(&id, &evt.Title, &dateRaw, &evt.Latency, &evt.Note, &evt.UserID, &evt.NotifyTime, &evt.Notified) if err != nil { return nil, fmt.Errorf("can't parce list of events getted from SQL DB: %w", err) } @@ -167,11 +168,10 @@ func (s *Storage) GetByDate(startDate time.Time, rng string) (map[event.ID]event func (s *Storage) GetNotifications() (map[event.ID]event.Event, error) { res := make(map[event.ID]event.Event) results, err := s.db.Query( - `SELECT (id,title,date,latency,note,userID,notifyTime) + `SELECT id,title,date,latency,note,userID,notifyTime from events - where ($1>date-notifyTime) AND (NOT notified) - ORDER BY id`, - time.Now()) + where (NOW()>date) AND (NOT notified) + ORDER BY id`) if err != nil { return nil, fmt.Errorf("can't get list of events from SQL DB: %w", err) } diff --git a/hw12_13_14_15_calendar/test/main_test.go b/hw12_13_14_15_calendar/test/main_test.go index 75e7250..4121222 100644 --- a/hw12_13_14_15_calendar/test/main_test.go +++ b/hw12_13_14_15_calendar/test/main_test.go @@ -1,11 +1,11 @@ package main import ( + "context" "github.com/stretchr/testify/require" "github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/pkg/api/public" "github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/test/client" "github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/test/misc" - "context" "log" "sync" "testing" @@ -18,7 +18,7 @@ func TestPublicAPIEndpoints(t *testing.T) { client.HTTPAPI{BaseURL: "http://localhost:50052", Name: "HTTP REST API"}, } wg := sync.WaitGroup{} - wg.Add(len(cli) * 5) + wg.Add(len(cli) * 6) for _, c := range cli { t.Run("test "+c.GetName()+" for Create, GetById and Delete", func(t *testing.T) { @@ -153,6 +153,25 @@ func TestPublicAPIEndpoints(t *testing.T) { require.NoError(t, err) require.Equal(t, 16, len(resp4.Events)) }) + + t.Run("test "+c.GetName()+" for send notification", func(t *testing.T) { + var ids []int64 + defer func() { + wg.Done() + clean(c, &ids) + }() + + resp1, err := c.Create(&misc.TestEvent03) + require.NoError(t, err) + require.Greater(t, resp1.ID, int64(0)) + ids = append(ids, resp1.ID) + time.Sleep(3 * time.Second) + + resp2, err := c.GetByID(&public.GetByIDReq{ID: resp1.ID}) + require.NoError(t, err) + require.Equal(t, 1, len(resp2.Events)) + require.True(t, resp2.Events[0].Notified) + }) } } diff --git a/hw12_13_14_15_calendar/test/misc/misc.go b/hw12_13_14_15_calendar/test/misc/misc.go index 61a8ccc..74d7d35 100644 --- a/hw12_13_14_15_calendar/test/misc/misc.go +++ b/hw12_13_14_15_calendar/test/misc/misc.go @@ -27,6 +27,15 @@ var TestEvent02 = public.CreateReq{ UserID: 2222, } +var TestEvent03 = public.CreateReq{ + Title: "Test event 03", + Date: Time2pbtimestamp(time.Now().Add(-5 * time.Minute)), + Latency: Dur2pbduration(24 * time.Hour), + Note: "Note of test event 03", + NotifyTime: Dur2pbduration(1 * time.Second), + UserID: 1111, +} + func Time2pbtimestamp(t time.Time) *timestamp.Timestamp { r, err := ptypes.TimestampProto(t) if err != nil {