func createApiPeerStates(peerStates map[enum.TrafficMonitorName]peer.Crstates, filter *PeerStateFilter, params url.Values) ApiPeerStates {
	apiPeerStates := ApiPeerStates{
		Peers:       map[enum.TrafficMonitorName]map[enum.CacheName][]CacheState{},
		QueryParams: http_server.ParametersStr(params),
		DateStr:     http_server.DateStr(time.Now()),
	}

	for peer, state := range peerStates {
		if !filter.UsePeer(peer) {
			continue
		}
		if _, ok := apiPeerStates.Peers[peer]; !ok {
			apiPeerStates.Peers[peer] = map[enum.CacheName][]CacheState{}
		}
		peerState := apiPeerStates.Peers[peer]
		for cache, available := range state.Caches {
			if !filter.UseCache(cache) {
				continue
			}
			peerState[cache] = []CacheState{CacheState{Value: available.IsAvailable}}
		}
		apiPeerStates.Peers[peer] = peerState
	}
	return apiPeerStates
}
// StatsMarshall encodes the stats in JSON, encoding up to historyCount of each stat. If statsToUse is empty, all stats are encoded; otherwise, only the given stats are encoded. If wildcard is true, stats which contain the text in each statsToUse are returned, instead of exact stat names. If cacheType is not CacheTypeInvalid, only stats for the given type are returned. If hosts is not empty, only the given hosts are returned.
func StatsMarshall(statHistory map[enum.CacheName][]Result, filter Filter, params url.Values) ([]byte, error) {
	stats := Stats{
		Caches:      map[enum.CacheName]map[string][]Stat{},
		QueryParams: http_server.ParametersStr(params),
		DateStr:     http_server.DateStr(time.Now()),
	}

	// TODO in 1.0, stats are divided into 'location', 'cache', and 'type'. 'cache' are hidden by default.

	for id, history := range statHistory {
		if !filter.UseCache(id) {
			continue
		}
		historyCount := 1
		for _, result := range history {
			if !filter.WithinStatHistoryMax(historyCount) {
				break
			}
			historyCount++
			for stat, value := range result.Astats.Ats {
				stat = "ats." + stat // TM 1.0 prefixes ATS stats with 'ats.'
				if !filter.UseStat(stat) {
					continue
				}
				s := Stat{
					Time:  result.Time.UnixNano() / 1000000,
					Value: value,
				}

				_, exists := stats.Caches[id]

				if !exists {
					stats.Caches[id] = map[string][]Stat{}
				}

				stats.Caches[id][stat] = append(stats.Caches[id][stat], s)
			}
		}
	}

	return json.Marshal(stats)
}
Exemple #3
0
// StatsJSON returns an object formatted as expected to be serialized to JSON and served.
func (dsStats Stats) JSON(filter dsdata.Filter, params url.Values) dsdata.StatsOld {
	now := time.Now().Unix()
	jsonObj := &dsdata.StatsOld{
		DeliveryService: map[enum.DeliveryServiceName]map[dsdata.StatName][]dsdata.StatOld{},
		QueryParams:     http_server.ParametersStr(params),
		DateStr:         http_server.DateStr(time.Now()),
	}

	for deliveryService, stat := range dsStats.DeliveryService {
		if !filter.UseDeliveryService(deliveryService) {
			continue
		}
		jsonObj.DeliveryService[deliveryService] = map[dsdata.StatName][]dsdata.StatOld{}
		jsonObj = addCommonData(jsonObj, &stat.CommonStats, deliveryService, now, filter)
		for cacheGroup, cacheGroupStats := range stat.CacheGroups {
			jsonObj = addStatCacheStats(jsonObj, cacheGroupStats, deliveryService, "location."+string(cacheGroup)+".", now, filter)
		}
		for cacheType, typeStats := range stat.Types {
			jsonObj = addStatCacheStats(jsonObj, typeStats, deliveryService, "type."+cacheType.String()+".", now, filter)
		}
		jsonObj = addStatCacheStats(jsonObj, stat.TotalStats, deliveryService, "total.", now, filter)
	}
	return *jsonObj
}