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.

79 lines
1.4 KiB

  1. package queries
  2. import (
  3. "context"
  4. "git.aiterp.net/rpdata/api/graphql/resolver/types"
  5. "git.aiterp.net/rpdata/api/model/log"
  6. )
  7. // LogsArgs is args for the logs query
  8. type LogsArgs struct {
  9. Filter *struct {
  10. Search *string
  11. Characters *[]string
  12. Channels *[]string
  13. Events *[]string
  14. Open *bool
  15. Limit *int32
  16. }
  17. }
  18. // Logs resolves the logs query
  19. func (r *QueryResolver) Logs(ctx context.Context, args *LogsArgs) ([]*types.LogResolver, error) {
  20. var logs []log.Log
  21. var err error
  22. filter := args.Filter
  23. if filter != nil {
  24. limit := 100
  25. search := ""
  26. if filter.Search != nil {
  27. search = *filter.Search
  28. limit = 0
  29. }
  30. channels := []string(nil)
  31. if filter.Channels != nil {
  32. channels = *filter.Channels
  33. limit = 0
  34. }
  35. characters := []string(nil)
  36. if filter.Characters != nil {
  37. characters = *filter.Characters
  38. limit = 0
  39. }
  40. events := []string(nil)
  41. if filter.Events != nil {
  42. events = *filter.Events
  43. limit = 0
  44. }
  45. if filter.Limit != nil {
  46. limit = int(*filter.Limit)
  47. }
  48. open := filter.Open != nil && *filter.Open == true
  49. logs, err = log.ListSearch(search, channels, characters, events, open, limit)
  50. if err != nil {
  51. return nil, err
  52. }
  53. } else {
  54. logs, err = log.List(100)
  55. if err != nil {
  56. return nil, err
  57. }
  58. }
  59. resolvers := make([]*types.LogResolver, len(logs))
  60. for i := range logs {
  61. resolvers[i] = &types.LogResolver{L: logs[i]}
  62. }
  63. return resolvers, nil
  64. }