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

Log from/to time parse and app by name #23

Merged
merged 6 commits into from
Mar 19, 2024
Merged
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
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ module github.com/G-core/gcore-cli

go 1.21.5


require (
github.com/G-Core/FastEdge-client-sdk-go v0.0.0-20240214130448-d87df1e38764
github.com/G-Core/FastEdge-client-sdk-go v0.0.0-20240304075046-db0c8c3d17e7
github.com/alecthomas/assert v1.0.0
github.com/docker/go-units v0.5.0
github.com/dustin/go-humanize v1.0.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/G-Core/FastEdge-client-sdk-go v0.0.0-20240214130448-d87df1e38764 h1:7CATrk7BJpU1t4wr2avczZaX1KrwsjhArBKBaiSQN2M=
github.com/G-Core/FastEdge-client-sdk-go v0.0.0-20240214130448-d87df1e38764/go.mod h1:ggyUVhy8/OCMBY4nbm7n9qDoPioROCk4vHhDJq9w7qE=
github.com/G-Core/FastEdge-client-sdk-go v0.0.0-20240304075046-db0c8c3d17e7 h1:99yyAfaF6OV2ghz75yE72LwdzxP6gUYa3hL7XkObb5s=
github.com/G-Core/FastEdge-client-sdk-go v0.0.0-20240304075046-db0c8c3d17e7/go.mod h1:ggyUVhy8/OCMBY4nbm7n9qDoPioROCk4vHhDJq9w7qE=
github.com/RaveNoX/go-jsoncommentstrip v1.0.0/go.mod h1:78ihd09MekBnJnxpICcwzCMzGrKSKYe4AqU6PDYYpjk=
github.com/alecthomas/assert v1.0.0 h1:3XmGh/PSuLzDbK3W2gUbRXwgW5lqPkuqvRgeQ30FI5o=
github.com/alecthomas/assert v1.0.0/go.mod h1:va/d2JC+M7F6s+80kl/R3G7FUiW6JzUO+hPhLyJ36ZY=
Expand Down
44 changes: 29 additions & 15 deletions internal/commands/fastedge/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ uploading binary using "--file <filename>". To load file from stdin, use "-" as
appPropertiesFlags(cmdCreate)

var cmdUpdate = &cobra.Command{
Use: "update <app_id>",
Use: "update <app_name>",
Short: "Update the app",
Long: `This command allows to change only specified properties of the app,
omitted properties are left intact. When changing key-value properties, such
Expand All @@ -92,9 +92,9 @@ You can use either previously-uploaded binary, by specifying "--binary <id>", or
uploading binary using "--file <filename>". To load file from stdin, use "-" as filename`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
id, err := strconv.ParseInt(args[0], 10, 64)
id, err := getAppIdByName(args[0])
if err != nil {
return fmt.Errorf("parsing app id: %w", err)
return fmt.Errorf("cannot find app by name: %w", err)
}

app, err := parseAppProperties(cmd)
Expand Down Expand Up @@ -184,17 +184,17 @@ uploading binary using "--file <filename>". To load file from stdin, use "-" as
}

var cmdGet = &cobra.Command{
Use: "show <app_id>",
Use: "show <app_name>",
Aliases: []string{"get"},
Short: "Show app details",
Long: `Show app properties. This command doesn't show app call statisrics.
To see statistics, use "fastedge stat app_calls" and "fastedge stat app_duration"
commands.`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
id, err := strconv.ParseInt(args[0], 10, 64)
id, err := getAppIdByName(args[0])
if err != nil {
return fmt.Errorf("parsing app id: %w", err)
return fmt.Errorf("cannot find app by name: %w", err)
}
rsp, err := client.GetAppWithResponse(
context.Background(),
Expand Down Expand Up @@ -230,13 +230,13 @@ commands.`,
}

var cmdEnable = &cobra.Command{
Use: "enable <app_id>",
Use: "enable <app_name>",
Short: "Enable the app",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
id, err := strconv.ParseInt(args[0], 10, 64)
id, err := getAppIdByName(args[0])
if err != nil {
return fmt.Errorf("parsing app id: %w", err)
return fmt.Errorf("cannot find app by name: %w", err)
}
rsp, err := client.PatchAppWithResponse(
context.Background(),
Expand All @@ -261,13 +261,13 @@ commands.`,
}

var cmdDisable = &cobra.Command{
Use: "disable <app_id>",
Use: "disable <app_name>",
Short: "Disable the app",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
id, err := strconv.ParseInt(args[0], 10, 64)
id, err := getAppIdByName(args[0])
if err != nil {
return fmt.Errorf("parsing app id: %w", err)
return fmt.Errorf("cannot find app by name: %w", err)
}
rsp, err := client.PatchAppWithResponse(
context.Background(),
Expand All @@ -292,17 +292,17 @@ commands.`,
}

var cmdDelete = &cobra.Command{
Use: "delete <app_id>",
Use: "delete <app_name>",
Short: "Delete the app",
Aliases: []string{"rm"},
Long: `This command deletes the app. The binary, referenced by the app, is not deleted,
however binaries, not referenced by any app, get deleted by cleanup process regularly,
so if you don't want this to happen, consider disabling the app to keep binary referenced`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
id, err := strconv.ParseInt(args[0], 10, 64)
id, err := getAppIdByName(args[0])
if err != nil {
return fmt.Errorf("parsing app id: %w", err)
return fmt.Errorf("cannot find app by name: %w", err)
}

if !sure.AreYou(cmd, fmt.Sprintf("delete app %d", id)) {
Expand Down Expand Up @@ -445,3 +445,17 @@ func outputMap(m *map[string]string, title string) {
output.Table(table, output.FmtHuman)
}
}

func getAppIdByName(appName string) (int64, error) {
idRsp, err := client.GetAppIdByNameWithResponse(context.Background(), appName)
if err != nil {
return 0, fmt.Errorf("api response: %w", err)
}
if idRsp.StatusCode() != http.StatusOK {
return 0, fmt.Errorf("%s", string(idRsp.Body))
}
if idRsp.JSON200 == nil {
return 0, fmt.Errorf("app '%s' not found", appName)
}
return *idRsp.JSON200, nil
}
58 changes: 22 additions & 36 deletions internal/commands/fastedge/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ package fastedge
import (
"bufio"
"context"
"errors"
"fmt"
"net/http"
"os"
"strconv"
"strings"
"time"

Expand All @@ -28,7 +26,7 @@ func appLogsFilterFlags(cmd *cobra.Command) {
// logs-related commands
func logs() *cobra.Command {
var (
from, to *time.Time
from, to time.Time
sort *sdk.GetV1AppsIdLogsParamsSort
edge *string
clientIp *string
Expand All @@ -41,20 +39,13 @@ func logs() *cobra.Command {
}

var cmdLogsShow = &cobra.Command{
Use: "show <app_id>",
Use: "show <app_name>",
Short: "Show app logs",
Long: `Show app logs printed to stdout/stderr.
This command allows you filtering by edge name, client ip and time range.`,
Args: cobra.ExactArgs(1),
PreRunE: func(cmd *cobra.Command, args []string) error {
fromFlag, err := cmd.Flags().GetString("from")
if err != nil {
return err
}
toFlag, err := cmd.Flags().GetString("to")
if err != nil {
return err
}

sortFlag, err := cmd.Flags().GetString("sort")
if err != nil {
return err
Expand All @@ -68,20 +59,14 @@ This command allows you filtering by edge name, client ip and time range.`,
return err
}

if fromFlag != "" {
f, err := time.Parse(time.RFC3339, fromFlag)
if err != nil {
return errors.New("invalid format for `from` expected RFC3339")
}
from = &f
from, err = parseTimeFlag(cmd, "from")
if err != nil {
return fmt.Errorf("cannot parse 'from' time: %w", err)
}

if toFlag != "" {
t, err := time.Parse(time.RFC3339, toFlag)
if err != nil {
return errors.New("invalid format for `to` expected RFC3339")
}
to = &t
to, err = parseTimeFlag(cmd, "to")
if err != nil {
return fmt.Errorf("cannot parse 'to' time: %w", err)
}

if sortFlag != "" {
Expand All @@ -99,16 +84,17 @@ This command allows you filtering by edge name, client ip and time range.`,
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
id, err := strconv.ParseInt(args[0], 10, 64)
id, err := getAppIdByName(args[0])
if err != nil {
return fmt.Errorf("parsing app id: %w", err)
return fmt.Errorf("cannot find app by name: %w", err)
}

rsp, err := client.GetV1AppsIdLogsWithResponse(
context.Background(),
id,
&sdk.GetV1AppsIdLogsParams{
From: from,
To: to,
From: &from,
To: &to,
Edge: edge,
Sort: sort,
ClientIp: clientIp,
Expand Down Expand Up @@ -155,8 +141,8 @@ This command allows you filtering by edge name, client ip and time range.`,
context.Background(),
id,
&sdk.GetV1AppsIdLogsParams{
From: from,
To: to,
From: &from,
To: &to,
Edge: edge,
Sort: sort,
ClientIp: clientIp,
Expand All @@ -179,13 +165,13 @@ This command allows you filtering by edge name, client ip and time range.`,
}

var cmdLogEnable = &cobra.Command{
Use: "enable <app_id>",
Use: "enable <app_name>",
Short: "Enable app logging",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
id, err := strconv.ParseInt(args[0], 10, 64)
id, err := getAppIdByName(args[0])
if err != nil {
return fmt.Errorf("parsing app id: %w", err)
return fmt.Errorf("cannot find app by name: %w", err)
}
rsp, err := client.PatchAppWithResponse(
context.Background(),
Expand Down Expand Up @@ -220,13 +206,13 @@ This command allows you filtering by edge name, client ip and time range.`,
}

var cmdLogDisable = &cobra.Command{
Use: "disable <app_id>",
Use: "disable <app_name>",
Short: "Disable app logging",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
id, err := strconv.ParseInt(args[0], 10, 64)
id, err := getAppIdByName(args[0])
if err != nil {
return fmt.Errorf("parsing app id: %w", err)
return fmt.Errorf("cannot find app by name: %w", err)
}
rsp, err := client.PatchAppWithResponse(
context.Background(),
Expand Down
12 changes: 6 additions & 6 deletions internal/commands/fastedge/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func stat() *cobra.Command {
}

var cmdCalls = &cobra.Command{
Use: "calls <app_id>",
Use: "calls <app_name>",
Aliases: []string{"calls"},
Short: "Show app calls statistic",
Long: `Show number of app calls, grouped by time slots and HTTP statuses.
Expand All @@ -67,9 +67,9 @@ can be omitted, or as UNIX timestamp) and reporting step duration with flag
"--step" (in seconds).`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
id, err := strconv.ParseInt(args[0], 10, 64)
id, err := getAppIdByName(args[0])
if err != nil {
return fmt.Errorf("parsing app id: %w", err)
return fmt.Errorf("cannot find app by name: %w", err)
}

from, err := parseTimeFlag(cmd, "from")
Expand Down Expand Up @@ -169,7 +169,7 @@ can be omitted, or as UNIX timestamp) and reporting step duration with flag
statFlags(cmdCalls)

var cmdDuration = &cobra.Command{
Use: "duration <app_id>",
Use: "duration <app_name>",
Aliases: []string{"duration", "time", "timing"},
Short: "Show app execution duration",
Long: `Show duration of app calls, grouped by time slots. All times are in msec
Expand All @@ -180,9 +180,9 @@ can be omitted, or as UNIX timestamp) and reporting step duration with flag
"--step" (in seconds).`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
id, err := strconv.ParseInt(args[0], 10, 64)
id, err := getAppIdByName(args[0])
if err != nil {
return fmt.Errorf("parsing app id: %w", err)
return fmt.Errorf("cannot find app by name: %w", err)
}

from, err := parseTimeFlag(cmd, "from")
Expand Down