Skip to content

Commit 2d9004e

Browse files
committed
Add backend endpoint
1 parent 1d5eca4 commit 2d9004e

File tree

16 files changed

+340
-117
lines changed

16 files changed

+340
-117
lines changed

Makefile

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ bin: $(shell find . -not -path './vendor/*' -type f -name '*.go') go.mod go.sum
3030
darwin:amd64,arm64
3131
.PHONY: bin
3232

33-
build: site/out/index.html $(shell find . -not -path './vendor/*' -type f -name '*.go') go.mod go.sum $(shell find ./examples/templates)
33+
GO_FILES=$(shell find . -not -path './vendor/*' -type f -name '*.go') go.mod go.sum $(shell find ./examples/templates)
34+
35+
build: site/out/index.html $(GO_FILES)
3436
rm -rf ./dist
3537
mkdir -p ./dist
3638
rm -f ./site/out/bin/coder*
@@ -55,6 +57,30 @@ build: site/out/index.html $(shell find . -not -path './vendor/*' -type f -name
5557
darwin:amd64,arm64
5658
.PHONY: build
5759

60+
# Builds a test binary for just Linux
61+
build-linux-test: site/out/index.html $(GO_FILES)
62+
rm -rf ./dist
63+
mkdir -p ./dist
64+
rm -f ./site/out/bin/coder*
65+
66+
# build slim artifacts and copy them to the site output directory
67+
./scripts/build_go_slim.sh \
68+
--version "$(VERSION)" \
69+
--compress 6 \
70+
--output ./dist/ \
71+
linux:amd64,armv7,arm64 \
72+
windows:amd64,arm64 \
73+
darwin:amd64,arm64
74+
75+
# build not-so-slim artifacts with the default name format
76+
./scripts/build_go_matrix.sh \
77+
--version "$(VERSION)" \
78+
--output ./dist/ \
79+
--archive \
80+
--package-linux \
81+
linux:amd64
82+
.PHONY: build-linux-test
83+
5884
# Runs migrations to output a dump of the database.
5985
coderd/database/dump.sql: coderd/database/gen/dump/main.go $(wildcard coderd/database/migrations/*.sql)
6086
go run coderd/database/gen/dump/main.go

agent/stats.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ func (c *ConnStats) Write(b []byte) (n int, err error) {
3131
}
3232

3333
type ProtocolStats struct {
34-
NumConns int64 `json:"num_comms,omitempty"`
34+
NumConns int64 `json:"num_comms"`
3535

3636
// RxBytes must be read with atomic.
37-
RxBytes int64 `json:"rx_bytes,omitempty"`
37+
RxBytes int64 `json:"rx_bytes"`
3838

3939
// TxBytes must be read with atomic.
40-
TxBytes int64 `json:"tx_bytes,omitempty"`
40+
TxBytes int64 `json:"tx_bytes"`
4141
}
4242

4343
var _ net.Conn = new(ConnStats)

cli/agent.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ func workspaceAgent() *cobra.Command {
182182
EnableWireguard: wireguard,
183183
UploadWireguardKeys: client.UploadWorkspaceAgentKeys,
184184
ListenWireguardPeers: client.WireguardPeerListener,
185+
StatsReporter: client.AgentReportStats,
185186
})
186187
<-cmd.Context().Done()
187188
return closer.Close()

coderd/coderd.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,12 @@ func New(options *Options) *API {
340340
r.Use(httpmw.ExtractWorkspaceAgent(options.Database))
341341
r.Get("/report-agent-stats", api.workspaceAgentReportStats)
342342
})
343+
r.Group(func(r chi.Router) {
344+
r.Use(
345+
apiKeyMiddleware,
346+
)
347+
r.Get("/daus", api.getDAUs)
348+
})
343349
})
344350
r.Route("/workspaceagents", func(r chi.Router) {
345351
r.Post("/azure-instance-identity", api.postWorkspaceAuthAzureInstanceIdentity)

coderd/database/databasefake/databasefake.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/google/uuid"
1212
"github.com/lib/pq"
13+
"golang.org/x/exp/maps"
1314
"golang.org/x/exp/slices"
1415

1516
"github.com/coder/coder/coderd/database"
@@ -153,6 +154,39 @@ func (q *fakeQuerier) InsertAgentStat(_ context.Context, p database.InsertAgentS
153154
return stat, nil
154155
}
155156

157+
func (q *fakeQuerier) GetDAUsFromAgentStats(_ context.Context) ([]database.GetDAUsFromAgentStatsRow, error) {
158+
q.mutex.Lock()
159+
defer q.mutex.Unlock()
160+
161+
counts := make(map[time.Time]map[string]struct{})
162+
163+
for _, as := range q.agentStats {
164+
date := as.CreatedAt.Truncate(time.Hour * 24)
165+
dateEntry := counts[date]
166+
if dateEntry == nil {
167+
dateEntry = make(map[string]struct{})
168+
}
169+
counts[date] = dateEntry
170+
171+
dateEntry[as.UserID.String()] = struct{}{}
172+
}
173+
174+
countKeys := maps.Keys(counts)
175+
sort.Slice(countKeys, func(i, j int) bool {
176+
return countKeys[i].Before(countKeys[j])
177+
})
178+
179+
var rs []database.GetDAUsFromAgentStatsRow
180+
for _, key := range countKeys {
181+
rs = append(rs, database.GetDAUsFromAgentStatsRow{
182+
Date: key,
183+
Daus: int64(len(counts[key])),
184+
})
185+
}
186+
187+
return rs, nil
188+
}
189+
156190
func (q *fakeQuerier) ParameterValue(_ context.Context, id uuid.UUID) (database.ParameterValue, error) {
157191
q.mutex.Lock()
158192
defer q.mutex.Unlock()

coderd/database/dump.sql

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/migrations/000039_agent_stats.up.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
CREATE TABLE agent_stats (
22
id text NOT NULL,
3+
PRIMARY KEY (id),
34
created_at timestamptz NOT NULL,
45
user_id uuid NOT NULL,
56
agent_id uuid NOT NULL,

coderd/database/querier.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries.sql.go

Lines changed: 42 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/agentstats.sql

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,16 @@ INSERT INTO
1010
)
1111
VALUES
1212
($1, $2, $3, $4, $5, $6) RETURNING *;
13+
14+
-- name: GetDAUsFromAgentStats :many
15+
select
16+
created_at::date as date,
17+
count(distinct(user_id)) as daus
18+
from
19+
agent_stats
20+
where
21+
cast(payload->>'num_comms' as integer) > 0
22+
group by
23+
date
24+
order by
25+
date asc;

0 commit comments

Comments
 (0)