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.

65 lines
1.5 KiB

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