db

package
v0.0.0-...-4aab756 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2024 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const EPSILION = 0.000001
View Source
const M000_TaskSchema = `` /* 406-byte string literal not displayed */
View Source
const M001_TagSchema = `` /* 129-byte string literal not displayed */
View Source
const M002_TaskTagsSchema = `` /* 291-byte string literal not displayed */
View Source
const M003_TaskSchema = `
ALTER TABLE tasks ADD COLUMN startedAtUtc TEXT;
PRAGMA user_version = 3;
`
View Source
const M004_TaskSchema = `
ALTER TABLE tasks ADD COLUMN state INTEGER NOT NULL DEFAULT 0 CHECK (state >= 0 AND state <= 2);
PRAGMA user_version = 4;
`
View Source
const M005_TaskSchema = `
ALTER TABLE tasks DROP COLUMN completed;
PRAGMA user_version = 5;
`
View Source
const M006_ProjectSchema = `` /* 127-byte string literal not displayed */
View Source
const M007_TaskProjectsSchema = `` /* 318-byte string literal not displayed */
View Source
const M008_TimeTrackingSchema = `` /* 282-byte string literal not displayed */
View Source
const M009_TaskSchema = `
ALTER TABLE tasks DROP COLUMN startedAtUtc;
PRAGMA user_version = 9;
`
View Source
const M010_TaskDependenciesSchema = `` /* 292-byte string literal not displayed */
View Source
const M011_TaskSchema = `
ALTER TABLE tasks ADD COLUMN next INTEGER NOT NULL DEFAULT 0 CHECK (next >= 0 AND next <= 1);
PRAGMA user_version = 11;
`
View Source
const SQLITE_TIME_FORMAT = "2006-01-02 15:04:05" // SQLite's default timestamp format
View Source
const URGENCY_MAX_AGES = time.Duration(365 * 24 * time.Hour)

Variables

Migrations contains all the migrations that need to be run. Each migration is a SQL statement. The migrations are run in order.

Functions

func BackupDatabase

func BackupDatabase(input, output string) error

BackupDatabase copies the SQLite database file from input to output. This will overwrite any previous backups

Types

type Project

type Project struct {
	ID    int64  `db:"id"`    // Unique identifier
	Title string `db:"title"` // Project title
}

A project may be assigned to a task, and that project may be multiple words.

type Store

type Store struct {
	Con *sqlx.DB // The database connection
}

Store is a wrapper around the database connection

func NewInMemoryStore

func NewInMemoryStore() *Store

NewInMemoryStore creates a new in-memory store, useful for testing

func NewStore

func NewStore(conf *config.SqlConnectionConfig) (*Store, error)

NewStore creates a new store with the given configuration

func (*Store) Close

func (store *Store) Close()

Close the database connection

func (*Store) CompleteTaskById

func (store *Store) CompleteTaskById(taskId int64) (bool, error)

CompleteTaskById marks a task as completed by its ID

func (*Store) CountTasks

func (store *Store) CountTasks(ctx context.Context) (int64, error)

CountTasks returns the total number of tasks in the database

func (*Store) CreateTask

func (store *Store) CreateTask(ctx context.Context, task *Task) (*Task, error)

CreateTask creates a new task in the database

func (*Store) DecreasePriority

func (store *Store) DecreasePriority(ctx context.Context, id int64) (bool, error)

DecreasePriority decreases the priority of a task by its ID (if possible)

func (*Store) DeleteDependenciesForCompletedTask

func (store *Store) DeleteDependenciesForCompletedTask(completedTaskId int64) error

DeleteDependenciesForTask deletes all dependencies for a task

func (*Store) DeleteTaskById

func (store *Store) DeleteTaskById(ctx context.Context, id int64) (bool, error)

DeleteTaskById deletes a task by its ID

func (*Store) FilterByTaskId

func (store *Store) FilterByTaskId(taskId int64, tasks []TaskDetailed) *TaskDetailed

FilterByTaskId returns a task by its ID

func (*Store) GetCumTime

func (store *Store) GetCumTime(ctx context.Context, taskId int64) (int64, error)

GetCumTime will get the cumulative time for a task

func (*Store) GetDependenciesForTask

func (store *Store) GetDependenciesForTask(taskId int64) ([]TaskDependency, error)

GetDependenciesForTask returns all the dependencies for a task

func (*Store) GetTaskById

