示例#1
0
文件: plugin.go 项目: jeffweiss/snap
func (s *Server) getPlugins(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
	var detail bool
	// make this a function because DRY
	for k := range r.URL.Query() {
		if k == "details" {
			detail = true
		}
	}

	plugins := new(rbody.PluginList)

	// Cache the catalog here to avoid multiple reads
	plCatalog := s.mm.PluginCatalog()
	plugins.LoadedPlugins = make([]rbody.LoadedPlugin, len(plCatalog))
	for i, p := range s.mm.PluginCatalog() {
		plugins.LoadedPlugins[i] = *catalogedPluginToLoaded(r.Host, p)
	}

	if detail {
		aPlugins := s.mm.AvailablePlugins()
		plugins.AvailablePlugins = make([]rbody.AvailablePlugin, len(aPlugins))
		for i, p := range aPlugins {
			plugins.AvailablePlugins[i] = rbody.AvailablePlugin{
				Name:             p.Name(),
				Version:          p.Version(),
				Type:             p.TypeName(),
				HitCount:         p.HitCount(),
				LastHitTimestamp: p.LastHit().Unix(),
				ID:               p.ID(),
			}
		}
	}

	respond(200, plugins, w)
}
示例#2
0
文件: plugin.go 项目: Collinux/snap
func getPlugins(mm managesMetrics, detail bool, h string, plName string, plType string) *rbody.PluginList {

	plCatalog := mm.PluginCatalog()

	plugins := rbody.PluginList{}

	plugins.LoadedPlugins = make([]rbody.LoadedPlugin, len(plCatalog))
	for i, p := range plCatalog {
		plugins.LoadedPlugins[i] = *catalogedPluginToLoaded(h, p)
	}

	if detail {
		aPlugins := mm.AvailablePlugins()
		plugins.AvailablePlugins = make([]rbody.AvailablePlugin, len(aPlugins))
		for i, p := range aPlugins {
			plugins.AvailablePlugins[i] = rbody.AvailablePlugin{
				Name:             p.Name(),
				Version:          p.Version(),
				Type:             p.TypeName(),
				HitCount:         p.HitCount(),
				LastHitTimestamp: p.LastHit().Unix(),
				ID:               p.ID(),
				Href:             pluginURI(h, p),
			}
		}
	}

	filteredPlugins := rbody.PluginList{}

	if plName != "" {
		for _, p := range plugins.LoadedPlugins {
			if p.Name == plName {
				filteredPlugins.LoadedPlugins = append(filteredPlugins.LoadedPlugins, p)
			}
		}
		for _, p := range plugins.AvailablePlugins {
			if p.Name == plName {
				filteredPlugins.AvailablePlugins = append(filteredPlugins.AvailablePlugins, p)
			}
		}
		// update plugins so that further filters consider previous filters
		plugins = filteredPlugins
	}

	filteredPlugins = rbody.PluginList{}

	if plType != "" {
		for _, p := range plugins.LoadedPlugins {
			if p.Type == plType {
				filteredPlugins.LoadedPlugins = append(filteredPlugins.LoadedPlugins, p)
			}
		}
		for _, p := range plugins.AvailablePlugins {
			if p.Type == plType {
				filteredPlugins.AvailablePlugins = append(filteredPlugins.AvailablePlugins, p)
			}
		}
		// filter based on type
		plugins = filteredPlugins
	}

	return &plugins
}