github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/api/dto/metric.go (about) 1 // This file is part of the Smart Home 2 // Program complex distribution https://github.com/e154/smart-home 3 // Copyright (C) 2023, Filippov Alex 4 // 5 // This library is free software: you can redistribute it and/or 6 // modify it under the terms of the GNU Lesser General Public 7 // License as published by the Free Software Foundation; either 8 // version 3 of the License, or (at your option) any later version. 9 // 10 // This library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 // Library General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public 16 // License along with this library. If not, see 17 // <https://www.gnu.org/licenses/>. 18 19 package dto 20 21 import ( 22 stub "github.com/e154/smart-home/api/stub" 23 "github.com/e154/smart-home/common" 24 m "github.com/e154/smart-home/models" 25 ) 26 27 func Metric(metric *m.Metric) (object *stub.ApiMetric) { 28 var options = &stub.ApiMetricOption{} 29 for _, item := range metric.Options.Items { 30 options.Items = append(options.Items, stub.ApiMetricOptionItem{ 31 Name: item.Name, 32 Description: item.Description, 33 Color: item.Color, 34 Translate: item.Translate, 35 Label: item.Label, 36 }) 37 } 38 39 var data = make([]stub.ApiMetricOptionData, 0, len(metric.Data)) 40 for _, item := range metric.Data { 41 data = append(data, stub.ApiMetricOptionData{ 42 Value: item.Value, 43 Time: item.Time, 44 }) 45 } 46 47 object = &stub.ApiMetric{ 48 Id: metric.Id, 49 Name: metric.Name, 50 Description: metric.Description, 51 Options: options, 52 Data: data, 53 Type: string(metric.Type), 54 Ranges: metric.Ranges, 55 CreatedAt: metric.CreatedAt, 56 UpdatedAt: metric.UpdatedAt, 57 } 58 59 return 60 } 61 62 func Metrics(metrics []*m.Metric) (objects []stub.ApiMetric) { 63 objects = make([]stub.ApiMetric, 0, len(metrics)) 64 for _, metric := range metrics { 65 objects = append(objects, *Metric(metric)) 66 } 67 return 68 } 69 70 func AddMetric(objects []stub.ApiMetric) (metrics []*m.Metric) { 71 metrics = make([]*m.Metric, 0, len(objects)) 72 for _, obj := range objects { 73 if obj.Options == nil { 74 continue 75 } 76 var options m.MetricOptions 77 for _, item := range obj.Options.Items { 78 options.Items = append(options.Items, m.MetricOptionsItem{ 79 Name: item.Name, 80 Description: item.Description, 81 Color: item.Color, 82 Translate: item.Translate, 83 Label: item.Label, 84 }) 85 } 86 metrics = append(metrics, &m.Metric{ 87 Id: obj.Id, 88 Name: obj.Name, 89 Description: obj.Description, 90 Options: options, 91 Type: common.MetricType(obj.Type), 92 Ranges: obj.Ranges, 93 }) 94 } 95 return 96 }