Skip to content

Commit

Permalink
Issues/metric (#191)
Browse files Browse the repository at this point in the history
  • Loading branch information
e154 authored Aug 14, 2022
1 parent 2777fbe commit 242b98c
Show file tree
Hide file tree
Showing 31 changed files with 949 additions and 202 deletions.
2 changes: 1 addition & 1 deletion adaptors/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ func (n *Entity) preloadMetric(ver *m.Entity) {
optionItems[i] = item.Name
}

if ver.Metrics[i].Data, err = bucketMetricBucketAdaptor.Simple24HPreview(metric.Id, optionItems); err != nil {
if ver.Metrics[i].Data, err = bucketMetricBucketAdaptor.SimpleListWithSoftRange(nil, nil, metric.Id, common.String(common.MetricRange24H.String()), optionItems); err != nil {
log.Error(err.Error())
return
}
Expand Down
2 changes: 1 addition & 1 deletion adaptors/metric_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (n *MetricBucket) SimpleListWithSoftRange(_from, _to *time.Time, metricId i
var dbList []db.MetricBucket

if _metricRange != nil {
if dbList, err = n.table.SimpleListByRangeType(metricId, common.StringValue(_metricRange), optionItems); err != nil {
if dbList, err = n.table.SimpleListByRangeType(metricId, common.MetricRange(common.StringValue(_metricRange)), optionItems); err != nil {
return
}
}
Expand Down
2 changes: 2 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ func (a *Api) Start() error {
gw.RegisterDashboardCardItemServiceServer(grpcServer, a.controllers.DashboardCardItem)
gw.RegisterVariableServiceServer(grpcServer, a.controllers.Variable)
gw.RegisterEntityStorageServiceServer(grpcServer, a.controllers.EntityStorage)
gw.RegisterMetricServiceServer(grpcServer, a.controllers.Metric)
grpc_prometheus.Register(grpcServer)

var group errgroup.Group
Expand Down Expand Up @@ -171,6 +172,7 @@ func (a *Api) Start() error {
_ = gw.RegisterDashboardTabServiceHandlerFromEndpoint(ctx, mux, a.cfg.GrpcHostPort, opts)
_ = gw.RegisterEntityStorageServiceHandlerFromEndpoint(ctx, mux, a.cfg.GrpcHostPort, opts)
_ = gw.RegisterVariableServiceHandlerFromEndpoint(ctx, mux, a.cfg.GrpcHostPort, opts)
_ = gw.RegisterMetricServiceHandlerFromEndpoint(ctx, mux, a.cfg.GrpcHostPort, opts)
return nil
})

Expand Down
60 changes: 60 additions & 0 deletions api/api.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
{
"name": "LogService"
},
{
"name": "MetricService"
},
{
"name": "PluginService"
},
Expand Down Expand Up @@ -2335,6 +2338,63 @@
]
}
},
"/v1/metric": {
"get": {
"summary": "get metric",
"operationId": "MetricService_GetMetric",
"responses": {
"200": {
"description": "A successful response.",
"schema": {
"$ref": "#/definitions/apiMetric"
}
},
"default": {
"description": "An unexpected error response.",
"schema": {
"$ref": "#/definitions/rpcStatus"
}
}
},
"parameters": [
{
"name": "id",
"in": "query",
"required": false,
"type": "string",
"format": "int64"
},
{
"name": "range",
"in": "query",
"required": false,
"type": "string"
},
{
"name": "from",
"in": "query",
"required": false,
"type": "string",
"format": "date-time"
},
{
"name": "to",
"in": "query",
"required": false,
"type": "string",
"format": "date-time"
}
],
"tags": [
"MetricService"
],
"security": [
{
"ApiKeyAuth": []
}
]
}
},
"/v1/plugin/{name}/disable": {
"post": {
"summary": "disable plugin",
Expand Down
2 changes: 2 additions & 0 deletions api/controllers/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type Controllers struct {
DashboardTab ControllerDashboardTab
Variable ControllerVariable
EntityStorage ControllerEntityStorage
Metric ControllerMetric
}

// NewControllers ...
Expand Down Expand Up @@ -78,5 +79,6 @@ func NewControllers(adaptors *adaptors.Adaptors,
DashboardTab: NewControllerDashboardTab(common),
Variable: NewControllerVariable(common),
EntityStorage: NewControllerEntityStorage(common),
Metric: NewControllerMetric(common),
}
}
58 changes: 58 additions & 0 deletions api/controllers/metric.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// This file is part of the Smart Home
// Program complex distribution https://github.com/e154/smart-home
// Copyright (C) 2016-2021, Filippov Alex
//
// This library is free software: you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Library General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library. If not, see
// <https://www.gnu.org/licenses/>.

package controllers

import (
"context"
"github.com/e154/smart-home/api/dto"
"github.com/e154/smart-home/common"
"time"

"github.com/e154/smart-home/api/stub/api"
)

// ControllerMetric ...
type ControllerMetric struct {
*ControllerCommon
}

// NewControllerMetric ...
func NewControllerMetric(common *ControllerCommon) ControllerMetric {
return ControllerMetric{
ControllerCommon: common,
}
}

// GetMetric ...
func (c ControllerMetric) GetMetric(ctx context.Context, req *api.GetMetricRequest) (*api.Metric, error) {

var from, to *time.Time
if req.From != nil {
from = common.Time(req.From.AsTime())
}
if req.To != nil {
to = common.Time(req.To.AsTime())
}
metric, err := c.endpoint.Metric.GetByIdWithData(ctx, from, to, req.Id, req.Range)
if err != nil {
return nil, c.error(ctx, nil, err)
}

return dto.Metric(metric), nil
}
65 changes: 0 additions & 65 deletions api/dto/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,68 +331,3 @@ func ToEntity(entity *m.Entity) (obj *api.Entity) {
}
return
}

func Metrics(metrics []*m.Metric) (objects []*api.Metric) {
objects = make([]*api.Metric, 0, len(metrics))
for _, metric := range metrics {

var options = &api.MetricOption{}
for _, item := range metric.Options.Items {
options.Items = append(options.Items, &api.MetricOptionItem{
Name: item.Name,
Description: item.Description,
Color: item.Color,
Translate: item.Translate,
Label: item.Label,
})
}

var data = make([]*api.MetricOptionData, 0, len(metric.Data))
for _, item := range metric.Data {
data = append(data, &api.MetricOptionData{
Value: item.Value,
MetricId: item.MetricId,
Time: timestamppb.New(item.Time),
})
}

obj := &api.Metric{
Id: metric.Id,
Name: metric.Name,
Description: metric.Description,
Options: options,
Data: data,
Type: string(metric.Type),
Ranges: metric.Ranges,
CreatedAt: timestamppb.New(metric.CreatedAt),
UpdatedAt: timestamppb.New(metric.UpdatedAt),
}
objects = append(objects, obj)
}
return
}

func AddMetric(objects []*api.Metric) (metrics []*m.Metric) {
metrics = make([]*m.Metric, 0, len(objects))
for _, obj := range objects {
var options m.MetricOptions
for _, item := range obj.Options.Items {
options.Items = append(options.Items, m.MetricOptionsItem{
Name: item.Name,
Description: item.Description,
Color: item.Color,
Translate: item.Translate,
Label: item.Label,
})
}
metrics = append(metrics, &m.Metric{
Id: obj.Id,
Name: obj.Name,
Description: obj.Description,
Options: options,
Type: common.MetricType(obj.Type),
Ranges: obj.Ranges,
})
}
return
}
77 changes: 77 additions & 0 deletions api/dto/metric.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package dto

import (
"github.com/e154/smart-home/api/stub/api"
"github.com/e154/smart-home/common"
m "github.com/e154/smart-home/models"
"google.golang.org/protobuf/types/known/timestamppb"
)

func Metric(metric *m.Metric) (object *api.Metric) {
var options = &api.MetricOption{}
for _, item := range metric.Options.Items {
options.Items = append(options.Items, &api.MetricOptionItem{
Name: item.Name,
Description: item.Description,
Color: item.Color,
Translate: item.Translate,
Label: item.Label,
})
}

var data = make([]*api.MetricOptionData, 0, len(metric.Data))
for _, item := range metric.Data {
data = append(data, &api.MetricOptionData{
Value: item.Value,
MetricId: item.MetricId,
Time: timestamppb.New(item.Time),
})
}

object = &api.Metric{
Id: metric.Id,
Name: metric.Name,
Description: metric.Description,
Options: options,
Data: data,
Type: string(metric.Type),
Ranges: metric.Ranges,
CreatedAt: timestamppb.New(metric.CreatedAt),
UpdatedAt: timestamppb.New(metric.UpdatedAt),
}

return
}

func Metrics(metrics []*m.Metric) (objects []*api.Metric) {
objects = make([]*api.Metric, 0, len(metrics))
for _, metric := range metrics {
objects = append(objects, Metric(metric))
}
return
}

func AddMetric(objects []*api.Metric) (metrics []*m.Metric) {
metrics = make([]*m.Metric, 0, len(objects))
for _, obj := range objects {
var options m.MetricOptions
for _, item := range obj.Options.Items {
options.Items = append(options.Items, m.MetricOptionsItem{
Name: item.Name,
Description: item.Description,
Color: item.Color,
Translate: item.Translate,
Label: item.Label,
})
}
metrics = append(metrics, &m.Metric{
Id: obj.Id,
Name: obj.Name,
Description: obj.Description,
Options: options,
Type: common.MetricType(obj.Type),
Ranges: obj.Ranges,
})
}
return
}
27 changes: 27 additions & 0 deletions api/protos/metric.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package api;

option go_package = "/api";

import "google/api/annotations.proto";
import "protoc-gen-swagger/options/annotations.proto";
import "google/protobuf/timestamp.proto";

message MetricOptionItem {
Expand Down Expand Up @@ -35,3 +37,28 @@ message Metric {
google.protobuf.Timestamp created_at = 99;
google.protobuf.Timestamp updated_at = 100;
}

message GetMetricRequest {
int64 id = 1;
optional string range = 2;
optional google.protobuf.Timestamp from = 99;
optional google.protobuf.Timestamp to = 100;
}

service MetricService {

// get metric
rpc GetMetric (GetMetricRequest) returns (Metric){
option (google.api.http) = {
get: "/v1/metric"
};
option (grpc.gateway.protoc_gen_swagger.options.openapiv2_operation) = {
security: {
security_requirement: {
key: "ApiKeyAuth"
value: {}
}
}
};
}
}
Loading

0 comments on commit 242b98c

Please sign in to comment.