Skip to content

Commit

Permalink
Migrate app and build logs to v2
Browse files Browse the repository at this point in the history
  • Loading branch information
Diana Doherty committed Aug 17, 2023
1 parent 5f09ace commit 64f5cb7
Show file tree
Hide file tree
Showing 14 changed files with 218 additions and 52 deletions.
5 changes: 3 additions & 2 deletions cmd/meroxa/root/apps/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type Logs struct {

type applicationLogsClient interface {
GetApplicationLogs(ctx context.Context, nameOrUUID string) (*meroxa.ApplicationLogs, error)
GetApplicationLogsV2(ctx context.Context, nameOrUUID string) (*meroxa.Logs, error)
AddHeader(key, value string)
}

Expand Down Expand Up @@ -112,12 +113,12 @@ func (l *Logs) Execute(ctx context.Context) error {
addTurbineHeaders(l.client, config.Language, turbineLibVersion)
}

appLogs, getErr := l.client.GetApplicationLogs(ctx, nameOrUUID)
appLogs, getErr := l.client.GetApplicationLogsV2(ctx, nameOrUUID)
if getErr != nil {
return getErr
}

output := display.AppLogsTable(appLogs)
output := display.AppLogsTableV2(appLogs)

l.logger.Info(ctx, output)
l.logger.JSON(ctx, appLogs)
Expand Down
63 changes: 45 additions & 18 deletions cmd/meroxa/root/apps/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"reflect"
"strings"
"testing"
"time"

"github.com/meroxa/turbine-core/pkg/ir"

Expand Down Expand Up @@ -71,15 +72,28 @@ func TestApplicationLogsExecution(t *testing.T) {
logger := log.NewTestLogger()

appName := "my-app-with-funcs"
log := "hello world"

appLogs := &meroxa.ApplicationLogs{
ConnectorLogs: map[string]string{"res1": log},
FunctionLogs: map[string]string{"fun1": log},
DeploymentLogs: map[string]string{"uu-id": log},
}

client.EXPECT().GetApplicationLogs(ctx, appName).Return(appLogs, nil)
appLogs := &meroxa.Logs{
Data: []meroxa.LogData{
{
Timestamp: time.Now().UTC(),
Log: "log just logging",
Source: "fun-name",
},
{
Timestamp: time.Now().UTC(),
Log: "another log",
Source: "deployment-uuid",
},
},
Metadata: meroxa.Metadata{
End: time.Now().UTC(),
Start: time.Now().UTC().Add(-12 * time.Hour),
Limit: 10,
},
}

client.EXPECT().GetApplicationLogsV2(ctx, appName).Return(appLogs, nil)

dc := &Logs{
client: client,
Expand All @@ -93,7 +107,7 @@ func TestApplicationLogsExecution(t *testing.T) {
}

gotLeveledOutput := logger.LeveledOutput()
wantLeveledOutput := display.AppLogsTable(appLogs)
wantLeveledOutput := display.AppLogsTableV2(appLogs)

// N.B. This comparison is undeterminstic when the test data map contains
// more than one key. Maps in golang are not guaranteed ordered so the result
Expand All @@ -103,7 +117,7 @@ func TestApplicationLogsExecution(t *testing.T) {
}

gotJSONOutput := logger.JSONOutput()
var gotAppLogs meroxa.ApplicationLogs
var gotAppLogs meroxa.Logs
err = json.Unmarshal([]byte(gotJSONOutput), &gotAppLogs)
if err != nil {
t.Fatalf("not expected error, got %q", err.Error())
Expand All @@ -129,7 +143,6 @@ func TestApplicationLogsExecutionWithPath(t *testing.T) {
mockTurbineCLI := turbineMock.NewMockCLI(ctrl)

appName := "my-app-with-funcs"
log := "hello world"

i := &Init{
logger: logger,
Expand All @@ -146,16 +159,30 @@ func TestApplicationLogsExecutionWithPath(t *testing.T) {
}(path)
require.NoError(t, err)

appLogs := &meroxa.ApplicationLogs{
ConnectorLogs: map[string]string{"res1": log},
FunctionLogs: map[string]string{"fun1": log},
DeploymentLogs: map[string]string{"uu-id": log},
appLogs := &meroxa.Logs{
Data: []meroxa.LogData{
{
Timestamp: time.Now().UTC(),
Log: "log just logging",
Source: "fun-name",
},
{
Timestamp: time.Now().UTC(),
Log: "another log",
Source: "deployment-uuid",
},
},
Metadata: meroxa.Metadata{
End: time.Now().UTC(),
Start: time.Now().UTC().Add(-12 * time.Hour),
Limit: 10,
},
}

mockTurbineCLI.EXPECT().GetVersion(ctx).Return("1.0", nil)
client.EXPECT().AddHeader("Meroxa-CLI-App-Lang", string(ir.GoLang)).Times(1)
client.EXPECT().AddHeader("Meroxa-CLI-App-Version", gomock.Any()).Times(1)
client.EXPECT().GetApplicationLogs(ctx, appName).Return(appLogs, nil)
client.EXPECT().GetApplicationLogsV2(ctx, appName).Return(appLogs, nil)

dc := &Logs{
client: client,
Expand All @@ -171,14 +198,14 @@ func TestApplicationLogsExecutionWithPath(t *testing.T) {
}

gotLeveledOutput := logger.LeveledOutput()
wantLeveledOutput := display.AppLogsTable(appLogs)
wantLeveledOutput := display.AppLogsTableV2(appLogs)

if !strings.Contains(gotLeveledOutput, wantLeveledOutput) {
t.Fatalf(cmp.Diff(wantLeveledOutput, gotLeveledOutput))
}

gotJSONOutput := logger.JSONOutput()
var gotAppLogs meroxa.ApplicationLogs
var gotAppLogs meroxa.Logs
err = json.Unmarshal([]byte(gotJSONOutput), &gotAppLogs)
if err != nil {
t.Fatalf("not expected error, got %q", err.Error())
Expand Down
18 changes: 8 additions & 10 deletions cmd/meroxa/root/builds/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ package builds
import (
"context"
"errors"
"io"
"net/http"

"github.com/meroxa/cli/cmd/meroxa/builder"
"github.com/meroxa/cli/log"
"github.com/meroxa/cli/utils/display"
"github.com/meroxa/meroxa-go/pkg/meroxa"
)

Expand All @@ -38,6 +38,7 @@ var (

type buildLogsClient interface {
GetBuildLogs(ctx context.Context, uuid string) (*http.Response, error)
GetBuildLogsV2(ctx context.Context, uuid string) (*meroxa.Logs, error)
}

type Logs struct {
Expand All @@ -64,18 +65,15 @@ func (l *Logs) Docs() builder.Docs {
}

func (l *Logs) Execute(ctx context.Context) error {
response, err := l.client.GetBuildLogs(ctx, l.args.UUID)
if err != nil {
return err
buildLogs, getErr := l.client.GetBuildLogsV2(ctx, l.args.UUID)
if getErr != nil {
return getErr
}
defer response.Body.Close()

body, err := io.ReadAll(response.Body)
if err != nil {
return err
}
output := display.BuildLogsTableV2(buildLogs)

l.logger.Info(ctx, string(body))
l.logger.Info(ctx, output)
l.logger.JSON(ctx, buildLogs)

return nil
}
Expand Down
48 changes: 34 additions & 14 deletions cmd/meroxa/root/builds/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,19 @@ limitations under the License.
package builds

import (
"bytes"
"context"
"encoding/json"
"errors"
"io"
"net/http"
"reflect"
"strings"
"testing"
"time"

"github.com/golang/mock/gomock"
"github.com/google/go-cmp/cmp"
"github.com/meroxa/cli/log"
"github.com/meroxa/cli/utils/display"
"github.com/meroxa/meroxa-go/pkg/meroxa"
"github.com/meroxa/meroxa-go/pkg/mock"
)

Expand Down Expand Up @@ -69,29 +72,46 @@ func TestLogsBuildExecution(t *testing.T) {

l.args.UUID = buildUUID

responseDetails := io.NopCloser(bytes.NewReader([]byte(
`[2021-04-29T12:16:42Z] Beep boop, robots doing build things`,
)))

httpResponse := &http.Response{
StatusCode: 200,
Body: responseDetails,
buildLog := &meroxa.Logs{
Data: []meroxa.LogData{
{
Timestamp: time.Now().UTC(),
Log: "Beep boop, robots doing build things",
Source: "function build",
},
},
Metadata: meroxa.Metadata{
End: time.Now().UTC(),
Start: time.Now().UTC().Add(-12 * time.Hour),
Limit: 10,
},
}

client.
EXPECT().
GetBuildLogs(ctx, buildUUID).
Return(httpResponse, nil)
GetBuildLogsV2(ctx, buildUUID).
Return(buildLog, nil)

err := l.Execute(ctx)
if err != nil {
t.Fatalf("not expected error, got \"%s\"", err.Error())
}

gotLeveledOutput := logger.LeveledOutput()
wantLeveledOutput := "[2021-04-29T12:16:42Z] Beep boop, robots doing build things"
wantLeveledOutput := display.BuildLogsTableV2(buildLog)

if !strings.Contains(gotLeveledOutput, wantLeveledOutput) {
t.Fatalf("expected output:\n%s\ngot:\n%s", wantLeveledOutput, gotLeveledOutput)
t.Fatalf(cmp.Diff(wantLeveledOutput, gotLeveledOutput))
}

gotJSONOutput := logger.JSONOutput()
var gotBuildLog meroxa.Logs
err = json.Unmarshal([]byte(gotJSONOutput), &gotBuildLog)
if err != nil {
t.Fatalf("not expected error, got %q", err.Error())
}

if !reflect.DeepEqual(gotBuildLog, *buildLog) {
t.Fatalf(cmp.Diff(*buildLog, gotBuildLog))
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/manifoldco/promptui v0.9.0
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/meroxa/meroxa-go v0.0.0-20230630181637-7cfd524c070a
github.com/meroxa/meroxa-go v0.0.0-20230817133406-26d1fde8c926
github.com/nirasan/go-oauth-pkce-code-verifier v0.0.0-20170819232839-0fbfe93532da
github.com/pkg/browser v0.0.0-20210115035449-ce105d075bb4
github.com/rivo/uniseg v0.2.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-shellwords v1.0.12 h1:M2zGm7EW6UQJvDeQxo4T51eKPurbeFbe8WtebGE2xrk=
github.com/mattn/go-shellwords v1.0.12/go.mod h1:EZzvwXDESEeg03EKmM+RmDnNOPKG4lLtQsUlTZDWQ8Y=
github.com/meroxa/meroxa-go v0.0.0-20230630181637-7cfd524c070a h1:Kd/d630d6ykAagdSGwc47zVenqYRYILTm7O70NmyFz8=
github.com/meroxa/meroxa-go v0.0.0-20230630181637-7cfd524c070a/go.mod h1:c1rom/GgYA74PAHorffVlOWCaUhMc5DFbAvt1UwD76Y=
github.com/meroxa/meroxa-go v0.0.0-20230817133406-26d1fde8c926 h1:i+H1Wvi9u9VwEeKYgzaIB20k3KMHLNfwmlV+71GKGaU=
github.com/meroxa/meroxa-go v0.0.0-20230817133406-26d1fde8c926/go.mod h1:aGLMvOqFX9O+vgy5JkBFH1/OzKWjYXVCDg21hIE3WtE=
github.com/meroxa/turbine-core v0.0.0-20230815153536-e0c914b74ea1 h1:4tx5X9TVepTLVYP2ZOokKwkCSBldtGZh69kArXZaI9c=
github.com/meroxa/turbine-core v0.0.0-20230815153536-e0c914b74ea1/go.mod h1:03beJfCWdChsKHzbhiDlOcYKyAKkrtoC3y8ualUFOrI=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
Expand Down
11 changes: 11 additions & 0 deletions utils/display/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package display
import (
"fmt"
"strings"
"time"

"github.com/alexeyco/simpletable"
"github.com/meroxa/meroxa-go/pkg/meroxa"
Expand Down Expand Up @@ -193,3 +194,13 @@ func AppLogsTable(appLogs *meroxa.ApplicationLogs) string {

return subTable
}

func AppLogsTableV2(appLogs *meroxa.Logs) string {
var subTable string

for _, l := range appLogs.Data {
subTable += fmt.Sprintf("[%s]\t%s\t%q\n", l.Timestamp.Format(time.RFC3339), l.Source, l.Log)
}

return subTable
}
13 changes: 13 additions & 0 deletions utils/display/builds.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package display

import (
"fmt"
"time"

"github.com/alexeyco/simpletable"
"github.com/meroxa/meroxa-go/pkg/meroxa"
)
Expand Down Expand Up @@ -35,3 +38,13 @@ func BuildTable(build *meroxa.Build) string {
mainTable.SetStyle(simpletable.StyleCompact)
return mainTable.String()
}

func BuildLogsTableV2(buildLogs *meroxa.Logs) string {
var subTable string

for _, l := range buildLogs.Data {
subTable += fmt.Sprintf("[%s]\t%q\n", l.Timestamp.Format(time.RFC3339), l.Log)
}

return subTable
}
21 changes: 21 additions & 0 deletions vendor/github.com/meroxa/meroxa-go/pkg/meroxa/application.go

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

Loading

0 comments on commit 64f5cb7

Please sign in to comment.