func (store *Store) GetTaskById(ctx context.Context, taskId int64) (*Task, error)

GetTaskById returns a task by its ID

func (*Store) GetTaskByIdOrPanic

func (store *Store) GetTaskByIdOrPanic(id int64) *Task

GetTaskByIdOrPanic returns a task by its ID or panics (ONLY FOR TESTING)

func (*Store) GetTaskTimes

func (store *Store) GetTaskTimes(ctx context.Context, taskId int64) ([]TaskTime, error)

GetTaskTimes will get all the times for a task

func (*Store) IncreasePriority

func (store *Store) IncreasePriority(ctx context.Context, id int64) (bool, error)

IncreasePriority increases the priority of a task by its ID (if possible)

func (*Store) IsConnected

func (store *Store) IsConnected() bool

IsConnected returns true if the store is connected to the database

func (*Store) ListProjects

func (s *Store) ListProjects() ([]Project, error)

ListProjects returns a list of all projects.

func (*Store) ListTasks

func (store *Store) ListTasks(ctx context.Context) ([]TaskDetailed, error)

ListTasks returns a list of all tasks in the database

func (*Store) MustCreateTxTodo

func (store *Store) MustCreateTxTodo() *sqlx.Tx

func (*Store) ProjectGetIDByNameOrCreateTx

func (s *Store) ProjectGetIDByNameOrCreateTx(tx *sqlx.Tx, title string) (int64, error)

ProjectGetIDByNameOrCreate will get the project ID by name or create it if it does not exist.

func (*Store) ProjectLinkTaskTx

func (s *Store) ProjectLinkTaskTx(tx *sqlx.Tx, projectId, taskId int64) error

ProjectLinkTaskTx will link a task to a project

func (*Store) ProjectTasksList

func (s *Store) ProjectTasksList() ([]TaskProjectLink, error)

ProjectUnlinkTaskTx will unlink a task from a project

func (*Store) RunMigrations

func (store *Store) RunMigrations() error

RunMigrations runs all migrations that have not been run. The PRAGMA user_version is used to determine the current schema version. If the schema version is less than the migration index, the migration is run. If the schema version is greater than the migration index, the migration is skipped.

func (*Store) SchemaVersion

func (store *Store) SchemaVersion() int

SchemaVersion returns the current schema version. The schema version is stored in the PRAGMA user_version.

func (*Store) SetPriority

func (store *Store) SetPriority(ctx context.Context, id int64, priority TaskPriority) (bool, error)

SetPriority sets the priority of a task by its ID

func (*Store) SetTaskState

func (store *Store) SetTaskState(taskId int64, state TaskState) error

SetTaskState sets the state of a task by its ID

func (*Store) SetTaskStateToCompleted

func (store *Store) SetTaskStateToCompleted(taskId int64) error

SetTaskStateToCompleted marks a task as completed by its ID

func (*Store) SetTaskStateToIncomplete

func (store *Store) SetTaskStateToIncomplete(taskId int64) error

SetTaskStateToIncomplete marks a task as incomplete by its ID

func (*Store) SetTaskStateToStarted

func (store *Store) SetTaskStateToStarted(taskId int64) error

SetTaskStateToCompleted marks a task as completed by its ID

func (*Store) StartTrackingTaskTime

func (store *Store) StartTrackingTaskTime(ctx context.Context, taskId int64) error

StartTrackingTaskTime will start tracking time for a task

func (*Store) StopTrackingTaskTime

func (store *Store) StopTrackingTaskTime(ctx context.Context, id int64) error

StopTrackingTaskTime will stop tracking time for a task

func (*Store) TagCreate

func (store *Store) TagCreate(ctx context.Context, name string) (int64, error)

CreateTag will create a new tag in the database (this should not exist)

func (*Store) TagCreateTx

func (store *Store) TagCreateTx(name string, tx *sqlx.Tx) (int64, error)

CreateTagTx will create a new in the database (this should not exist) the transaction is NOT rolled back on err

func (*Store) TagGetByName

func (store *Store) TagGetByName(name string) (*Tag, error)

GetTagByName will get a single row for the tag with the name specified

func (*Store) TagGetByNameTx

func (store *Store) TagGetByNameTx(name string, tx *sqlx.Tx) (*Tag, error)

GetTagByNameTx will get a single row for the tag with then name specified NOTE: the transaction is not rolled back on error

func (*Store) TagLinkTaskCtx

