Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated Usage page display to fix several issues. #2082

Merged
merged 1 commit into from
Apr 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions app/controllers/api/v0/statistics_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ def users_joined
r[k] = scoped.where(created_at: dates_to_range(v)).count
end

# Reverse hash r, so dates in ascending order
r = Hash[r.to_a.reverse]

respond_to do |format|
format.json { render(json: r.to_json) }
format.csv {
Expand Down Expand Up @@ -66,6 +69,9 @@ def completed_plans
r[k] = scoped.where(created_at: dates_to_range(v)).count
end

# Reverse hash r, so dates in ascending order
r = Hash[r.to_a.reverse]

respond_to do |format|
format.json { render(json: r.to_json) }
format.csv {
Expand Down Expand Up @@ -104,6 +110,9 @@ def created_plans
r[k] = scoped.where(created_at: dates_to_range(v)).count
end

# Reverse hash r, so dates in ascending order
r = Hash[r.to_a.reverse]

respond_to do |format|
format.json { render(json: r.to_json) }
format.csv {
Expand Down
47 changes: 38 additions & 9 deletions app/javascript/views/usage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,11 @@ $(() => {
aspectRatio,
scales: {
xAxes: [{
position: 'top',
ticks: { beginAtZero: true, stepSize: 10 },
stacked: true,
}],
yAxes: [{
stacked: true,
}],
},
},
Expand All @@ -203,7 +206,6 @@ $(() => {

const buildData = (data) => {
const labels = data.map(current => yAxisLabel(current.date));

const datasetsMap = data.reduce((acc, statCreatedPlan) => {
statCreatedPlan.by_template.forEach((template) => {
if (!acc[template.name]) {
Expand All @@ -213,9 +215,28 @@ $(() => {
});
return acc;
}, {});

const datasets = Object.keys(datasetsMap).map(key => datasetsMap[key]);

// const datasets = Object.keys(datasetsMap).map(key => datasetsMap[key]);
const compare = (a, b) => {
const aIndex = labels.indexOf(a.y);
const bIndex = labels.indexOf(b.y);
if (aIndex > bIndex) return 1;
if (aIndex < bIndex) return -1;
return 0;
};
const datasets = Object.keys(datasetsMap).map((key) => {
const datasetByKey = datasetsMap[key];
const availableMonths = datasetByKey.data.reduce((acc, value) => {
// month has y as key
acc.push(value.y);
return acc;
}, []);
// Find missing months in data
const missingMonths = labels.filter(month => !availableMonths.includes(month));
// Add data for missing months with x value set to 0
missingMonths.forEach(month => datasetByKey.data.push({ x: 0, y: month }));
datasetByKey.data = datasetByKey.data.sort(compare);
return datasetByKey;
});
return { labels, datasets };
};

Expand Down Expand Up @@ -246,24 +267,32 @@ $(() => {
switch (diffInMonths) {
case 0:
case 1:
aspectRatio = 5;
break;
case 2:
case 3:
aspectRatio = 4;
aspectRatio = 3.5;
break;
case 4:
case 5:
aspectRatio = 3;
case 6:
aspectRatio = 2.5;
break;
case 7:
case 8:
case 9:
case 10:
aspectRatio = 2;
break;
case 11:
case 12:
aspectRatio = 1.5;
break;
default:
aspectRatio = 1;
aspectRatio = 0.9;
}
} catch (e) {
aspectRatio = 1;
aspectRatio = 0.9;
}

return aspectRatio;
Expand Down
14 changes: 10 additions & 4 deletions app/models/stat_created_plan/create_or_update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,16 @@ def by_template(start_date:, end_date:, org:)
.administrator
.merge(users(org))
.merge(plans(start_date: start_date, end_date: end_date))
.select(:plan_id)
.distinct
template_counts = Plan.where(id: roleable_plan_ids).group(:template_id).count
template_names = Template.where(id: template_counts.keys).pluck(:id, :title)
.pluck(:plan_id)
.uniq

template_counts = Plan.joins(:template).where(id: roleable_plan_ids)
.group("templates.family_id").count
most_recent_versions = Template.where(family_id: template_counts.keys)
.group(:family_id).maximum("version")
most_recent_versions = most_recent_versions.map { |k, v| "#{k}=#{v}" }
template_names = Template.where("CONCAT(family_id, '=', version) IN (?)",
most_recent_versions).pluck(:family_id, :title)
template_names.map do |t|
{ name: t[1], count: template_counts[t[0]] }
end
Expand Down
18 changes: 12 additions & 6 deletions app/views/usage/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@
<hr />
</div>
</div>

<div class="row">
<div class="col-md-12">
<p class="red">&#42;&nbsp;<%= _('Move the mouse pointer over the bars of a chart to see numbers.') %></p>
</div>
</div>

<div class="row">
<div class="col-md-6">
<div class="pull-left">
Expand Down Expand Up @@ -143,21 +150,20 @@
<div class="col-md-6">
<div class="pull-left">
<h4 class="bold"><%= _('No. plans by template') %></h4>
<p class="red">&#42;&nbsp;<%= _('Mouse over the bars of the chart for data for each month.') %></p>
</div>
</div>
<div class="col-md-6">
<div class="pull-right"">
<ul class="list-inline">
<li>
<div class="form-group">
<%= label_tag('monthly_plans_by_template', _('Time picker')) %>
<%= label_tag('monthly_plans_by_template', _('Time period')) %>
<select class="form-control" name="monthly_plans_by_template" data-url="<%= stat_created_plans_by_template_index_path %>">
<option value="<%= Date.today.last_month.end_of_month %>"><%= _('Last month') %></option>
<option value="<%= Date.today.months_ago(3).end_of_month %>"><%= _('3 months ago') %></option>
<option value="<%= Date.today.months_ago(6).end_of_month %>"><%= _('6 months ago') %></option>
<option value="<%= Date.today.months_ago(9).end_of_month %>"><%= _('9 months ago') %></option>
<option value="<%= Date.today.months_ago(12).end_of_month %>"><%= _('12 months ago') %></option>
<option value="<%= Date.today.months_ago(3).end_of_month %>"><%= _('Last 3 months') %></option>
<option value="<%= Date.today.months_ago(6).end_of_month %>"><%= _('Last 6 months') %></option>
<option value="<%= Date.today.months_ago(9).end_of_month %>"><%= _('Last 9 months') %></option>
<option value="<%= Date.today.months_ago(12).end_of_month %>"><%= _('Last 12 months') %></option>
</select>
</div>
</li>
Expand Down