Skip to content

Commit

Permalink
progess
Browse files Browse the repository at this point in the history
  • Loading branch information
Adis Durakovic committed Jan 14, 2024
1 parent d36545b commit fd43b63
Show file tree
Hide file tree
Showing 11 changed files with 423 additions and 242 deletions.
100 changes: 99 additions & 1 deletion app.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/energye/systray"
"github.com/go-co-op/gocron/v2"
"github.com/google/uuid"
"github.com/thoas/go-funk"
"github.com/wailsapp/wails/v2/pkg/runtime"
)

Expand All @@ -20,6 +21,99 @@ type App struct {
settings *Settings
}

type Settings struct {
Config Config `json:"config"`
}

type B2Options struct {
B2AccountId string `json:"b2_account_id"`
B2AccountKey string `json:"b2_account_key"`
}

type AzureOptions struct {
AzureAccountName string `json:"azure_account_name"`
AzureAccountKey string `json:"azure_account_key"`
AzureAccountSas string `json:"azure_account_sas"`
AzureEndpointSuffix string `json:"azure_endpoint_suffix"`
}

type Options struct {
B2Options
AzureOptions
}

type RepositoryType int32

const (
LOCAL RepositoryType = iota
B2 RepositoryType = iota
AWS RepositoryType = iota
AZURE RepositoryType = iota
GOOGLE RepositoryType = iota
)

type Snapshot struct {
Id string `json:"id"`
Time time.Time `json:"time"`
Paths []string `json:"paths"`
Hostname string `json:"hostname"`
Username string `json:"username"`
UID uint32 `json:"uid"`
GID uint32 `json:"gid"`
ShortId string `json:"short_id"`
Tags []string `json:"tags"`
ProgramVersion string `json:"program_version"`
}

type FileDescriptor struct {
Name string `json:"name"`
Type string `json:"type"`
Path string `json:"path"`
Size uint32 `json:"size"`
Mtime string `json:"mtime"`
}

type Mount struct {
RepositoryId string `json:"repository_id"`
Path string `json:"path"`
}

type Repository struct {
Id string `json:"id"`
Name string `json:"name"`
Type RepositoryType `json:"type"`
PruneParams [][]string `json:"prune_params"`
Path string `json:"path"`
Password string `json:"password"`
Options Options `json:"options"`
}

type Backup struct {
Id string `json:"id"`
Path string `json:"path"`
Name string `json:"name"`
Cron string `json:"cron"`
BackupParams [][]string `json:"backup_params"`
Targets []string `json:"targets"`
}

type Schedule struct {
Id string `json:"id"`
Action string `json:"action"`
BackupId string `json:"backup_id"`
ToRepositoryId string `json:"to_repository_id"`
FromRepositoryId string `json:"from_repository_id"`
Cron string `json:"cron"`
Active bool `json:"active"`
}

type Config struct {
Mounts []Mount `json:"mounts"`
Repositories []Repository `json:"repositories"`
Backups []Backup `json:"backups"`
Schedules []Schedule `json:"schedules"`
}

