@@ -142,26 +142,25 @@ ON CONFLICT (check_id, service_id) DO UPDATE SET
142142`
143143
144144 var (
145- details []byte
146- errorKind * string
147- errorMessage * string
145+ rawDetails []byte
146+ rawErrorKind * string
147+ rawErrorMessage * string
148148 )
149149
150150 if state .LastDetails != nil {
151151 data , err := json .Marshal (state .LastDetails )
152152 if err != nil {
153153 return err
154154 }
155- details = data
155+ rawDetails = data
156156 }
157157
158158 if state .LastErrorKind != e .ErrNone {
159- value := string (state .LastErrorKind )
160- errorKind = & value
159+ rawErrorKind = new (string (state.LastErrorKind ))
161160 }
162161
163162 if state .LastErrorMessage != "" {
164- errorMessage = & state .LastErrorMessage
163+ rawErrorMessage = & state .LastErrorMessage
165164 }
166165
167166 _ , err := s .db .Exec (ctx , query ,
@@ -171,10 +170,10 @@ ON CONFLICT (check_id, service_id) DO UPDATE SET
171170 state .Status ,
172171 state .LastExecutionID ,
173172 state .LastStatus ,
174- errorKind ,
175- errorMessage ,
173+ rawErrorKind ,
174+ rawErrorMessage ,
176175 state .LastDuration .Microseconds (),
177- details ,
176+ rawDetails ,
178177 state .LastSuccessAt ,
179178 state .LastFailureAt ,
180179 state .ConsecutiveSuccesses ,
@@ -185,6 +184,92 @@ ON CONFLICT (check_id, service_id) DO UPDATE SET
185184 return err
186185}
187186
187+ func (s * StateCheck ) ListCheckStatesByService (
188+ ctx context.Context ,
189+ serviceID string ,
190+ ) ([]model.CheckState , error ) {
191+ query := `
192+ SELECT
193+ check_id,
194+ check_type,
195+ status,
196+ last_execution_id,
197+ last_status,
198+ last_error_kind,
199+ last_error_message,
200+ last_duration,
201+ last_details,
202+ last_success_at,
203+ last_failure_at,
204+ consecutive_successes,
205+ consecutive_failures,
206+ updated_at
207+ FROM pulse.check_states
208+ WHERE service_id = $1
209+ ORDER BY check_id
210+ `
211+
212+ var result []model.CheckState
213+ rows , err := s .db .Query (ctx , query , serviceID )
214+ if err != nil {
215+ return nil , err
216+ }
217+ defer rows .Close ()
218+
219+ for rows .Next () {
220+ var (
221+ rawDetails []byte
222+ rawErrorKind * string
223+ rawErrorMessage * string
224+ rawDurationUs int64
225+ )
226+ state := model.CheckState {ServiceID : serviceID }
227+ err = rows .Scan (
228+ & state .CheckID ,
229+ & state .CheckType ,
230+ & state .Status ,
231+ & state .LastExecutionID ,
232+ & state .LastStatus ,
233+ & rawErrorKind ,
234+ & rawErrorMessage ,
235+ & rawDurationUs ,
236+ & rawDetails ,
237+ & state .LastSuccessAt ,
238+ & state .LastFailureAt ,
239+ & state .ConsecutiveSuccesses ,
240+ & state .ConsecutiveFailures ,
241+ & state .UpdatedAt ,
242+ )
243+ if err != nil {
244+ return nil , err
245+ }
246+
247+ if rawErrorKind != nil {
248+ state .LastErrorKind = e .ErrorKind (* rawErrorKind )
249+ }
250+
251+ if rawErrorMessage != nil {
252+ state .LastErrorMessage = * rawErrorMessage
253+ }
254+
255+ if rawDetails != nil {
256+ if err = json .Unmarshal (rawDetails , & state .LastDetails ); err != nil {
257+ return nil , err
258+ }
259+ }
260+
261+ state .LastDuration = time .Duration (rawDurationUs ) * time .Microsecond
262+
263+ result = append (result , state )
264+ }
265+
266+ if err = rows .Err (); err != nil {
267+ return nil , err
268+ }
269+
270+ return result , nil
271+ }
272+
188273func NewStateRepository (db repository.QueryExecutor ) * StateCheck {
189274 return & StateCheck {db }
190275}
0 commit comments