mirror of https://github.com/coder/coder.git
Add tests for command reports
This commit is contained in:
parent
89d50a7a85
commit
852ba0a9dc
|
@ -826,6 +826,7 @@ func New(options *Options) *API {
|
|||
r.Get("/serve", api.intelDaemonServe)
|
||||
r.Get("/machines", api.intelMachines)
|
||||
r.Get("/cohorts", api.intelCohorts)
|
||||
r.Get("/report", api.intelReport)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
|
@ -2547,10 +2547,10 @@ func (q *FakeQuerier) GetIntelReportCommands(_ context.Context, arg database.Get
|
|||
}
|
||||
}
|
||||
command.TotalInvocations += summary.TotalInvocations
|
||||
command.AggregatedBinaryPaths = append(command.AggregatedBinaryPaths, summary.BinaryPaths)
|
||||
command.AggregatedExitCodes = append(command.AggregatedExitCodes, summary.ExitCodes)
|
||||
command.AggregatedGitRemoteUrls = append(command.AggregatedGitRemoteUrls, summary.GitRemoteUrls)
|
||||
command.AggregatedWorkingDirectories = append(command.AggregatedWorkingDirectories, summary.WorkingDirectories)
|
||||
command.AggregatedBinaryPaths = append(command.AggregatedBinaryPaths, string(summary.BinaryPaths))
|
||||
command.AggregatedExitCodes = append(command.AggregatedExitCodes, string(summary.ExitCodes))
|
||||
command.AggregatedGitRemoteUrls = append(command.AggregatedGitRemoteUrls, string(summary.GitRemoteUrls))
|
||||
command.AggregatedWorkingDirectories = append(command.AggregatedWorkingDirectories, string(summary.WorkingDirectories))
|
||||
commandMedianDurations[key] = append(commandMedianDurations[key], summary.MedianDurationMs)
|
||||
commands[key] = command
|
||||
}
|
||||
|
|
|
@ -770,6 +770,36 @@ func TestIntelReports(t *testing.T) {
|
|||
})
|
||||
t.Run("ReportCommands", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
//
|
||||
db, _ := dbtestutil.NewDB(t)
|
||||
// A cohort that matches all machines is necessary.
|
||||
cohort := dbgen.IntelCohort(t, db, database.IntelCohort{})
|
||||
machine := dbgen.IntelMachine(t, db, database.IntelMachine{})
|
||||
dbgen.IntelInvocations(t, db, database.IntelInvocation{
|
||||
MachineID: machine.ID,
|
||||
BinaryName: "go",
|
||||
BinaryArgs: []byte(`["test"]`),
|
||||
}, 50)
|
||||
dbgen.IntelInvocations(t, db, database.IntelInvocation{
|
||||
MachineID: machine.ID,
|
||||
BinaryName: "go",
|
||||
BinaryArgs: []byte(`["build"]`),
|
||||
}, 50)
|
||||
err := db.UpsertIntelInvocationSummaries(context.Background())
|
||||
require.NoError(t, err)
|
||||
rows, err := db.GetIntelReportCommands(context.Background(), database.GetIntelReportCommandsParams{
|
||||
CohortIds: []uuid.UUID{cohort.ID},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.Len(t, rows, 2)
|
||||
for _, row := range rows {
|
||||
switch string(row.BinaryArgs) {
|
||||
case `["test"]`:
|
||||
require.Equal(t, int64(50), row.TotalInvocations)
|
||||
case `["build"]`:
|
||||
require.Equal(t, int64(50), row.TotalInvocations)
|
||||
default:
|
||||
t.Fatalf("unexpected binary args: %s", row.BinaryArgs)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
@ -3107,10 +3107,10 @@ SELECT
|
|||
SUM(total_invocations) AS total_invocations,
|
||||
PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY median_duration_ms) AS median_duration_ms,
|
||||
|
||||
array_agg(working_directories):: jsonb [] AS aggregated_working_directories,
|
||||
array_agg(binary_paths):: jsonb [] AS aggregated_binary_paths,
|
||||
array_agg(git_remote_urls):: jsonb [] AS aggregated_git_remote_urls,
|
||||
array_agg(exit_codes):: jsonb [] AS aggregated_exit_codes
|
||||
array_agg(working_directories):: text [] AS aggregated_working_directories,
|
||||
array_agg(binary_paths):: text [] AS aggregated_binary_paths,
|
||||
array_agg(git_remote_urls):: text [] AS aggregated_git_remote_urls,
|
||||
array_agg(exit_codes):: text [] AS aggregated_exit_codes
|
||||
FROM
|
||||
intel_invocation_summaries
|
||||
WHERE
|
||||
|
@ -3127,17 +3127,17 @@ type GetIntelReportCommandsParams struct {
|
|||
}
|
||||
|
||||
type GetIntelReportCommandsRow struct {
|
||||
StartsAt time.Time `db:"starts_at" json:"starts_at"`
|
||||
EndsAt time.Time `db:"ends_at" json:"ends_at"`
|
||||
CohortID uuid.UUID `db:"cohort_id" json:"cohort_id"`
|
||||
BinaryName string `db:"binary_name" json:"binary_name"`
|
||||
BinaryArgs json.RawMessage `db:"binary_args" json:"binary_args"`
|
||||
TotalInvocations int64 `db:"total_invocations" json:"total_invocations"`
|
||||
MedianDurationMs float64 `db:"median_duration_ms" json:"median_duration_ms"`
|
||||
AggregatedWorkingDirectories []json.RawMessage `db:"aggregated_working_directories" json:"aggregated_working_directories"`
|
||||
AggregatedBinaryPaths []json.RawMessage `db:"aggregated_binary_paths" json:"aggregated_binary_paths"`
|
||||
AggregatedGitRemoteUrls []json.RawMessage `db:"aggregated_git_remote_urls" json:"aggregated_git_remote_urls"`
|
||||
AggregatedExitCodes []json.RawMessage `db:"aggregated_exit_codes" json:"aggregated_exit_codes"`
|
||||
StartsAt time.Time `db:"starts_at" json:"starts_at"`
|
||||
EndsAt time.Time `db:"ends_at" json:"ends_at"`
|
||||
CohortID uuid.UUID `db:"cohort_id" json:"cohort_id"`
|
||||
BinaryName string `db:"binary_name" json:"binary_name"`
|
||||
BinaryArgs json.RawMessage `db:"binary_args" json:"binary_args"`
|
||||
TotalInvocations int64 `db:"total_invocations" json:"total_invocations"`
|
||||
MedianDurationMs float64 `db:"median_duration_ms" json:"median_duration_ms"`
|
||||
AggregatedWorkingDirectories []string `db:"aggregated_working_directories" json:"aggregated_working_directories"`
|
||||
AggregatedBinaryPaths []string `db:"aggregated_binary_paths" json:"aggregated_binary_paths"`
|
||||
AggregatedGitRemoteUrls []string `db:"aggregated_git_remote_urls" json:"aggregated_git_remote_urls"`
|
||||
AggregatedExitCodes []string `db:"aggregated_exit_codes" json:"aggregated_exit_codes"`
|
||||
}
|
||||
|
||||
func (q *sqlQuerier) GetIntelReportCommands(ctx context.Context, arg GetIntelReportCommandsParams) ([]GetIntelReportCommandsRow, error) {
|
||||
|
|
|
@ -236,11 +236,12 @@ SELECT
|
|||
binary_args,
|
||||
SUM(total_invocations) AS total_invocations,
|
||||
PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY median_duration_ms) AS median_duration_ms,
|
||||
|
||||
array_agg(working_directories):: jsonb [] AS aggregated_working_directories,
|
||||
array_agg(binary_paths):: jsonb [] AS aggregated_binary_paths,
|
||||
array_agg(git_remote_urls):: jsonb [] AS aggregated_git_remote_urls,
|
||||
array_agg(exit_codes):: jsonb [] AS aggregated_exit_codes
|
||||
-- We have to convert to text here because Go cannot scan
|
||||
-- an array of jsonb.
|
||||
array_agg(working_directories):: text [] AS aggregated_working_directories,
|
||||
array_agg(binary_paths):: text [] AS aggregated_binary_paths,
|
||||
array_agg(git_remote_urls):: text [] AS aggregated_git_remote_urls,
|
||||
array_agg(exit_codes):: text [] AS aggregated_exit_codes
|
||||
FROM
|
||||
intel_invocation_summaries
|
||||
WHERE
|
||||
|
|
|
@ -125,7 +125,7 @@ func (api *API) intelReport(rw http.ResponseWriter, r *http.Request) {
|
|||
// Merge exit codes
|
||||
exitCodes := map[string]int64{}
|
||||
for _, exitCodeRaw := range row.AggregatedExitCodes {
|
||||
err = json.Unmarshal(exitCodeRaw, &exitCodes)
|
||||
err = json.Unmarshal([]byte(exitCodeRaw), &exitCodes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -140,7 +140,7 @@ func (api *API) intelReport(rw http.ResponseWriter, r *http.Request) {
|
|||
// Merge binary paths
|
||||
binaryPaths := map[string]int64{}
|
||||
for _, binaryPathRaw := range row.AggregatedBinaryPaths {
|
||||
err = json.Unmarshal(binaryPathRaw, &binaryPaths)
|
||||
err = json.Unmarshal([]byte(binaryPathRaw), &binaryPaths)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -151,7 +151,7 @@ func (api *API) intelReport(rw http.ResponseWriter, r *http.Request) {
|
|||
// Merge working directories
|
||||
workingDirectories := map[string]int64{}
|
||||
for _, workingDirectoryRaw := range row.AggregatedWorkingDirectories {
|
||||
err = json.Unmarshal(workingDirectoryRaw, &workingDirectories)
|
||||
err = json.Unmarshal([]byte(workingDirectoryRaw), &workingDirectories)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -162,7 +162,7 @@ func (api *API) intelReport(rw http.ResponseWriter, r *http.Request) {
|
|||
// Merge git remote URLs
|
||||
gitRemoteURLs := map[string]int64{}
|
||||
for _, gitRemoteURLRaw := range row.AggregatedGitRemoteUrls {
|
||||
err = json.Unmarshal(gitRemoteURLRaw, &gitRemoteURLs)
|
||||
err = json.Unmarshal([]byte(gitRemoteURLRaw), &gitRemoteURLs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue