"Fossies" - the Fresh Open Source Software Archive

Member "gogs-0.12.3/internal/route/admin/admin.go" (7 Oct 2020, 7336 Bytes) of package /linux/misc/gogs-0.12.3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Go source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 // Copyright 2014 The Gogs Authors. All rights reserved.
    2 // Use of this source code is governed by a MIT-style
    3 // license that can be found in the LICENSE file.
    4 
    5 package admin
    6 
    7 import (
    8     "fmt"
    9     "runtime"
   10     "strings"
   11     "time"
   12 
   13     "github.com/json-iterator/go"
   14 
   15     "gogs.io/gogs/internal/conf"
   16     "gogs.io/gogs/internal/context"
   17     "gogs.io/gogs/internal/cron"
   18     "gogs.io/gogs/internal/db"
   19     "gogs.io/gogs/internal/email"
   20     "gogs.io/gogs/internal/process"
   21     "gogs.io/gogs/internal/tool"
   22 )
   23 
   24 const (
   25     tmplDashboard = "admin/dashboard"
   26     tmplConfig    = "admin/config"
   27     tmplMonitor   = "admin/monitor"
   28 )
   29 
   30 // initTime is the time when the application was initialized.
   31 var initTime = time.Now()
   32 
   33 var sysStatus struct {
   34     Uptime       string
   35     NumGoroutine int
   36 
   37     // General statistics.
   38     MemAllocated string // bytes allocated and still in use
   39     MemTotal     string // bytes allocated (even if freed)
   40     MemSys       string // bytes obtained from system (sum of XxxSys below)
   41     Lookups      uint64 // number of pointer lookups
   42     MemMallocs   uint64 // number of mallocs
   43     MemFrees     uint64 // number of frees
   44 
   45     // Main allocation heap statistics.
   46     HeapAlloc    string // bytes allocated and still in use
   47     HeapSys      string // bytes obtained from system
   48     HeapIdle     string // bytes in idle spans
   49     HeapInuse    string // bytes in non-idle span
   50     HeapReleased string // bytes released to the OS
   51     HeapObjects  uint64 // total number of allocated objects
   52 
   53     // Low-level fixed-size structure allocator statistics.
   54     //  Inuse is bytes used now.
   55     //  Sys is bytes obtained from system.
   56     StackInuse  string // bootstrap stacks
   57     StackSys    string
   58     MSpanInuse  string // mspan structures
   59     MSpanSys    string
   60     MCacheInuse string // mcache structures
   61     MCacheSys   string
   62     BuckHashSys string // profiling bucket hash table
   63     GCSys       string // GC metadata
   64     OtherSys    string // other system allocations
   65 
   66     // Garbage collector statistics.
   67     NextGC       string // next run in HeapAlloc time (bytes)
   68     LastGC       string // last run in absolute time (ns)
   69     PauseTotalNs string
   70     PauseNs      string // circular buffer of recent GC pause times, most recent at [(NumGC+255)%256]
   71     NumGC        uint32
   72 }
   73 
   74 func updateSystemStatus() {
   75     sysStatus.Uptime = tool.TimeSincePro(initTime)
   76 
   77     m := new(runtime.MemStats)
   78     runtime.ReadMemStats(m)
   79     sysStatus.NumGoroutine = runtime.NumGoroutine()
   80 
   81     sysStatus.MemAllocated = tool.FileSize(int64(m.Alloc))
   82     sysStatus.MemTotal = tool.FileSize(int64(m.TotalAlloc))
   83     sysStatus.MemSys = tool.FileSize(int64(m.Sys))
   84     sysStatus.Lookups = m.Lookups
   85     sysStatus.MemMallocs = m.Mallocs
   86     sysStatus.MemFrees = m.Frees
   87 
   88     sysStatus.HeapAlloc = tool.FileSize(int64(m.HeapAlloc))
   89     sysStatus.HeapSys = tool.FileSize(int64(m.HeapSys))
   90     sysStatus.HeapIdle = tool.FileSize(int64(m.HeapIdle))
   91     sysStatus.HeapInuse = tool.FileSize(int64(m.HeapInuse))
   92     sysStatus.HeapReleased = tool.FileSize(int64(m.HeapReleased))
   93     sysStatus.HeapObjects = m.HeapObjects
   94 
   95     sysStatus.StackInuse = tool.FileSize(int64(m.StackInuse))
   96     sysStatus.StackSys = tool.FileSize(int64(m.StackSys))
   97     sysStatus.MSpanInuse = tool.FileSize(int64(m.MSpanInuse))
   98     sysStatus.MSpanSys = tool.FileSize(int64(m.MSpanSys))
   99     sysStatus.MCacheInuse = tool.FileSize(int64(m.MCacheInuse))
  100     sysStatus.MCacheSys = tool.FileSize(int64(m.MCacheSys))
  101     sysStatus.BuckHashSys = tool.FileSize(int64(m.BuckHashSys))
  102     sysStatus.GCSys = tool.FileSize(int64(m.GCSys))
  103     sysStatus.OtherSys = tool.FileSize(int64(m.OtherSys))
  104 
  105     sysStatus.NextGC = tool.FileSize(int64(m.NextGC))
  106     sysStatus.LastGC = fmt.Sprintf("%.1fs", float64(time.Now().UnixNano()-int64(m.LastGC))/1000/1000/1000)
  107     sysStatus.PauseTotalNs = fmt.Sprintf("%.1fs", float64(m.PauseTotalNs)/1000/1000/1000)
  108     sysStatus.PauseNs = fmt.Sprintf("%.3fs", float64(m.PauseNs[(m.NumGC+255)%256])/1000/1000/1000)
  109     sysStatus.NumGC = m.NumGC
  110 }
  111 
  112 func Dashboard(c *context.Context) {
  113     c.Title("admin.dashboard")
  114     c.PageIs("Admin")
  115     c.PageIs("AdminDashboard")
  116 
  117     c.Data["GitVersion"] = conf.Git.Version
  118     c.Data["GoVersion"] = runtime.Version()
  119     c.Data["BuildTime"] = conf.BuildTime
  120     c.Data["BuildCommit"] = conf.BuildCommit
  121 
  122     c.Data["Stats"] = db.GetStatistic()
  123     // FIXME: update periodically
  124     updateSystemStatus()
  125     c.Data["SysStatus"] = sysStatus
  126     c.Success(tmplDashboard)
  127 }
  128 
  129 // Operation types.
  130 type AdminOperation int
  131 
  132 const (
  133     CleanInactivateUser AdminOperation = iota + 1
  134     CleanRepoArchives
  135     CleanMissingRepos
  136     GitGCRepos
  137     SyncSSHAuthorizedKey
  138     SyncRepositoryHooks
  139     ReinitMissingRepository
  140 )
  141 
  142 func Operation(c *context.Context) {
  143     var err error
  144     var success string
  145     switch AdminOperation(c.QueryInt("op")) {
  146     case CleanInactivateUser:
  147         success = c.Tr("admin.dashboard.delete_inactivate_accounts_success")
  148         err = db.DeleteInactivateUsers()
  149     case CleanRepoArchives:
  150         success = c.Tr("admin.dashboard.delete_repo_archives_success")
  151         err = db.DeleteRepositoryArchives()
  152     case CleanMissingRepos:
  153         success = c.Tr("admin.dashboard.delete_missing_repos_success")
  154         err = db.DeleteMissingRepositories()
  155     case GitGCRepos:
  156         success = c.Tr("admin.dashboard.git_gc_repos_success")
  157         err = db.GitGcRepos()
  158     case SyncSSHAuthorizedKey:
  159         success = c.Tr("admin.dashboard.resync_all_sshkeys_success")
  160         err = db.RewriteAuthorizedKeys()
  161     case SyncRepositoryHooks:
  162         success = c.Tr("admin.dashboard.resync_all_hooks_success")
  163         err = db.SyncRepositoryHooks()
  164     case ReinitMissingRepository:
  165         success = c.Tr("admin.dashboard.reinit_missing_repos_success")
  166         err = db.ReinitMissingRepositories()
  167     }
  168 
  169     if err != nil {
  170         c.Flash.Error(err.Error())
  171     } else {
  172         c.Flash.Success(success)
  173     }
  174     c.RedirectSubpath("/admin")
  175 }
  176 
  177 func SendTestMail(c *context.Context) {
  178     emailAddr := c.Query("email")
  179     // Send a test email to the user's email address and redirect back to Config
  180     if err := email.SendTestMail(emailAddr); err != nil {
  181         c.Flash.Error(c.Tr("admin.config.email.test_mail_failed", emailAddr, err))
  182     } else {
  183         c.Flash.Info(c.Tr("admin.config.email.test_mail_sent", emailAddr))
  184     }
  185 
  186     c.Redirect(conf.Server.Subpath + "/admin/config")
  187 }
  188 
  189 func Config(c *context.Context) {
  190     c.Title("admin.config")
  191     c.PageIs("Admin")
  192     c.PageIs("AdminConfig")
  193 
  194     c.Data["App"] = conf.App
  195     c.Data["Server"] = conf.Server
  196     c.Data["SSH"] = conf.SSH
  197     c.Data["Repository"] = conf.Repository
  198     c.Data["Database"] = conf.Database
  199     c.Data["Security"] = conf.Security
  200     c.Data["Email"] = conf.Email
  201     c.Data["Auth"] = conf.Auth
  202     c.Data["User"] = conf.User
  203     c.Data["Session"] = conf.Session
  204     c.Data["Cache"] = conf.Cache
  205     c.Data["Attachment"] = conf.Attachment
  206     c.Data["Release"] = conf.Release
  207     c.Data["Picture"] = conf.Picture
  208     c.Data["HTTP"] = conf.HTTP
  209     c.Data["Mirror"] = conf.Mirror
  210     c.Data["Webhook"] = conf.Webhook
  211     c.Data["Git"] = conf.Git
  212     c.Data["LFS"] = conf.LFS
  213 
  214     c.Data["LogRootPath"] = conf.Log.RootPath
  215     type logger struct {
  216         Mode, Config string
  217     }
  218     loggers := make([]*logger, len(conf.Log.Modes))
  219     for i := range conf.Log.Modes {
  220         loggers[i] = &logger{
  221             Mode: strings.Title(conf.Log.Modes[i]),
  222         }
  223 
  224         result, _ := jsoniter.MarshalIndent(conf.Log.Configs[i], "", "  ")
  225         loggers[i].Config = string(result)
  226     }
  227     c.Data["Loggers"] = loggers
  228 
  229     c.Success(tmplConfig)
  230 }
  231 
  232 func Monitor(c *context.Context) {
  233     c.Data["Title"] = c.Tr("admin.monitor")
  234     c.Data["PageIsAdmin"] = true
  235     c.Data["PageIsAdminMonitor"] = true
  236     c.Data["Processes"] = process.Processes
  237     c.Data["Entries"] = cron.ListTasks()
  238     c.Success(tmplMonitor)
  239 }