<commit_before>package tools

// In /etc/hummingbird/andrewd-server.conf:
// [unmounted-monitor]
// initial_delay = 1                # seconds to wait between requests for the first pass
// pass_time_target = 600           # seconds to try to make subsequent passes take
// report_interval = 60             # seconds between progress reports
// state_retention = 86400          # seconds to retain state entries
// server_down_limit = 14400        # seconds a server can be down before removal
// device_unmounted_limit = 3600    # seconds a device can be unmounted before removal
// ignore_duration = 14400          # seconds to ignore a device's state change after updating its state in a ring

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"sync/atomic"
	"time"

	"github.com/troubling/hummingbird/common/ring"
	"github.com/uber-go/tally"
	"go.uber.org/zap"
)

type unmountedMonitor struct {
	aa *AutoAdmin
	// delay between each request; adjusted each pass to try to make passes last passTimeTarget
	delay                  time.Duration
	passTimeTarget         time.Duration
	reportInterval         time.Duration
	stateRetention         time.Duration
	serverDownLimit        time.Duration
	deviceUnmountedLimit   time.Duration
	ignoreDuration         time.Duration
	ignore                 map[string]time.Time
	passesMetric           tally.Timer
	queriesMetric          tally.Counter
	errorsMetric           tally.Counter
	serversUpMetric        tally.Counter
	serversDownMetric      tally.Counter
	devicesMountedMetric   tally.Counter
	devicesUnmountedMetric tally.Counter
	serverRemovals         tally.Counter
	deviceRemovals         tally.Counter
}

func newUnmountedMonitor(aa *AutoAdmin) *unmountedMonitor {
	um := &unmountedMonitor{
		aa:                     aa,
		delay:                  time.Duration(aa.serverconf.GetInt("unmounted-monitor", "initial_delay", 10)) * time.Second,
		passTimeTarget:         time.Duration(aa.serverconf.GetInt("unmounted-monitor", "pass_time_target", 600)) * time.Second,
		reportInterval:         time.Duration(aa.serverconf.GetInt("unmounted-monitor", "report_interval", 60)) * time.Second,
		stateRetention:         time.Duration(aa.serverconf.GetInt("unmounted-monitor", "state_retention", 86400)) * time.Second,
		serverDownLimit:        time.Duration(aa.serverconf.GetInt("unmounted-monitor", "server_down_limit", 14400)) * time.Second,
		deviceUnmountedLimit:   time.Duration(aa.serverconf.GetInt("unmounted-monitor", "device_unmounted_limit", 3600)) * time.Second,
		ignoreDuration:         time.Duration(aa.serverconf.GetInt("unmounted-monitor", "ignore_duration", 14400)) * time.Second,
		ignore:                 map[string]time.Time{},
		passesMetric:           aa.metricsScope.Timer("unmounted_passes"),
		queriesMetric:          aa.metricsScope.Counter("unmounted_queries"),
		errorsMetric:           aa.metricsScope.Counter("unmounted_errors"),
		serversUpMetric:        aa.metricsScope.Counter("unmounted_server_up_hits"),
		serversDownMetric:      aa.metricsScope.Counter("unmounted_server_down_hits"),
		devicesMountedMetric:   aa.metricsScope.Counter("unmounted_device_mounted_hits"),
		devicesUnmountedMetric: aa.metricsScope.Counter("unmounted_device_unmounted_hits"),
		serverRemovals:         aa.metricsScope.Counter("unmounted_server_removals"),
		deviceRemovals:         aa.metricsScope.Counter("unmounted_device_removals"),
	}
	if um.delay < 0 {<commit_msg>HUM-1008 Andrewd: Update the default down timeouts for servers<commit_after>package tools

// In /etc/hummingbird/andrewd-server.conf:
// [unmounted-monitor]
// initial_delay = 1                # seconds to wait between requests for the first pass
// pass_time_target = 600           # seconds to try to make subsequent passes take
// report_interval = 60             # seconds between progress reports
// state_retention = 86400          # seconds to retain state entries
// server_down_limit = 172800       # seconds a server can be down before removal
// device_unmounted_limit = 3600    # seconds a device can be unmounted before removal
// ignore_duration = 14400          # seconds to ignore a device's state change after updating its state in a ring

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"sync/atomic"
	"time"

	"github.com/troubling/hummingbird/common/ring"
	"github.com/uber-go/tally"
	"go.uber.org/zap"
)

type unmountedMonitor struct {
	aa *AutoAdmin
	// delay between each request; adjusted each pass to try to make passes last passTimeTarget
	delay                  time.Duration
	passTimeTarget         time.Duration
	reportInterval         time.Duration
	stateRetention         time.Duration
	serverDownLimit        time.Duration
	deviceUnmountedLimit   time.Duration
	ignoreDuration         time.Duration
	ignore                 map[string]time.Time
	passesMetric           tally.Timer
	queriesMetric          tally.Counter
	errorsMetric           tally.Counter
	serversUpMetric        tally.Counter
	serversDownMetric      tally.Counter
	devicesMountedMetric   tally.Counter
	devicesUnmountedMetric tally.Counter
	serverRemovals         tally.Counter
	deviceRemovals         tally.Counter
}

func newUnmountedMonitor(aa *AutoAdmin) *unmountedMonitor {
	um := &unmountedMonitor{
		aa:                     aa,
		delay:                  time.Duration(aa.serverconf.GetInt("unmounted-monitor", "initial_delay", 10)) * time.Second,
		passTimeTarget:         time.Duration(aa.serverconf.GetInt("unmounted-monitor", "pass_time_target", 600)) * time.Second,
		reportInterval:         time.Duration(aa.serverconf.GetInt("unmounted-monitor", "report_interval", 60)) * time.Second,
		stateRetention:         time.Duration(aa.serverconf.GetInt("unmounted-monitor", "state_retention", 86400)) * time.Second,
		serverDownLimit:        time.Duration(aa.serverconf.GetInt("unmounted-monitor", "server_down_limit", 172800)) * time.Second,
		deviceUnmountedLimit:   time.Duration(aa.serverconf.GetInt("unmounted-monitor", "device_unmounted_limit", 3600)) * time.Second,
		ignoreDuration:         time.Duration(aa.serverconf.GetInt("unmounted-monitor", "ignore_duration", 14400)) * time.Second,
		ignore:                 map[string]time.Time{},
		passesMetric:           aa.metricsScope.Timer("unmounted_passes"),
		queriesMetric:          aa.metricsScope.Counter("unmounted_queries"),
		errorsMetric:           aa.metricsScope.Counter("unmounted_errors"),
		serversUpMetric:        aa.metricsScope.Counter("unmounted_server_up_hits"),
		serversDownMetric:      aa.metricsScope.Counter("unmounted_server_down_hits"),
		devicesMountedMetric:   aa.metricsScope.Counter("unmounted_device_mounted_hits"),
		devicesUnmountedMetric: aa.metricsScope.Counter("unmounted_device_unmounted_hits"),
		serverRemovals:         aa.metricsScope.Counter("unmounted_server_removals"),
		deviceRemovals:         aa.metricsScope.Counter("unmounted_device_removals"),
	}
	if um.delay < 0 {
{'fraction_non_alphanumeric': 0.0944267975743575, 'fraction_numerical': 0.015882183078255847, 'mean_word_length': 3.4149139579349903, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3797132', 'n_tokens_mistral': 2204, 'n_tokens_neox': 1992, 'n_words': 416}