code.vegaprotocol.io/vega@v0.79.0/core/datasource/internal/vegatime/vegatime.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 vegatime
    19  
    20  import (
    21  	"fmt"
    22  
    23  	"code.vegaprotocol.io/vega/core/datasource/common"
    24  	vegapb "code.vegaprotocol.io/vega/protos/vega"
    25  	datapb "code.vegaprotocol.io/vega/protos/vega/data/v1"
    26  )
    27  
    28  const VegaTimeKey = "vegaprotocol.builtin.timestamp"
    29  
    30  // SpecConfiguration is used internally.
    31  type SpecConfiguration struct {
    32  	Conditions []*common.SpecCondition
    33  }
    34  
    35  // String returns the content of DataSourceSpecConfigurationTime as a string.
    36  func (s SpecConfiguration) String() string {
    37  	return fmt.Sprintf(
    38  		"conditions(%s)", common.SpecConditions(s.Conditions).String(),
    39  	)
    40  }
    41  
    42  func (s SpecConfiguration) IntoProto() *vegapb.DataSourceSpecConfigurationTime {
    43  	return &vegapb.DataSourceSpecConfigurationTime{
    44  		Conditions: common.SpecConditions(s.Conditions).IntoProto(),
    45  	}
    46  }
    47  
    48  func (s SpecConfiguration) DeepClone() common.DataSourceType {
    49  	conditions := []*common.SpecCondition{}
    50  	conditions = append(conditions, s.Conditions...)
    51  
    52  	return SpecConfiguration{
    53  		Conditions: conditions,
    54  	}
    55  }
    56  
    57  func (s SpecConfiguration) GetFilters() []*common.SpecFilter {
    58  	filters := []*common.SpecFilter{}
    59  	// For the case the internal data source is time based
    60  	// (https://github.com/vegaprotocol/specs/blob/master/protocol/0048-DSRI-data_source_internal.md#13-vega-time-changed)
    61  	// We add the filter key values manually to match a time based data source
    62  	// Ensure only a single filter has been created, that holds the first condition
    63  	if len(s.Conditions) > 0 {
    64  		filters = append(
    65  			filters,
    66  			&common.SpecFilter{
    67  				Key: &common.SpecPropertyKey{
    68  					Name: VegaTimeKey,
    69  					Type: datapb.PropertyKey_TYPE_TIMESTAMP,
    70  				},
    71  				Conditions: []*common.SpecCondition{
    72  					s.Conditions[0],
    73  				},
    74  			},
    75  		)
    76  	}
    77  	return filters
    78  }
    79  
    80  func SpecConfigurationFromProto(protoConfig *vegapb.DataSourceSpecConfigurationTime) SpecConfiguration {
    81  	if protoConfig == nil {
    82  		return SpecConfiguration{}
    83  	}
    84  
    85  	return SpecConfiguration{
    86  		Conditions: common.SpecConditionsFromProto(protoConfig.Conditions),
    87  	}
    88  }
    89  
    90  func (s SpecConfiguration) ToDefinitionProto() (*vegapb.DataSourceDefinition, error) {
    91  	return &vegapb.DataSourceDefinition{
    92  		SourceType: &vegapb.DataSourceDefinition_Internal{
    93  			Internal: &vegapb.DataSourceDefinitionInternal{
    94  				SourceType: &vegapb.DataSourceDefinitionInternal_Time{
    95  					Time: s.IntoProto(),
    96  				},
    97  			},
    98  		},
    99  	}, nil
   100  }