-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogs.go
More file actions
231 lines (204 loc) · 5.54 KB
/
Copy pathlogs.go
File metadata and controls
231 lines (204 loc) · 5.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package main
import (
"bufio"
"bytes"
"context"
// To use go:embed
_ "embed"
"fmt"
"log/slog"
"os"
"path/filepath"
"text/template"
"time"
"github.com/goccy/go-json"
"github.com/pkg/errors"
)
//go:embed files/index.html
var indexhtml []byte
func (o *Opt) createServiceLog() error {
day := time.Now().Format("20060102")
path := filepath.Join(o.Data, fmt.Sprintf("log%s.txt", day))
file, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
return err
}
defer file.Close()
return nil
}
func (o *Opt) appendServiceLog(log *ServiceLog) error {
day := log.Time.Format("20060102")
path := filepath.Join(o.Data, fmt.Sprintf("log%s.txt", day))
file, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
return err
}
defer file.Close()
return json.NewEncoder(file).Encode(log)
}
func (o *Opt) loadServiceLog(_ context.Context, d time.Time) (time.Time, []*ServiceLog, []*ServiceLog, error) {
logs := make([]*ServiceLog, 0, 500)
latestLogs := make([]*ServiceLog, 0, 500)
day := d.Format("20060102")
path := filepath.Join(o.Data, fmt.Sprintf("log%s.txt", day))
file, err := os.Open(path)
if err != nil {
return time.Now(), logs, latestLogs, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
lastUpdated := time.Now()
// 各行を読み込み
for scanner.Scan() {
servicelog := &ServiceLog{}
// JSON をデコード
err := json.Unmarshal(scanner.Bytes(), servicelog)
if err != nil {
slog.Warn("Error decoding JSON", slog.Any("error", err))
continue
}
lastUpdated = servicelog.Time
logs = append(logs, servicelog)
// 直近のログ
now := time.Now()
diff := now.Sub(servicelog.Time)
if diff <= o.config.LatestTimeRange.Duration {
latestLogs = append(latestLogs, servicelog)
}
}
// エラーチェック
if err := scanner.Err(); err != nil {
slog.Warn("Error reading file", slog.Any("error", err))
}
return lastUpdated, logs, latestLogs, nil
}
func sameCommand(c, s []string) bool {
if len(c) != len(s) {
return false
}
for i := len(c) - 1; i >= 0; i-- {
if c[i] != s[i] {
return false
}
}
return true
}
func (o *Opt) countByService(logs []*ServiceLog, service *Service) (int, int) {
ok := 0
fail := 0
for _, log := range logs {
// カテゴリ名とサービス名が一致 or コマンドが一緒する行を対象とする
if (log.CategoryName == service.categoryName && log.Name == service.Name) ||
sameCommand(log.Command, service.Command) {
if log.Status == 0 {
ok++
} else {
fail++
}
}
}
return ok, fail
}
func (o *Opt) loadLog(ctx context.Context) {
d := time.Now()
days := make([]string, 0, 10)
days = append(days, o.config.LatestTimeRange.ShortString())
// initialize
o.initializeServices()
// process 7 days of logs
for i := 0; i < 7; i++ {
days = append(days, d.Format("01/02"))
lastUpdated, logs, latestLogs, err := o.loadServiceLog(ctx, d)
d = d.Add(-1 * time.Hour * 24)
if err != nil && !errors.Is(err, os.ErrNotExist) {
slog.Warn("failed to loadlog", slog.Any("error", err))
continue
}
o.updateServiceStatuses(logs, latestLogs, lastUpdated, i == 0, i)
}
o.updateCategoryStatuses()
o.config.Days = days
o.config.LastUpdatedAt = time.Now()
}
// initializeServices resets all services to their default state
func (o *Opt) initializeServices() {
for _, category := range o.config.Categories {
for _, service := range category.Services {
service.LatestStatus = NoDATA
service.LatestStatusAt = time.Now()
service.StatusHistory = []*statusText{
NoDATA, NoDATA, NoDATA, NoDATA,
NoDATA, NoDATA, NoDATA,
}
}
}
}
// updateServiceStatuses calculates and updates status for all services based on logs
func (o *Opt) updateServiceStatuses(
logs []*ServiceLog,
latestLogs []*ServiceLog,
lastUpdated time.Time,
isLatest bool,
dayIndex int,
) {
for _, category := range o.config.Categories {
for _, service := range category.Services {
service.LatestStatusAt = lastUpdated
if isLatest {
ok, fail := o.countByService(latestLogs, service)
service.LatestStatus = o.calculateServiceStatus(ok, fail)
} else {
ok, fail := o.countByService(logs, service)
service.StatusHistory[dayIndex] = o.calculateServiceStatus(ok, fail)
}
}
}
}
// calculateServiceStatus returns the appropriate statusText based on ok and fail counts
func (o *Opt) calculateServiceStatus(ok, fail int) *statusText {
if ok == 0 && fail == 0 {
return NoDATA
} else if fail > 0 {
return Outage
}
return Operational
}
// updateCategoryStatuses calculates category-level status from its services
func (o *Opt) updateCategoryStatuses() {
for _, category := range o.config.Categories {
var operational, outage int
for _, service := range category.Services {
switch {
case service.LatestStatus.IsOperational():
operational++
case service.LatestStatus.IsOutage():
outage++
default:
// NoDATA, do nothing
}
}
category.LatestStatus = o.calculateCategoryStatus(operational, outage)
}
}
// calculateCategoryStatus returns the appropriate category status
func (o *Opt) calculateCategoryStatus(operational, outage int) *statusText {
if outage == 0 && operational > 0 {
return Operational
} else if outage > 0 {
return Outage
}
return NoDATA
}
func (o *Opt) renderStatusPage(ctx context.Context) error {
r := template.Must(template.New("index").Parse(string(indexhtml)))
o.rwlock.Lock()
defer o.rwlock.Unlock()
o.loadLog(ctx)
w := &bytes.Buffer{}
err := r.ExecuteTemplate(w, "index", o.config)
if err != nil {
return err
}
o.htmlBlob = w.Bytes()
return nil
}