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

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"gitee.com/quant1x/engine/cache"
     6  	"gitee.com/quant1x/engine/datasource/base"
     7  	"gitee.com/quant1x/engine/storages"
     8  	"gitee.com/quant1x/exchange"
     9  	"gitee.com/quant1x/gox/logger"
    10  	"gitee.com/quant1x/gox/progressbar"
    11  	cmder "github.com/spf13/cobra"
    12  	"strings"
    13  	"time"
    14  )
    15  
    16  const (
    17  	repairCommand     = "repair"
    18  	repairDescription = "修复数据"
    19  )
    20  
    21  var (
    22  	// CmdRepair 补登历史数据
    23  	CmdRepair *cmder.Command = nil
    24  )
    25  
    26  func initRepair() {
    27  	CmdRepair = &cmder.Command{
    28  		Use:     repairCommand,
    29  		Example: Application + " " + repairCommand + " --all",
    30  		//Args:    args.MinimumNArgs(0),
    31  		Args: func(cmd *cmder.Command, args []string) error {
    32  			return nil
    33  		},
    34  		Short: repairDescription,
    35  		Long:  repairDescription,
    36  		Run: func(cmd *cmder.Command, args []string) {
    37  			beginDate := exchange.FixTradeDate(flagStartDate.Value)
    38  			endDate := cache.DefaultCanReadDate()
    39  			if len(flagEndDate.Value) > 0 {
    40  				endDate = exchange.FixTradeDate(flagEndDate.Value)
    41  			}
    42  			dates := exchange.TradingDateRange(beginDate, endDate)
    43  			count := len(dates)
    44  			if count == 0 {
    45  				fmt.Printf("start=%s ~ end=%s 休市, 没有数据\n", beginDate, endDate)
    46  				return
    47  			}
    48  			fmt.Printf("修复数据: %s => %s"+strings.Repeat("\r\n", 2), dates[0], dates[count-1])
    49  			base.UpdateBeginDateOfHistoricalTradingData(dates[0])
    50  			if flagAll.Value {
    51  				handleRepairAll(dates)
    52  			} else if len(flagBaseData.Value) > 0 {
    53  				all, keywords := parseFields(flagBaseData.Value)
    54  				if all || len(keywords) == 0 {
    55  					handleRepairAllDataSets(dates)
    56  				} else {
    57  					plugins := cache.PluginsWithName(cache.PluginMaskBaseData, keywords...)
    58  					if len(plugins) == 0 {
    59  						fmt.Printf("没有找到名字是[%s]的数据插件\n", strings.Join(keywords, ","))
    60  					} else {
    61  						handleRepairDataSetsWithPlugins(dates, plugins)
    62  					}
    63  				}
    64  			} else if len(flagFeatures.Value) > 0 {
    65  				all, keywords := parseFields(flagFeatures.Value)
    66  				if all || len(keywords) == 0 {
    67  					handleRepairAllFeatures(dates)
    68  				} else {
    69  					plugins := cache.PluginsWithName(cache.PluginMaskFeature, keywords...)
    70  					if len(plugins) == 0 {
    71  						fmt.Printf("没有找到名字是[%s]的数据插件\n", strings.Join(keywords, ","))
    72  					} else {
    73  						handleRepairFeaturesWithPlugins(dates, plugins)
    74  					}
    75  				}
    76  			} else {
    77  				fmt.Println("Error: 非全部修复, 必须携带--features或--base")
    78  				_ = cmd.Usage()
    79  			}
    80  
    81  		},
    82  	}
    83  	commandInit(CmdRepair, &flagAll)
    84  	commandInit(CmdRepair, &flagStartDate)
    85  	commandInit(CmdRepair, &flagEndDate)
    86  
    87  	// 1. 基础数据
    88  	plugins := cache.Plugins(cache.PluginMaskBaseData)
    89  	flagBaseData.Usage = getPluginsUsage(plugins)
    90  	commandInit(CmdRepair, &flagBaseData)
    91  
    92  	// 2. 特征数据
    93  	plugins = cache.Plugins(cache.PluginMaskFeature)
    94  	flagFeatures.Usage = getPluginsUsage(plugins)
    95  	commandInit(CmdRepair, &flagFeatures)
    96  }
    97  
    98  func handleRepairAll(dates []string) {
    99  	handleRepairAllDataSets(dates)
   100  	handleRepairAllFeatures(dates)
   101  }
   102  
   103  func handleRepairAllDataSets(dates []string) {
   104  	fmt.Println()
   105  	moduleName := "补登数据集合"
   106  	logger.Info(moduleName + ", 任务开始")
   107  	mask := cache.PluginMaskBaseData
   108  	plugins := cache.Plugins(mask)
   109  	count := len(dates)
   110  	barIndex := 1
   111  	bar := progressbar.NewBar(barIndex, "执行["+moduleName+"]", count)
   112  	for _, date := range dates {
   113  		//cacheDate, featureDate := cache.CorrectDate(date)
   114  		barIndex++
   115  		storages.BaseDataUpdate(barIndex, date, plugins, cache.OpRepair)
   116  		bar.Add(1)
   117  	}
   118  	bar.Wait()
   119  	logger.Info(moduleName+", 任务执行完毕.", time.Now())
   120  	fmt.Println()
   121  }
   122  
   123  // 修复 - 指定的基础数据
   124  func handleRepairDataSetsWithPlugins(dates []string, plugins []cache.DataAdapter) {
   125  	fmt.Println()
   126  	moduleName := "修复数据"
   127  	logger.Info(moduleName + ", 任务开始")
   128  	count := len(dates)
   129  	barIndex := 1
   130  	bar := progressbar.NewBar(barIndex, "执行["+moduleName+"]", count)
   131  	for _, date := range dates {
   132  		//cacheDate, featureDate := cache.CorrectDate(date)
   133  		//barIndex++
   134  		storages.BaseDataUpdate(barIndex+1, date, plugins, cache.OpRepair)
   135  		bar.Add(1)
   136  	}
   137  	logger.Info(moduleName+", 任务执行完毕.", time.Now())
   138  	fmt.Println()
   139  }
   140  
   141  // 修复 - 特征数据
   142  func handleRepairAllFeatures(dates []string) {
   143  	moduleName := "补登特征数据"
   144  	logger.Info(moduleName + ", 任务开始")
   145  	mask := cache.PluginMaskFeature
   146  	plugins := cache.Plugins(mask)
   147  	count := len(dates)
   148  	barIndex := 1
   149  	bar := progressbar.NewBar(barIndex, "执行["+moduleName+"]", count)
   150  	for _, date := range dates {
   151  		cacheDate, featureDate := cache.CorrectDate(date)
   152  		barIndex++
   153  		storages.FeaturesUpdate(&barIndex, cacheDate, featureDate, plugins, cache.OpRepair)
   154  		bar.Add(1)
   155  	}
   156  	logger.Info(moduleName+", 任务执行完毕.", time.Now())
   157  	fmt.Println()
   158  }
   159  
   160  // 修复 - 指定的特征数据
   161  func handleRepairFeaturesWithPlugins(dates []string, plugins []cache.DataAdapter) {
   162  	fmt.Println()
   163  	moduleName := "修复数据"
   164  	logger.Info(moduleName + ", 任务开始")
   165  	count := len(dates)
   166  	barIndex := 1
   167  	bar := progressbar.NewBar(barIndex, "执行["+moduleName+"]", count)
   168  	for _, date := range dates {
   169  		cacheDate, featureDate := cache.CorrectDate(date)
   170  		barIndex++
   171  		storages.FeaturesUpdate(&barIndex, cacheDate, featureDate, plugins, cache.OpRepair)
   172  		bar.Add(1)
   173  	}
   174  	logger.Info(moduleName+", 任务执行完毕.", time.Now())
   175  	fmt.Println()
   176  }