0

Job timeseries data

by
Published Dec 20, 2024

Get timeseries data for all jobs within a workflow. Hourly granularity data is only retained for 48 hours while daily granularity data is retained for 90 days.

Script circleci Verified

The script

Submitted by hugo697 Bun
Verified 536 days ago
1
//native
2
type Circleci = {
3
	token: string
4
}
5
/**
6
 * Job timeseries data
7
 * Get timeseries data for all jobs within a workflow. Hourly granularity data is only retained for 48 hours while daily granularity data is retained for 90 days.
8
 */
9
export async function main(
10
	auth: Circleci,
11
	project_slug: string,
12
	workflow_name: string,
13
	branch: string | undefined,
14
	granularity: 'daily' | 'hourly' | undefined,
15
	start_date: string | undefined,
16
	end_date: string | undefined
17
) {
18
	const url = new URL(
19
		`https://circleci.com/api/v2/insights/time-series/${project_slug}/workflows/${workflow_name}/jobs`
20
	)
21
	for (const [k, v] of [
22
		['branch', branch],
23
		['granularity', granularity],
24
		['start-date', start_date],
25
		['end-date', end_date]
26
	]) {
27
		if (v !== undefined && v !== '' && k !== undefined) {
28
			url.searchParams.append(k, v)
29
		}
30
	}
31
	const response = await fetch(url, {
32
		method: 'GET',
33
		headers: {
34
			'Circle-Token': auth.token
35
		},
36
		body: undefined
37
	})
38
	if (!response.ok) {
39
		const text = await response.text()
40
		throw new Error(`${response.status} ${text}`)
41
	}
42
	return await response.json()
43
}
44