Skip to content

Commit

Permalink
🔄 synced local 'pkg/' with remote 'pkg/'
Browse files Browse the repository at this point in the history
  • Loading branch information
constient-altrova committed Nov 30, 2023
1 parent 7f362bb commit 702104e
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 82 deletions.
24 changes: 24 additions & 0 deletions pkg/cmd/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type LoginOptions struct {
Password string
Token string
Staging bool
Local bool
}

func NewLoginCmd(f *cmdutil.Factory) *cobra.Command {
Expand Down Expand Up @@ -83,6 +84,7 @@ func NewLoginCmd(f *cmdutil.Factory) *cobra.Command {
cmd.Flags().StringVarP(&opts.Password, "password", "p", "", "Password of the user.")
cmd.Flags().StringVarP(&opts.Token, "token", "t", "", "Single Sign in token of the user.")
cmd.Flags().BoolVarP(&opts.Staging, "staging", "s", false, "Use staging server?")
cmd.Flags().BoolVarP(&opts.Local, "local", "l", false, "Use local server?")
return cmd
}

Expand All @@ -106,6 +108,18 @@ func loginRun(opts *LoginOptions) {
}
}

if opts.Local {
endpoint := "http://localhost:8888/"
grpc_endpoint := "localhost:8888"
grpc_ingestion := "http://localhost:8888/logfire.sh"

err = cfg.UpdateConfig(nil, nil, nil, nil,
nil, nil, &endpoint, &grpc_endpoint, &grpc_ingestion, nil)
if err != nil {
return
}
}

var choiceList = []string{"Magic link", "Password"}

if opts.Interactive && opts.Token == "" && opts.Email == "" && opts.Password == "" {
Expand Down Expand Up @@ -284,6 +298,12 @@ func PasswordSignin(io *iostreams.IOStreams, cfg config.Config, cs *iostreams.Co
return err
}
fmt.Fprintf(io.Out, "\n%s Logged in as %s\n", cs.SuccessIcon(), cs.Bold(response.UserBody.Email))


if !response.UserBody.Onboarded {
fmt.Fprintf(io.Out, "\n%s Looks like your onboarding isn't done yet. Complete it now with `logfire bootstrap` for the full experience! \n \n", cs.WarningIcon())
}

return nil
}

