Skip to content

Commit

Permalink
Merge pull request #46 from cloudnativedaysjp/some-fix
Browse files Browse the repository at this point in the history
bug fix
  • Loading branch information
tkc66-buzz authored Oct 15, 2022
2 parents 565a78e + 60cfc21 commit 55aacd9
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 95 deletions.
16 changes: 8 additions & 8 deletions pkg/dkwatcher/dkwatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ func procedure(ctx context.Context,
dkClient dreamkast.Client, mw sharedmem.WriterIface, mr sharedmem.ReaderIface,
notificationEventSendChan chan<- model.CurrentAndNextTalk,
) error {
logger := utils.GetLogger(ctx)
rootLogger := utils.GetLogger(ctx)

tracks, err := dkClient.ListTracks(ctx)
if err != nil {
logger.Error(xerrors.Errorf("message: %w", err), "dkClient.ListTalks was failed")
rootLogger.Error(xerrors.Errorf("message: %w", err), "dkClient.ListTalks was failed")
return nil
}
for _, track := range tracks {
logger = logger.WithValues("trackId", track.Id)
logger := rootLogger.WithValues("trackId", track.Id)

if disabled, err := mr.DisableAutomation(track.Id); err != nil {
logger.Error(xerrors.Errorf("message: %w", err), "mr.DisableAutomation() was failed")
Expand All @@ -105,13 +105,13 @@ func procedure(ctx context.Context,
if track.Talks.WillStartNextTalkSince(howManyMinutesUntilNotify) {
currentTalk, err := track.Talks.GetCurrentTalk()
if err != nil {
logger.Error(xerrors.Errorf("message: %w", err), "dkClient.GetCurrentTalk was failed")
continue
logger.Info("currentTalk is none")
currentTalk = &model.Talk{}
}
nextTalk, err := track.Talks.GetNextTalk(currentTalk)
nextTalk, err := track.Talks.GetNextTalk()
if err != nil {
logger.Error(xerrors.Errorf("message: %w", err), "talks.GetNextTalk was failed")
continue
logger.Info("nextTalk is none")
nextTalk = &model.Talk{}
}
notificationEventSendChan <- model.CurrentAndNextTalk{
Current: *currentTalk, Next: *nextTalk}
Expand Down
36 changes: 25 additions & 11 deletions pkg/model/talk.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,17 @@ type Talks []Talk

func (ts Talks) WillStartNextTalkSince(untilNotify time.Duration) bool {
now := nowFunc()
for _, talk := range ts {
if now.After(talk.StartAt) {
diffTime := time.Duration(talk.EndAt.Sub(now))
if 0 < diffTime && diffTime <= untilNotify {
return true
}
}
nextTalk, err := ts.GetNextTalk()
if err != nil {
return false
}
return false
return nextTalk.StartAt.Sub(now) <= untilNotify
}

func (ts Talks) GetCurrentTalk() (*Talk, error) {
if len(ts) == 0 {
return nil, fmt.Errorf("Talks is empty")
}
now := nowFunc()
for _, talk := range ts {
if now.After(talk.StartAt) && now.Before(talk.EndAt) {
Expand All @@ -46,16 +45,31 @@ func (ts Talks) GetCurrentTalk() (*Talk, error) {
return nil, fmt.Errorf("Current talk not found")
}

func (ts Talks) GetNextTalk(currentTalk *Talk) (*Talk, error) {
func (ts Talks) GetNextTalk() (*Talk, error) {
if len(ts) == 0 {
return nil, fmt.Errorf("Talks is empty")
}

currentTalk, err := ts.GetCurrentTalk()
if err != nil {
// When currentTalk is none,
// if now is before event then return Talks[0] else raise an error
now := nowFunc()
lastTalk := ts[len(ts)-1]
if now.After(lastTalk.EndAt) {
return nil, fmt.Errorf("talks has already finished")
}
return &ts[0], nil
}
for i, talk := range ts {
if talk.Id == currentTalk.Id {
if i+1 == len(ts) {
return nil, fmt.Errorf("This talk is last")
return nil, fmt.Errorf("Current talk is last")
}
return &ts[i+1], nil
}
}
return nil, fmt.Errorf("Next talk not found")
return nil, fmt.Errorf("Something Wrong")
}

//
Expand Down
47 changes: 12 additions & 35 deletions pkg/model/talk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,17 @@ func TestTalk_WillStartNextTalkSince(t *testing.T) {
StartAt: setTestTime("2022-10-01T12:00:00"),
EndAt: setTestTime("2022-10-01T12:30:00"),
},
Talk{
Id: 4,
TalkName: "talk4",
TrackId: 1,
TrackName: "track1",
EventAbbr: "test event",
SpeakerNames: []string{"speakerA", "speaker"},
Type: 1,
StartAt: setTestTime("2022-10-01T12:30:00"),
EndAt: setTestTime("2022-10-01T13:00:00"),
},
},
want: true,
},
Expand Down Expand Up @@ -310,17 +321,6 @@ func TestTalk_GetNextTalk(t *testing.T) {
},
{
name: "not found next talk",
args: &Talk{
Id: 999,
TalkName: "talk999",
TrackId: 1,
TrackName: "track1",
EventAbbr: "test event",
SpeakerNames: []string{"speakerA", "speaker"},
Type: 1,
StartAt: setTestTime("2022-10-01T12:00:00"),
EndAt: setTestTime("2022-10-01T12:30:00"),
},
talks: Talks{
Talk{
Id: 1,
Expand Down Expand Up @@ -360,17 +360,6 @@ func TestTalk_GetNextTalk(t *testing.T) {
},
{
name: "last talk",
args: &Talk{
Id: 3,
TalkName: "talk3",
TrackId: 1,
TrackName: "track1",
EventAbbr: "test event",
SpeakerNames: []string{"speakerA", "speaker"},
Type: 1,
StartAt: setTestTime("2022-10-01T12:00:00"),
EndAt: setTestTime("2022-10-01T12:30:00"),
},
talks: Talks{
Talk{
Id: 1,
Expand Down Expand Up @@ -410,17 +399,6 @@ func TestTalk_GetNextTalk(t *testing.T) {
},
{
name: "found next talk",
args: &Talk{
Id: 3,
TalkName: "talk3",
TrackId: 1,
TrackName: "track1",
EventAbbr: "test event",
SpeakerNames: []string{"speakerA", "speaker"},
Type: 1,
StartAt: setTestTime("2022-10-01T12:00:00"),
EndAt: setTestTime("2022-10-01T12:30:00"),
},
talks: Talks{
Talk{
Id: 1,
Expand Down Expand Up @@ -472,7 +450,7 @@ func TestTalk_GetNextTalk(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := tt.talks.GetNextTalk(tt.args)
_, err := tt.talks.GetNextTalk()
if err == nil && tt.wantErr {
t.Errorf("Talk.GetTalkType() wantErr %v", tt.wantErr)
}
Expand Down Expand Up @@ -524,7 +502,6 @@ func TestTalk_GetActualStartAtAndEndAt(t *testing.T) {
t.Errorf("Talk.GetActualStartAtAndEndAt() error = %v, wantStatAt = %v, wantEndAt = %v,", err, tt.wantStartAt, tt.wantEndAt)
return
}

})
}
}
8 changes: 7 additions & 1 deletion pkg/notifier/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package notifier

import (
"context"
"fmt"

"github.com/go-logr/logr"
"golang.org/x/xerrors"
Expand All @@ -26,7 +27,12 @@ func NewController(logger logr.Logger,
func (c *Controller) Receive(m model.CurrentAndNextTalk) error {
ctx := logr.NewContext(context.Background(), c.logger)
slackClient := c.slackClients[m.TrackId()]
if err := slackClient.PostMessage(ctx, c.channelIds[m.TrackId()], ViewSession(m)); err != nil {
slackChannelId, ok := c.channelIds[m.TrackId()]
if !ok {
c.logger.Info(fmt.Sprintf("notifier is disabled on trackId %d", m.TrackId()))
return nil
}
if err := slackClient.PostMessage(ctx, slackChannelId, ViewSession(m)); err != nil {
return xerrors.Errorf("message: %w", err)
}
return nil
Expand Down
49 changes: 30 additions & 19 deletions pkg/notifier/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,35 @@ func viewSession(m model.CurrentAndNextTalk) (slack.Msg, error) {
currentTalk := m.Current
nextTalk := m.Next

accessory := make(map[string]interface{})
accessory := &slack.Accessory{}
if currentTalk.IsOnDemand() || nextTalk.IsOnDemand() {
accessory = map[string]interface{}{
"action_id": seaman_api.ActIdBroadcast_SceneNext,
"type": "multi_static_select",
"placeholder": map[string]interface{}{
"text": "switching",
"emoji": true,
"type": "plain_text",
},
"options": []interface{}{
map[string]interface{}{
"text": map[string]interface{}{
"type": "plain_text",
"text": "シーンを切り替える",
"emoji": true,
accessory = &slack.Accessory{
ButtonElement: &slack.ButtonBlockElement{
Type: slack.METButton,
ActionID: seaman_api.ActIdBroadcast_SceneNext,
Value: seaman_api.Track{Id: m.TrackId(), Name: m.TrackName()}.String(),
Text: &slack.TextBlockObject{
Type: "plain_text",
Text: "Switching",
},
Style: slack.StylePrimary,
Confirm: &slack.ConfirmationBlockObject{
Title: &slack.TextBlockObject{
Type: "plain_text",
Text: "Move to Next Scene",
},
Text: &slack.TextBlockObject{
Type: "plain_text",
Text: "Are you sure?",
},
Confirm: &slack.TextBlockObject{
Type: "plain_text",
Text: "OK",
},
Deny: &slack.TextBlockObject{
Type: "plain_text",
Text: "Cancel",
},
"value": seaman_api.Track{Id: m.TrackId(), Name: m.TrackName()}.String(),
},
},
}
Expand All @@ -46,6 +57,9 @@ func viewSession(m model.CurrentAndNextTalk) (slack.Msg, error) {
return castFromMapToMsg(
map[string]interface{}{
"blocks": []interface{}{
map[string]interface{}{
"type": "divider",
},
map[string]interface{}{
"type": "context",
"elements": []interface{}{
Expand Down Expand Up @@ -144,9 +158,6 @@ func viewSession(m model.CurrentAndNextTalk) (slack.Msg, error) {
eventUrlBase, nextTalk.EventAbbr, nextTalk.Id, nextTalk.TalkName),
},
},
map[string]interface{}{
"type": "divider",
},
},
},
)
Expand Down
46 changes: 28 additions & 18 deletions pkg/notifier/view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ import (
"testing"
"time"

"github.com/cloudnativedaysjp/cnd-operation-server/pkg/model"
"github.com/google/go-cmp/cmp"

"github.com/cloudnativedaysjp/cnd-operation-server/pkg/model"
)

func Test_viewSession(t *testing.T) {
t.Run("test", func(t *testing.T) {
expectedStr := `
{
"blocks": [
{
"type": "divider"
},
{
"type": "context",
"elements": [
Expand Down Expand Up @@ -100,27 +104,33 @@ func Test_viewSession(t *testing.T) {
"text": "Title: <https://event.cloudnativedays.jp/cndt2101/talks/10002|さらにものすごい発表>"
},
"accessory": {
"type": "multi_static_select",
"placeholder": {
"type": "button",
"action_id": "broadcast_scenenext",
"value": "1__A",
"text": {
"type": "plain_text",
"text": "switching",
"emoji": true
"text": "Switching"
},
"options": [
{
"text": {
"type": "plain_text",
"text": "シーンを切り替える",
"emoji": true
},
"value": "1__A"
"style": "primary",
"confirm": {
"title": {
"type": "plain_text",
"text": "Move to Next Scene"
},
"text": {
"type": "plain_text",
"text": "Are you sure?"
},
"confirm": {
"type": "plain_text",
"text": "OK"
},
"deny": {
"type": "plain_text",
"text": "Cancel"
}
],
"action_id": "broadcast_scenenext"
}
}
},
{
"type": "divider"
}
]
}
Expand Down
10 changes: 7 additions & 3 deletions pkg/obswatcher/obswatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,16 @@ func procedure(ctx context.Context, trackId int32,
remainingMilliSecond := t.DurationMilliSecond - t.CursorMilliSecond

if float64(startPreparetionPeriod/time.Millisecond) < remainingMilliSecond {
logger.Info(fmt.Sprintf("remainingTime on current Scene's MediaInput is %ds: continue",
startPreparetionPeriod/time.Second))
logger.Info(fmt.Sprintf("remainingTime on current Scene's MediaInput is over %ds: continue",
startPreparetionPeriod/time.Second),
"duration", t.DurationMilliSecond/float64(time.Millisecond),
"cursor", t.CursorMilliSecond/float64(time.Millisecond))
return nil
}
logger.Info(fmt.Sprintf("remainingTime on current Scene's MediaInput is within %ds",
startPreparetionPeriod/time.Second), "duration", t.DurationMilliSecond, "cursor", t.CursorMilliSecond)
startPreparetionPeriod/time.Second),
"duration", t.DurationMilliSecond/float64(time.Millisecond),
"cursor", t.CursorMilliSecond/float64(time.Millisecond))

// sleep until MediaInput is finished
time.Sleep(time.Duration(remainingMilliSecond) * time.Millisecond)
Expand Down

0 comments on commit 55aacd9

Please sign in to comment.