func (store *Store) TagLinkTaskCtx(ctx context.Context, taskId, tagId int64) error

TagLinkTask will link a task to a tag

func (*Store) TagLinkTaskTx

func (store *Store) TagLinkTaskTx(tx *sqlx.Tx, taskId, tagId int64) error

TagLinkTaskTx will link a task to a tag inside of a transaction

func (*Store) TagList

func (store *Store) TagList(ctx context.Context) ([]Tag, error)

Used to list all tags

func (*Store) TagUnlinkTask

func (store *Store) TagUnlinkTask(taskId, tagId int64) error

TagUnlinkTask will unlink a tag

func (*Store) TagUnlinkTaskTx

func (store *Store) TagUnlinkTaskTx(tx *sqlx.Tx, taskId, tagId int64) error

TagUnlinkTask will unlink a tag inside of a transaction

func (*Store) TaskDependsOnTx

func (store *Store) TaskDependsOnTx(tx *sqlx.Tx, taskId int64, dependsOnId int64) error

TaskDependsOn creates a dependency between two tasks

func (*Store) TaskIdExistsAndNotCompleted

func (store *Store) TaskIdExistsAndNotCompleted(tx *sqlx.Tx, taskId int64) bool

TaskIdExistsAndNotCompleted returns true if a task exists and is not completed

func (*Store) TaskToggleNextTx

func (store *Store) TaskToggleNextTx(tx *sqlx.Tx, taskId int64) error

TaskToggleNext toggles the next flag of a task by its ID

type Tag

type Tag struct {
	ID   int    `json:"id" db:"id"`     // Unique identifier of the tag
	Name string `json:"name" db:"name"` // Name of the tag
}

Tag is a struct that represents a tag e.g "+work"

type Task

type Task struct {
	ID           int64          `json:"id" db:"id"`                       // ID of the task
	Title        string         `json:"title" db:"title"`                 // Title of the task
	Priority     TaskPriority   `json:"priority" db:"priority"`           // Priority of the task
	CreatedUtc   string         `json:"createdUtc" db:"createdAtUtc"`     // Created timestamp
	State        TaskState      `json:"state" db:"state"`                 // State of the task
	Description  sql.NullString `json:"description" db:"description"`     // Optional Description of the task (this is not the title)
	Due          sql.NullString `json:"due" db:"dueUtc"`                  // Optional Due Date
	UpdatedAtUtc sql.NullString `json:"updatedAtUtc" db:"updatedAtUtc"`   // Optional UpdatedAtUtc
	CompletedUtc sql.NullString `json:"completedUtc" db:"completedAtUtc"` // Set once the task is marked as complete
	Next         bool           `json:"next" db:"next"`                   // If the tasks is flaged as next to be started on
}

Task represents a task in the database

func (*Task) AgeStr

func (task *Task) AgeStr() string

AgeStr returns the pretty version of the time since the task was created

func (*Task) AgeTime

func (task *Task) AgeTime() time.Duration

AgeTime returns the time since the task was created

func (*Task) PrettyAge

func (task *Task) PrettyAge(duration time.Duration) string

PrettyAge returns the pretty version of the time.Duration

func (*Task) PriorityStr

func (task *Task) PriorityStr() string

PriorityStr returns the string version of the Priority Int

type TaskDependency

type TaskDependency struct {
	TaskID      int64 `db:"taskId"`      // Unique identifier of the task
	DependsOnID int64 `db:"dependsOnId"` // Unique identifier of the task that this task depends on
}

type TaskDetailed

type TaskDetailed struct {
	Task

	ProjectCount    int            `json:"projectCount" db:"projectCount"`       // The number of projects the task is associated with
	ProjectNames    sql.NullString `json:"projectNames" db:"projectNames"`       // The names of projects the task is associated joined using commas
	TagCount        int            `json:"tagCount" db:"tagCount"`               // The number of tags the task is associated with
	TagNames        sql.NullString `json:"tagNames" db:"tagNames"`               // The names of tags the task is associated joined using commas
	FirstStartedUtc sql.NullString `json:"firstStartedUtc" db:"firstStartedUtc"` // When the task was first started (if it ever was)
	CumulativeTime  sql.NullString `json:"cumulativeTime" db:"cumulativeTime"`   // Total time spent on task throughout multiple sessions
	Inprogress      bool           `json:"inprogress" db:"inprogress"`           // If the task is inprogress
	Dependencies    sql.NullString `json:"dependencies" db:"dependencies"`       // Comma serperated list of Dependencies
	Blocked         bool           `json:"blocked" db:"blocked"`                 // If the current task has unmet Dependencies
	Blocking        int            `json:"blocking" db:"blocking"`               // The total number of tasks that this task is blocking
	// contains filtered or unexported fields
}

