(ctx context.Context, datapoints []*proto.PushResourcesMonitoringUsageRequest_Datapoint)
| 184 | } |
| 185 | |
| 186 | func (a *ResourcesMonitoringAPI) monitorVolumes(ctx context.Context, datapoints []*proto.PushResourcesMonitoringUsageRequest_Datapoint) error { |
| 187 | outOfDiskVolumes := make([]map[string]any, 0) |
| 188 | |
| 189 | for i, monitor := range a.volumeMonitors { |
| 190 | if !monitor.Enabled { |
| 191 | continue |
| 192 | } |
| 193 | |
| 194 | usageDatapoints := make([]*proto.PushResourcesMonitoringUsageRequest_Datapoint_VolumeUsage, 0, len(datapoints)) |
| 195 | for _, datapoint := range datapoints { |
| 196 | var usage *proto.PushResourcesMonitoringUsageRequest_Datapoint_VolumeUsage |
| 197 | |
| 198 | for _, volume := range datapoint.Volumes { |
| 199 | if volume.Volume == monitor.Path { |
| 200 | usage = volume |
| 201 | break |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | usageDatapoints = append(usageDatapoints, usage) |
| 206 | } |
| 207 | |
| 208 | usageStates := resourcesmonitor.CalculateVolumeUsageStates(monitor, usageDatapoints) |
| 209 | |
| 210 | oldState := monitor.State |
| 211 | newState := resourcesmonitor.NextState(a.Config, oldState, usageStates) |
| 212 | |
| 213 | debouncedUntil, shouldNotify := monitor.Debounce(a.Debounce, a.Clock.Now(), oldState, newState) |
| 214 | |
| 215 | if shouldNotify { |
| 216 | outOfDiskVolumes = append(outOfDiskVolumes, map[string]any{ |
| 217 | "path": monitor.Path, |
| 218 | "threshold": fmt.Sprintf("%d%%", monitor.Threshold), |
| 219 | }) |
| 220 | } |
| 221 | |
| 222 | //nolint:gocritic // We need to be able to update the resource monitor here. |
| 223 | if err := a.Database.UpdateVolumeResourceMonitor(dbauthz.AsResourceMonitor(ctx), database.UpdateVolumeResourceMonitorParams{ |
| 224 | AgentID: a.AgentID, |
| 225 | Path: monitor.Path, |
| 226 | State: newState, |
| 227 | UpdatedAt: dbtime.Time(a.Clock.Now()), |
| 228 | DebouncedUntil: dbtime.Time(debouncedUntil), |
| 229 | }); err != nil { |
| 230 | return xerrors.Errorf("update workspace monitor: %w", err) |
| 231 | } |
| 232 | |
| 233 | // Update cached state |
| 234 | a.volumeMonitors[i].State = newState |
| 235 | a.volumeMonitors[i].DebouncedUntil = dbtime.Time(debouncedUntil) |
| 236 | a.volumeMonitors[i].UpdatedAt = dbtime.Time(a.Clock.Now()) |
| 237 | } |
| 238 | |
| 239 | if len(outOfDiskVolumes) == 0 { |
| 240 | return nil |
| 241 | } |
| 242 | |
| 243 | workspace, err := a.Database.GetWorkspaceByID(ctx, a.WorkspaceID) |
no test coverage detected