file_store.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package stores
  2. //
  3. // import (
  4. // "encoding/json"
  5. // "io"
  6. // "os"
  7. // "path/filepath"
  8. // "strings"
  9. //
  10. // log "github.com/alecthomas/log4go"
  11. // "github.com/torkelo/grafana-pro/pkg/models"
  12. // )
  13. //
  14. // type fileStore struct {
  15. // dataDir string
  16. // dashDir string
  17. // cache map[string]*models.Dashboard
  18. // }
  19. //
  20. // func NewFileStore(dataDir string) *fileStore {
  21. //
  22. // if dirDoesNotExist(dataDir) {
  23. // log.Crashf("FileStore failed to initialize, dataDir does not exist %v", dataDir)
  24. // }
  25. //
  26. // dashDir := filepath.Join(dataDir, "dashboards")
  27. //
  28. // if dirDoesNotExist(dashDir) {
  29. // log.Debug("Did not find dashboard dir, creating...")
  30. // err := os.Mkdir(dashDir, 0777)
  31. // if err != nil {
  32. // log.Crashf("FileStore failed to initialize, could not create directory %v, error: %v", dashDir, err)
  33. // }
  34. // }
  35. //
  36. // store := &fileStore{}
  37. // store.dataDir = dataDir
  38. // store.dashDir = dashDir
  39. // store.cache = make(map[string]*models.Dashboard)
  40. // store.scanFiles()
  41. //
  42. // return store
  43. // }
  44. //
  45. // func (store *fileStore) scanFiles() {
  46. // visitor := func(path string, f os.FileInfo, err error) error {
  47. // if err != nil {
  48. // return err
  49. // }
  50. // if f.IsDir() {
  51. // return nil
  52. // }
  53. // if strings.HasSuffix(f.Name(), ".json") {
  54. // err = store.loadDashboardIntoCache(path)
  55. // if err != nil {
  56. // return err
  57. // }
  58. // }
  59. // return nil
  60. // }
  61. //
  62. // err := filepath.Walk(store.dashDir, visitor)
  63. // if err != nil {
  64. // log.Error("FileStore::updateCache failed %v", err)
  65. // }
  66. // }
  67. //
  68. // func (store fileStore) loadDashboardIntoCache(filename string) error {
  69. // log.Info("Loading dashboard file %v into cache", filename)
  70. // dash, err := loadDashboardFromFile(filename)
  71. // if err != nil {
  72. // return err
  73. // }
  74. //
  75. // store.cache[dash.Title] = dash
  76. //
  77. // return nil
  78. // }
  79. //
  80. // func (store *fileStore) Close() {
  81. //
  82. // }
  83. //
  84. // func (store *fileStore) GetById(id string) (*models.Dashboard, error) {
  85. // log.Debug("FileStore::GetById id = %v", id)
  86. // filename := store.getFilePathForDashboard(id)
  87. //
  88. // return loadDashboardFromFile(filename)
  89. // }
  90. //
  91. // func (store *fileStore) Save(dash *models.Dashboard) error {
  92. // filename := store.getFilePathForDashboard(dash.Title)
  93. //
  94. // log.Debug("Saving dashboard %v to %v", dash.Title, filename)
  95. //
  96. // var err error
  97. // var data []byte
  98. // if data, err = json.Marshal(dash.Data); err != nil {
  99. // return err
  100. // }
  101. //
  102. // return writeFile(filename, data)
  103. // }
  104. //
  105. // func (store *fileStore) Query(query string) ([]*models.SearchResult, error) {
  106. // results := make([]*models.SearchResult, 0, 50)
  107. //
  108. // for _, dash := range store.cache {
  109. // item := &models.SearchResult{
  110. // Id: dash.Title,
  111. // Type: "dashboard",
  112. // }
  113. // results = append(results, item)
  114. // }
  115. //
  116. // return results, nil
  117. // }
  118. //
  119. // func loadDashboardFromFile(filename string) (*models.Dashboard, error) {
  120. // log.Debug("FileStore::loading dashboard from file %v", filename)
  121. //
  122. // configFile, err := os.Open(filename)
  123. // if err != nil {
  124. // return nil, err
  125. // }
  126. //
  127. // return models.NewFromJson(configFile)
  128. // }
  129. //
  130. // func (store *fileStore) getFilePathForDashboard(id string) string {
  131. // id = strings.ToLower(id)
  132. // id = strings.Replace(id, " ", "-", -1)
  133. // return filepath.Join(store.dashDir, id) + ".json"
  134. // }
  135. //
  136. // func dirDoesNotExist(dir string) bool {
  137. // _, err := os.Stat(dir)
  138. // return os.IsNotExist(err)
  139. // }
  140. //
  141. // func writeFile(filename string, data []byte) error {
  142. // f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
  143. // if err != nil {
  144. // return err
  145. // }
  146. // n, err := f.Write(data)
  147. // if err == nil && n < len(data) {
  148. // err = io.ErrShortWrite
  149. // }
  150. // if err1 := f.Close(); err == nil {
  151. // err = err1
  152. // }
  153. //
  154. // return err
  155. // }