forked from gtestault/freeflix
/
monitoring.go
36 lines (31 loc) · 926 Bytes
/
monitoring.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
package torrent
import (
"fmt"
log "github.com/sirupsen/logrus"
"html/template"
"net/http"
"os"
"path/filepath"
"strconv"
)
var statusTemplate *template.Template
func init() {
cwd, _ := os.Getwd()
fp := filepath.Join(cwd, "./torrent/templates/status.html")
statusTemplate = template.Must(
template.
New("status.html").
Funcs(template.FuncMap{"progress": downloadProgressString}).
ParseFiles(fp))
}
//Status serves a html with an overview of all active torrents.
func (c *Client) Status(w http.ResponseWriter, r *http.Request) {
if err := statusTemplate.Execute(w, c); err != nil {
log.Error(fmt.Errorf("error while displaying status: %v", err))
}
}
func downloadProgressString(completed int64, missing int64) string {
mbCompleted := strconv.FormatInt(completed/1000000, 10)
mbTotal := strconv.FormatInt((completed+missing)/1000000, 10)
return fmt.Sprintf("%s / %s Mb", mbCompleted, mbTotal)
}