package store import ( "time" "git.lucasrguerra.dev.br/ciberfauna/urutau/internal/evlog" ) // Gap is a stretch of hours with no event at all from a source that usually has some. type Gap struct { From int64 `json:"from"` To int64 `json:"to"` } // MinGapHours is the shortest silence reported as a hole. const MinGapHours = 2 // HourlyFromTimeline folds a timeline (any step up to one hour, or exactly // one hour) into events per hour for the hours [fromHour, toHour] (hour numbers, // unix/3600). Hours outside the timeline stay zero. func HourlyFromTimeline(tl Timeline, fromHour, toHour int64) []uint32 { if toHour < fromHour { return []uint32{} } n := int(toHour-fromHour) + 1 if n > 24*31 { n = 24 * 31 fromHour = toHour - int64(n) + 1 } out := make([]uint32, n) if tl.Step <= 0 { return out } for _, vals := range tl.Series { for i, v := range vals { if v == 0 { continue } h := (tl.Start + int64(i)*tl.Step) / 3600 if h < fromHour || h > toHour { continue } out[h-fromHour] += v } } return out } // FindGaps lists runs of at least MinGapHours empty hours after the first // busy hour, provided the source is a regular one (at least three busy hours). // Gaps are returned as unix seconds [from, to). func FindGaps(hourly []uint32, fromHour int64) []Gap { busy, first := 0, -1 for i, v := range hourly { if v > 0 { busy++ if first < 0 { first = i } } } gaps := []Gap{} if busy < 3 { return gaps } run := -1 flush := func(end int) { if run >= 0 && end-run >= MinGapHours { gaps = append(gaps, Gap{From: (fromHour + int64(run)) * 3600, To: (fromHour + int64(end)) * 3600}) } run = -1 } for i := first; i < len(hourly); i++ { if hourly[i] == 0 { if run < 0 { run = i } } else { flush(i) } } flush(len(hourly)) return gaps } // LogStats describes the event log on disk. type LogStats struct { Enabled bool `json:"enabled"` Days int `json:"days"` Bytes int64 `json:"bytes"` Dropped uint64 `json:"dropped"` } // LogStats counts the days with events (scan cached for five minutes), the // size on disk and the events dropped by the writer. func (s *Store) LogStats() LogStats { if s.elog == nil { return LogStats{} } k := studyKey{kind: "integrity-log"} d := s.cachedTTL(k, 5*time.Minute, func() any { days := map[int64]bool{} now := s.now().Unix() s.elog.Scan(evlog.Filter{From: now - int64(s.cfg.Retention/time.Second) - 86400, To: now + 86400}, studyBudget, false, func(r evlog.Rec, _ []byte) bool { days[r.T/86400] = true return true }) return len(days) }).(int) return LogStats{Enabled: true, Days: d, Bytes: s.elog.Size(), Dropped: s.elog.Dropped()} } // GeoBuilds, when set by the application (serve.go: geo.Resolver.Status() -> // City.Build / ASN.Build, as `urutau doctor` shows them), returns the build // time of the city and ASN databases in unix seconds (0 when unknown). The // source list the dashboard receives hides paths, so the server cannot open the // geo directory itself. var GeoBuilds func() (city, asn int64)