Skip to content

Commit

Permalink
Merge pull request #111 from internetee/fix-statistics-1
Browse files Browse the repository at this point in the history
Fixed statistics
  • Loading branch information
vohmar authored Dec 21, 2023
2 parents 7ac8899 + a89352d commit ae7c71e
Show file tree
Hide file tree
Showing 14 changed files with 46 additions and 57 deletions.
3 changes: 1 addition & 2 deletions app/controllers/stats/market_share_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ def growth_rate_data

def set_default_search_params
@search_params = params[:search]&.to_unsafe_h || {}
today = Time.zone.today
end_date = @search_params[:end_date].presence || format_date(today)
end_date = @search_params[:end_date].presence || format_date(Time.zone.today)
last_month = Date.strptime(end_date, '%m.%y').last_month
compare_to_date = @search_params[:compare_to_end_date].presence || format_date(last_month)
@search_params[:end_date] = end_date
Expand Down
9 changes: 4 additions & 5 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,18 @@ def login_with_role(user, registrar: true)
end

def back_link
link_to :back, class: 'back-link' do
link_to :back, class: 'back-link', data: { turbo: false } do
out = []
out << tag.i(nil, class: 'fas fa-arrow-left')
out << tag.span(t(:back))
safe_join(out)
end
end

def toggle_filter
def toggle_filter(reverse: false)
button_tag(data: { toggle: 'filters' }, type: '', class: 'button button--toggle') do
out = []
out << tag.span(t(:open_filter))
out << tag.span(t(:close_filter))
out = [tag.span(t(:open_filter)), tag.span(t(:close_filter))]
out.reverse! if reverse
out << tag.i(nil, class: 'fas fa-filter')
safe_join(out)
end
Expand Down
26 changes: 15 additions & 11 deletions app/helpers/stats_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,24 @@ module StatsHelper
def market_share_distribution_chart(search_params)
url = market_share_distribution_data_path(search: search_params)
title = t('stats.market_share.distribution.chart_title',
date: title_period(search_params[:start_date],
search_params[:end_date]))
date: title_period(search_params[:end_date]))
tag.div(nil, data: chart_data_params(url: url, title: title, type: __method__.to_s)) do
tag.div(preloader, class: 'pie_chart')
end
end

# rubocop:disable Metrics/MethodLength
def market_share_growth_rate_chart(search_params)
url = market_share_growth_rate_data_path(search: search_params)
title = t('stats.market_share.growth_rate.chart_title',
date: title_period(search_params[:start_date],
search_params[:end_date]))
date: title_period(search_params[:end_date]))
subtitle = t('stats.market_share.growth_rate.chart_subtitle',
date: title_period(search_params[:compare_to_start_date],
search_params[:compare_to_end_date]))
date: title_period(search_params[:compare_to_end_date]))
tag.div(nil, data: chart_data_params(url: url, title: title, type: __method__.to_s,
subtitle: subtitle,
translations: date_translations(search_params))) do
data_type_radio_buttons + tag.div(preloader, class: 'bar_chart')
end
end
# rubocop:enable Metrics/MethodLength

private

Expand All @@ -46,14 +41,22 @@ def data_type_radio_buttons(tags: [])
end
end

def title_period(start_date, end_date, period: '')
period += "#{translate_date(to_date(start_date))} - " if start_date.present?
def title_period(end_date, period: '')
period += translate_date(to_date(end_date)).to_s

period
end

def to_date(month_year)
Date.strptime(month_year, '%m.%y')
parsed_date = Date.strptime(month_year, '%m.%y')

current_date = Time.zone.today

if parsed_date.month == current_date.month && parsed_date.year == current_date.year
current_date
else
parsed_date.end_of_month
end
end

def translate_date(date)
Expand All @@ -76,6 +79,7 @@ def date_translations(params, dates: {})
compare_to_end_date = to_date(params[:compare_to_end_date])
dates[params[:end_date]] = translate_date(end_date)
dates[params[:compare_to_end_date]] = translate_date(compare_to_end_date)

