code.vegaprotocol.io/vega@v0.79.0/core/datasource/internal/timetrigger/timetrigger.go (about) 1 // Copyright (C) 2023 Gobalsky Labs Limited 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Affero General Public License as 5 // published by the Free Software Foundation, either version 3 of the 6 // License, or (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Affero General Public License for more details. 12 // 13 // You should have received a copy of the GNU Affero General Public License 14 // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16 //lint:file-ignore ST1003 Ignore underscores in names, this is straigh copied from the proto package to ease introducing the domain types 17 18 package timetrigger 19 20 import ( 21 "fmt" 22 "time" 23 24 "code.vegaprotocol.io/vega/core/datasource/common" 25 vegapb "code.vegaprotocol.io/vega/protos/vega" 26 datapb "code.vegaprotocol.io/vega/protos/vega/data/v1" 27 ) 28 29 const InternalTimeTriggerKey = "vegaprotocol.builtin.timetrigger" 30 31 type SpecConfiguration struct { 32 Triggers common.InternalTimeTriggers 33 Conditions []*common.SpecCondition 34 } 35 36 func (s *SpecConfiguration) SetInitial(initial, timeNow time.Time) error { 37 if err := s.Triggers.Empty(); err != nil { 38 return err 39 } 40 41 s.Triggers[0].SetInitial(initial, timeNow) 42 return nil 43 } 44 45 func (s *SpecConfiguration) SetNextTrigger(timeNow time.Time) error { 46 if err := s.Triggers.Empty(); err != nil { 47 return err 48 } 49 50 s.Triggers[0].SetNextTrigger(timeNow) 51 return nil 52 } 53 54 func (s SpecConfiguration) String() string { 55 return fmt.Sprintf( 56 "trigger(%s), conditions(%s)", 57 s.Triggers.String(), 58 common.SpecConditions(s.Conditions).String(), 59 ) 60 } 61 62 func (s SpecConfiguration) IntoProto() *vegapb.DataSourceSpecConfigurationTimeTrigger { 63 return &vegapb.DataSourceSpecConfigurationTimeTrigger{ 64 Triggers: s.Triggers.IntoProto(), 65 Conditions: common.SpecConditions(s.Conditions).IntoProto(), 66 } 67 } 68 69 func (s SpecConfiguration) DeepClone() common.DataSourceType { 70 return SpecConfiguration{ 71 Triggers: s.Triggers.DeepClone(), 72 Conditions: s.Conditions, 73 } 74 } 75 76 func (s SpecConfiguration) GetFilters() []*common.SpecFilter { 77 filters := []*common.SpecFilter{} 78 79 conditions := []*common.SpecCondition{} 80 if s.Conditions != nil { 81 conditions = s.Conditions 82 } 83 84 // For the case the internal data source is time based 85 // (https://github.com/vegaprotocol/specs/blob/master/protocol/0048-DSRI-data_source_internal.md#12-time-triggered) 86 // We add the filter key values manually to match a time based data source 87 // if len(s.Conditions) > 0 { 88 filters = append( 89 filters, 90 &common.SpecFilter{ 91 Key: &common.SpecPropertyKey{ 92 Name: InternalTimeTriggerKey, 93 Type: datapb.PropertyKey_TYPE_TIMESTAMP, 94 }, 95 Conditions: conditions, 96 }, 97 ) 98 //} 99 return filters 100 } 101 102 func (s SpecConfiguration) GetTimeTriggers() common.InternalTimeTriggers { 103 return s.Triggers 104 } 105 106 func (s SpecConfiguration) IsTriggered(tm time.Time) bool { 107 return s.Triggers.IsTriggered(tm) 108 } 109 110 func SpecConfigurationFromProto(protoConfig *vegapb.DataSourceSpecConfigurationTimeTrigger, tm *time.Time) (SpecConfiguration, error) { 111 if protoConfig == nil { 112 return SpecConfiguration{}, nil 113 } 114 115 return SpecConfiguration{ 116 Triggers: common.InternalTimeTriggersFromProto(protoConfig.Triggers), 117 Conditions: common.SpecConditionsFromProto(protoConfig.Conditions), 118 }, nil 119 } 120 121 func (s SpecConfiguration) ToDefinitionProto() (*vegapb.DataSourceDefinition, error) { 122 return &vegapb.DataSourceDefinition{ 123 SourceType: &vegapb.DataSourceDefinition_Internal{ 124 Internal: &vegapb.DataSourceDefinitionInternal{ 125 SourceType: &vegapb.DataSourceDefinitionInternal_TimeTrigger{ 126 TimeTrigger: s.IntoProto(), 127 }, 128 }, 129 }, 130 }, nil 131 }