Skip to content
Snippets Groups Projects
Commit aa6c49aa authored by ko_oler's avatar ko_oler
Browse files

- правки в клиента

- правки в сервис
parent a439a781
No related branches found
No related tags found
No related merge requests found
......@@ -3,49 +3,86 @@ package log
import (
"context"
"git.perx.ru/perxis/perxis-go/pkg/errors"
errorsgrpc "git.perx.ru/perxis/perxis-go/pkg/errors/grpc"
"git.perx.ru/perxis/perxis-go/pkg/options"
"git.perx.ru/perxis/perxis-go/proto/common"
"git.perx.ru/perxis/perxis-go/proto/log"
pb "git.perx.ru/perxis/perxis-go/proto/log"
"google.golang.org/grpc"
)
type Client struct {
client log.LogServiceClient
client pb.LogServiceClient
}
var _ Service = &Client{}
func NewClient(conn *grpc.ClientConn) *Client {
return &Client{
client: log.NewLogServiceClient(conn),
client: pb.NewLogServiceClient(conn),
}
}
func (c *Client) Log(ctx context.Context, entries <-chan *Entry) error {
stream, err := c.client.Log(ctx)
if err != nil {
}
for e := range entries {
err := stream.Send(&pb.LogRequest{Entry: EntryToPB(e)})
if err != nil {
return errors.Wrap(err, "send request to stream")
}
}
func (c *Client) Log(ctx context.Context, entries chan<- *Entry) error {
return nil
}
func (c *Client) LogEntry(ctx context.Context, entry *Entry) error {
_, err := c.client.LogEntry(ctx, &log.LogRequest{Entry: EntryToPB(entry)})
response, err := c.client.LogEntry(ctx, &pb.LogRequest{Entry: EntryToPB(entry)})
if err != nil {
return err
}
if response.GetError() != nil {
return errorsgrpc.ErrorFromProto(nil, response.GetError())
}
return nil
}
func (c *Client) Find(ctx context.Context, request *FindRequest) (*FindResult, error) {
response, err := c.client.Find(ctx, &log.FindRequest{Filter: &log.Filter{Q: request.Filter.Q}, Options: &common.FindOptions{
Sort: request.Options.Sort,
PageNum: int32(request.Options.PageNum),
PageSize: int32(request.Options.PageSize),
}}, nil)
func (c *Client) Find(ctx context.Context, filter *Filter, options *options.FindOptions) (*FindResult, error) {
request := new(pb.FindRequest)
if filter != nil {
request.Filter = &pb.Filter{Q: filter.Q}
}
if options != nil {
request.Options = &common.FindOptions{
Sort: options.Sort,
PageNum: int32(options.PageNum),
PageSize: int32(options.PageSize),
}
}
response, err := c.client.Find(ctx, request)
if err != nil {
return nil, err
}
if response.GetError() != nil {
return nil, errorsgrpc.ErrorFromProto(nil, response.GetError())
}
return FindResultFromPB(response.GetResult()), nil
}
func (c *Client) Delete(ctx context.Context, filter *Filter) error {
_, err := c.client.Delete(ctx, &log.DeleteRequest{Filter: &log.Filter{Q: filter.Q}})
request := new(pb.DeleteRequest)
if filter != nil {
request.Filter = &pb.Filter{Q: filter.Q}
}
response, err := c.client.Delete(ctx, &pb.DeleteRequest{Filter: &pb.Filter{Q: filter.Q}})
if err != nil {
return err
}
if response.GetError() != nil {
return errorsgrpc.ErrorFromProto(nil, response.GetError())
}
return nil
}
......@@ -7,45 +7,36 @@ import (
"google.golang.org/protobuf/types/known/timestamppb"
)
type Level int
const (
Info = iota
Warning
Error
Critical
Fatal
Info = Level(log.LogLevel_INFO)
Warning = Level(log.LogLevel_WARNING)
Error = Level(log.LogLevel_ERROR)
Critical = Level(log.LogLevel_CRITICAL)
Fatal = Level(log.LogLevel_FATAL)
)
type Level int
func (l Level) String() string {
switch l {
case Info:
return "INFO"
case Warning:
return "WARNING"
case Error:
return "ERROR"
case Critical:
return "CRITICAL"
case Fatal:
return "FATAL"
default:
return "UNKNOWN"
s := log.LogLevel_name[int32(l)]
if s == "" {
s = "UNKNOWN"
}
return s
}
type Entry struct {
ID string `json:"id,omitempty" bson:"id"`
Timestamp time.Time `json:"timestamp" bson:"timestamp"`
LogLevel Level `json:"logLevel,omitempty" bson:"logLevel"`
Message string `json:"message,omitempty" bson:"message"`
Category string `json:"category,omitempty" bson:"category"`
Component string `json:"component,omitempty" bson:"component"`
Event string `json:"event,omitempty" bson:"event"`
Object string `json:"object,omitempty" bson:"object"`
Caller string `json:"caller" bson:"caller"`
Attr interface{} `json:"attr,omitempty" bson:"attr"`
Tags []string `json:"tags,omitempty" bson:"tags"`
ID string `json:"id" bson:"id"`
Timestamp time.Time `json:"timestamp,omitempty" bson:"timestamp,omitempty"`
LogLevel Level `json:"log_level,omitempty" bson:"log_level,omitempty"`
Message string `json:"message,omitempty" bson:"message,omitempty"`
Category string `json:"category,omitempty" bson:"category,omitempty"`
Component string `json:"component,omitempty" bson:"component,omitempty"`
Event string `json:"event,omitempty" bson:"event,omitempty"`
Object string `json:"object,omitempty" bson:"object,omitempty"`
Caller string `json:"caller,omitempty" bson:"caller,omitempty"`
Attr interface{} `json:"attr,omitempty" bson:"attr,omitempty"`
Tags []string `json:"tags,omitempty" bson:"tags,omitempty"`
}
func EntryToPB(entry *Entry) *log.LogEntry {
......
......@@ -3,20 +3,21 @@ package log
import (
"context"
transportgrpc "git.perx.ru/perxis/perxis-go/pkg/items/transport/grpc"
itemstransportgrpc "git.perx.ru/perxis/perxis-go/pkg/items/transport/grpc"
"git.perx.ru/perxis/perxis-go/pkg/options"
"git.perx.ru/perxis/perxis-go/proto/log"
)
type Service interface {
// Log метод для записи логов
Log(ctx context.Context, entries chan<- *Entry) error
// Log метод для потоковой записи логов
Log(ctx context.Context, entries <-chan *Entry) error
// LogEntry метод для записи одного лога
LogEntry(ctx context.Context, entry *Entry) error
// Find метод для поиска логов по заданным параметрам
Find(ctx context.Context, filter *Filter, options options.FindOptions) (*FindResult, error)
Find(ctx context.Context, filter *Filter, options *options.FindOptions) (*FindResult, error)
// Delete метод для удаления логов по заданным параметрам
Delete(ctx context.Context, filter *Filter) error
......@@ -26,10 +27,10 @@ type Filter struct {
Q []string
}
type FindRequest struct {
Filter *Filter
Options *options.FindOptions
}
//type FindRequest struct {
// Filter *Filter
// Options *options.FindOptions
//}
type FindResult struct {
Entries []*Entry
......@@ -39,27 +40,32 @@ type FindResult struct {
}
func FindResultToPB(result *FindResult) *log.FindResult {
findResult := &log.FindResult{}
entries := make([]*log.LogEntry, 0)
findResult := &log.FindResult{
Total: result.Total,
}
entries := make([]*log.LogEntry, 0, len(result.Entries))
for _, e := range result.Entries {
entries = append(entries, EntryToPB(e))
}
findResult.Entries = entries
if result.Filter != nil {
findResult.Filter.Q = result.Filter.Q
}
if result.Options != nil {
findResult.Options, _ = transportgrpc.PtrServicesFindOptionsToProto(result.Options)
findResult.Options, _ = itemstransportgrpc.PtrServicesFindOptionsToProto(result.Options)
}
findResult.Total = result.Total
return findResult
}
func FindResultFromPB(result *log.FindResult) *FindResult {
findResult := &FindResult{}
entries := make([]*Entry, 0)
findResult := &FindResult{
Total: result.Total,
}
entries := make([]*Entry, 0, len(result.Entries))
for _, e := range result.Entries {
entries = append(entries, EntryFromPB(e))
}
......@@ -69,10 +75,8 @@ func FindResultFromPB(result *log.FindResult) *FindResult {
findResult.Filter.Q = result.Filter.Q
}
if result.Options != nil {
findResult.Options, _ = transportgrpc.ProtoToPtrServicesFindOptions(result.Options)
findResult.Options, _ = itemstransportgrpc.ProtoToPtrServicesFindOptions(result.Options)
}
findResult.Total = result.Total
return findResult
}
......@@ -10,6 +10,6 @@ type Storage interface {
Init(ctx context.Context) error
Reset(ctx context.Context) error
Log(ctx context.Context, entry []*Entry) error
Find(ctx context.Context, filter *Filter, options options.FindOptions) ([]*Entry, int, error)
Find(ctx context.Context, filter *Filter, options *options.FindOptions) ([]*Entry, int, error)
Delete(ctx context.Context, filter *Filter) error
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment