diff --git a/go.mod b/go.mod index b0ea2843..a65bfc49 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,7 @@ module github.com/mattermost/mattermost-plugin-jira go 1.22 -toolchain go1.22.2 +toolchain go1.22.8 require ( github.com/andygrunwald/go-jira v1.16.0 @@ -10,9 +10,10 @@ require ( github.com/dghubble/oauth1 v0.5.0 github.com/golang-jwt/jwt/v5 v5.2.1 github.com/gorilla/mux v1.8.1 + github.com/hashicorp/go-multierror v1.1.1 github.com/jarcoal/httpmock v1.0.8 github.com/mattermost/mattermost-plugin-autolink v1.2.2-0.20210709183311-c8fa30db649f - github.com/mattermost/mattermost/server/public v0.1.7 + github.com/mattermost/mattermost/server/public v0.1.9 github.com/pkg/errors v0.9.1 github.com/rbriski/atlassian-jwt v0.0.0-20240408161306-6b6d681cf2d9 github.com/rudderlabs/analytics-go v3.3.3+incompatible @@ -20,6 +21,7 @@ require ( github.com/trivago/tgo v1.0.7 golang.org/x/oauth2 v0.21.0 golang.org/x/text v0.16.0 + gopkg.in/yaml.v3 v3.0.1 ) require ( @@ -38,7 +40,6 @@ require ( github.com/gorilla/websocket v1.5.3 // indirect github.com/hashicorp/errwrap v1.1.0 // indirect github.com/hashicorp/go-hclog v1.6.3 // indirect - github.com/hashicorp/go-multierror v1.1.1 // indirect github.com/hashicorp/go-plugin v1.6.1 // indirect github.com/hashicorp/yamux v0.1.1 // indirect github.com/klauspost/compress v1.15.14 // indirect @@ -79,5 +80,4 @@ require ( gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index fb58eacc..f41036d9 100644 --- a/go.sum +++ b/go.sum @@ -630,8 +630,8 @@ github.com/mattermost/mattermost-plugin-autolink v1.2.2-0.20210709183311-c8fa30d github.com/mattermost/mattermost-plugin-autolink v1.2.2-0.20210709183311-c8fa30db649f/go.mod h1:JO5RQqTYL2D1zDz7+5ucAmNeRMiudDvZQoRtqbm+1sU= github.com/mattermost/mattermost-server/v5 v5.3.2-0.20210621071817-df224571d8a1 h1:5/lZibP83SrOpuKs4fE782pfWNFX/QTWwO2PuJJwGJ4= github.com/mattermost/mattermost-server/v5 v5.3.2-0.20210621071817-df224571d8a1/go.mod h1:S3zT7H4bAxsev1d2ThoSRBhyEXZa6JZOfpxvy2B25y4= -github.com/mattermost/mattermost/server/public v0.1.7 h1:WA+fnLrQQeE6xTyHERqcGiKljBFK6m8WYL4Pez07ko4= -github.com/mattermost/mattermost/server/public v0.1.7/go.mod h1:SkTKbMul91Rq0v2dIxe8mqzUOY+3KwlwwLmAlxDfGCk= +github.com/mattermost/mattermost/server/public v0.1.9 h1:l/OKPRVuFeqL0yqRVC/JpveG5sLNKcT9llxqMkO9e+s= +github.com/mattermost/mattermost/server/public v0.1.9/go.mod h1:SkTKbMul91Rq0v2dIxe8mqzUOY+3KwlwwLmAlxDfGCk= github.com/mattermost/rsc v0.0.0-20160330161541-bbaefb05eaa0/go.mod h1:nV5bfVpT//+B1RPD2JvRnxbkLmJEYXmRaaVl15fsXjs= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ= diff --git a/server/support_packet.go b/server/support_packet.go new file mode 100644 index 00000000..2475ab13 --- /dev/null +++ b/server/support_packet.go @@ -0,0 +1,59 @@ +package main + +import ( + "path/filepath" + + "github.com/hashicorp/go-multierror" + "github.com/pkg/errors" + "gopkg.in/yaml.v3" + + "github.com/mattermost/mattermost/server/public/model" + "github.com/mattermost/mattermost/server/public/plugin" +) + +type SupportPacket struct { + Version string `yaml:"version"` + + InstanceCount int `yaml:"instance_count"` + ServerInstanceCount int `yaml:"server_instance_count"` + CloudInstanceCount int `yaml:"cloud_instance_count"` + SubscriptionCount int `yaml:"subscription_count"` + ConnectedUserCount int `yaml:"connected_user_count"` +} + +func (p *Plugin) GenerateSupportData(_ *plugin.Context) ([]*model.FileData, error) { + var result *multierror.Error + + connectedUserCount, err := p.userCount() + if err != nil { + result = multierror.Append(result, errors.Wrap(err, "failed to get the number of connected users for Support Packet")) + } + + serverICount, cloudICount, err := p.instanceCount() + if err != nil { + result = multierror.Append(result, errors.Wrap(err, "failed to get the number of instances for Support Packet")) + } + + subscriptionCount, err := p.subscriptionCount() + if err != nil { + result = multierror.Append(result, errors.Wrap(err, "failed to get the number of subscriptions for Support Packet")) + } + + diagnostics := SupportPacket{ + Version: manifest.Version, + InstanceCount: serverICount + cloudICount, + ServerInstanceCount: serverICount, + CloudInstanceCount: cloudICount, + SubscriptionCount: subscriptionCount, + ConnectedUserCount: connectedUserCount, + } + body, err := yaml.Marshal(diagnostics) + if err != nil { + return nil, errors.Wrap(err, "failed to marshal diagnostics") + } + + return []*model.FileData{{ + Filename: filepath.Join(manifest.Id, "diagnostics.yaml"), + Body: body, + }}, result.ErrorOrNil() +} diff --git a/server/telemetry.go b/server/telemetry.go index eeaca372..fdaf663e 100644 --- a/server/telemetry.go +++ b/server/telemetry.go @@ -1,6 +1,8 @@ package main import ( + "github.com/pkg/errors" + "github.com/mattermost/mattermost-plugin-jira/server/telemetry" ) @@ -22,52 +24,79 @@ func (p *Plugin) OnSendDailyTelemetry() { args := map[string]interface{}{} // Jira instances - server, cloud := 0, 0 - instances, err := p.instanceStore.LoadInstances() + serverICount, cloudICount, err := p.instanceCount() if err != nil { p.client.Log.Warn("Failed to get instances for telemetry", "error", err) } else { - for _, id := range instances.IDs() { - switch instances.Get(id).Type { - case ServerInstanceType: - server++ - case CloudInstanceType: - cloud++ - } - } - args["instance_count"] = server + cloud - if server > 0 { - args["server_instance_count"] = server - } - if cloud > 0 { - args["cloud_instance_count"] = cloud + args["instance_count"] = serverICount + cloudICount + if serverICount > 0 { + args["server_instance_count"] = serverICount } - - // Subscriptions - numSubscriptions := 0 - var subs *Subscriptions - for _, id := range instances.IDs() { - subs, err = p.getSubscriptions(id) - if err != nil { - p.client.Log.Warn("Failed to get subscriptions for telemetry", "error", err) - } - numSubscriptions += len(subs.Channel.ByID) + if cloudICount > 0 { + args["cloud_instance_count"] = cloudICount } + } - args["subscriptions"] = numSubscriptions + subscriptionCount, err := p.subscriptionCount() + if err != nil { + p.client.Log.Warn("Failed to get the number of subscriptions for telemetry", "error", err) + } else { + args["subscriptions"] = subscriptionCount } // Connected users - connected, err := p.userStore.CountUsers() + connectedUsers, err := p.userCount() if err != nil { p.client.Log.Warn("Failed to get the number of connected users for telemetry", "error", err) } else { - args["connected_user_count"] = connected + args["connected_user_count"] = connectedUsers } p.TrackEvent("stats", args) } +func (p *Plugin) instanceCount() (server int, cloud int, err error) { + instances, err := p.instanceStore.LoadInstances() + if err != nil { + return + } + + for _, id := range instances.IDs() { + switch instances.Get(id).Type { + case ServerInstanceType: + server++ + case CloudInstanceType: + cloud++ + } + } + + return +} + +func (p *Plugin) subscriptionCount() (int, error) { + instances, err := p.instanceStore.LoadInstances() + if err != nil { + return 0, err + } + + // Subscriptions + numSubscriptions := 0 + var subs *Subscriptions + for _, id := range instances.IDs() { + subs, err = p.getSubscriptions(id) + if err != nil { + return 0, errors.Wrapf(err, "failed to get subscription %q for telemetry", id) + } + numSubscriptions += len(subs.Channel.ByID) + } + + return numSubscriptions, nil +} + +func (p *Plugin) userCount() (int, error) { + return p.userStore.CountUsers() +} + // Initialize telemetry setups the tracker/clients needed to send telemetry data. // The telemetry.NewTrackerConfig(...) param will take care of extract/parse the config to set the right settings. // If you don't want the default behavior you still can pass a different telemetry.TrackerConfig data.