|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "os/signal" |
| 8 | + "syscall" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/jedib0t/go-pretty/v6/table" |
| 12 | + "github.com/omarabdelaz1z/go-monitor/cmd/config" |
| 13 | + "github.com/omarabdelaz1z/go-monitor/internal/model" |
| 14 | + "github.com/omarabdelaz1z/go-monitor/internal/util" |
| 15 | + |
| 16 | + "github.com/rs/zerolog" |
| 17 | + "golang.org/x/sync/errgroup" |
| 18 | +) |
| 19 | + |
| 20 | +type Service struct { |
| 21 | + snapshots *model.SnapshotModel |
| 22 | + config *config.Config |
| 23 | + logger zerolog.Logger |
| 24 | + |
| 25 | + monthSafeList []string |
| 26 | +} |
| 27 | + |
| 28 | +func (s *Service) Run() error { |
| 29 | + ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) |
| 30 | + defer stop() |
| 31 | + |
| 32 | + g, gCtx := errgroup.WithContext(ctx) |
| 33 | + |
| 34 | + months, err := s.snapshots.GetMonthsInYear(gCtx, fmt.Sprint(time.Now().Year())) |
| 35 | + |
| 36 | + if err != nil { |
| 37 | + return fmt.Errorf("failed to get months: %w", err) |
| 38 | + } |
| 39 | + |
| 40 | + s.monthSafeList = transformMonthName(months...) |
| 41 | + |
| 42 | + g.Go(func() error { |
| 43 | + return s.Respond(gCtx) |
| 44 | + }) |
| 45 | + |
| 46 | + if err = g.Wait(); err != nil { |
| 47 | + s.logger.Error().Err(err).Caller().Msg("service failed while running") |
| 48 | + return fmt.Errorf("service failed while running: %w", err) |
| 49 | + } |
| 50 | + |
| 51 | + return nil |
| 52 | +} |
| 53 | + |
| 54 | +func (s *Service) Respond(ctx context.Context) error { |
| 55 | + t := table.NewWriter() |
| 56 | + t.SetOutputMirror(os.Stdout) |
| 57 | + |
| 58 | + for { |
| 59 | + select { |
| 60 | + case <-ctx.Done(): |
| 61 | + return nil |
| 62 | + default: |
| 63 | + option, _, err := selectPrompt("What would you like to do?", "View today's stats", "View stats for a month", "View all stats", "Exit") |
| 64 | + |
| 65 | + if option == -1 && nil == err { |
| 66 | + return nil |
| 67 | + } |
| 68 | + |
| 69 | + if err != nil { |
| 70 | + return err |
| 71 | + } |
| 72 | + |
| 73 | + switch option { |
| 74 | + case 0: |
| 75 | + err = s.HandleTodayStats(ctx, t) |
| 76 | + |
| 77 | + if err != nil { |
| 78 | + switch err { |
| 79 | + case model.ErrNoRows: |
| 80 | + fmt.Println("No stats for today") |
| 81 | + case model.ErrTimedOut: |
| 82 | + fmt.Println("Timed out while fetching stats") |
| 83 | + default: |
| 84 | + return err |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + t.Render() |
| 89 | + case 1: |
| 90 | + err = s.HandleMonthStats(ctx, t) |
| 91 | + |
| 92 | + if err != nil { |
| 93 | + return err |
| 94 | + } |
| 95 | + |
| 96 | + t.Render() |
| 97 | + case 2: |
| 98 | + err = s.HandleAllStats(ctx, t) |
| 99 | + |
| 100 | + if err != nil { |
| 101 | + return err |
| 102 | + } |
| 103 | + |
| 104 | + t.Render() |
| 105 | + case 3: |
| 106 | + return nil |
| 107 | + } |
| 108 | + |
| 109 | + t.ResetHeaders() |
| 110 | + t.ResetRows() |
| 111 | + t.ResetFooters() |
| 112 | + } |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +func (s *Service) HandleMonthStats(ctx context.Context, t table.Writer) error { |
| 117 | + var ( |
| 118 | + err error |
| 119 | + option string |
| 120 | + dailyStats []model.Snapshot |
| 121 | + ) |
| 122 | + |
| 123 | + _, option, err = selectPrompt("Which month would you like to view?", s.monthSafeList...) |
| 124 | + |
| 125 | + if err != nil { |
| 126 | + return err |
| 127 | + } |
| 128 | + |
| 129 | + if nil == err && option == "" { |
| 130 | + return nil |
| 131 | + } |
| 132 | + |
| 133 | + dailyStats, err = s.snapshots.GetStatsByMonth(ctx, option[:2]) |
| 134 | + |
| 135 | + if err != nil { |
| 136 | + return fmt.Errorf("failed to get stats by month: %w", err) |
| 137 | + } |
| 138 | + |
| 139 | + t.SetCaption(fmt.Sprintf("Stats for %s", option[5:])) |
| 140 | + t.AppendHeader(table.Row{"Date", "Uploaded", "Downloaded", "Total"}) |
| 141 | + |
| 142 | + for i := 0; i < len(dailyStats)-1; i++ { |
| 143 | + t.AppendRow(table.Row{ |
| 144 | + time.Unix(dailyStats[i].Timestamp, 0).Format("2006-01-02"), |
| 145 | + util.ByteCountSI(dailyStats[i].Stat.Sent), |
| 146 | + util.ByteCountSI(dailyStats[i].Stat.Received), |
| 147 | + util.ByteCountSI(dailyStats[i].Stat.Total), |
| 148 | + }) |
| 149 | + } |
| 150 | + |
| 151 | + t.AppendSeparator() |
| 152 | + t.AppendFooter(table.Row{ |
| 153 | + "Cumulative", |
| 154 | + util.ByteCountSI(dailyStats[len(dailyStats)-1].Stat.Sent), |
| 155 | + util.ByteCountSI(dailyStats[len(dailyStats)-1].Stat.Received), |
| 156 | + util.ByteCountSI(dailyStats[len(dailyStats)-1].Stat.Total), |
| 157 | + }) |
| 158 | + |
| 159 | + return nil |
| 160 | +} |
| 161 | + |
| 162 | +func (s *Service) HandleAllStats(ctx context.Context, t table.Writer) error { |
| 163 | + stats, err := s.snapshots.GetAllStats(ctx) |
| 164 | + |
| 165 | + if err != nil { |
| 166 | + return fmt.Errorf("failed to get stats: %w", err) |
| 167 | + } |
| 168 | + |
| 169 | + t.SetCaption("All stats") |
| 170 | + t.AppendHeader(table.Row{"Month", "Uploaded", "Downloaded", "Total"}) |
| 171 | + |
| 172 | + for _, stat := range stats { |
| 173 | + t.AppendRow(table.Row{ |
| 174 | + time.Unix(stat.Timestamp, 0).Format("2006-01-02"), |
| 175 | + util.ByteCountSI(stat.Stat.Sent), |
| 176 | + util.ByteCountSI(stat.Stat.Received), |
| 177 | + util.ByteCountSI(stat.Stat.Total), |
| 178 | + }) |
| 179 | + } |
| 180 | + |
| 181 | + return nil |
| 182 | +} |
| 183 | + |
| 184 | +func (s *Service) HandleTodayStats(ctx context.Context, t table.Writer) error { |
| 185 | + today := time.Now().Format("2006-01-02") |
| 186 | + |
| 187 | + stat, err := s.snapshots.GetStatByDate(ctx, today) |
| 188 | + |
| 189 | + if err != nil { |
| 190 | + return err |
| 191 | + } |
| 192 | + |
| 193 | + t.SetCaption(fmt.Sprintf("Monitored %d hours on %s", stat.HoursMonitored, today)) |
| 194 | + t.AppendHeader(table.Row{"Uploaded", "Downloaded", "Total"}) |
| 195 | + |
| 196 | + t.AppendRow(table.Row{ |
| 197 | + util.ByteCountSI(stat.Stat.Sent), |
| 198 | + util.ByteCountSI(stat.Stat.Received), |
| 199 | + util.ByteCountSI(stat.Stat.Total), |
| 200 | + }) |
| 201 | + |
| 202 | + return nil |
| 203 | +} |
0 commit comments