Skip to content

Commit

Permalink
Merge branch 'release/v0.1.48'
Browse files Browse the repository at this point in the history
* release/v0.1.48:
  chore: enhance discord message for delivery metric
  feat: rank delivery metric item (#642)
  chore: role permission seeding
  feat: delivery metric get leader board api (#640)
  fix: emoji displaying
  • Loading branch information
namnhce committed Jul 21, 2023
2 parents 68a1e75 + 2b5d4e7 commit 0354572
Show file tree
Hide file tree
Showing 19 changed files with 255 additions and 21 deletions.
3 changes: 2 additions & 1 deletion migrations/seed/permissions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,5 @@ INSERT INTO public.permissions (id, deleted_at, created_at, updated_at, name, co
('70109d50-6ec3-4475-9e45-757514781daf', null, '2023-06-04 11:19:08.204248', '2023-06-04 11:19:08.204248', 'Engagement Metrics Write', 'engagementMetrics.write'),
('8dd29797-e5fb-45ce-b9ef-8fbcfc3961bd', null, '2023-06-20 16:35:12.475872', '2023-06-20 16:35:12.475872', 'Brainery Logs Read', 'braineryLogs.read'),
('fa85ee6a-c335-4edb-8100-6aa840a0e520', null, '2023-06-20 16:35:12.475872', '2023-06-20 16:35:12.475872', 'Brainery Logs Write', 'braineryLogs.write'),
('e5a515aa-6f4f-4ac8-8eac-490a9a358833', null, '2023-07-19 16:35:12.475872', '2023-06-20 16:35:12.475872', 'Delivery Metrics Read', 'deliveryMetrics.read');
('e5a515aa-6f4f-4ac8-8eac-490a9a358833', null, '2023-07-19 16:35:12.475872', '2023-06-20 16:35:12.475872', 'Delivery Metrics Read', 'deliveryMetrics.read'),
('51a3ec4a-6bae-4f02-9d9b-89ba539346da', null, '2023-07-20 16:35:12.475872', '2023-06-20 16:35:12.475872', 'Delivery Metrics Leader Board Read', 'deliveryMetrics.leaderBoard.read');
12 changes: 8 additions & 4 deletions migrations/seed/role_permissions.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- CRONJOB
-- FORTRESS CRONJOB
INSERT INTO public.role_permissions (id, deleted_at, created_at, updated_at, role_id, permission_id) VALUES
('35db0910-66e6-4362-9082-9eb800730c74', NULL, '2022-11-11 18:35:27.069944', '2022-11-11 18:35:27.069944', '349876e0-4d3b-463a-a9b0-4083b3ebcd83', '475d8075-aec7-4d75-aa79-dc90c9dd9edb'),
('05f97a71-c065-4afd-a1e0-39777a933f0c', NULL, '2023-06-01 13:11:15.699280', '2023-06-01 13:11:15.699280', '349876e0-4d3b-463a-a9b0-4083b3ebcd83', '38a36881-7fb0-4003-9a9f-cbcc7eaa3deb'), -- engagementMetrics.read
Expand All @@ -10,7 +10,7 @@ INSERT INTO public.role_permissions (id, deleted_at, created_at, updated_at, rol
('9983379c-2207-44d5-8bdb-ffb9230376c8', NULL, '2022-11-11 18:35:27.069944', '2022-11-11 18:35:27.069944', '5a02645d-bbfd-4da5-9582-3d4a96bcfeb8', 'f1bef8de-8a6b-4d7d-ae1a-e09ec631fd94'),
('0c509f75-b97e-41a8-bf13-a91f9a2b3ba1', NULL, '2022-11-11 18:35:27.069944', '2022-11-11 18:35:27.069944', '5a02645d-bbfd-4da5-9582-3d4a96bcfeb8', '9be15ffa-3695-4d7f-93aa-ef594a08c36a');

-- DISCORD FORTRESS
-- FORTRESS DISCORD
INSERT INTO public.role_permissions (id, deleted_at, created_at, updated_at, role_id, permission_id) VALUES
('34a22220-725c-4b9c-817f-abfdd0e9d975', NULL, '2022-11-11 18:35:27.069944', '2022-11-11 18:35:27.069944', '3fcf9e36-2501-4f86-8418-cfe3a137b7f9', 'f75db77f-e299-4135-81b7-0784b452f7b7'),
('4fdd56b0-b85c-463d-85ef-4d192949a6e8', NULL, '2022-11-11 18:35:27.069944', '2022-11-11 18:35:27.069944', '3fcf9e36-2501-4f86-8418-cfe3a137b7f9', 'd500ea57-da9a-4b24-b147-59d009f0d7bc'),
Expand All @@ -22,7 +22,9 @@ INSERT INTO public.role_permissions (id, deleted_at, created_at, updated_at, rol
('63e4610b-87db-457e-a61f-5a2def006eae', NULL, '2023-06-04 11:17:06.279025', '2023-06-04 11:17:06.279025', '3fcf9e36-2501-4f86-8418-cfe3a137b7f9', '38a36881-7fb0-4003-9a9f-cbcc7eaa3deb'), -- engagementMetrics.read
('8c0db283-d1e0-4f45-abb8-660212c13404', NULL, '2023-06-04 11:17:20.603667', '2023-06-04 11:17:20.603667', '3fcf9e36-2501-4f86-8418-cfe3a137b7f9', '70109d50-6ec3-4475-9e45-757514781daf'), -- engagementMetrics.write
('c781b3b2-9047-4686-9461-30a00d78b50a', NULL, '2023-06-20 16:35:12.475872', '2023-06-20 16:35:12.475872', '3fcf9e36-2501-4f86-8418-cfe3a137b7f9', '8dd29797-e5fb-45ce-b9ef-8fbcfc3961bd'), -- braineryLogs.read
('8c2f4d63-884a-4e42-a93b-073196aef945', NULL, '2023-06-20 16:35:12.475872', '2023-06-20 16:35:12.475872', '3fcf9e36-2501-4f86-8418-cfe3a137b7f9', 'fa85ee6a-c335-4edb-8100-6aa840a0e520'); -- braineryLogs.write
('8c2f4d63-884a-4e42-a93b-073196aef945', NULL, '2023-06-20 16:35:12.475872', '2023-06-20 16:35:12.475872', '3fcf9e36-2501-4f86-8418-cfe3a137b7f9', 'fa85ee6a-c335-4edb-8100-6aa840a0e520'), -- braineryLogs.write
('7c6b1f91-f22f-46fb-9c71-5868967d485b', NULL, '2023-06-20 16:35:12.475872', '2023-06-20 16:35:12.475872', '3fcf9e36-2501-4f86-8418-cfe3a137b7f9', 'e5a515aa-6f4f-4ac8-8eac-490a9a358833'), -- deliveryMetrics.read
('ec847eff-7ca4-4b57-a406-96a44f09e0fd', NULL, '2022-11-11 18:35:27.069944', '2022-11-11 18:35:27.069944', '3fcf9e36-2501-4f86-8418-cfe3a137b7f9', '51a3ec4a-6bae-4f02-9d9b-89ba539346da'); -- deliveryMetrics.leaderBoard.read

-- CONTRACTOR
INSERT INTO public.role_permissions (id, deleted_at, created_at, updated_at, role_id, permission_id) VALUES
Expand Down Expand Up @@ -174,6 +176,7 @@ INSERT INTO public.role_permissions (id, deleted_at, created_at, updated_at, rol

-- ADMIN
INSERT INTO public.role_permissions (id, deleted_at, created_at, updated_at, role_id, permission_id) VALUES
('bf4a2d78-da1c-447e-bd4e-a6f4a3997e4a', NULL, '2023-07-20 16:35:12.475872', '2023-07-20 16:35:12.475872', 'c23c1c1c-bfaf-41e6-a4d7-6ef196fd2736', '51a3ec4a-6bae-4f02-9d9b-89ba539346da'); -- deliveryMetrics.leaderBoard.read
('3dc8e4ab-8402-4655-9795-0c1ade988e64', null, '2022-11-11 18:35:27.069944', '2022-11-11 18:35:27.069944', 'c23c1c1c-bfaf-41e6-a4d7-6ef196fd2736', '93886b0b-6592-4838-b024-1aefb123fed6'),
('19f61ff9-003e-4ea2-8ede-fea5f671530b', null, '2022-11-11 18:35:27.069944', '2022-11-11 18:35:27.069944', 'c23c1c1c-bfaf-41e6-a4d7-6ef196fd2736', '5820dd37-a9bd-455e-b49b-b54ad7df180e'),
('8bfac1eb-3d44-49f3-a4cd-69126605448b', null, '2022-11-11 18:35:27.069944', '2022-11-11 18:35:27.069944', 'c23c1c1c-bfaf-41e6-a4d7-6ef196fd2736', '495c96ae-60f9-4c57-bc96-9504d0fedde6'),
Expand Down Expand Up @@ -267,4 +270,5 @@ INSERT INTO public.role_permissions (id, deleted_at, created_at, updated_at, rol
('2bf409ad-46e3-47b9-b746-571735b69c92', null, '2023-06-04 11:15:12.249251', '2023-06-04 11:15:12.249251', 'c23c1c1c-bfaf-41e6-a4d7-6ef196fd2736', '70109d50-6ec3-4475-9e45-757514781daf'), -- engagementMetrics.write
('b03375c3-328b-4af7-98f8-bf901a106b0b', NULL, '2023-06-20 16:35:12.475872', '2023-06-20 16:35:12.475872', 'c23c1c1c-bfaf-41e6-a4d7-6ef196fd2736', '8dd29797-e5fb-45ce-b9ef-8fbcfc3961bd'), -- braineryLogs.read
('10b6dedf-b939-4a5a-9f02-b1b0db917058', NULL, '2023-06-20 16:35:12.475872', '2023-06-20 16:35:12.475872', 'c23c1c1c-bfaf-41e6-a4d7-6ef196fd2736', 'fa85ee6a-c335-4edb-8100-6aa840a0e520'), -- braineryLogs.write
('1f26de44-0546-44f3-8d68-9fd437ffb369', NULL, '2023-07-19 16:35:12.475872', '2023-06-20 16:35:12.475872', 'c23c1c1c-bfaf-41e6-a4d7-6ef196fd2736', 'e5a515aa-6f4f-4ac8-8eac-490a9a358833'); -- deliveryMetrics.read
('1f26de44-0546-44f3-8d68-9fd437ffb369', NULL, '2023-07-19 16:35:12.475872', '2023-06-20 16:35:12.475872', 'c23c1c1c-bfaf-41e6-a4d7-6ef196fd2736', 'e5a515aa-6f4f-4ac8-8eac-490a9a358833'), -- deliveryMetrics.read
('bf4a2d78-da1c-447e-bd4e-a6f4a3997e4a', NULL, '2023-07-20 16:35:12.475872', '2023-07-20 16:35:12.475872', 'c23c1c1c-bfaf-41e6-a4d7-6ef196fd2736', '51a3ec4a-6bae-4f02-9d9b-89ba539346da'); -- deliveryMetrics.leaderBoard.read
62 changes: 62 additions & 0 deletions pkg/controller/deliverymetrics/leaderboard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package deliverymetrics

import (
"github.com/pkg/errors"

"github.com/dwarvesf/fortress-api/pkg/model"
)

func (c controller) GetWeeklyLeaderBoard() (*model.WeeklyLeaderBoard, error) {
w, err := c.store.DeliveryMetric.GetLatestWeek(c.repo.DB())
if err != nil {
return nil, errors.Wrap(err, "failed to get latest week")
}

// Get top 10 users with highest points
metrics, err := c.store.DeliveryMetric.GetTopWeighMetrics(c.repo.DB(), w, 5)
if err != nil {
return nil, errors.Wrap(err, "failed to get top users with highest points")
}

items := make([]model.LeaderBoardItem, 0, len(metrics))
// Get user info
for _, m := range metrics {
e, err := c.store.Employee.One(c.repo.DB(), m.EmployeeID.String(), false)
if err != nil {
return nil, errors.Wrap(err, "failed to get employee "+m.EmployeeID.String())
}

// Get discord acc
d, err := c.store.DiscordAccount.One(c.repo.DB(), e.DiscordAccountID.String())
if err != nil {
return nil, errors.Wrap(err, "failed to get discord account "+e.DiscordAccountID.String()+" of employee "+e.ID.String())
}

items = append(items, model.LeaderBoardItem{
EmployeeID: e.ID.String(),
EmployeeName: e.DisplayName,
Points: m.Weight,
Effectiveness: m.Effectiveness,
DiscordID: d.DiscordID,
DiscordUsername: d.Username,
})
}

return &model.WeeklyLeaderBoard{
Date: w,
Items: rankItems(items),
}, nil
}

func rankItems(data []model.LeaderBoardItem) []model.LeaderBoardItem {
// Set the rank for each employee
for i := range data {
if i > 0 && data[i].Points.Equal(data[i-1].Points) && data[i].Effectiveness.Equal(data[i-1].Effectiveness) {
data[i].Rank = data[i-1].Rank
} else {
data[i].Rank = i + 1
}
}

return data
}
3 changes: 3 additions & 0 deletions pkg/controller/deliverymetrics/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ package deliverymetrics
import (
"github.com/dwarvesf/fortress-api/pkg/config"
"github.com/dwarvesf/fortress-api/pkg/logger"
"github.com/dwarvesf/fortress-api/pkg/model"
"github.com/dwarvesf/fortress-api/pkg/service"
"github.com/dwarvesf/fortress-api/pkg/store"
)

type IController interface {
GetWeeklyReport() (*WeeklyReport, error)
GetMonthlyReport() (*MonthlyReport, error)
GetWeeklyLeaderBoard() (*model.WeeklyLeaderBoard, error)

Sync() error
}

Expand Down
22 changes: 20 additions & 2 deletions pkg/handler/deliverymetric/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (h *handler) GetWeeklyReport(c *gin.Context) {
}

// Return data
c.JSON(http.StatusOK, view.CreateResponse[any](report, nil, nil, nil, "ok"))
c.JSON(http.StatusOK, view.CreateResponse[any](report, nil, nil, nil, ""))
}

func (h *handler) GetMonthlyReport(c *gin.Context) {
Expand All @@ -42,5 +42,23 @@ func (h *handler) GetMonthlyReport(c *gin.Context) {
}

// Return data
c.JSON(http.StatusOK, view.CreateResponse[any](report, nil, nil, nil, "ok"))
c.JSON(http.StatusOK, view.CreateResponse[any](report, nil, nil, nil, ""))
}

func (h *handler) GetWeeklyLeaderBoard(c *gin.Context) {
l := h.logger.Fields(logger.Fields{
"handler": "delivery",
"method": "GetWeeklyLeaderBoard",
})

// Get data of current week
report, err := h.controller.DeliveryMetric.GetWeeklyLeaderBoard()
if err != nil {
l.Error(err, "failed to get weekly leaderboard")
c.JSON(http.StatusInternalServerError, view.CreateResponse[any](nil, nil, err, nil, "failed to get weekly leaderboard"))
return
}

// Return data
c.JSON(http.StatusOK, view.CreateResponse[any](view.ToDeliveryMetricLeaderBoard(report), nil, nil, nil, ""))
}
2 changes: 2 additions & 0 deletions pkg/handler/deliverymetric/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ import "github.com/gin-gonic/gin"
type IHandler interface {
GetWeeklyReport(c *gin.Context)
GetMonthlyReport(c *gin.Context)
GetWeeklyLeaderBoard(c *gin.Context)

Sync(c *gin.Context)
}
9 changes: 8 additions & 1 deletion pkg/handler/discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,13 @@ func (h *handler) DeliveryMetricsReport(c *gin.Context) {
return
}

leaderBoard, err := h.controller.DeliveryMetric.GetWeeklyLeaderBoard()
if err != nil {
l.Errorf(err, "failed to get delivery metric weekly report", "body", in)
c.JSON(http.StatusInternalServerError, view.CreateResponse[any](nil, nil, err, in, ""))
return
}

vw := &view.DeliveryMetricWeeklyReport{
LastWeek: view.DeliveryMetricWeekReport{
Date: report.LastWeek.Date,
Expand All @@ -338,7 +345,7 @@ func (h *handler) DeliveryMetricsReport(c *gin.Context) {
AvgEffortChangePercentage: report.AvgEffortChangePercentage,
}

discordMsg, err := h.service.Discord.DeliveryMetricWeeklyReport(vw, in.ChannelID)
discordMsg, err := h.service.Discord.DeliveryMetricWeeklyReport(vw, view.ToDeliveryMetricLeaderBoard(leaderBoard), in.ChannelID)
if err != nil {
h.logger.Error(err, "failed to post Discord message")
c.JSON(http.StatusOK, view.CreateResponse[any](nil, nil, err, discordMsg, ""))
Expand Down
15 changes: 15 additions & 0 deletions pkg/model/delivery_metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,18 @@ type DeliveryMetric struct {
}

type DeliveryMetrics []DeliveryMetric

type WeeklyLeaderBoard struct {
Date *time.Time
Items []LeaderBoardItem
}

type LeaderBoardItem struct {
EmployeeID string
EmployeeName string
Points decimal.Decimal
Effectiveness decimal.Decimal
DiscordID string
DiscordUsername string
Rank int
}
1 change: 1 addition & 0 deletions pkg/model/permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ const (
PermissionBraineryLogsWrite PermissionCode = "braineryLogs.write"
PermissionBraineryLogsRead PermissionCode = "braineryLogs.read"
PermissionDeliveryMetricsRead PermissionCode = "deliveryMetrics.read"
PermissionDeliveryMetricsLeaderBoardRead PermissionCode = "deliveryMetrics.leaderBoard.read"
)

func (p PermissionCode) String() string {
Expand Down
1 change: 1 addition & 0 deletions pkg/routes/v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ func loadV1Routes(r *gin.Engine, h *handler.Handler, repo store.DBRepo, s *store
deliveryGroup := v1.Group("/delivery-metrics")
deliveryGroup.GET("/report/weekly", amw.WithAuth, pmw.WithPerm(model.PermissionDeliveryMetricsRead), h.DeliveryMetric.GetWeeklyReport)
deliveryGroup.GET("/report/monthly", amw.WithAuth, pmw.WithPerm(model.PermissionDeliveryMetricsRead), h.DeliveryMetric.GetMonthlyReport)
deliveryGroup.GET("/leader-board/weekly", amw.WithAuth, pmw.WithPerm(model.PermissionDeliveryMetricsLeaderBoardRead), h.DeliveryMetric.GetWeeklyLeaderBoard)
}

/////////////////
Expand Down
6 changes: 6 additions & 0 deletions pkg/routes/v1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,12 @@ func Test_loadV1Routes(t *testing.T) {
Handler: "github.com/dwarvesf/fortress-api/pkg/handler/deliverymetric.IHandler.GetMonthlyReport-fm",
},
},
"/api/v1/delivery-metrics/leader-board/weekly": {
"GET": {
Method: "GET",
Handler: "github.com/dwarvesf/fortress-api/pkg/handler/deliverymetric.IHandler.GetWeeklyLeaderBoard-fm",
},
},

"/api/v1/public/clients": {
"GET": {
Expand Down
14 changes: 14 additions & 0 deletions pkg/service/discord/const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package discord

var mapEmoji = map[string]string{
"ARROW_DOWN_ANIMATED": "<a:arrow_down_animated:1131789144759214171>",
"ARROW_UP_ANIMATED": "<a:arrow_up_animated:1131789319644921936>",
"BADGE1": "<a:badge1:1131850989062852638>",
"BADGE2": "<a:badge2:1131850991663337554>",
"BADGE3": "<a:badge3:1131850996159610930>",
"BADGE5": "<a:badge5:1131851001117294672>",
}

func getEmoji(emoji string) string {
return mapEmoji[emoji]
}
54 changes: 43 additions & 11 deletions pkg/service/discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,9 +478,16 @@ func calculateTopContributor(topContributors []view.TopContributor) string {

return topContributor
}
func (d *discordClient) DeliveryMetricWeeklyReport(deliveryMetric *view.DeliveryMetricWeeklyReport, channelID string) (*discordgo.Message, error) {

func (d *discordClient) DeliveryMetricWeeklyReport(deliveryMetric *view.DeliveryMetricWeeklyReport, leaderBoard *view.WeeklyLeaderBoard, channelID string) (*discordgo.Message, error) {
var messageEmbed []*discordgo.MessageEmbedField
content := "\n"
content := "\n\n"

if leaderBoard != nil {
leaderBoardStr := getLeaderboardAsString(leaderBoard.Items)
content += leaderBoardStr
content += "\n\n"
}

previousWeek := fmt.Sprintf("**Previous Week - %v**\n", deliveryMetric.LastWeek.Date.Format("02 Jan 2006"))
previousWeek += fmt.Sprintf("`Total Point. %vpts`\n", deliveryMetric.LastWeek.TotalPoints)
Expand All @@ -490,27 +497,27 @@ func (d *discordClient) DeliveryMetricWeeklyReport(deliveryMetric *view.Delivery

content += previousWeek

emojiUp := ":arrow_up_animated:"
emojiDown := ":arrow_down_animated:"
emojiUp := getEmoji("ARROW_UP_ANIMATED")
emojiDown := getEmoji("ARROW_DOWN_ANIMATED")

pointChange := fmt.Sprintf("%v %v%%", emojiUp, deliveryMetric.TotalPointChangePercentage)
if deliveryMetric.TotalPointChangePercentage < 0 {
pointChange = fmt.Sprintf("%v %v%%", emojiDown, deliveryMetric.TotalPointChangePercentage)
pointChange = fmt.Sprintf("%v%v%%", emojiDown, deliveryMetric.TotalPointChangePercentage)
}

effortChange := fmt.Sprintf("%v %v%%", emojiUp, deliveryMetric.EffortChangePercentage)
effortChange := fmt.Sprintf("%v%v%%", emojiUp, deliveryMetric.EffortChangePercentage)
if deliveryMetric.EffortChangePercentage < 0 {
effortChange = fmt.Sprintf("%v %v%%", emojiDown, deliveryMetric.EffortChangePercentage)
effortChange = fmt.Sprintf("%v%v%%", emojiDown, deliveryMetric.EffortChangePercentage)
}

avgPointChange := fmt.Sprintf("%v %v%%", emojiUp, deliveryMetric.AvgPointChangePercentage)
avgPointChange := fmt.Sprintf("%v%v%%", emojiUp, deliveryMetric.AvgPointChangePercentage)
if deliveryMetric.AvgPointChangePercentage < 0 {
avgPointChange = fmt.Sprintf("%v %v%%", emojiDown, deliveryMetric.AvgPointChangePercentage)
avgPointChange = fmt.Sprintf("%v%v%%", emojiDown, deliveryMetric.AvgPointChangePercentage)
}

avgEffortChange := fmt.Sprintf("%v %v%%", emojiUp, deliveryMetric.AvgEffortChangePercentage)
if deliveryMetric.AvgEffortChangePercentage < 0 {
avgEffortChange = fmt.Sprintf("%v %v%%", emojiDown, deliveryMetric.AvgEffortChangePercentage)
avgEffortChange = fmt.Sprintf("%v%v%%", emojiDown, deliveryMetric.AvgEffortChangePercentage)
}

currentWeek := fmt.Sprintf("\n**Current Week - %v**\n", deliveryMetric.CurrentWeek.Date.Format("02 Jan 2006"))
Expand All @@ -522,7 +529,7 @@ func (d *discordClient) DeliveryMetricWeeklyReport(deliveryMetric *view.Delivery
content += currentWeek

msg := &discordgo.MessageEmbed{
Title: "**DELIVERY METRIC WEEKLY REPORT**",
Title: "**🏆 DELIVERY WEEKLY REPORT 🏆**",
Fields: messageEmbed,
Description: content,
Footer: &discordgo.MessageEmbedFooter{
Expand All @@ -534,6 +541,31 @@ func (d *discordClient) DeliveryMetricWeeklyReport(deliveryMetric *view.Delivery
return d.SendEmbeddedMessageWithChannel(nil, msg, channelID)
}

func getLeaderboardAsString(data []view.LeaderBoardItem) string {
emojiMap := map[int]string{
1: getEmoji("BADGE1"),
2: getEmoji("BADGE2"),
3: getEmoji("BADGE3"),
4: getEmoji("BADGE5"),
5: getEmoji("BADGE5"),
}
// Sort the data by rank in ascending order
var currentRank int
var leaderboardString strings.Builder
for _, employee := range data {
if employee.Rank != currentRank {
if currentRank > 0 {
leaderboardString.WriteString("\n")
}
currentRank = employee.Rank
leaderboardString.WriteString(fmt.Sprintf("%v ", emojiMap[currentRank]))
}

leaderboardString.WriteString(fmt.Sprintf("<@%v> ", employee.DiscordID))
}

return leaderboardString.String()
}
func (d *discordClient) SendEmbeddedMessageWithChannel(original *model.OriginalDiscordMessage, embed *discordgo.MessageEmbed, channelId string) (*discordgo.Message, error) {
msg, err := d.session.ChannelMessageSendEmbed(channelId, normalize(original, embed))
return msg, err
Expand Down
2 changes: 1 addition & 1 deletion pkg/service/discord/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ type IService interface {
GetMessagesAfterCursor(channelID string, cursorMessageID string, lastMessageID string) ([]*discordgo.Message, error)
ReportBraineryMetrics(queryView string, braineryMetric *view.BraineryMetric, channelID string) (*discordgo.Message, error)
SendEmbeddedMessageWithChannel(original *model.OriginalDiscordMessage, embed *discordgo.MessageEmbed, channelId string) (*discordgo.Message, error)
DeliveryMetricWeeklyReport(deliveryMetrics *view.DeliveryMetricWeeklyReport, channelID string) (*discordgo.Message, error)
DeliveryMetricWeeklyReport(deliveryMetrics *view.DeliveryMetricWeeklyReport, leaderBoard *view.WeeklyLeaderBoard, channelID string) (*discordgo.Message, error)
}
12 changes: 12 additions & 0 deletions pkg/store/deliverymetric/delivery_metric.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package deliverymetric

import (
"time"

"gorm.io/gorm"

"github.com/dwarvesf/fortress-api/pkg/model"
Expand All @@ -23,6 +25,16 @@ func (s *store) Create(db *gorm.DB, e []model.DeliveryMetric) (rs []model.Delive
return e, db.Create(&e).Error
}

func (s *store) GetLatestWeek(db *gorm.DB) (*time.Time, error) {
var rs *time.Time
return rs, db.Model(&model.DeliveryMetric{}).Select("date").Order("date DESC").Limit(1).First(&rs).Error
}

func (s *store) GetTopWeighMetrics(db *gorm.DB, w *time.Time, limit int) ([]model.DeliveryMetric, error) {
var rs []model.DeliveryMetric
return rs, db.Where("date = ?", w).Order("weight DESC, effectiveness DESC").Limit(limit).Find(&rs).Error
}

// UpdateSelectedFieldsByID just update selected fields by id
func (s *store) UpdateSelectedFieldsByID(db *gorm.DB, id string, updateModel model.DeliveryMetric, updatedFields ...string) (*model.DeliveryMetric, error) {
rs := model.DeliveryMetric{}
Expand Down
Loading

0 comments on commit 0354572

Please sign in to comment.