diff --git a/app/assets/javascripts/activityChart.js b/app/assets/javascripts/activityChart.js
index 438271fa0..820e47a16 100644
--- a/app/assets/javascripts/activityChart.js
+++ b/app/assets/javascripts/activityChart.js
@@ -13,7 +13,7 @@
const FONT_WEIGHT = 'bold';
const MAX_Y = 120;
- const createChart = function(containerId, labels, deliveredData, failedData) {
+ const createChart = function(containerId, labels, deliveredData, failedData, pendingData) {
const container = d3.select(containerId);
container.selectAll('*').remove(); // Clear any existing content
@@ -36,7 +36,7 @@
}
// Calculate total messages
- const totalMessages = d3.sum(deliveredData) + d3.sum(failedData);
+ const totalMessages = d3.sum(deliveredData) + d3.sum(failedData) + d3.sum(pendingData);
// Create legend only if there are messages
const legendContainer = d3.select('.chart-legend');
@@ -78,8 +78,9 @@
.range([0, width])
.padding(0.1);
// Adjust the y-axis domain to add some space above the tallest bar
- const maxY = d3.max(deliveredData.map((d, i) => d + (failedData[i] || 0)));
- const y = d3.scaleSqrt()
+ const maxY = d3.max(deliveredData.map((d, i) => d + (failedData[i] || 0) + (pendingData[i] || 0)));
+
+ const y = d3.scaleSymlog()
.domain([0, maxY + 2]) // Add 2 units of space at the top
.nice()
.range([height, 0]);
@@ -91,7 +92,7 @@
// Generate the y-axis with whole numbers
const yAxis = d3.axisLeft(y)
- .ticks(Math.min(maxY + 2, 10)) // Generate up to 10 ticks based on the data
+ .ticks(Math.min(maxY + 2, 3))
.tickFormat(d3.format('d')); // Ensure whole numbers on the y-axis
svg.append('g')
@@ -102,7 +103,7 @@
const stackData = labels.map((label, i) => ({
label: label,
delivered: deliveredData[i],
- failed: failedData[i] || 0, // Ensure there's a value for failed, even if it's 0
+ failed: failedData[i] || 0,
pending: pendingData[i] || 0
}));
@@ -119,149 +120,150 @@
.domain(['delivered', 'failed', 'pending'])
.range([COLORS.delivered, COLORS.failed, COLORS.pending]);
- // Create bars with animation
- const barGroups = svg.selectAll('.bar-group')
- .data(series)
- .enter()
- .append('g')
- .attr('class', 'bar-group')
- .attr('fill', d => color(d.key));
+ // Create bars with animation
+ const barGroups = svg.selectAll('.bar-group')
+ .data(series)
+ .enter()
+ .append('g')
+ .attr('class', 'bar-group')
+ .attr('fill', d => color(d.key));
- barGroups.selectAll('rect')
- .data(d => d)
- .enter()
- .append('rect')
- .attr('x', d => x(d.data.label))
- .attr('y', height)
- .attr('height', 0)
- .attr('width', x.bandwidth())
- .on('mouseover', function(event, d) {
- const key = d3.select(this.parentNode).datum().key;
- const capitalizedKey = key.charAt(0).toUpperCase() + key.slice(1);
- tooltip.style('display', 'block')
- .html(`${d.data.label}
${capitalizedKey}: ${d.data[key]}`);
- })
- .on('mousemove', function(event) {
- tooltip.style('left', `${event.pageX + 10}px`)
- .style('top', `${event.pageY - 20}px`);
- })
- .on('mouseout', function() {
- tooltip.style('display', 'none');
- })
- .transition()
- .duration(1000)
- .attr('y', d => y(d[1]))
- .attr('height', d => y(d[0]) - y(d[1]));
- };
+ barGroups.selectAll('rect')
+ .data(d => d)
+ .enter()
+ .append('rect')
+ .attr('x', d => x(d.data.label))
+ .attr('y', height)
+ .attr('height', 0)
+ .attr('width', x.bandwidth())
+ .on('mouseover', function(event, d) {
+ const key = d3.select(this.parentNode).datum().key;
+ const capitalizedKey = key.charAt(0).toUpperCase() + key.slice(1);
+ tooltip.style('display', 'block')
+ .html(`${d.data.label}
${capitalizedKey}: ${d.data[key]}`);
+ })
+ .on('mousemove', function(event) {
+ tooltip.style('left', `${event.pageX + 10}px`)
+ .style('top', `${event.pageY - 20}px`);
+ })
+ .on('mouseout', function() {
+ tooltip.style('display', 'none');
+ })
+ .transition()
+ .duration(1000)
+ .attr('y', d => y(d[1]))
+ .attr('height', d => y(d[0]) - y(d[1]));
+ };
- // Function to create an accessible table
- const createTable = function(tableId, chartType, labels, deliveredData, failedData) {
- const table = document.getElementById(tableId);
- table.innerHTML = ""; // Clear previous data
+ // Function to create an accessible table
+ const createTable = function(tableId, chartType, labels, deliveredData, failedData, pendingData) {
+ const table = document.getElementById(tableId);
+ table.innerHTML = ""; // Clear previous data
- const captionText = document.querySelector(`#${chartType} .chart-subtitle`).textContent;
- const caption = document.createElement('caption');
- caption.textContent = captionText;
- const thead = document.createElement('thead');
- const tbody = document.createElement('tbody');
+ const captionText = document.querySelector(`#${chartType} .chart-subtitle`).textContent;
+ const caption = document.createElement('caption');
+ caption.textContent = captionText;
+ const thead = document.createElement('thead');
+ const tbody = document.createElement('tbody');
- // Create table header
- const headerRow = document.createElement('tr');
- const headers = ['Day', 'Delivered', 'Failed', 'Pending'];
- headers.forEach(headerText => {
- const th = document.createElement('th');
- th.textContent = headerText;
- headerRow.appendChild(th);
- });
- thead.appendChild(headerRow);
+ // Create table header
+ const headerRow = document.createElement('tr');
+ const headers = ['Day', 'Delivered', 'Failed', 'Pending'];
+ headers.forEach(headerText => {
+ const th = document.createElement('th');
+ th.textContent = headerText;
+ headerRow.appendChild(th);
+ });
+ thead.appendChild(headerRow);
- // Create table body
- labels.forEach((label, index) => {
- const row = document.createElement('tr');
- const cellDay = document.createElement('td');
- cellDay.textContent = label;
- row.appendChild(cellDay);
+ // Create table body
+ labels.forEach((label, index) => {
+ const row = document.createElement('tr');
+ const cellDay = document.createElement('td');
+ cellDay.textContent = label;
+ row.appendChild(cellDay);
- const cellDelivered = document.createElement('td');
- cellDelivered.textContent = deliveredData[index];
- row.appendChild(cellDelivered);
+ const cellDelivered = document.createElement('td');
+ cellDelivered.textContent = deliveredData[index];
+ row.appendChild(cellDelivered);
- const cellFailed = document.createElement('td');
- cellFailed.textContent = failedData[index];
- row.appendChild(cellFailed);
+ const cellFailed = document.createElement('td');
+ cellFailed.textContent = failedData[index];
+ row.appendChild(cellFailed);
- const cellPending = document.createElement('td');
- cellPending.textContent = pendingData[index];
- row.appendChild(cellPending);
+ const cellPending = document.createElement('td');
+ cellPending.textContent = pendingData[index];
+ row.appendChild(cellPending);
- tbody.appendChild(row);
- });
+ tbody.appendChild(row);
+ });
- table.appendChild(caption);
- table.appendChild(thead);
- table.append(tbody);
- };
+ table.appendChild(caption);
+ table.appendChild(thead);
+ table.append(tbody);
+ };
- const fetchData = function(type) {
- var ctx = document.getElementById('weeklyChart');
- if (!ctx) {
- return;
- }
+ const fetchData = function(type) {
+ var ctx = document.getElementById('weeklyChart');
+ if (!ctx) {
+ return;
+ }
- var url = type === 'service' ? `/daily_stats.json` : `/daily_stats_by_user.json`;
- return fetch(url)
- .then(response => {
- if (!response.ok) {
- throw new Error('Network response was not ok');
+ var url = type === 'service' ? `/daily_stats.json` : `/daily_stats_by_user.json`;
+ return fetch(url)
+ .then(response => {
+ if (!response.ok) {
+ throw new Error('Network response was not ok');
+ }
+ return response.json();
+ })
+ .then(data => {
+ labels = [];
+ deliveredData = [];
+ failedData = [];
+ pendingData = [];
+ let totalMessages = 0;
+
+ for (var dateString in data) {
+ if (data.hasOwnProperty(dateString)) {
+ const dateParts = dateString.split('-');
+ const formattedDate = `${dateParts[1]}/${dateParts[2]}/${dateParts[0].slice(2)}`;
+
+ labels.push(formattedDate);
+ deliveredData.push(data[dateString].sms.delivered);
+ failedData.push(data[dateString].sms.failure);
+ pendingData.push(data[dateString].sms.pending || 0);
+ totalMessages += data[dateString].sms.delivered + data[dateString].sms.failure + data[dateString].sms.pending;
+
+ // Calculate the total number of messages
+ totalMessages += data[dateString].sms.delivered + data[dateString].sms.failure;
}
- return response.json();
- })
- .then(data => {
- labels = [];
- deliveredData = [];
- failedData = [];
- pendingData = [];
+ }
- let totalMessages = 0;
-
- for (var dateString in data) {
- if (data.hasOwnProperty(dateString)) {
- const dateParts = dateString.split('-');
- const formattedDate = `${dateParts[1]}/${dateParts[2]}/${dateParts[0].slice(2)}`;
-
- labels.push(formattedDate);
- deliveredData.push(data[dateString].sms.delivered);
- failedData.push(data[dateString].sms.failure);
- pendingData.push(data[dateString].sms.pending || 0);
- // Calculate the total number of messages
- totalMessages += data[dateString].sms.delivered + data[dateString].sms.failure;
- }
- }
-
- // Check if there are no messages sent
- const subTitle = document.querySelector(`#activityChartContainer .chart-subtitle`);
- if (totalMessages === 0) {
- // Remove existing chart and render the alert message
- d3.select('#weeklyChart').selectAll('*').remove();
- d3.select('#weeklyChart')
- .append('div')
- .html(`
-
- No messages sent in the last 7 days -
-+ No messages sent in the last 7 days +