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.

85 lines
1.3 KiB

  1. package main
  2. import (
  3. "bufio"
  4. "errors"
  5. "io"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. type logLine struct {
  11. Verb string
  12. Args []string
  13. Text string
  14. }
  15. func parseFile(reader io.Reader) []logLine {
  16. bufReader := bufio.NewReader(reader)
  17. results := make([]logLine, 0, 512)
  18. for {
  19. line, err := bufReader.ReadString('\n')
  20. if err == io.EOF {
  21. if len(line) < 2 {
  22. break
  23. }
  24. } else if err != nil {
  25. break
  26. }
  27. if len(line) <= 2 {
  28. continue
  29. }
  30. line = strings.Replace(line, "\n", "", 1)
  31. line = strings.Replace(line, "\r", "", 1)
  32. results = append(results, parseLine(line))
  33. }
  34. return results
  35. }
  36. func parseLine(line string) logLine {
  37. textSplit := strings.SplitN(line, " :", 2)
  38. tokens := strings.Split(textSplit[0], " ")
  39. ll := logLine{
  40. Verb: tokens[0],
  41. }
  42. if len(tokens) > 1 {
  43. ll.Args = tokens[1:]
  44. }
  45. if len(textSplit) > 1 {
  46. ll.Text = textSplit[1]
  47. }
  48. return ll
  49. }
  50. func parseFilename(fname string) (date time.Time, channel string, err error) {
  51. date, err = time.ParseInLocation("2006-01-02_150405", fname[:17], time.Local)
  52. if err != nil {
  53. return
  54. }
  55. ms, err := strconv.Atoi(fname[17:20])
  56. if err != nil {
  57. return
  58. }
  59. date = date.Add(time.Duration(ms) * time.Millisecond)
  60. if len(fname) < 23 {
  61. err = errors.New("filename too short")
  62. return
  63. }
  64. channel = fname[21:]
  65. return
  66. }