Return repo-specific data in REST interface

This commit is contained in:
Jakob Borg 2014-04-09 22:03:30 +02:00
parent 346b6f4f11
commit c42a6b511c
2 changed files with 30 additions and 54 deletions

View File

@ -29,11 +29,11 @@ func startGUI(cfg GUIConfiguration, m *Model) {
router := martini.NewRouter() router := martini.NewRouter()
router.Get("/", getRoot) router.Get("/", getRoot)
router.Get("/rest/version", restGetVersion) router.Get("/rest/version", restGetVersion)
router.Get("/rest/model", restGetModel) router.Get("/rest/model/:repo", restGetModel)
router.Get("/rest/connections", restGetConnections) router.Get("/rest/connections", restGetConnections)
router.Get("/rest/config", restGetConfig) router.Get("/rest/config", restGetConfig)
router.Get("/rest/config/sync", restGetConfigInSync) router.Get("/rest/config/sync", restGetConfigInSync)
router.Get("/rest/need", restGetNeed) router.Get("/rest/need/:repo", restGetNeed)
router.Get("/rest/system", restGetSystem) router.Get("/rest/system", restGetSystem)
router.Get("/rest/errors", restGetErrors) router.Get("/rest/errors", restGetErrors)
@ -73,20 +73,20 @@ func restGetVersion() string {
return Version return Version
} }
func restGetModel(m *Model, w http.ResponseWriter) { func restGetModel(m *Model, w http.ResponseWriter, params martini.Params) {
var repo = params["repo"]
var res = make(map[string]interface{}) var res = make(map[string]interface{})
globalFiles, globalDeleted, globalBytes := m.GlobalSize() globalFiles, globalDeleted, globalBytes := m.GlobalSize(repo)
res["globalFiles"], res["globalDeleted"], res["globalBytes"] = globalFiles, globalDeleted, globalBytes res["globalFiles"], res["globalDeleted"], res["globalBytes"] = globalFiles, globalDeleted, globalBytes
localFiles, localDeleted, localBytes := m.LocalSize() localFiles, localDeleted, localBytes := m.LocalSize(repo)
res["localFiles"], res["localDeleted"], res["localBytes"] = localFiles, localDeleted, localBytes res["localFiles"], res["localDeleted"], res["localBytes"] = localFiles, localDeleted, localBytes
inSyncFiles, inSyncBytes := m.InSyncSize() needFiles, needBytes := m.NeedSize(repo)
res["inSyncFiles"], res["inSyncBytes"] = inSyncFiles, inSyncBytes res["needFiles"], res["needBytes"] = needFiles, needBytes
files, total := m.NeedFiles() res["inSyncFiles"], res["inSyncBytes"] = globalFiles-needFiles, globalBytes-needBytes
res["needFiles"], res["needBytes"] = len(files), total
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(res) json.NewEncoder(w).Encode(res)
@ -142,8 +142,9 @@ func (f guiFile) MarshalJSON() ([]byte, error) {
}) })
} }
func restGetNeed(m *Model, w http.ResponseWriter) { func restGetNeed(m *Model, w http.ResponseWriter, params martini.Params) {
files, _ := m.NeedFiles() repo := params["repo"]
files := m.NeedFilesRepo(repo)
gfs := make([]guiFile, len(files)) gfs := make([]guiFile, len(files))
for i, f := range files { for i, f := range files {
gfs[i] = guiFile(f) gfs[i] = guiFile(f)

View File

@ -158,70 +158,45 @@ func sizeOf(fs []scanner.File) (files, deleted int, bytes int64) {
// GlobalSize returns the number of files, deleted files and total bytes for all // GlobalSize returns the number of files, deleted files and total bytes for all
// files in the global model. // files in the global model.
func (m *Model) GlobalSize() (files, deleted int, bytes int64) { func (m *Model) GlobalSize(repo string) (files, deleted int, bytes int64) {
m.rmut.RLock() m.rmut.RLock()
var fs []scanner.File defer m.rmut.RUnlock()
for _, rf := range m.repoFiles { if rf, ok := m.repoFiles[repo]; ok {
fs = append(fs, rf.Global()...) return sizeOf(rf.Global())
} }
m.rmut.RUnlock() return 0, 0, 0
return sizeOf(fs)
} }
// LocalSize returns the number of files, deleted files and total bytes for all // LocalSize returns the number of files, deleted files and total bytes for all
// files in the local repository. // files in the local repository.
func (m *Model) LocalSize() (files, deleted int, bytes int64) { func (m *Model) LocalSize(repo string) (files, deleted int, bytes int64) {
m.rmut.RLock() m.rmut.RLock()
var fs []scanner.File defer m.rmut.RUnlock()
for _, rf := range m.repoFiles { if rf, ok := m.repoFiles[repo]; ok {
fs = append(fs, rf.Have(cid.LocalID)...) return sizeOf(rf.Have(cid.LocalID))
} }
m.rmut.RUnlock() return 0, 0, 0
return sizeOf(fs)
}
// InSyncSize returns the number and total byte size of the local files that
// are in sync with the global model.
func (m *Model) InSyncSize() (files int, bytes int64) {
var gf []scanner.File
var nf []scanner.File
m.rmut.RLock()
for _, rf := range m.repoFiles {
gf = append(gf, rf.Global()...)
nf = append(nf, rf.Need(cid.LocalID)...)
}
m.rmut.RUnlock()
gn, _, gb := sizeOf(gf)
nn, _, nb := sizeOf(nf)
return gn - nn, gb - nb
} }
// NeedFiles returns the list of currently needed files and the total size. // NeedFiles returns the list of currently needed files and the total size.
func (m *Model) NeedFiles() ([]scanner.File, int64) { func (m *Model) NeedSize(repo string) (files int, bytes int64) {
var nf []scanner.File var nf = m.NeedFilesRepo(repo)
m.rmut.RLock()
for _, rf := range m.repoFiles {
nf = append(nf, rf.Need(cid.LocalID)...)
}
m.rmut.RUnlock()
var bytes int64
for _, f := range nf { for _, f := range nf {
bytes += f.Size bytes += f.Size
} }
return nf, bytes return len(nf), bytes
} }
// NeedFiles returns the list of currently needed files and the total size. // NeedFiles returns the list of currently needed files and the total size.
func (m *Model) NeedFilesRepo(repo string) []scanner.File { func (m *Model) NeedFilesRepo(repo string) []scanner.File {
m.rmut.RLock() m.rmut.RLock()
nf := m.repoFiles[repo].Need(cid.LocalID) defer m.rmut.RUnlock()
m.rmut.RUnlock() if rf, ok := m.repoFiles[repo]; ok {
return nf return rf.Need(cid.LocalID)
}
return nil
} }
// Index is called when a new node is connected and we receive their full index. // Index is called when a new node is connected and we receive their full index.