@@ -15,13 +15,17 @@ import (
1515type AlertIssueCache interface {
1616 WriteIssue (ctx context.Context , r * AlertIssueRow , a AMAlert ) error
1717 TryMarkIdempotent (ctx context.Context , a AMAlert ) (bool , error )
18+ WriteServiceState (ctx context.Context , service , version string , reportAt time.Time , healthState string ) error
1819}
1920
2021// NoopCache is a no-op implementation of AlertIssueCache.
2122type NoopCache struct {}
2223
2324func (NoopCache ) WriteIssue (ctx context.Context , r * AlertIssueRow , a AMAlert ) error { return nil }
2425func (NoopCache ) TryMarkIdempotent (ctx context.Context , a AMAlert ) (bool , error ) { return true , nil }
26+ func (NoopCache ) WriteServiceState (ctx context.Context , service , version string , reportAt time.Time , healthState string ) error {
27+ return nil
28+ }
2529
2630// Cache implements AlertIssueCache using Redis.
2731type Cache struct { R * redis.Client }
@@ -79,3 +83,30 @@ func (c *Cache) TryMarkIdempotent(ctx context.Context, a AMAlert) (bool, error)
7983 ok , err := c .R .SetNX (ctx , k , "1" , 10 * time .Minute ).Result ()
8084 return ok , err
8185}
86+
87+ // WriteServiceState writes the service state snapshot into Redis and maintains simple indices.
88+ func (c * Cache ) WriteServiceState (ctx context.Context , service , version string , reportAt time.Time , healthState string ) error {
89+ if c == nil || c .R == nil {
90+ return nil
91+ }
92+ s := strings .TrimSpace (service )
93+ v := strings .TrimSpace (version )
94+ key := "service_state:" + s + ":" + v
95+ payload := map [string ]any {
96+ "service" : s ,
97+ "version" : v ,
98+ "report_at" : reportAt ,
99+ "health_state" : healthState ,
100+ }
101+ b , _ := json .Marshal (payload )
102+ pipe := c .R .Pipeline ()
103+ pipe .Set (ctx , key , b , 72 * time .Hour )
104+ if s != "" {
105+ pipe .SAdd (ctx , "service_state:index:service:" + s , key )
106+ }
107+ if healthState != "" {
108+ pipe .SAdd (ctx , "service_state:index:health:" + healthState , key )
109+ }
110+ _ , err := pipe .Exec (ctx )
111+ return err
112+ }
0 commit comments