Published
Edited
Oct 29, 2021
23 forks
38 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
region = "us-east-1"
Insert cell
Insert cell
logGroupName = "apache/access"
Insert cell
Insert cell
field = "status"
Insert cell
Insert cell
interval = d3.utcMinute
Insert cell
// The current time, rounded to the nearest minute
stop = interval.floor(new Date)
Insert cell
// Uses the width of our TimeChart to give each interval 1 pixel
start = interval.offset(stop, -width)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
AWS = import("https://cdn.skypack.dev/@aws-sdk/client-cloudwatch-logs@3.39.0")
Insert cell
cwl = {
try {
return new AWS.CloudWatchLogsClient({
region,
credentials: {
accessKeyId: Secret("AWS_ACCESS_KEY_ID"),
secretAccessKey: Secret("AWS_SECRET_ACCESS_KEY")
}
});
} catch (error) {
console.error(error);
return false;
}
}
Insert cell
Insert cell
query = {
try {
// Bin the query by the number of seconds in the interval
const binSeconds = d3.utcSecond.count(
new Date(),
interval.offset(new Date(), 1)
);
return cwl.send(
new AWS.StartQueryCommand({
queryString: `stats count(*) by ${field}, bin(${binSeconds}s)`,
startTime: +start / 1000,
endTime: +stop / 1000,
logGroupName
})
);
} catch {
return false;
}
}
Insert cell
results = {
let res = [];
yield res;

// Not authenticated; do nothing
if (!query || !query.queryId) return;

// Get the initial response, usually "Running"
let response = await cwl.send(
new AWS.GetQueryResultsCommand({
queryId: query.queryId
})
);

// Poll the query every second until complete
while (response.status !== "Complete") {
response = await Promises.tick(
1000,
cwl.send(
new AWS.GetQueryResultsCommand({
queryId: query.queryId
})
)
);
}
yield response.results;
}
Insert cell
Insert cell
data = {
if (!cwl) {
// Not authenticated; use demo data
return FileAttachment("demo-403.csv").csv({ typed: true });
}
return results.map((d) => {
return {
status: +d[0].value,
date: new Date(d[1].value),
value: +d[2].value
};
});
}
Insert cell
Insert cell
import {TimeChart, TimeAxis} from "@observablehq/timechart"
Insert cell
timeOptions = {
if (!cwl) {
// Not authenticated; use demo data
const interval = d3.utcMinute.every(15);
const stop = new Date("2021-04-20T12:45");
return {
interval,
start: interval.offset(stop, -width),
stop,
width
};
}

return {
interval,
start,
stop,
width
};
}
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more