-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharchive.go
157 lines (128 loc) · 3.06 KB
/
archive.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package models
import (
"fmt"
"log"
"os"
"os/exec"
"time"
"github.com/tonyalaribe/monitor-server/config"
"github.com/tonyalaribe/monitor-server/constants"
"path/filepath"
"github.com/boltdb/bolt"
)
type Archive struct {
UserID string
}
//Create adds a new user to the database
func (a Archive) Create(db *bolt.DB, date time.Time) error {
day := date.Format(time.RFC3339)
archivePath := filepath.Join("archive", a.UserID)
err := os.MkdirAll(archivePath, os.ModePerm)
if err != nil {
return err
}
imagePath := filepath.Join("static", a.UserID, day)
err = os.MkdirAll(imagePath, os.ModePerm)
if err != nil {
return err
}
completeImagePath := filepath.Join(imagePath, "%06d.png")
log.Println(completeImagePath)
completePath := filepath.Join(archivePath, day+".mp4")
log.Println(completePath)
ffmpegCmd := exec.Command("ffmpeg", "-framerate", "1/5", "-i", completeImagePath, "-c:v", "libx264", "-r", "30", "-pix_fmt", "yuv420p", completePath)
err = ffmpegCmd.Run()
if err != nil {
return err
}
tx, err := db.Begin(true)
if err != nil {
return err
}
defer tx.Rollback()
b := tx.Bucket([]byte(constants.ARCHIVE_BUCKET))
aBkt, err := b.CreateBucketIfNotExists([]byte(a.UserID))
if err != nil {
return err
}
err = aBkt.Put([]byte(day), []byte(completePath))
if err != nil {
return err
}
err = tx.Commit()
if err != nil {
return err
}
return nil
}
//GetForDate returns the url to the video responsible for a particular day
func (a Archive) GetForDate(db *bolt.DB, date time.Time) (string, error) {
var resultURL string
day := date.Format(time.RFC3339)
tx, err := db.Begin(true)
if err != nil {
return resultURL, err
}
defer tx.Rollback()
b := tx.Bucket([]byte(constants.ARCHIVE_BUCKET))
aBkt := b.Bucket([]byte(a.UserID))
resultURLByte := aBkt.Get([]byte(day))
return string(resultURLByte), nil
}
func worker(id int, jobs <-chan string, results chan<- error) {
for j := range jobs {
fmt.Println("worker", id, "processing job", j)
archive := Archive{
UserID: j,
}
c := Client{Path: config.Get().DB.TestFile}
err := c.Open()
if err != nil {
results <- err
return
}
defer c.DB.Close()
now := time.Now().AddDate(0, 0, 0)
err = archive.Create(c.DB, time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()))
if err != nil {
results <- err
return
}
results <- nil
}
}
func DoArchiveForYesterday() error {
c := Client{Path: config.Get().DB.TestFile}
err := c.Open()
if err != nil {
log.Println(err)
return err
}
defer c.DB.Close()
allUsers, err := User{}.GetAll(c.DB)
if err != nil {
log.Println(err)
return err
}
maxConcurrentWorkers := 5
jobs := make(chan string, len(allUsers))
results := make(chan error, len(allUsers))
for w := 1; w <= maxConcurrentWorkers; w++ {
go worker(w, jobs, results)
}
for _, user := range allUsers {
jobs <- user
}
close(jobs)
for a := 1; a <= len(allUsers); a++ {
err = <-results
log.Println(err)
}
return nil
}
func DoArchiveForYesterdayWrapper() {
err := DoArchiveForYesterday()
if err != nil {
log.Println(err)
}
}