Skip to content

feat: use active users instead of total users in Template views #3900

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ site/out/
.terraform/

.vscode/*.log
.vscode/launch.json
**/*.swp
.coderv2/*
**/__debug_bin
14 changes: 4 additions & 10 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -161,16 +161,10 @@
// To reduce redundancy in tests, it's covered by other packages.
// Since package coverage pairing can't be defined, all packages cover
// all other packages.
"go.testFlags": ["-short", "-coverpkg=./..."],
"go.coverageDecorator": {
"type": "gutter",
"coveredHighlightColor": "rgba(64,128,128,0.5)",
"uncoveredHighlightColor": "rgba(128,64,64,0.25)",
"coveredBorderColor": "rgba(64,128,128,0.5)",
"uncoveredBorderColor": "rgba(128,64,64,0.25)",
"coveredGutterStyle": "blockgreen",
"uncoveredGutterStyle": "blockred"
},
"go.testFlags": [
"-short",
"-coverpkg=./..."
],
// We often use a version of TypeScript that's ahead of the version shipped
// with VS Code.
"typescript.tsdk": "./site/node_modules/typescript/lib"
Expand Down
16 changes: 8 additions & 8 deletions cli/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func create() *cobra.Command {
}

slices.SortFunc(templates, func(a, b codersdk.Template) bool {
return a.WorkspaceOwnerCount > b.WorkspaceOwnerCount
return a.ActiveUserCount > b.ActiveUserCount
})

templateNames := make([]string, 0, len(templates))
Expand All @@ -81,13 +81,13 @@ func create() *cobra.Command {
for _, template := range templates {
templateName := template.Name

if template.WorkspaceOwnerCount > 0 {
developerText := "developer"
if template.WorkspaceOwnerCount != 1 {
developerText = "developers"
}

templateName += cliui.Styles.Placeholder.Render(fmt.Sprintf(" (used by %d %s)", template.WorkspaceOwnerCount, developerText))
if template.ActiveUserCount > 0 {
templateName += cliui.Styles.Placeholder.Render(
fmt.Sprintf(
" (used by %s)",
formatActiveDevelopers(template.ActiveUserCount),
),
)
}

templateNames = append(templateNames, templateName)
Expand Down
8 changes: 1 addition & 7 deletions cli/templates.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cli

import (
"fmt"
"time"

"github.com/google/uuid"
Expand Down Expand Up @@ -64,19 +63,14 @@ type templateTableRow struct {
func displayTemplates(filterColumns []string, templates ...codersdk.Template) (string, error) {
rows := make([]templateTableRow, len(templates))
for i, template := range templates {
suffix := ""
if template.WorkspaceOwnerCount != 1 {
suffix = "s"
}

rows[i] = templateTableRow{
Name: template.Name,
CreatedAt: template.CreatedAt.Format("January 2, 2006"),
LastUpdated: template.UpdatedAt.Format("January 2, 2006"),
OrganizationID: template.OrganizationID,
Provisioner: template.Provisioner,
ActiveVersionID: template.ActiveVersionID,
UsedBy: cliui.Styles.Fuchsia.Render(fmt.Sprintf("%d developer%s", template.WorkspaceOwnerCount, suffix)),
UsedBy: cliui.Styles.Fuchsia.Render(formatActiveDevelopers(template.ActiveUserCount)),
MaxTTL: (time.Duration(template.MaxTTLMillis) * time.Millisecond),
MinAutostartInterval: (time.Duration(template.MinAutostartIntervalMillis) * time.Millisecond),
}
Expand Down
17 changes: 17 additions & 0 deletions cli/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cli

import (
"fmt"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -175,3 +176,19 @@ func parseTime(s string) (time.Time, error) {
}
return time.Time{}, errInvalidTimeFormat
}

func formatActiveDevelopers(n int) string {
developerText := "developer"
if n != 1 {
developerText = "developers"
}

var nStr string
if n < 0 {
nStr = "-"
} else {
nStr = strconv.Itoa(n)
}

return fmt.Sprintf("%s active %s", nStr, developerText)
}
31 changes: 17 additions & 14 deletions coderd/database/databasefake/databasefake.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,34 +163,37 @@ func (q *fakeQuerier) GetTemplateDAUs(_ context.Context, templateID uuid.UUID) (
q.mutex.Lock()
defer q.mutex.Unlock()

counts := make(map[time.Time]map[string]struct{})
seens := make(map[time.Time]map[uuid.UUID]struct{})

for _, as := range q.agentStats {
if as.TemplateID != templateID {
continue
}

date := as.CreatedAt.Truncate(time.Hour * 24)
dateEntry := counts[date]

dateEntry := seens[date]
if dateEntry == nil {
dateEntry = make(map[string]struct{})
dateEntry = make(map[uuid.UUID]struct{})
}
counts[date] = dateEntry

dateEntry[as.UserID.String()] = struct{}{}
dateEntry[as.UserID] = struct{}{}
seens[date] = dateEntry
}

countKeys := maps.Keys(counts)
sort.Slice(countKeys, func(i, j int) bool {
return countKeys[i].Before(countKeys[j])
seenKeys := maps.Keys(seens)
sort.Slice(seenKeys, func(i, j int) bool {
return seenKeys[i].Before(seenKeys[j])
})

var rs []database.GetTemplateDAUsRow
for _, key := range countKeys {
rs = append(rs, database.GetTemplateDAUsRow{
Date: key,
Amount: int64(len(counts[key])),
})
for _, key := range seenKeys {
ids := seens[key]
for id := range ids {
rs = append(rs, database.GetTemplateDAUsRow{
Date: key,
UserID: id,
})
}
}

return rs, nil
Expand Down
8 changes: 4 additions & 4 deletions coderd/database/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions coderd/database/queries/agentstats.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ VALUES
-- name: GetTemplateDAUs :many
select
(created_at at TIME ZONE 'UTC')::date as date,
count(distinct(user_id)) as amount
user_id
from
agent_stats
where template_id = $1
group by
date
date, user_id
order by
date asc;

Expand Down
Loading