Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MI-3068] Replace app level token with user level token and Make 4 concurrent API requests #8

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"description": "Microsoft Calendar Integration",
"homepage_url": "https://mattermost.gitbook.io/plugin-mscalendar",
"support_url": "https://github.com/mattermost/mattermost-plugin-mscalendar/issues",
"release_notes_url": "https://github.com/mattermost/mattermost-plugin-mscalendar/releases/tag/v1.2.0",
"release_notes_url": "https://github.com/mattermost/mattermost-plugin-mscalendar/releases/tag/v1.2.1",
"icon_path": "assets/profile.svg",
"version": "1.2.0",
"version": "1.2.1",
"min_server_version": "5.37.0",
"server": {
"executables": {
Expand Down
2 changes: 1 addition & 1 deletion server/manifest.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions server/mscalendar/availability.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ func (m *mscalendar) GetCalendarViews(users []*store.User) ([]*remote.ViewCalend
RemoteUserID: u.Remote.ID,
StartTime: start,
EndTime: end,
AccessToken: u.OAuth2Token,
})
}

Expand Down
19 changes: 10 additions & 9 deletions server/mscalendar/daily_summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
const dailySummaryTimeWindow = time.Minute * 2

// Run daily summary job every 15 minutes
const DailySummaryJobInterval = 15 * time.Minute
const DailySummaryJobInterval = 1 * time.Minute

type DailySummary interface {
GetDailySummaryForUser(user *User) (string, error)
Expand Down Expand Up @@ -121,20 +121,21 @@ func (m *mscalendar) ProcessAllDailySummary(now time.Time) error {
continue
}

shouldPost, shouldPostErr := shouldPostDailySummary(dsum, now)
if shouldPostErr != nil {
m.Logger.Warnf("Error posting daily summary for user %s. err=%v", user.MattermostUserID, shouldPostErr)
continue
}
if !shouldPost {
continue
}
// shouldPost, shouldPostErr := shouldPostDailySummary(dsum, now)
// if shouldPostErr != nil {
// m.Logger.Warnf("Error posting daily summary for user %s. err=%v", user.MattermostUserID, shouldPostErr)
// continue
// }
// if !shouldPost {
// continue
// }

start, end := getTodayHoursForTimezone(now, dsum.Timezone)
req := &remote.ViewCalendarParams{
RemoteUserID: storeUser.Remote.ID,
StartTime: start,
EndTime: end,
AccessToken: storeUser.OAuth2Token,
}
requests = append(requests, req)
}
Expand Down
3 changes: 3 additions & 0 deletions server/remote/calendar.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package remote

import (
"time"

"golang.org/x/oauth2"
)

