func createCacheStatuses()

in traffic_monitor/datareq/cachestate.go [120:267]


func createCacheStatuses(
	cacheTypes map[tc.CacheName]tc.CacheType,
	statInfoHistory cache.ResultInfoHistory,
	statResultHistory threadsafe.ResultStatHistory,
	healthHistory map[tc.CacheName][]cache.Result,
	lastHealthDurations map[tc.CacheName]time.Duration,
	localCacheStatusThreadsafe threadsafe.CacheAvailableStatus,
	statMaxKbpses threadsafe.CacheKbpses,
	servers map[string]tc.TrafficServer,
) map[string]CacheStatus {
	conns := createCacheConnections(statResultHistory)
	statii := make(map[string]CacheStatus, len(servers))
	localCacheStatus := localCacheStatusThreadsafe.Get().Copy() // TODO test whether copy is necessary
	maxKbpses := statMaxKbpses.Get()

	for cacheName, serverInfo := range servers {
		interfaceStatuses := make(map[string]CacheInterfaceStatus, len(serverInfo.Interfaces))

		var totalMaxKbps float64 = 0
		maxKbps, maxKbpsOk := maxKbpses[cacheName]
		if !maxKbpsOk {
			log.Infof("Cache server '%s' not in max kbps cache", cacheName)
		} else {
			totalMaxKbps = float64(maxKbps)
		}

		health, healthOk := healthHistory[tc.CacheName(cacheName)]
		if !healthOk {
			log.Infof("Cache server '%s' not in max kbps cache", cacheName)
		} else if len(health) < 1 {
			log.Infof("No health data history for cache server '%s'", cacheName)
			healthOk = false
		}

		var totalKbps float64 = 0

		cacheStatus, statusOk := localCacheStatus[cacheName]
		poller := "unknown"
		if !statusOk {
			log.Warnf("No cache status found for cache '%s'", cacheName)
		} else {
			poller = cacheStatus.Poller
		}
		for _, inf := range serverInfo.Interfaces {
			interfaceName := inf.Name

			infStatus := CacheInterfaceStatus{
				Available:     false,
				BandwidthKbps: 0,
				Status:        NotFoundStatus,
				StatusPoller:  poller,
			}

			if healthOk {
				infStatus.Status, infStatus.Available = interfaceStatus(inf, health[0])
				if infVit, ok := health[0].InterfaceVitals[inf.Name]; ok {
					infStatus.BandwidthKbps = float64(infVit.KbpsOut)
					totalKbps += infStatus.BandwidthKbps
				} else {
					log.Infof("Cache server '%s' interface '%s' not in last health measurement.", cacheName, inf.Name)
				}
			}

			if serverInfo.ServerStatus == tc.CacheStatusOnline.String() {
				infStatus.Status = OnlineStatus
				infStatus.Available = true
			}

			interfaceStatuses[interfaceName] = infStatus
		}

		var connections int64 = 0
		connectionsVal, ok := conns[cacheName]
		if !ok {
			log.Infof("Cache server '%s' not in connections.", cacheName)
		} else {
			connections = connectionsVal
		}

		cacheTypeStr := ""
		if cacheType, ok := cacheTypes[tc.CacheName(cacheName)]; !ok {
			log.Infof("Error getting cache type for %v: not in types\n", cacheName)
		} else {
			cacheTypeStr = string(cacheType)
		}

		loadAverage := 0.0
		if infoHistory, ok := statInfoHistory[tc.CacheName(cacheName)]; !ok {
			log.Infof("createCacheStatuses stat info history missing cache %s\n", cacheName)
		} else if len(infoHistory) < 1 {
			log.Infof("createCacheStatuses stat info history empty for cache %s\n", cacheName)
		} else {
			loadAverage = infoHistory[0].Vitals.LoadAvg
		}

		healthQueryTime, err := latestQueryTimeMS(cacheName, lastHealthDurations)
		if err != nil {
			log.Infof("Error getting cache %v health query time: %v\n", cacheName, err)
		}

		statTime, err := latestResultInfoTimeMS(tc.CacheName(cacheName), statInfoHistory)
		if err != nil {
			log.Infof("Error getting cache %v stat result time: %v\n", cacheName, err)
		}

		healthTime, err := latestResultTimeMS(tc.CacheName(cacheName), healthHistory)
		if err != nil {
			log.Infof("Error getting cache %v health result time: %v\n", cacheName, err)
		}

		statSpan, err := infoResultSpanMS(tc.CacheName(cacheName), statInfoHistory)
		if err != nil {
			log.Infof("Error getting cache %v stat span: %v\n", cacheName, err)
		}

		healthSpan, err := resultSpanMS(tc.CacheName(cacheName), healthHistory)
		if err != nil {
			log.Infof("Error getting cache %v health span: %v\n", cacheName, err)
		}

		if serverInfo.ServerStatus == tc.CacheStatusOnline.String() {
			cacheStatus.Why = "ONLINE - available"
			cacheStatus.Available.IPv4 = serverInfo.IPv4() != ""
			cacheStatus.Available.IPv6 = serverInfo.IPv6() != ""
			cacheStatus.ProcessedAvailable = cacheStatus.Available.IPv4 || cacheStatus.Available.IPv6
		}

		statii[cacheName] = CacheStatus{
			Type:                   &cacheTypeStr,
			LoadAverage:            &loadAverage,
			QueryTimeMilliseconds:  &healthQueryTime,
			StatTimeMilliseconds:   &statTime,
			HealthTimeMilliseconds: &healthTime,
			StatSpanMilliseconds:   &statSpan,
			HealthSpanMilliseconds: &healthSpan,
			BandwidthKbps:          &totalKbps,
			BandwidthCapacityKbps:  &totalMaxKbps,
			ConnectionCount:        &connections,
			Status:                 &cacheStatus.Why,
			StatusPoller:           &poller,
			IPv4Available:          &cacheStatus.Available.IPv4,
			IPv6Available:          &cacheStatus.Available.IPv6,
			CombinedAvailable:      &cacheStatus.ProcessedAvailable,
			Interfaces:             &interfaceStatuses,
		}
	}
	return statii
}