Expand Down Expand Up @@ -353,5 +373,9 @@ func TokenSignin(IO *iostreams.IOStreams, cfg config.Config, cs *iostreams.Color

fmt.Fprintf(IO.Out, "%s Logged in as %s\n", cs.SuccessIcon(), cs.Bold(response.Email))

if !response.UserBody.Onboarded {
fmt.Fprintf(IO.Out, "\n%s Looks like your onboarding isn't done yet. Complete it now with `logfire bootstrap` for the full experience! \n", cs.WarningIcon())
}

return nil
}
33 changes: 29 additions & 4 deletions pkg/cmd/roundtrip/roundtrip.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,18 @@ func PromptRoundTripRun(opts *PromptRoundTripOptions) {

id := uuid.New()

istLocation, err := time.LoadLocation("Asia/Kolkata")
if err != nil {
fmt.Println("Error loading IST location:", err)
return
}

// Get the current time in the IST time zone
currentTime := time.Now().In(istLocation)

// Format and print the time
formattedTime := currentTime.Format("2006-01-02 15:04:05")

cmd := exec.Command("curl",
"--location",
cfg.Get().GrpcIngestion,
Expand All @@ -125,7 +137,7 @@ func PromptRoundTripRun(opts *PromptRoundTripOptions) {
"--header",
fmt.Sprintf("Github-Run: %v", opts.Run),
"--data",
fmt.Sprintf("[{\"dt\":\"2023-06-15T6:00:39.351Z\",\"message\":\"%s\"}]", id),
fmt.Sprintf("[{\"dt\":\"%s\",\"message\":\"%s\"}]",formattedTime, id),
)

go grpcutil.WaitForLog(cfg, id, opts.TeamId, opts.SourceId, stop)
Expand Down Expand Up @@ -223,18 +235,31 @@ func PromptRoundTripRun(opts *PromptRoundTripOptions) {

id := uuid.New()

istLocation, err := time.LoadLocation("Asia/Kolkata")
if err != nil {
fmt.Println("Error loading IST location:", err)
return
}

// Get the current time in the IST time zone
currentTime := time.Now().In(istLocation)

// Format and print the time
formattedTime := currentTime.Format("2006-01-02 15:04:05")

cmd := exec.Command("curl",
"--location",
cfg.Get().GrpcIngestion,
"--header",
"Content-Type: application/json",
"--header",
fmt.Sprintf("Authorization: Bearer %s", sourceToken),
"--header",
"Diagnostic: True",
"--header",
fmt.Sprintf("Authorization: Bearer %s", sourceToken),
"--data",
fmt.Sprintf("[{\"dt\":\"2023-06-15T6:00:39.351Z\",\"message\":\"%s\"}]", id),
fmt.Sprintf("[{\"dt\":\"%s\",\"message\":\"%s\"}]", formattedTime, id),
)

if err != nil {
log.Fatal(err)
}
Expand Down
10 changes: 7 additions & 3 deletions pkg/cmd/sql/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/logfire-sh/cli/pkg/cmdutil/grpcutil"
"log"
"net/http"
"os"
"regexp"

"github.com/logfire-sh/cli/pkg/cmdutil/grpcutil"

"github.com/MakeNowJust/heredoc"
"github.com/logfire-sh/cli/internal/config"
"github.com/logfire-sh/cli/internal/prompter"
Expand Down Expand Up @@ -75,7 +76,10 @@ func NewCmdSql(f *cmdutil.Factory) *cobra.Command {
func GetRecommendations(opts *SQLQueryOptions, cfg config.Config) {
opts.IO.StartProgressIndicatorWithLabel("Getting recommendations, please wait...")

recommendations, _ := APICalls.GetRecommendations(cfg.Get().Token, cfg.Get().EndPoint, opts.TeamId, cfg.Get().Role)
recommendations, err := APICalls.GetRecommendations(cfg.Get().Token, cfg.Get().EndPoint, opts.TeamId, cfg.Get().Role)
if err != nil {
fmt.Fprintf(opts.IO.ErrOut, "%s",err)
}

var options []string

Expand Down Expand Up @@ -230,7 +234,7 @@ func createGrpcSource(sources []sourceModels.Source) []*pb.Source {
}

// getSQL makes the actual grpc call to connect with flink-service.
func getSQL(client pb.FlinkServiceClient, sources []*pb.Source, opts *SQLQueryOptions) (*pb.SQLResponse, error) {
func getSQL(client pb.FilterServiceClient, sources []*pb.Source, opts *SQLQueryOptions) (*pb.SQLResponse, error) {
// Prepare the request payload
request := &pb.SQLRequest{
Sql: opts.SQLQuery,
Expand Down
3 changes: 2 additions & 1 deletion pkg/cmd/stream/livetail/livetail.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func livetailRun(opts *LivetailOptions) {
FieldBasedFilters: []*pb.FieldBasedFilter{},
SearchQueries: []string{},
Sources: []*pb.Source{},
BatchSize: 100,
BatchSize: 15,
IsScrollDown: true,
}

Expand Down Expand Up @@ -235,6 +235,7 @@ func livetailRun(opts *LivetailOptions) {
for {
response, err := filterService.Client.GetFilteredData(context.Background(), request)
if err != nil {
//log.Fatal(err)
continue
}

Expand Down
34 changes: 4 additions & 30 deletions pkg/cmd/stream/view/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"time"

"github.com/logfire-sh/cli/pkg/cmdutil/grpcutil"
"google.golang.org/protobuf/types/known/timestamppb"

"github.com/MakeNowJust/heredoc"
"github.com/logfire-sh/cli/internal/config"
Expand Down Expand Up @@ -82,7 +81,7 @@ func ViewStreamRun(opts *ViewStreamOptions) {
FieldBasedFilters: []*pb.FieldBasedFilter{},
SearchQueries: []string{},
Sources: []*pb.Source{},
BatchSize: 100,
BatchSize: 15,
IsScrollDown: true,
}

Expand All @@ -109,37 +108,12 @@ func ViewStreamRun(opts *ViewStreamOptions) {
return
}

if !view.DateFilter.StartDate.IsZero() {
request.DateTimeFilter.StartTimeStamp = timestamppb.New(view.DateFilter.StartDate)

if !view.DateFilter.EndDate.IsZero() {
request.DateTimeFilter.EndTimeStamp = timestamppb.New(view.DateFilter.EndDate)
}
}

if len(view.SearchFilter) != 0 {
for _, v := range view.SearchFilter {
if v.Key != "" {
if v.Condition != "" {
if v.Value != "" {
request.FieldBasedFilters = append(request.FieldBasedFilters, &pb.FieldBasedFilter{
FieldName: v.Key,
FieldValue: v.Value,
Operator: pb.FieldBasedFilter_Operator(pb.FieldBasedFilter_Operator_value[v.Condition]),
})
}
}
}
}
}

if len(view.TextFilter) != 0 {
request.SearchQueries = append(request.SearchQueries, view.TextFilter...)
}

pbSources := grpcutil.CreateGrpcSource(view.SourcesFilter)
var sourcesOffset = make(map[string]uint64)

request.Sources = pbSources
request.ViewID = view.Id

filterService := grpcutil.NewFilterService()
defer filterService.CloseConnection()

Expand Down
2 changes: 1 addition & 1 deletion pkg/cmdutil/APICalls/source_api_calls.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func CreateSource(token, endpoint string, teamId, sourceName, platform string) (
defer conn.Close()

// Create a gRPC client
grpcClient := pb.NewFlinkServiceClient(conn)
grpcClient := pb.NewFilterServiceClient(conn)

grpcClient.CreateSource(context.Background(), pbSource)

Expand Down
53 changes: 11 additions & 42 deletions pkg/cmdutil/grpcutil/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@ package grpcutil

import (
"context"

"github.com/logfire-sh/cli/internal/config"
"github.com/logfire-sh/cli/pkg/cmd/sources/models"
pb "github.com/logfire-sh/cli/services/flink-service"
"google.golang.org/grpc"

//"google.golang.org/grpc/credentials"
"log"

"google.golang.org/grpc/credentials"
"google.golang.org/grpc/metadata"
"log"
)

type FilterService struct {
conn *grpc.ClientConn
Client pb.FlinkServiceClient
Client pb.FilterServiceClient
}

func authUnaryInterceptor(kv ...string) func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
Expand All @@ -34,20 +38,19 @@ func (fs *FilterService) CloseConnection() {
func NewFilterService(kv ...string) *FilterService {
cfg, _ := config.NewConfig()
grpc_url := cfg.Get().GrpcEndpoint
//grpc_url := "localhost:50051"
allParams := make([]string, 0, len(kv)+2)
allParams = append(allParams, "Authorization", "Bearer "+cfg.Get().Token)
for _, arg := range kv {
allParams = append(allParams, arg)
}
allParams = append(allParams, kv...)

conn, err := grpc.Dial(grpc_url, grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(nil, "")), grpc.WithUnaryInterceptor(authUnaryInterceptor(allParams...)), grpc.WithUserAgent("Logfire-cli"))
//conn, err := grpc.Dial(grpc_url, grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithUnaryInterceptor(authUnaryInterceptor(allParams...)), grpc.WithUserAgent("Logfire-cli"))
// conn, err := grpc.Dial(grpc_url, grpc.WithInsecure(), grpc.WithUnaryInterceptor(authUnaryInterceptor(allParams...)), grpc.WithUserAgent("Logfire-cli"))

if err != nil {
log.Fatalf("Failed to dial server: %v", err)
}

// Create a gRPC client
client := pb.NewFlinkServiceClient(conn)
client := pb.NewFilterServiceClient(conn)

return &FilterService{
conn: conn,
Expand Down Expand Up @@ -85,40 +88,6 @@ func AddOffset(sources []*pb.Source, offset map[string]uint64) []*pb.Source {
return sources
}

func CreateSource(request *pb.Source) {
cfg, _ := config.NewConfig()
grpc_url := cfg.Get().GrpcEndpoint
conn, err := grpc.Dial(grpc_url, grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(nil, "")))
if err != nil {
log.Fatalf("Failed to dial server: %v", err)
}
defer conn.Close()

// Create a gRPC client
client := pb.NewFlinkServiceClient(conn)

_, err = client.CreateSource(context.Background(), request)
if err != nil {
return
}
if err != nil {
return
}
}

// GetFilteredData makes the actual grpc call to connect with flink-service.
func GetFilteredData(client pb.FlinkServiceClient, request *pb.FilterRequest) (*pb.FilteredRecords, error) {
// Prepare the request payload

// Invoke the gRPC method
response, err := client.GetFilteredData(context.Background(), request)
if err != nil {
return nil, err
}

return response, nil
}

func GetOffsets(offsets map[string]uint64, records []*pb.FilteredRecord) map[string]uint64 {
for _, record := range records {
if offsets[record.SourceName] == 0 || record.Offset >= offsets[record.SourceName] {
Expand Down
5 changes: 4 additions & 1 deletion pkg/cmdutil/grpcutil/roundtrip_grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package grpcutil

import (
"context"
"log"
"time"

"github.com/google/uuid"
Expand Down Expand Up @@ -47,7 +48,9 @@ func WaitForLog(cfg config.Config, id uuid.UUID, teamId string, sourceId string,

response, err := filterService.Client.GetFilteredData(context.Background(), request)
if err != nil {
continue
log.Printf("Request: %v", request)
log.Printf("Error %v", err)
return
}

if len(response.Records) > 0 {
Expand Down

0 comments on commit 702104e

Please sign in to comment.