(t *testing.T)
| 133 | } |
| 134 | |
| 135 | func TestRollupTemplateUsageStats(t *testing.T) { |
| 136 | t.Parallel() |
| 137 | |
| 138 | db, ps := dbtestutil.NewDB(t, dbtestutil.WithDumpOnFailure()) |
| 139 | logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true}).Leveled(slog.LevelDebug) |
| 140 | |
| 141 | anHourAgo := dbtime.Now().Add(-time.Hour).Truncate(time.Hour).UTC() |
| 142 | anHourAndSixMonthsAgo := anHourAgo.AddDate(0, -6, 0).UTC() |
| 143 | |
| 144 | var ( |
| 145 | org = dbgen.Organization(t, db, database.Organization{}) |
| 146 | user = dbgen.User(t, db, database.User{Name: "user1"}) |
| 147 | tpl = dbgen.Template(t, db, database.Template{OrganizationID: org.ID, CreatedBy: user.ID}) |
| 148 | ver = dbgen.TemplateVersion(t, db, database.TemplateVersion{OrganizationID: org.ID, TemplateID: uuid.NullUUID{UUID: tpl.ID, Valid: true}, CreatedBy: user.ID}) |
| 149 | ws = dbgen.Workspace(t, db, database.WorkspaceTable{OrganizationID: org.ID, TemplateID: tpl.ID, OwnerID: user.ID}) |
| 150 | job = dbgen.ProvisionerJob(t, db, ps, database.ProvisionerJob{OrganizationID: org.ID}) |
| 151 | build = dbgen.WorkspaceBuild(t, db, database.WorkspaceBuild{WorkspaceID: ws.ID, JobID: job.ID, TemplateVersionID: ver.ID}) |
| 152 | res = dbgen.WorkspaceResource(t, db, database.WorkspaceResource{JobID: build.JobID}) |
| 153 | agent = dbgen.WorkspaceAgent(t, db, database.WorkspaceAgent{ResourceID: res.ID}) |
| 154 | app = dbgen.WorkspaceApp(t, db, database.WorkspaceApp{AgentID: agent.ID}) |
| 155 | ) |
| 156 | |
| 157 | // Stats inserted 6 months + 1 day ago, should be excluded. |
| 158 | _ = dbgen.WorkspaceAgentStat(t, db, database.WorkspaceAgentStat{ |
| 159 | TemplateID: tpl.ID, |
| 160 | WorkspaceID: ws.ID, |
| 161 | AgentID: agent.ID, |
| 162 | UserID: user.ID, |
| 163 | CreatedAt: anHourAndSixMonthsAgo.AddDate(0, 0, -1), |
| 164 | ConnectionMedianLatencyMS: 1, |
| 165 | ConnectionCount: 1, |
| 166 | SessionCountSSH: 1, |
| 167 | }) |
| 168 | _ = dbgen.WorkspaceAppStat(t, db, database.WorkspaceAppStat{ |
| 169 | UserID: user.ID, |
| 170 | WorkspaceID: ws.ID, |
| 171 | AgentID: agent.ID, |
| 172 | SessionStartedAt: anHourAndSixMonthsAgo.AddDate(0, 0, -1), |
| 173 | SessionEndedAt: anHourAndSixMonthsAgo.AddDate(0, 0, -1).Add(time.Minute), |
| 174 | SlugOrPort: app.Slug, |
| 175 | }) |
| 176 | |
| 177 | // Stats inserted 6 months - 1 day ago, should be rolled up. |
| 178 | wags1 := dbgen.WorkspaceAgentStat(t, db, database.WorkspaceAgentStat{ |
| 179 | TemplateID: tpl.ID, |
| 180 | WorkspaceID: ws.ID, |
| 181 | AgentID: agent.ID, |
| 182 | UserID: user.ID, |
| 183 | CreatedAt: anHourAndSixMonthsAgo.AddDate(0, 0, 1), |
| 184 | ConnectionMedianLatencyMS: 1, |
| 185 | ConnectionCount: 1, |
| 186 | SessionCountReconnectingPTY: 1, |
| 187 | }) |
| 188 | wags2 := dbgen.WorkspaceAgentStat(t, db, database.WorkspaceAgentStat{ |
| 189 | TemplateID: tpl.ID, |
| 190 | WorkspaceID: ws.ID, |
| 191 | AgentID: agent.ID, |
| 192 | UserID: user.ID, |
nothing calls this directly
no test coverage detected