dates
end
end
5 changes: 0 additions & 5 deletions app/javascript/controllers/chart_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,8 @@ export default class extends Controller {
var curData = data['current'], prevData = data['previous'];
this.setLegends(curData[data_type]);
var apndx = ((data_type == 'market_share') ? '%' : '');
// console.log(curData[data_type]);
// console.log(this.getData(curData[data_type], prevData[data_type], data_type).slice());
const newPrevData = this.filterData(prevData[data_type]),
newCurData = this.filterData(curData[data_type]);
// console.log(newPrevData);
// console.log(this.getData(newCurData, newPrevData, data_type).slice());
return [{ color: 'rgba(158, 159, 163, 0.5)',
pointPlacement: -0.15,
linkedTo: 'main',
Expand Down Expand Up @@ -281,7 +277,6 @@ export default class extends Controller {
} else {
this.hiddenRegistrars = this.hiddenRegistrars.filter(function(e) { return e !== name });
}
// console.log(this.hiddenRegistrars);
this.updateChart(data_type);
}
getData(curData, prevData, data_type) {
Expand Down
4 changes: 4 additions & 0 deletions app/javascript/datepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default class Datepicker {
init() {
const that = this;
let dateFormat = that.target.getAttribute('dateFormat');
let minDate = that.target.getAttribute('minDate');
if (dateFormat == "m.y") {
that.options['plugins'] = [
new monthSelectPlugin({
Expand All @@ -23,6 +24,9 @@ export default class Datepicker {
})
];
that.options['maxDate'] = new Date();
if (minDate) {
that.options['minDate'] = new Date(minDate);
}
}
let options = {
allowInput: true,
Expand Down
4 changes: 2 additions & 2 deletions app/views/contacts/partials/_search_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
<% end %>
<%= form.text_field :name_matches, 'aria-label': t(:search), class: 'input search', placeholder: t(:search), value: params[:search]&.dig(:name_matches) %>
</div>
<%= toggle_filter %>
<%= toggle_filter(reverse: params[:search].present?) %>
</div>
</div>
</div>
<div id="filters" class="form--filters">
<div id="filters" class="form--filters <%= 'open' if params[:search] %>">
<div class="row bottom-xs">
<div class="col-md-3 col-xs-12">
<%= form.label :code_eq, t('contacts.index.code'), class: 'form--label' %>
Expand Down
4 changes: 2 additions & 2 deletions app/views/domains/partials/_search_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
<%= form.text_field :name_matches, 'aria-label': t(:search), class: 'input search',
placeholder: t(:search), value: params[:search]&.dig(:name_matches) %>
</div>
<%= toggle_filter %>
<%= toggle_filter(reverse: params[:search].present?) %>
</div>
</div>
</div>
<div id="filters" class="form--filters">
<div id="filters" class="form--filters <%= 'open' if params[:search] %>">
<div class="row bottom-xs">
<div class="col-md-4 col-xs-12">
<%= form.label :registrant_code_eq, t('domains.index.registrant_name'), class: 'form--label' %>
Expand Down
4 changes: 2 additions & 2 deletions app/views/invoices/partials/_search_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
placeholder: t('invoices.index.search_number'),
value: params[:search]&.dig(:number_str_matches) %>
</div>
<%= toggle_filter %>
<%= toggle_filter(reverse: params[:search].present?) %>
</div>
</div>
</div>
<div id="filters" class="form--filters">
<div id="filters" class="form--filters <%= 'open' if params[:search] %>">
<div class="row bottom-xs">
<div class="col-xs-auto">
<%= form.label :due_date_gteq, t('invoices.index.due_date'), class: 'form--label' %>
Expand Down
20 changes: 5 additions & 15 deletions app/views/stats/_search_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,17 @@
<div class="row">
<div class="col-md-12 col-xs center-xs">
<div class="text-center">
<%= toggle_filter %>
<%= toggle_filter(reverse: params[:search].present?) %>
</div>
</div>
</div>
<div id="filters" class="form--filters">
<div id="filters" class="form--filters <%= 'open' if params[:search] %>">
<div class="row bottom-xs">
<div class="col-md-5 col-xs-12">
<%= form.label :period, t('stats.market_share.index.period'), class: 'form--label' %>
<div class="form--field">
<div class="form--input small">
<%= form.date_field :start_date, class: 'datepicker', value: params[:search]&.dig(:start_date), dateFormat: 'm.y' %>
<div class="form--suffix"><i class="fas fa-calendar"></i></div>
</div>
<span class="form--sep">-</span>
<div class="form--input small">
<%= form.date_field :end_date, class: 'datepicker', value: params[:search]&.dig(:end_date), dateFormat: 'm.y' %>
<%= form.date_field :end_date, class: 'datepicker', value: params[:search]&.dig(:end_date), dateFormat: 'm.y', minDate: '2015/12/01' %>
<div class="form--suffix"><i class="fas fa-calendar"></i></div>
</div>
</div>
Expand All @@ -27,12 +22,7 @@
<%= form.label :compare_period, t('stats.market_share.index.compare_to'), class: 'form--label' %>
<div class="form--field">
<div class="form--input small">
<%= form.date_field :compare_to_start_date, class: 'datepicker', value: params[:search]&.dig(:compare_to_start_date), dateFormat: 'm.y' %>
<div class="form--suffix"><i class="fas fa-calendar"></i></div>
</div>
<span class="form--sep">-</span>
<div class="form--input small">
<%= form.date_field :compare_to_end_date, class: 'datepicker', value: params[:search]&.dig(:compare_to_end_date), dateFormat: 'm.y' %>
<%= form.date_field :compare_to_end_date, class: 'datepicker', value: params[:search]&.dig(:compare_to_end_date), dateFormat: 'm.y', minDate: '2015/12/01' %>
<div class="form--suffix"><i class="fas fa-calendar"></i></div>
</div>
</div>
Expand All @@ -41,7 +31,7 @@
<div class="col-md-auto col-xs-8">
<div class="form--actions">
<%= button_tag t(:filter), type: 'submit', class: 'button button--primary small' %>
<%= link_to t(:clear_filter), "#{url_for(request.params.slice(:type))}", class: 'button button--link' %>
<%= link_to t(:clear_filter), "#{url_for(request.params.slice(:type))}", class: 'button button--link', data: { turbo: false } %>
</div>
</div>
</div>
Expand Down
4 changes: 2 additions & 2 deletions app/views/stats/market_share/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
<div class="page--actions">
<div class="wrapper">
<div class="page--actions__left">
<%= link_to t('.market_share'), market_share_path(type: 'distribution'), class: 'button button--secondary' %>
<%= link_to t('.market_share_growth'), market_share_path(type: 'growth_rate'), class: 'button button--secondary' %>
<%= link_to t('.market_share'), market_share_path(type: 'distribution'), class: 'button button--secondary', data: { turbo: false } %>
<%= link_to t('.market_share_growth'), market_share_path(type: 'growth_rate'), class: 'button button--secondary', data: { turbo: false } %>
</div>
<div class="page--actions__right">
<%= link_to "#{t(:download_csv)} <i class='fas fa-download'></i>".html_safe, market_share_path(format: 'csv', search: params[:search]&.to_unsafe_h) %>
Expand Down
8 changes: 4 additions & 4 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ en:
[~, January, February, March, April, May, June, July, August, September, October, November, December]
formats:
default: "%Y-%m-%d"
month_year: "%B, %Y"
month_year: "%d %B %Y"
month_year_short: "%b, %Y"

auth:
Expand Down Expand Up @@ -605,7 +605,7 @@ en:
title: "Stats"
market_share: "Market share and distribution"
market_share_growth: "Market share and growth rate"
period: "Period"
period: "Month"
compare_to: "Compare to"
domains: "Domains"
registrars: "Registrars"
Expand Down Expand Up @@ -638,9 +638,9 @@ en:
- ['Choose', '']
- ['Previous month', 'previous_month']
distribution:
chart_title: "Market share and distribution in %{date}"
chart_title: "Market share and distribution %{date}"
growth_rate:
chart_title: "Market share and growth rate in %{date}"
chart_title: "Market share and growth rate %{date}"
chart_subtitle: "Comparing to results from %{date}"

xml_console:
Expand Down
6 changes: 3 additions & 3 deletions config/locales/et.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ et:
[~, Jaan, Veeb, Märts, Apr, Mai, Juuni, Juuli, Aug, Sep, Okt, Nov, Det]
formats:
default: "%Y-%m-%d"
month_year: "%B, %Y"
month_year: "%d %B %Y"
month_year_short: "%b, %Y"

auth:
Expand Down Expand Up @@ -607,8 +607,8 @@ et:
title: "Statistika"
market_share: "Turuosa ja jaotus"
market_share_growth: "Turuosa ja kasvumäär"
period: "Periood"
compare_to: "Võrdlusperiood"
period: "Kuu"
compare_to: "Võrdluskuu"
domains: "Domeenid"
registrars: "Registripidajad"
csv:
Expand Down
4 changes: 1 addition & 3 deletions spec/features/managing_stats_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
visit market_share_path
expect(page).to have_content('Stats')

page.fill_in 'search_start_date', with: '01.22'
page.fill_in 'search_end_date', with: '09.22'

click_button('Filter')
expect(page).to have_current_path(/search\[start_date\]=01.22/)
expect(page).to have_current_path(/search\[end_date\]=09.22/)
end
end

Expand All @@ -38,7 +37,6 @@
visit market_share_path(type: 'distribution')
expect(page).to have_content('Stats')

page.fill_in 'search_start_date', with: '01.22'
page.fill_in 'search_end_date', with: '09.22'

click_link('Download CSV')
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ae7c71e

Please sign in to comment.