MCPcopy Index your code
hub / github.com/NdoleStudio/httpsms / Store

Method Store

api/pkg/services/heartbeat_service.go:99–138  ·  view source on GitHub ↗

Store a new entities.Heartbeat

(ctx context.Context, params HeartbeatStoreParams)

Source from the content-addressed store, hash-verified

97
98// Store a new entities.Heartbeat
99func (service *HeartbeatService) Store(ctx context.Context, params HeartbeatStoreParams) (*entities.Heartbeat, error) {
100 ctx, span := service.tracer.Start(ctx)
101 defer span.End()
102
103 ctxLogger := service.tracer.CtxLogger(service.logger, span)
104
105 heartbeat := &entities.Heartbeat{
106 ID: uuid.New(),
107 Owner: params.Owner,
108 Charging: params.Charging,
109 Timestamp: params.Timestamp,
110 Version: params.Version,
111 UserID: params.UserID,
112 }
113
114 if err := service.repository.Store(ctx, heartbeat); err != nil {
115 msg := fmt.Sprintf("cannot save heartbeat with id [%s]", heartbeat.ID)
116 return nil, service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))
117 }
118
119 ctxLogger.Info(fmt.Sprintf("heartbeat saved with id [%s] for user [%s]", heartbeat.ID, heartbeat.UserID))
120
121 monitor, err := service.monitorRepository.Load(ctx, params.UserID, params.Owner)
122 if stacktrace.GetCode(err) == repositories.ErrCodeNotFound {
123 ctxLogger.Info(fmt.Sprintf("heartbeat monitor does not exist for owner [%s] and user [%s]", params.Owner, params.UserID))
124 return heartbeat, nil
125 }
126 if err != nil {
127 msg := fmt.Sprintf("cannot load heartbeat monitor for owner [%s] and user [%s]", params.Owner, params.UserID)
128 ctxLogger.Error(stacktrace.Propagate(err, msg))
129 return heartbeat, nil
130 }
131
132 if monitor.PhoneIsOffline() {
133 ctxLogger.Info(fmt.Sprintf("phone with monitor ID [%s] was offline for user [%s]", monitor.ID, monitor.UserID))
134 service.handleHeartbeatWhenPhoneWasOffline(ctx, params.Source, heartbeat, monitor)
135 }
136
137 return heartbeat, nil
138}
139
140// HeartbeatMonitorStoreParams are parameters for creating a new entities.Heartbeat
141type HeartbeatMonitorStoreParams struct {

Callers

nothing calls this directly

Calls 9

PhoneIsOfflineMethod · 0.80
StartMethod · 0.65
CtxLoggerMethod · 0.65
StoreMethod · 0.65
WrapErrorSpanMethod · 0.65
InfoMethod · 0.65
LoadMethod · 0.65
ErrorMethod · 0.65

Tested by

no test coverage detected