imports

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package imports is the batch record of a history import (design: history-import-design.md section 4): one row of the imports table per commit, the drives and charges it brought in carry its id (import_id), and Undo removes them all in one transaction.

Nothing here parses a file or decides what a row is; it records, finds and undoes batches. The filename column holds "<source>.<ext>", never the name the user gave the file (it can carry a name or an email).

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound: no such batch for this user, or it is already undone.
	ErrNotFound = errors.New("imports: no such import")
)

Functions

This section is empty.

Types

type AlreadyImported

type AlreadyImported struct {
	ID uuid.UUID
	At time.Time
}

AlreadyImported is returned by Begin when this user has a live (not undone) batch of the same source and file hash.

func (*AlreadyImported) Error

func (e *AlreadyImported) Error() string

type Batch

type Batch struct {
	Source    string // "tezlab_import" style source name; a lowercase identifier
	Ext       string // the file's extension without the dot ("csv", "xlsx"); the filename stored is Source + "." + Ext
	SHA256    string // lowercase hex of the uploaded bytes
	Bytes     int64
	VehicleID *uuid.UUID // the vehicles.id the rows go to, nil when the file covers none yet
	Metadata  map[string]any
}

Batch is what a commit records about itself when it starts.

type Counts

type Counts struct {
	Rows, Drives, Charges, Skipped int
}

Counts is what a commit reports when it ends.

type Item

type Item struct {
	ID         uuid.UUID  `json:"id"`
	Source     string     `json:"source"`
	StartedAt  time.Time  `json:"started_at"`
	FinishedAt *time.Time `json:"finished_at,omitempty"`
	UndoneAt   *time.Time `json:"undone_at,omitempty"`
	Rows       int        `json:"rows"`
	Drives     int        `json:"drives"`
	Charges    int        `json:"charges"`
	Skipped    int        `json:"skipped"`
	First      string     `json:"first,omitempty"`
	Last       string     `json:"last,omitempty"`
}

Item is one row of the user's import list.

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store records batches for one user.

func New

func New(d *sql.DB, userID uuid.UUID) (*Store, error)

New binds a store to a user.

func (*Store) Begin

func (s *Store) Begin(ctx context.Context, b Batch) (uuid.UUID, error)

Begin records a new batch and returns its id. A live batch of the same source and file hash for this user refuses it with *AlreadyImported (the unique index imports_user_sha_live, which Undo frees): two concurrent commits of one file give exactly one batch.

func (*Store) Check

func (s *Store) Check(ctx context.Context, source, sha string) error

Check reports a live batch of this source and file hash as *AlreadyImported, nil when there is none. It writes nothing (the preview's "already imported on <date>").

func (*Store) Finish

func (s *Store) Finish(ctx context.Context, id uuid.UUID, c Counts) error

Finish records the counts and the end time of a batch.

func (*Store) List

func (s *Store) List(ctx context.Context) ([]Item, error)

List returns the user's batches, newest first (at most 100).

func (*Store) Started

func (s *Store) Started(ctx context.Context, since time.Time) (int, error)

Started counts the batches this user began since t, undone ones included: the commit rate limit.

func (*Store) Undo

func (s *Store) Undo(ctx context.Context, id uuid.UUID) (Undone, error)

Undo deletes every drive and charge the batch brought in (with the per-drive rows), and marks the batch undone, in one transaction. The same file can then be imported again. Notes or tags the user added to those rows go with them. ErrNotFound when the batch is not this user's or is already undone.

type Undone

type Undone struct {
	Drives, Charges int64
}

Undone is what Undo removed.

Jump to

Keyboard shortcuts

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