TaskDetailed represents a task with additional information from other tables

func (*TaskDetailed) IsStarted

func (task *TaskDetailed) IsStarted() bool

IsStarted returns true if the task is in progress

func (*TaskDetailed) PrettyCumTime

func (task *TaskDetailed) PrettyCumTime() string

PrettyCumTime returns the pretty version of the CumulativeTime

func (*TaskDetailed) Urgency

func (task *TaskDetailed) Urgency() float64

Urgency returns the urgency of the task based on the task's properties (will be cached)

func (*TaskDetailed) UrgencyColourAnsiBackground

func (task *TaskDetailed) UrgencyColourAnsiBackground() string

UrgencyColourAnsiBackground returns the ANSI background colour for the task urgency

func (*TaskDetailed) UrgencyColourAnsiForeground

func (task *TaskDetailed) UrgencyColourAnsiForeground() string

UrgencyColourAnsiForeground returns the ANSI foreground colour for the task urgency

func (*TaskDetailed) UrgencyStr

func (task *TaskDetailed) UrgencyStr() string

UrgencyStr returns the string version of the Urgency Float

type TaskPriority

type TaskPriority int // Task priority levels
const (
	TaskPriorityNone TaskPriority = iota // Default priority
	TaskPriorityLow
	TaskPriorityMedium
	TaskPriorityHigh
)
type TaskProjectLink struct {
	TaskID    int64 `db:"taskId"`
	ProjectID int64 `db:"projectId"`
}

TaskProjectLink is a struct that represents a link between a task and a project

type TaskState

type TaskState int // e.g Incomplete, Started, Completed
const (
	TaskStateIncomplete TaskState = iota // Default state
	TaskStateStarted
	TaskStateCompleted
)

type TaskTag

type TaskTag struct {
	ID     int `json:"id" db:"id"`         // Unique identifier of the task-tag link
	TaskID int `json:"taskID" db:"taskID"` // ID of the task
	TagID  int `json:"tagID" db:"tagID"`   // ID of the tag
}

Used to link tasks and tags together

type TaskTime

type TaskTime struct {
	Id           int64          `db:"id"`           // Unique identifier
	TaskId       int64          `db:"taskId"`       // Unique identifier of the task
	StartTimeUtc string         `db:"startTimeUtc"` // Start time in UTC
	EndTimeUtc   sql.NullString `db:"endTimeUtc"`   // End time in UTC
	TotalTime    sql.NullString `db:"totalTime"`    // Total time in seconds
}

TaskTime represents the time tracking for a task

type UrgencyCoefficient

type UrgencyCoefficient float64 // Used to calculate task urgency
const (
	URGENCY_NEXT_TAG_COEFFICIENT        UrgencyCoefficient = 15.0 // +Next
	URGENCY_PRIORITY_HIGH_COEFFICIENT   UrgencyCoefficient = 4.0  // P:High
	URGENCY_PRIORITY_MEDIUM_COEFFICIENT UrgencyCoefficient = 2.0  // P:Med
	URGENCY_PRIORITY_LOW_COEFFICIENT    UrgencyCoefficient = 1.0  // P:Low
	URGENCY_PRIORITY_NONE_COEFFICIENT   UrgencyCoefficient = 0.0  // P:None
	URGENCY_DUE_COEFFICIENT             UrgencyCoefficient = 12.0 // Due:now
	URGENCY_BLOCKING_COEFFICIENT        UrgencyCoefficient = 8.0  // Task Dependencies
	URGENCY_ACTIVE_COEFFICIENT          UrgencyCoefficient = 20.0 // Task is started
	URGENCY_SCHEDULED_COEFFICIENT       UrgencyCoefficient = 5.0  // Task is scheduled
	URGENCY_PROJECT_COEFFICIENT         UrgencyCoefficient = 1.0  // Task has project
	URGENCY_BLOCKED_COEFFICIENT         UrgencyCoefficient = -5.0 // Task is blocked
)

Jump to

Keyboard shortcuts

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