// NewApp creates a new App application struct
func NewApp(restic *Restic, scheduler *Scheduler, settings *Settings) *App {
return &App{restic: restic, scheduler: scheduler, settings: settings}
Expand All @@ -37,7 +131,11 @@ func (a *App) toggleSysTrayIcon() {
gocron.DurationJob(500*time.Millisecond),
gocron.NewTask(func() {
if def {
if len(a.scheduler.RunningJobs) > 0 {
running := funk.Filter(
a.scheduler.Jobs,
func(j Job) bool { return j.Running == true },
).([]Job)
if len(running) > 0 {
systray.SetIcon(active_icon)
}
def = false
Expand Down
20 changes: 18 additions & 2 deletions frontend/components/Schedule/List.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
<h1 class="text-yellow-500 font-bold mb-3"><UIcon name="i-heroicons-clock" class="mr-2" dynamic />Schedules</h1>

<UTable :rows="useSettings().settings?.schedules" :columns="columns" class="bg-gray-950 rounded-xl bg-opacity-50 shadow-lg" @select="">
<template #id-data="{ row }">
<div class="inline-flex items-center gap-1">
<UTooltip :text="row.id"
><span class="text-gray-400">{{ row.id.split('-')[0] }}...</span></UTooltip
>
</div>
</template>
<template #task-data="{ row }">
<div class="inline-flex items-center gap-1">
<span v-if="row.action === 'backup'"
Expand All @@ -25,6 +32,14 @@
<span>{{ useSettings().settings?.repositories.find((r: Repository) => r?.id === row.to_repository_id)?.name || '' }}</span></span
>
</div>
<div v-if="useJobs().scheduleProgress(row.id) !== null">
<UProgress :value="useJobs().scheduleProgress(row.id).percent_done * 100" class="mt-2" color="sky" />
<div class="text-xs opacity-50 flex justify-between mt-2">
<span>{{ useJobs().scheduleProgress(row.id).files_done }}/{{ useJobs().scheduleProgress(row.id).total_files }} files</span>
<span>{{ humanFileSize(useJobs().scheduleProgress(row.id).bytes_done) }}/{{ humanFileSize(useJobs().scheduleProgress(row.id).total_bytes) }}</span>
<span>{{ useJobs().scheduleProgress(row.id).seconds_remaining || 'unknown' }} seconds remaining</span>
</div>
</div>
</template>
<template #status-data="{ row }">
<div v-if="row.active">
Expand Down Expand Up @@ -53,9 +68,10 @@
<script setup lang="ts">
import cronstrue from 'cronstrue'
const columns = [
{ key: 'id', class: 'w-32', label: 'ID' },
{ key: 'status', label: 'Status', class: 'w-32' },
{ key: 'task', label: 'Task' },
{ key: 'cron', label: 'Scheduled' },
{ key: 'task', label: 'Task', class: 'w-128' },
{ key: 'cron', label: 'Scheduled', class: 'w-32' },
{ key: 'actions', class: 'w-10' },
]
Expand Down
13 changes: 11 additions & 2 deletions frontend/composables/useJobs.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
export const useJobs = defineStore('useJobs', () => {
const running = ref<Awaited<ReturnType<typeof GetBackupJobs>>>([])
const running = ref([])
const progress = ref([])

function scheduleIsRunning(id: string) {
return running.value?.find((job: BackupJob) => job.schedule.id === id) ? true : false
return running.value?.find((job: any) => job.id === id) ? true : false
}

function scheduleProgress(id: string): any | null {
const job: any = running.value?.find((job: any) => job.id === id)
if (job) return job.out
return null
}

function repoIsRunning(id: string) {
Expand All @@ -18,7 +25,9 @@ export const useJobs = defineStore('useJobs', () => {

return {
running,
progress,
scheduleIsRunning,
scheduleProgress,
repoIsRunning,
repoIsSynching,
backupIsRunning,
Expand Down
19 changes: 14 additions & 5 deletions frontend/composables/useSocket.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
export const useSocket = defineStore('useSocket', () => {
function init() {
const jobsocket = new WebSocket('ws://127.0.0.1:11278/api/ws')
jobsocket.onmessage = (event) => {
const socket = new WebSocket('ws://127.0.0.1:11278/api/ws')
socket.onmessage = (event) => {
try {
const data = JSON.parse(event.data)
useJobs().running = data.jobs || []
useLogs().out = data.out
useLogs().err = data.err
console.log(data)
useJobs().running =
data.map((j: any) => {
try {
j.out = JSON.parse(j.out)
} catch {
j.out = {}
}
return j
}) || []
// useLogs().out = data.out
// useLogs().err = data.err
} catch (e) {
useJobs().running = []
console.error(e)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/gofiber/contrib/websocket v1.3.0
github.com/gofiber/fiber/v2 v2.51.0
github.com/google/uuid v1.5.0
github.com/thoas/go-funk v0.9.3
github.com/wailsapp/wails/v2 v2.7.1
)

Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,14 @@ github.com/samber/lo v1.38.1/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXn
github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee h1:8Iv5m6xEo1NR1AvpV+7XmhI4r39LGNzwUL4YpMuL5vk=
github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee/go.mod h1:qwtSXrKuJh/zsFQ12yEE89xfCrGKK63Rr7ctU/uCo4g=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/tevino/abool v0.0.0-20220530134649-2bfc934cb23c h1:coVla7zpsycc+kA9NXpcvv2E4I7+ii6L5hZO2S6C3kw=
github.com/tevino/abool v0.0.0-20220530134649-2bfc934cb23c/go.mod h1:qc66Pna1RiIsPa7O4Egxxs9OqkuxDX55zznh9K07Tzg=
github.com/thoas/go-funk v0.9.3 h1:7+nAEx3kn5ZJcnDm2Bh23N2yOtweO14bi//dvRtgLpw=
github.com/thoas/go-funk v0.9.3/go.mod h1:+IWnUfUmFO1+WVYQWQtIJHeRRdaIyyYglZN7xzUPe4Q=
github.com/tkrajina/go-reflector v0.5.6 h1:hKQ0gyocG7vgMD2M3dRlYN6WBBOmdoOzJ6njQSepKdE=
github.com/tkrajina/go-reflector v0.5.6/go.mod h1:ECbqLgccecY5kPmPmXg1MrHW585yMcDkVl6IvJe64T4=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
Expand Down Expand Up @@ -125,6 +128,7 @@ golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
13 changes: 10 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,21 @@ import (
//go:embed all:frontend/.output/public
var assets embed.FS

type ChanMsg struct {
Id string `json:"id"`
Out string `json:"out"`
Schedule Schedule `json:"schedule"`
}

func main() {
errb := bytes.NewBuffer([]byte{})
outb := bytes.NewBuffer([]byte{})
restic := NewRestic(errb, outb)
outputChan := make(chan ChanMsg)
settings := NewSettings()
if scheduler, err := NewScheduler(settings, restic); err == nil {
restic := NewRestic(errb, outb, settings)
if scheduler, err := NewScheduler(settings, restic, &outputChan); err == nil {
(scheduler).RescheduleBackups()
go RunServer(scheduler, restic, settings, errb, outb)
go RunServer(scheduler, restic, settings, errb, outb, &outputChan)
Desktop(scheduler, restic, settings)
} else {
fmt.Println("SCHEDULER ERROR", err)
Expand Down
Loading

0 comments on commit fd43b63

Please sign in to comment.