datasource

package
v0.2.17 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 1, 2023 License: Apache-2.0, MIT Imports: 18 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AddCmd = &cli.Command{
	Name:  "add",
	Usage: "Add a new data source to the dataset",
	Subcommands: underscore.Map(underscore.Filter(fs.Registry, func(r *fs.RegInfo) bool {
		return !slices.Contains([]string{"crypt", "memory", "tardigrade"}, r.Prefix)
	}), func(r *fs.RegInfo) *cli.Command {
		cmd := datasource.OptionsToCLIFlags(r)
		cmd.Flags = append(cmd.Flags, &cli.BoolFlag{
			Name:     "delete-after-export",
			Usage:    "[Dangerous] Delete the files of the dataset after exporting it to CAR files. ",
			Category: "Data Preparation Options",
		}, &cli.DurationFlag{
			Name:        "rescan-interval",
			Usage:       "Automatically rescan the source directory when this interval has passed from last successful scan",
			Category:    "Data Preparation Options",
			DefaultText: "disabled",
		})
		cmd.Action = func(c *cli.Context) error {
			datasetName := c.Args().Get(0)
			path := c.Args().Get(1)
			db := database.MustOpenFromCLI(c)
			dataset, err := database.FindDatasetByName(db, datasetName)
			if err != nil {
				return handler.NewBadRequestError(err)
			}
			if path == "" {
				return handler.NewBadRequestString("path is required")
			}
			if r.Prefix == "local" {
				path, err = filepath.Abs(path)
				if err != nil {
					return errors.Wrap(err, "failed to get absolute path")
				}
			}
			deleteAfterExport := c.Bool("delete-after-export")
			result := map[string]string{}
			for _, flag := range c.Command.Flags {
				flagName := flag.Names()[0]
				if strings.HasPrefix(flagName, r.Prefix) && c.String(flagName) != "" {
					optionName := strings.SplitN(strings.ReplaceAll(flagName, "-", "_"), "_", 2)[1]
					reg, err := fs.Find(r.Prefix)
					if err != nil {
						return errors.Wrap(err, "failed to find fs")
					}
					option, err := underscore.Find(reg.Options, func(o fs.Option) bool {
						return o.Name == optionName
					})
					if err != nil {
						return errors.Wrap(err, "failed to find option")
					}
					result[option.Name] = c.String(flagName)
				}
			}
			source := model.Source{
				DatasetID:           dataset.ID,
				Type:                r.Prefix,
				Path:                path,
				Metadata:            model.Metadata(result),
				ScanIntervalSeconds: 0,
				ScanningState:       model.Ready,
				DeleteAfterExport:   deleteAfterExport,
				DagGenState:         model.Created,
			}

			handler, err := datasource.DefaultHandlerResolver{}.Resolve(c.Context, source)
			if err != nil {
				return errors.Wrap(err, "failed to resolve handler")
			}

			_, err = handler.List(c.Context, "")
			if err != nil {
				return errors.Wrap(err, "failed to check source")
			}

			err = database.DoRetry(func() error {
				return db.Transaction(func(db *gorm.DB) error {
					err := db.Create(&source).Error
					if err != nil {
						return errors.Wrap(err, "failed to create source")
					}
					dir := model.Directory{
						Name:     path,
						SourceID: source.ID,
					}
					err = db.Create(&dir).Error
					if err != nil {
						return errors.Wrap(err, "failed to create directory")
					}
					return nil
				})
			})
			if err != nil {
				return cli.Exit(errors.Wrap(err, "failed to add source").Error(), 1)
			}

			cliutil.PrintToConsole(source, c.Bool("json"), exclude)
			return nil
		}
		return cmd
	}),
}
View Source
var CheckCmd = &cli.Command{
	Name:      "check",
	Usage:     "Check a data source by listing its entries. This is not to list prepared items but a direct listing of the data source to verify datasource connection",
	ArgsUsage: "<source_id> [sub_path]",
	Description: "This command will list entries in a data source under <sub_path>. " +
		"If <sub_path> is not provided, it will use the root directory",
	Action: func(c *cli.Context) error {
		db := database.MustOpenFromCLI(c)
		entries, err := datasource.CheckSourceHandler(
			db,
			c.Context,
			c.Args().Get(0),
			datasource.CheckSourceRequest{
				Path: c.Args().Get(1),
			},
		)
		if err != nil {
			return err.CliError()
		}

		cliutil.PrintToConsole(entries, c.Bool("json"), nil)
		return nil
	},
}
View Source
var DagGenCmd = &cli.Command{
	Name:      "daggen",
	Usage:     "Generate and export the DAG which represents the full folder structure of the data source",
	ArgsUsage: "<source_id>",
	Description: "This step is required for:\n" +
		"  1. Lookup and download files or folders using unixfs path\n" +
		"  2. Retrieve file that are splited across multiple pieces/deals",
	Action: func(c *cli.Context) error {
		db := database.MustOpenFromCLI(c)
		source, err := datasource.DagGenHandler(db, c.Args().Get(0))
		if err != nil {
			return err.CliError()
		}
		cliutil.PrintToConsole(source, c.Bool("json"), exclude)
		return nil
	},
}
View Source
var ListCmd = &cli.Command{
	Name:  "list",
	Usage: "List all sources",
	Flags: []cli.Flag{
		&cli.StringFlag{
			Name:  "dataset",
			Usage: "Filter by dataset name",
		},
	},
	Action: func(c *cli.Context) error {
		db := database.MustOpenFromCLI(c)
		datasetName := c.String("dataset")
		sources, err := datasource.ListSourceHandler(
			db,
			datasetName,
		)
		if err != nil {
			return err.CliError()
		}
		cliutil.PrintToConsole(sources, c.Bool("json"), exclude)
		return nil
	},
}
View Source
var RemoveCmd = &cli.Command{
	Name:      "remove",
	Usage:     "Remove a data source",
	ArgsUsage: "<source_id>",
	Action: func(c *cli.Context) error {
		db := database.MustOpenFromCLI(c)
		err := datasource.RemoveSourceHandler(
			db,
			c.Args().Get(0),
		)
		return err.CliError()
	},
}
View Source
var RescanCmd = &cli.Command{
	Name:        "rescan",
	Usage:       "Rescan a data source",
	ArgsUsage:   "<source_id>",
	Description: "This command will clear any error of a data source and rescan it",
	Action: func(c *cli.Context) error {
		db := database.MustOpenFromCLI(c)
		source, err := datasource.RescanSourceHandler(
			db,
			c.Args().Get(0),
		)
		if err != nil {
			return err.CliError()
		}

		cliutil.PrintToConsole(source, c.Bool("json"), exclude)
		return nil
	},
}
View Source
var StatusCmd = &cli.Command{
	Name:      "status",
	Usage:     "Get the data preparation summary of a data source",
	ArgsUsage: "<source_id>",
	Action: func(c *cli.Context) error {
		db := database.MustOpenFromCLI(c)
		result, err := datasource.GetSourceStatusHandler(
			db,
			c.Args().Get(0),
		)
		if err != nil {
			return err.CliError()
		}

		if c.Bool("json") {
			objJSON, err := json.MarshalIndent(result, "", "  ")
			if err != nil {
				return err
			}
			fmt.Println(string(objJSON))
			return nil
		}

		fmt.Println("Chunks by state:")
		cliutil.PrintToConsole(result.ChunkSummary, c.Bool("json"), nil)
		fmt.Println("Items by state:")
		cliutil.PrintToConsole(result.ItemSummary, c.Bool("json"), nil)
		return nil
	},
}
View Source
var UpdateCmd = &cli.Command{
	Name:      "update",
	Usage:     "Update the config options of a source",
	ArgsUsage: "<source_id>",
	Flags: func() []cli.Flag {
		var flags []cli.Flag
		for _, cmd := range AddCmd.Subcommands {
			cmdFlags := underscore.Map(cmd.Flags, func(flag cli.Flag) cli.Flag {
				stringFlag, ok := flag.(*cli.StringFlag)
				if !ok {
					return flag
				}
				stringFlag.Required = false
				stringFlag.Category = "Options for " + cmd.Name
				stringFlag.Aliases = nil
				return stringFlag
			})
			flags = append(flags, cmdFlags...)
		}
		keys := make(map[string]cli.Flag)
		newFlags := make([]cli.Flag, 0)
		for _, flag := range flags {
			if _, ok := keys[flag.Names()[0]]; !ok {
				keys[flag.Names()[0]] = flag
				newFlags = append(newFlags, flag)
			}
		}
		return newFlags
	}(),
	Action: func(c *cli.Context) error {
		db := database.MustOpenFromCLI(c)
		config := map[string]string{}
		var deleteAfterExport *bool
		var rescanInterval *time.Duration
		for _, name := range c.LocalFlagNames() {
			if c.IsSet(name) {
				if name == "delete-after-export" {
					b := c.Bool(name)
					deleteAfterExport = &b
					continue
				}
				if name == "rescan-interval" {
					d := c.Duration(name)
					rescanInterval = &d
					continue
				}
				value := c.String(name)
				config[name] = value
			}
		}

		source, err := datasource.UpdateSourceHandler(
			db,
			context.Background(),
			c.Args().Get(0),
			deleteAfterExport,
			rescanInterval,
			config,
		)
		if err != nil {
			return err.CliError()
		}

		cliutil.PrintToConsole(source, c.Bool("json"), nil)
		return nil
	},
}

Functions

This section is empty.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL