gitee.com/quant1x/engine@v1.8.4/command/command_update.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"gitee.com/quant1x/engine/cache"
     6  	"gitee.com/quant1x/engine/storages"
     7  	cmder "github.com/spf13/cobra"
     8  )
     9  
    10  const (
    11  	updateCommand     = "update"
    12  	updateDescription = "更新数据"
    13  )
    14  
    15  var (
    16  	// CmdUpdate 更新数据
    17  	CmdUpdate *cmder.Command = nil
    18  	barIndex                 = 1
    19  )
    20  
    21  func initUpdate() {
    22  	CmdUpdate = &cmder.Command{
    23  		Use:     updateCommand,
    24  		Example: Application + " " + updateCommand + " --all",
    25  		//Args:    args.MinimumNArgs(0),
    26  		Args: func(cmd *cmder.Command, args []string) error {
    27  			return nil
    28  		},
    29  		Short: updateDescription,
    30  		Long:  updateDescription,
    31  		Run: func(cmd *cmder.Command, args []string) {
    32  			fmt.Println()
    33  			currentDate := cache.DefaultCanUpdateDate()
    34  			cacheDate, featureDate := cache.CorrectDate(currentDate)
    35  			if flagAll.Value {
    36  				// 全部更新
    37  				handleUpdateAll(cacheDate, featureDate)
    38  			} else if len(flagBaseData.Value) > 0 {
    39  				all, keywords := parseFields(flagBaseData.Value)
    40  				if all || len(keywords) == 0 {
    41  					clear(keywords)
    42  				}
    43  				handleUpdateBaseDataWithKeywords(cacheDate, featureDate, keywords...)
    44  			} else if len(flagFeatures.Value) > 0 {
    45  				all, keywords := parseFields(flagFeatures.Value)
    46  				if all || len(keywords) == 0 {
    47  					clear(keywords)
    48  				}
    49  				handleUpdateFeaturesWithKeywords(cacheDate, featureDate, keywords...)
    50  			} else {
    51  				fmt.Println("Error: 非全部更新, 必须携带--features或--base")
    52  				_ = cmd.Usage()
    53  			}
    54  		},
    55  	}
    56  	commandInit(CmdUpdate, &flagAll)
    57  
    58  	// 1. 基础数据
    59  	plugins := cache.Plugins(cache.PluginMaskBaseData)
    60  	flagBaseData.Usage = getPluginsUsage(plugins)
    61  	commandInit(CmdUpdate, &flagBaseData)
    62  
    63  	// 2. 特征数据
    64  	plugins = cache.Plugins(cache.PluginMaskFeature)
    65  	flagFeatures.Usage = getPluginsUsage(plugins)
    66  	commandInit(CmdUpdate, &flagFeatures)
    67  
    68  	//// 3. 处理异常
    69  	//CmdUpdate.SetFlagErrorFunc(func(cmd *cmder.Command, err error) error {
    70  	//	return nil
    71  	//})
    72  }
    73  
    74  // 全部更新
    75  func handleUpdateAll(cacheDate, featureDate string) {
    76  	handleUpdateBaseData(featureDate)
    77  	handleUpdateFeaturesWithKeywords(cacheDate, featureDate)
    78  }
    79  
    80  // 更新基础数据
    81  func handleUpdateBaseData(date string) {
    82  	// 1. 获取全部注册的数据集插件
    83  	mask := cache.PluginMaskBaseData
    84  	plugins := cache.Plugins(mask)
    85  	// 2. 执行操作
    86  	storages.BaseDataUpdate(barIndex, date, plugins, cache.OpUpdate)
    87  }
    88  
    89  // 更新基础数据
    90  func handleUpdateBaseDataWithKeywords(cacheDate, featureDate string, keywords ...string) {
    91  	plugins := cache.PluginsWithName(cache.PluginMaskBaseData, keywords...)
    92  	if len(plugins) == 0 {
    93  		// 1. 获取全部注册的数据集插件
    94  		mask := cache.PluginMaskBaseData
    95  		plugins = cache.Plugins(mask)
    96  	}
    97  	storages.BaseDataUpdate(barIndex, featureDate, plugins, cache.OpUpdate)
    98  	_ = cacheDate
    99  }
   100  
   101  // 更新特征组合
   102  func handleUpdateFeaturesWithKeywords(cacheDate, featureDate string, keywords ...string) {
   103  	plugins := cache.PluginsWithName(cache.PluginMaskFeature, keywords...)
   104  	if len(plugins) == 0 {
   105  		// 1. 获取全部注册的数据集插件
   106  		mask := cache.PluginMaskFeature
   107  		plugins = cache.Plugins(mask)
   108  	}
   109  	storages.FeaturesUpdate(&barIndex, cacheDate, featureDate, plugins, cache.OpUpdate)
   110  }