GraphQL API and utilities for the rpdata project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.1 KiB

  1. package logs
  2. import (
  3. "fmt"
  4. "time"
  5. "git.aiterp.net/rpdata/api/internal/store"
  6. "git.aiterp.net/rpdata/api/models"
  7. "github.com/globalsign/mgo"
  8. )
  9. var collection *mgo.Collection
  10. var postCollection *mgo.Collection
  11. func find(query interface{}) (models.Log, error) {
  12. log := models.Log{}
  13. err := collection.Find(query).One(&log)
  14. if err != nil {
  15. return models.Log{}, err
  16. }
  17. return log, nil
  18. }
  19. func list(query interface{}, limit int) ([]models.Log, error) {
  20. logs := make([]models.Log, 0, 64)
  21. err := collection.Find(query).Limit(limit).Sort("-date").All(&logs)
  22. if err != nil {
  23. return nil, err
  24. }
  25. return logs, nil
  26. }
  27. func iter(query interface{}, limit int) *mgo.Iter {
  28. return collection.Find(query).Sort("-date").Limit(limit).Batch(8).Iter()
  29. }
  30. func makeLogID(date time.Time, channel string) string {
  31. return fmt.Sprintf("%s%03d_%s", date.UTC().Format("2006-01-02_150405"), (date.Nanosecond() / int(time.Millisecond/time.Nanosecond)), channel[1:])
  32. }
  33. func init() {
  34. store.HandleInit(func(db *mgo.Database) {
  35. collection = db.C("logbot3.logs")
  36. postCollection = db.C("logbot3.posts")
  37. })
  38. }