Copyright 2020 The Go Authors. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

package worker

import (
	
	
	
	
	
	
	
	
	
	

	
	
	
	
	
	
	
	
)

type annotation struct {
	error
	msg string
}

var startTime = time.Now()
doIndexPage writes the status page. On error it returns the error and a short string to be written back to the client.
func ( *Server) ( http.ResponseWriter,  *http.Request) ( error) {
	defer derrors.Wrap(&, "doIndexPage")
	var (
		 []*internal.Experiment
		    []string
	)
	if .getExperiments != nil {
		 = .getExperiments()
	}
	,  := errgroup.WithContext(.Context())
	.Go(func() error {
		var  error
		,  = .db.GetExcludedPrefixes()
		if  != nil {
			return annotation{, "error fetching excluded"}
		}
		return nil
	})
	if  := .Wait();  != nil {
		var  annotation
		if errors.As(, &) {
			log.Errorf(, .msg, )
		}
		return 
	}

	var  runtime.MemStats
	runtime.ReadMemStats(&)
	,  := getSystemMemStats()
	if  != nil {
		log.Errorf(, "could not get system stats: %v", )
	}
	,  := getProcessMemStats()
	if  != nil {
		log.Errorf(, "could not get process stats: %v", )
	}

	var  string
	if .cfg.OnGKE() {
		 := .cfg.DeploymentEnvironment()
		 := "pkgsite"
		if  != "exp" {
			 =  + "-" + 
		}
		 = `https://pantheon.corp.google.com/logs/query;query=resource.type%3D%22k8s_container%22%20resource.labels.cluster_name%3D%22` +
			 +
			`%22%20resource.labels.container_name%3D%22worker%22?project=` +
			.cfg.ProjectID
	} else {
		 = `https://cloud.google.com/console/logs/viewer?resource=gae_app%2Fmodule_id%2F` + .cfg.ServiceID + `&project=` +
			.cfg.ProjectID
	}
	 := struct {
		          *config.Config
		             string
		  string
		 *time.Time
		      string
		        string
		       time.Time
		     []*internal.Experiment
		        []string
		   fetch.LoadShedStats
		      runtime.MemStats
		    processMemStats
		     systemMemStats
		     map[string]uint64
		         []*fetch.FetchInfo
		         string
	}{
		:         .cfg,
		:            env(.cfg),
		: strings.ToLower(env(.cfg)) + "-",
		:     .cfg.LocationID,
		:       os.Getenv("HOSTNAME"),
		:      startTime,
		:    ,
		:       ,
		:  fetch.ZipLoadShedStats(),
		:     ,
		:   ,
		:    ,
		:    getCgroupMemStats(),
		:        fetch.FetchInfos(),
		:        ,
	}
	return renderPage(, , , .templates[indexTemplate])
}

func ( *Server) ( http.ResponseWriter,  *http.Request) ( error) {
	defer derrors.Wrap(&, "doVersionsPage")
	const  = 20
	,  := errgroup.WithContext(.Context())
	var (
		, ,  []*internal.ModuleVersionState
		                   *postgres.VersionStats
	)
	.Go(func() error {
		var  error
		,  = .db.GetNextModulesToFetch(, )
		if  != nil {
			return annotation{, "error fetching next versions"}
		}
		return nil
	})
	.Go(func() error {
		var  error
		,  = .db.GetRecentFailedVersions(, )
		if  != nil {
			return annotation{, "error fetching recent failures"}
		}
		return nil
	})
	.Go(func() error {
		var  error
		,  = .db.GetRecentVersions(, )
		if  != nil {
			return annotation{, "error fetching recent versions"}
		}
		return nil
	})
	.Go(func() error {
		var  error
		,  = .db.GetVersionStats()
		if  != nil {
			return annotation{, "error fetching stats"}
		}
		return nil
	})
	if  := .Wait();  != nil {
		var  annotation
		if errors.As(, &) {
			log.Errorf(, .msg, )
		}
		return 
	}

	type  struct {
		  int
		  string
		 int
	}
	var  []*
	for ,  := range .VersionCounts {
		 := &{: , : }
		if  := derrors.FromStatus(, "");  != nil &&  != derrors.Unknown {
			. = .Error()
		}
		 = append(, )
	}
	sort.Slice(, func(,  int) bool { return []. < []. })
	 := struct {
		, ,  []*internal.ModuleVersionState
		                       *config.Config
		                          string
		               string
		              *time.Time
		                       []*
	}{
		:            ,
		:          ,
		:  ,
		:          .cfg,
		:             env(.cfg),
		:  strings.ToLower(env(.cfg)) + "-",
		: &.LatestTimestamp,
		:          ,
	}
	return renderPage(, , , .templates[versionsTemplate])
}

func ( *config.Config) string {
	 := .DeploymentEnvironment()
	return strings.ToUpper([:1]) + [1:]
}

func ( context.Context,  http.ResponseWriter,  interface{},  *template.Template) ( error) {
	defer derrors.Wrap(&, "renderPage")
	var  bytes.Buffer
	if  := .Execute(&, );  != nil {
		return 
	}
	if ,  := io.Copy(, &);  != nil {
		log.Errorf(, "Error copying buffer to ResponseWriter: %v", )
		return 
	}
	return nil