github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/plugins/logs/actor.go (about)

     1  // This file is part of the Smart Home
     2  // Program complex distribution https://github.com/e154/smart-home
     3  // Copyright (C) 2016-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 logs
    20  
    21  import (
    22  	"github.com/rcrowley/go-metrics"
    23  
    24  	"github.com/e154/smart-home/common"
    25  	m "github.com/e154/smart-home/models"
    26  	"github.com/e154/smart-home/system/supervisor"
    27  )
    28  
    29  // Actor ...
    30  type Actor struct {
    31  	*supervisor.BaseActor
    32  	cores         int64
    33  	model         string
    34  	ErrTotal      metrics.Counter
    35  	ErrToday      metrics.Counter
    36  	ErrYesterday  metrics.Counter
    37  	WarnTotal     metrics.Counter
    38  	WarnToday     metrics.Counter
    39  	WarnYesterday metrics.Counter
    40  }
    41  
    42  // NewActor ...
    43  func NewActor(entity *m.Entity,
    44  	service supervisor.Service) *Actor {
    45  
    46  	actor := &Actor{
    47  		BaseActor:     supervisor.NewBaseActor(entity, service),
    48  		ErrTotal:      metrics.NewCounter(),
    49  		ErrToday:      metrics.NewCounter(),
    50  		ErrYesterday:  metrics.NewCounter(),
    51  		WarnTotal:     metrics.NewCounter(),
    52  		WarnToday:     metrics.NewCounter(),
    53  		WarnYesterday: metrics.NewCounter(),
    54  	}
    55  
    56  	if entity != nil {
    57  		actor.Metric = entity.Metrics
    58  		attrs := entity.Attributes
    59  		actor.ErrTotal.Inc(attrs[AttrErrTotal].Int64())
    60  		actor.ErrToday.Inc(attrs[AttrErrToday].Int64())
    61  		actor.ErrYesterday.Inc(attrs[AttrErrYesterday].Int64())
    62  		actor.WarnTotal.Inc(attrs[AttrWarnTotal].Int64())
    63  		actor.WarnToday.Inc(attrs[AttrWarnToday].Int64())
    64  		actor.WarnYesterday.Inc(attrs[AttrWarnYesterday].Int64())
    65  	}
    66  	return actor
    67  }
    68  
    69  func (e *Actor) Destroy() {
    70  
    71  }
    72  
    73  func (e *Actor) Spawn() {
    74  	go e.selfUpdate()
    75  }
    76  
    77  func (e *Actor) selfUpdate() {
    78  
    79  	e.AttrMu.Lock()
    80  	e.Attrs[AttrErrTotal].Value = e.ErrTotal.Count()
    81  	e.Attrs[AttrErrToday].Value = e.ErrToday.Count()
    82  	e.Attrs[AttrErrYesterday].Value = e.ErrYesterday.Count()
    83  	e.Attrs[AttrWarnTotal].Value = e.WarnTotal.Count()
    84  	e.Attrs[AttrWarnToday].Value = e.WarnToday.Count()
    85  	e.Attrs[AttrWarnYesterday].Value = e.WarnYesterday.Count()
    86  	e.AttrMu.Unlock()
    87  
    88  	e.SaveState(false, true)
    89  }
    90  
    91  func (e *Actor) LogsHook(level common.LogLevel) {
    92  
    93  	switch level {
    94  	case common.LogLevelError:
    95  		e.ErrTotal.Inc(1)
    96  		e.ErrToday.Inc(1)
    97  	case common.LogLevelWarning:
    98  		e.WarnTotal.Inc(1)
    99  		e.WarnToday.Inc(1)
   100  	//case common.LogLevelInfo:
   101  	//case common.LogLevelDebug:
   102  	default:
   103  		return
   104  	}
   105  	e.selfUpdate()
   106  }
   107  
   108  func (e *Actor) UpdateDay() {
   109  	e.ErrYesterday.Clear()
   110  	e.ErrYesterday.Inc(e.ErrToday.Count())
   111  	e.WarnYesterday.Clear()
   112  	e.WarnYesterday.Inc(e.WarnToday.Count())
   113  	e.ErrToday.Clear()
   114  	e.WarnToday.Clear()
   115  
   116  	e.selfUpdate()
   117  }