Files
threshold/internal/bsr/types.go
T
hashicorp-copywrite[bot]andJohan Brandhorst-Satzkorn 29da0bcb92 [COMPLIANCE] License changes (#3567)
* Adding explicit MPL license for sub-package.

This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository.

* Adding explicit MPL license for sub-package

This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository.

* Updating the license from MPL to Business Source License

Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl.

* Update copyright file headers to BUS-1.1

* Rerun make gen

This will pick up the last of the license changes

* Revert "Rerun make gen"

This reverts commit dbe0968bffd92745d8663c6940debae098395a6b.

* Remove copywrite from generation script

The current version of copywrite is not sophisticated enough
to only apply the copywrite changes to certain subdirectories,
so to avoid accidentally relicensing our api and sdk modules,
we will stop running the copywrite script until such time
that it can do the right thing.

* License protobuf API as MPL

The protobuf API files are used to generate the SDK,
since we want the SDK to be MPL, the protobuf files
must also be MPL. This extends to all protobuf files
whose sources end up in the SDK directory, including:

- internal/proto/controller/api
- internal/proto/controller/custom_options
- internal/proto/plugin

It also includes the generated service directory in
internal/gen/controller/api/services, since they are
generated from the protobuf sources used for the SDK.

* Change website license file name

This makes the naming scheme consistent with the rest
of the repository.

* Add note on licensing to README

* Set copywrite license to BUSL

* Revert "Add note on licensing to README"

This reverts commit 4d865a91a7a4f11605f0dc4609355d7e3c3cfffe.

---------

Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
Co-authored-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
2023-08-11 10:31:53 -07:00

279 lines
6.8 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package bsr
import (
"context"
"encoding/json"
"errors"
"time"
)
type SummaryError struct {
Message string `json:"message"`
}
func (e *SummaryError) Error() string {
return e.Message
}
func (e *SummaryError) MarshalJSON() ([]byte, error) {
return json.Marshal(e.Message)
}
func (e *SummaryError) UnmarshalJSON(data []byte) error {
var rawMessage json.RawMessage
var message string
err := json.Unmarshal(data, &rawMessage)
if err != nil {
return err
}
switch {
case len(rawMessage) > 0 && string(rawMessage) == `"{}"`:
e.Message = ""
case len(rawMessage) > 0 && string(rawMessage) == `"null"`:
e.Message = ""
case len(rawMessage) <= 0:
e.Message = ""
default:
if err := json.Unmarshal(data, &message); err != nil {
return err
}
e.Message = message
}
return nil
}
// BaseSummary contains the common fields of all summary types.
type BaseSummary struct {
Id string
StartTime time.Time
EndTime time.Time
Errors error
}
// Summary contains statistics for a container
type Summary interface {
// GetId returns the Id of the container.
GetId() string
// GetStartTime returns the start time using a monotonic clock.
GetStartTime() time.Time
// GetEndTime returns the end time using a monotonic clock.
GetEndTime() time.Time
}
// GetId returns the Id of the container.
func (b *BaseSummary) GetId() string {
return b.Id
}
// GetStartTime returns the start time using a monotonic clock.
func (b *BaseSummary) GetStartTime() time.Time {
return b.StartTime
}
// GetEndTime returns the end time using a monotonic clock.
func (b *BaseSummary) GetEndTime() time.Time {
return b.EndTime
}
// BaseSessionSummary encapsulates data for a session, including its session id, connection count,
// and start/end time using a monotonic clock
type BaseSessionSummary struct {
Id string
ConnectionCount uint64
StartTime time.Time
EndTime time.Time
Errors SummaryError
}
// SessionSummary contains statistics for a session container
type SessionSummary interface {
Summary
// GetConnectionCount returns the connection count.
GetConnectionCount() uint64
// GetErrors returns errors.
GetErrors() error
// SetErrors sets errors.
SetErrors(e error)
}
func AllocSessionSummary(_ context.Context) Summary {
return &BaseSessionSummary{}
}
// GetId returns the Id of the container.
func (b *BaseSessionSummary) GetId() string {
return b.Id
}
// GetStartTime returns the start time using a monotonic clock.
func (b *BaseSessionSummary) GetStartTime() time.Time {
return b.StartTime
}
// GetEndTime returns the end time using a monotonic clock.
func (b *BaseSessionSummary) GetEndTime() time.Time {
return b.EndTime
}
// GetConnectionCount returns the connection count.
func (b *BaseSessionSummary) GetConnectionCount() uint64 {
return b.ConnectionCount
}
// GetErrors returns errors.
func (b *BaseSessionSummary) GetErrors() error {
if len(b.Errors.Message) == 0 {
return nil
}
return errors.New(b.Errors.Message)
}
// SetErrors sets errors.
func (b *BaseSessionSummary) SetErrors(e error) {
b.Errors = SummaryError{Message: e.Error()}
}
// BaseChannelSummary encapsulates data for a channel, including its id, channel type,
// start/end time using a monotonic clock, and the bytes up/ down seen on this channel
type BaseChannelSummary struct {
Id string
ConnectionRecordingId string
StartTime time.Time
EndTime time.Time
BytesUp uint64
BytesDown uint64
ChannelType string
}
// ChannelSummary contains statistics for a channel container
type ChannelSummary interface {
Summary
// GetConnectionRecordingId returns the connection recording id of the channel.
GetConnectionRecordingId() string
// GetBytesUp returns upload bytes.
GetBytesUp() uint64
// BytesDown returns download bytes.
GetBytesDown() uint64
// GetChannelType the type of summary channel.
GetChannelType() string
}
func AllocChannelSummary(_ context.Context) Summary {
return &BaseChannelSummary{}
}
// GetId returns the Id of the container.
func (b *BaseChannelSummary) GetId() string {
return b.Id
}
// GetId returns the Id of the container.
func (b *BaseChannelSummary) GetConnectionRecordingId() string {
return b.ConnectionRecordingId
}
// GetStartTime returns the start time using a monotonic clock.
func (b *BaseChannelSummary) GetStartTime() time.Time {
return b.StartTime
}
// GetEndTime returns the end time using a monotonic clock.
func (b *BaseChannelSummary) GetEndTime() time.Time {
return b.EndTime
}
// GetBytesUp returns upload bytes.
func (b *BaseChannelSummary) GetBytesUp() uint64 {
return b.BytesUp
}
// GetBytesDown returns download bytes.
func (b *BaseChannelSummary) GetBytesDown() uint64 {
return b.BytesDown
}
// GetChannelType returns the type of summary channel.
func (b *BaseChannelSummary) GetChannelType() string {
return b.ChannelType
}
// BaseConnectionSummary encapsulates data for a connection, including its connection id, channel count,
// start/end time using a monotonic clock, and the aggregate bytes up/ down of its channels
type BaseConnectionSummary struct {
Id string
ChannelCount uint64
StartTime time.Time
EndTime time.Time
BytesUp uint64
BytesDown uint64
Errors SummaryError
}
// ConnectionSummary contains statistics for a connection container
type ConnectionSummary interface {
Summary
// GetChannelCount returns the channel count.
GetChannelCount() uint64
// GetBytesUp returns upload bytes.
GetBytesUp() uint64
// BytesDown returns download bytes.
GetBytesDown() uint64
// GetErrors returns errors.
GetErrors() error
// SetErrors sets errors.
SetErrors(e error)
}
func AllocConnectionSummary(_ context.Context) Summary {
return &BaseConnectionSummary{}
}
// GetChannelCount returns the channel count.
func (b *BaseConnectionSummary) GetChannelCount() uint64 {
return b.ChannelCount
}
// GetId returns the Id of the container.
func (b *BaseConnectionSummary) GetId() string {
return b.Id
}
// GetStartTime returns the start time using a monotonic clock.
func (b *BaseConnectionSummary) GetStartTime() time.Time {
return b.StartTime
}
// GetEndTime returns the end time using a monotonic clock.
func (b *BaseConnectionSummary) GetEndTime() time.Time {
return b.EndTime
}
// GetBytesUp BaseConnectionSummary upload bytes.
func (b *BaseConnectionSummary) GetBytesUp() uint64 {
return b.BytesUp
}
// GetBytesDown returns download bytes.
func (b *BaseConnectionSummary) GetBytesDown() uint64 {
return b.BytesDown
}
// GetErrors returns errors.
func (b *BaseConnectionSummary) GetErrors() error {
if len(b.Errors.Message) == 0 {
return nil
}
return &b.Errors
}
// SetErrors sets errors.
func (b *BaseConnectionSummary) SetErrors(e error) {
b.Errors = SummaryError{Message: e.Error()}
}