type Calendar struct {
Expand All @@ -19,6 +21,7 @@ type ViewCalendarParams struct {
StartTime time.Time
EndTime time.Time
RemoteUserID string
AccessToken *oauth2.Token
}

type ViewCalendarResponse struct {
Expand Down
13 changes: 8 additions & 5 deletions server/remote/msgraph/batch_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ package msgraph

import (
"net/http"

"golang.org/x/oauth2"
)

const maxNumRequestsPerBatch = 20

type singleRequest struct {
Body interface{} `json:"body"`
Headers map[string]string `json:"headers"`
ID string `json:"id"`
URL string `json:"url"`
Method string `json:"method"`
Body interface{} `json:"body"`
Headers map[string]string `json:"headers"`
ID string `json:"id"`
URL string `json:"url"`
Method string `json:"method"`
AccessToken *oauth2.Token
}

type fullBatchRequest struct {
Expand Down
6 changes: 2 additions & 4 deletions server/remote/msgraph/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,8 @@ func (c *client) call(method, path, contentType string, inBody io.Reader, out in
errResp := msgraph.ErrorResponse{Response: resp}
err = json.Unmarshal(responseData, &errResp)
if err != nil {
return responseData, errors.WithMessagef(err, "status: %s", resp.Status)
}
if err != nil {
return responseData, err
return responseData, errors.WithMessagef(err, "status: %s. response: %s", resp.Status, string(responseData))
}

return responseData, &errResp
}
123 changes: 100 additions & 23 deletions server/remote/msgraph/get_default_calendar_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
package msgraph

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"sync"
"time"

"github.com/pkg/errors"
Expand All @@ -22,6 +26,7 @@ type calendarViewSingleResponse struct {
Headers map[string]string `json:"headers"`
ID string `json:"id"`
Body calendarViewResponse `json:"body"`
Value []*remote.Event `json:"value,omitempty"`
Status int `json:"status"`
}

Expand All @@ -42,41 +47,113 @@ func (c *client) GetDefaultCalendarView(remoteUserID string, start, end time.Tim
return res.Value, nil
}

func (c *client) getCalenderView(reqs *singleRequest) (*calendarViewSingleResponse, error) {
req, err := http.NewRequest(reqs.Method, reqs.URL, nil)
if err != nil {
return nil, err
}

req.Header.Add("Authorization", reqs.AccessToken.AccessToken)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}

var out *calendarViewSingleResponse
err = json.Unmarshal(body, &out)
if err != nil {
return nil, err
}

out.ID = reqs.ID

return out, err
}

func (c *client) DoBatchViewCalendarRequests(allParams []*remote.ViewCalendarParams) ([]*remote.ViewCalendarResponse, error) {
requests := []*singleRequest{}
for _, params := range allParams {
u := getCalendarViewURL(params)
paramStr := getQueryParamStringForCalendarView(params.StartTime, params.EndTime)
url := fmt.Sprintf("https://graph.microsoft.com/v1.0/me/calendarview%s", paramStr)
req := &singleRequest{
ID: params.RemoteUserID,
URL: u,
Method: http.MethodGet,
Headers: map[string]string{},
ID: params.RemoteUserID,
URL: url,
Method: http.MethodGet,
Headers: map[string]string{},
AccessToken: params.AccessToken,
}

requests = append(requests, req)
}

batchRequests := prepareBatchRequests(requests)
var batchResponses []*calendarViewBatchResponse
for _, req := range batchRequests {
batchRes := &calendarViewBatchResponse{}
err := c.batchRequest(req, batchRes)
if err != nil {
return nil, errors.Wrap(err, "msgraph ViewCalendar batch request")
}

batchResponses = append(batchResponses, batchRes)
// batchRequests := prepareBatchRequests(requests)
// var batchResponses []*calendarViewBatchResponse
// for _, req := range batchRequests {
// batchRes := &calendarViewBatchResponse{}
// err := c.batchRequest(req, batchRes)
// if err != nil {
// return nil, errors.Wrap(err, "msgraph ViewCalendar batch request")
// }

// batchResponses = append(batchResponses, batchRes)
// }

// result := []*remote.ViewCalendarResponse{}
// for _, res := range batchResponses {
// for _, res := range batchRes.Responses {
// viewCalRes := &remote.ViewCalendarResponse{
// RemoteUserID: res.ID,
// Events: res.Body.Value,
// Error: res.Body.Error,
// }
// result = append(result, viewCalRes)
// }
// }

var wg = &sync.WaitGroup{}
maxGoroutines := 4
guard := make(chan struct{}, maxGoroutines)
responses := make(chan *calendarViewSingleResponse, len(requests))
for i, req := range requests {
guard <- struct{}{} // stops execution if the channel is full
wg.Add(1)
go func(n int, wg *sync.WaitGroup, req *singleRequest, guard chan struct{}, responses chan *calendarViewSingleResponse) {
resp, err := c.getCalenderView(req)
if err != nil {
fmt.Printf("\n\n\nerrr: %+v\n\n\n", err)
}
<-guard
responses <- resp
wg.Done()
}(i, wg, req, guard, responses)
}
fmt.Printf("\n\n\nheree -44\n\n\n")

wg.Wait()
close(responses)
close(guard)
// var responses []*calendarViewSingleResponse
// for _, req := range requests {
// resp, err := c.getCalenderView(req)
// if err != nil {
// return nil, errors.Wrap(err, "msgraph ViewCalendar batch request")
// }

// responses = append(responses, resp)
// }

result := []*remote.ViewCalendarResponse{}
for _, batchRes := range batchResponses {
for _, res := range batchRes.Responses {
viewCalRes := &remote.ViewCalendarResponse{
RemoteUserID: res.ID,
Events: res.Body.Value,
Error: res.Body.Error,
}
result = append(result, viewCalRes)
for res := range responses {
viewCalRes := &remote.ViewCalendarResponse{
RemoteUserID: res.ID,
Events: res.Value,
}
result = append(result, viewCalRes)
}

return result, nil
Expand Down