Skip to content

Commit

Permalink
added final statistics task for country including generator task.
Browse files Browse the repository at this point in the history
  • Loading branch information
mattRedBox committed Dec 20, 2016
1 parent 04958de commit efb8359
Show file tree
Hide file tree
Showing 13 changed files with 234 additions and 41 deletions.
21 changes: 20 additions & 1 deletion app/controllers/statistics_controller.rb
Expand Up @@ -31,6 +31,25 @@ def all_organisations
}
end
end

def all_organisations_by_country
json_data = []
csv_data = create_country_csv_header()
CountryScoresDatum.all.each do |country_score|
next_json = JSON.parse(country_score[:data])
json_data << next_json
csv_data << create_country_csv_row(country_score[:name], @dimensions, next_json)
end
json_data << {:level_names => ["Initial", "Repeatable", "Defined", "Managed", "Optimising"]}
respond_to do |format|
format.json {
render :json => json_data
}
format.csv {
send_data csv_data, content_type: "text/csv; charset=utf-8"
}
end
end

def all_dgu_organisations
@dimensions = Questionnaire.current.dimensions
Expand Down Expand Up @@ -71,7 +90,7 @@ def summary
end
end

def country_summary
def summary_by_country
@countries = Country.all
data = []
other = {
Expand Down
21 changes: 19 additions & 2 deletions app/helpers/statistics_helper.rb
Expand Up @@ -7,7 +7,7 @@ def heatmap_colour(value)
def percentage(count, results)
return (count.to_f / results[:completed].to_f * 100).to_i
end

def create_public_scores(dimensions, scores)
data = {
completed: scores[:completed],
Expand Down Expand Up @@ -35,6 +35,24 @@ def create_csv(dimensions, scores)
end
return data
end

def create_country_csv_header()
data = CSV.generate({col_sep: ",", row_sep: "\r\n", quote_char: '"'}) do |csv|
csv << ["Country", "Theme", "Activity", "1-Initial", "2-Repeatable", "3-Defined", "4-Managed", "5-Optimising", "Completed", "Organisations"]
end
return data
end

def create_country_csv_row(country, dimensions, scores)
data = CSV.generate({col_sep: ",", row_sep: "\r\n", quote_char: '"'}) do |csv|
Questionnaire.current.dimensions.each do |dimension|
dimension.activities.each do |activity|
csv << [ country, dimension.title, activity.title ] + scores["themes"][dimension.title][activity.title] + [scores["completed"], scores["organisations"]]
end
end
end
return data
end

def create_summary_csv(summary)
data = CSV.generate({col_sep: ",", row_sep: "\r\n", quote_char: '"'}) do |csv|
Expand Down Expand Up @@ -90,7 +108,6 @@ def create_other_country_summary_data(other)
end

def create_country_summary_csv(country_summary)
# csv_summary = ["Statistic", "Value"] << summary
data = CSV.generate({col_sep: ",", row_sep: "\r\n", quote_char: '"'}) do |csv|
csv << ["Country", "Registered Users", "Organisations With Users", "Completed Assessments","Total Assessments","Questionnaire Version"]
country_summary.each do |cs|
Expand Down
5 changes: 5 additions & 0 deletions app/models/country.rb
@@ -1,6 +1,7 @@
class Country < ActiveRecord::Base
has_many :users
has_many :assessments, through: :users
has_many :statistics, class_name: "CountryScore"

validates :name, presence: true
validates :name, uniqueness: true
Expand All @@ -18,6 +19,10 @@ def as_json(options)
def users_with_organisations
return users.where("organisation_id is not null")
end

def users_with_no_organisation
return users.where("organisation_id is null")
end

def users_with_completed_assessments
return assessments.where("completion_date is not null")
Expand Down
3 changes: 3 additions & 0 deletions app/models/country_scores_datum.rb
@@ -0,0 +1,3 @@
class CountryScoresDatum < ActiveRecord::Base

end
15 changes: 8 additions & 7 deletions app/models/user.rb
Expand Up @@ -37,13 +37,14 @@ def associated_organisation
self.organisation
end

def associated_country=(name)
country = Country.where(name: name).first
self.country = country
end
def associated_country
self.country
end
def associated_country=(name)
country = Country.where(name: name).first
self.country = country
end
def associated_country
self.country
end

def latest_completed_assessment
return assessments.where("completion_date is not null").order(completion_date: :desc).first
end
Expand Down
56 changes: 47 additions & 9 deletions app/views/statistics/data.html.erb
Expand Up @@ -40,20 +40,24 @@
<td><a href="/statistics/summary.csv">CSV</a></td>
</tr>
<tr>
<td>Summary usage statistics by country</td>
<td><a href="/statistics/country_summary.json">JSON</a></td>
<td><a href="/statistics/country_summary.csv">CSV</a></td>
<td>Summary usage statistics by country</td>
<td><a href="/statistics/summary_by_country.json">JSON</a></td>
<td><a href="/statistics/summary_by_country.csv">CSV</a></td>
</tr>
<tr>
<td>Aggregate statistics: all organisational assessments</td>
<td><a href="/statistics/all_organisations.json">JSON</a></td>
<td><a href="/statistics/all_organisations.csv">CSV</a></td>
</tr>
<tr>
<td>Aggregate statistics: all organisational assessments by country</td>
<td><a href="/statistics/all_organisations_by_country.json">JSON</a></td>
<td><a href="/statistics/all_organisations_by_country.csv">CSV</a></td>
</tr>
<tr>
<td>Aggregate statistics: all assessments from data.gov.uk organisations</td>
<td><a href="/statistics/all_dgu_organisations.json">JSON</a></td>
<td><a href="/statistics/all_dgu_organisations.csv">CSV</a></td>

</tr>
</tbody>
</table>
Expand Down Expand Up @@ -100,20 +104,50 @@

<h4>Summary usage statistics by country</h4>
<p>
The summary usage statistics by country file groups usage statistics by <code>Country</code>.
The summary usage statistics by country data groups usage statistics by <code>Country</code>.
</p>
<ul>
<li>Where registered organisations have not yet nominated their country, their statistics are grouped under "Not specified".</li>
<li>Where registered users have not yet nominated their country, their statistics are grouped under "Not specified".</li>
<li>Where the number of assessments for a country is below the privacy threshold, statistics are grouped under "All other countries".</li>
</ul>

<table class="table">
<thead>
<tr><th>Field</th><th>Definition</th></tr>
</thead>
<tbody>
<tr>
<td><code>Country</code></td>
<td>A country name or a grouping where country isn't specified or would risk disclosing individual results.</td>
</tr>
<tr>
<td><code>Registered users</code></td>
<td>Number of users registered on the website.</td>
</tr>
<tr>
<td><code>Users with organisations</code></td>
<td>Number of users that have specified an organisation.</td>
</tr>
<tr>
<td><code>Completed assessments</code></td>
<td>Total number of completed assessments. A user can have multiple completed assessments.</td>
</tr>
<tr>
<td><code>Total assessments</code></td>
<td>Total number of assessments. A user can have multiple assessments, including one in-progress and several completed.</td>
</tr>
</tbody>
</table>

<h4>Aggregate statistics</h4>

<p>
Both of the aggregate statistics files have the same content but aggregate statistics for different collections of organisations.
The aggregate statistics files have similar content but aggregate statistics for different collections of organisations or group statistics by country.
</p>

<ul>
<li>"All organisations" refers to all users of the system</li>
<li>"All organisations" refers to all users of the system.</li>
<li>"All organisations by country" refers to all users of the system, grouped by country.</li>
<li>"All data.gov.uk organisations" refers to all users that have registered with an organisation defined in data.gov.uk.</li>
</ul>

Expand All @@ -128,6 +162,10 @@
<tr><th>Column</th><th>Definition</th></tr>
</thead>
<tbody>
<tr>
<td><code>Country</code></td>
<td>A country name or a grouping where country isn't specified or would risk disclosing an individual organisation's results (only included for statistics by country)</td>
</tr>
<tr>
<td><code>Theme</code></td>
<td>A theme defined in the open data maturity model.</td>
Expand Down Expand Up @@ -168,4 +206,4 @@
</tbody>
</table>

</div>
</div>
3 changes: 2 additions & 1 deletion config/routes.rb
Expand Up @@ -34,9 +34,10 @@
get '/statistics' => 'statistics#index'
get '/statistics/data' => 'statistics#data'
get '/statistics/all_organisations' => 'statistics#all_organisations', defaults: {format: :json}
get '/statistics/all_organisations_by_country' => 'statistics#all_organisations_by_country', defaults: {format: :json}
get '/statistics/all_dgu_organisations' => 'statistics#all_dgu_organisations', defaults: {format: :json}
get '/statistics/summary' => 'statistics#summary', defaults: {format: :json}
get '/statistics/country_summary' => 'statistics#country_summary', defaults: {format: :json}
get '/statistics/summary_by_country' => 'statistics#summary_by_country', defaults: {format: :json}
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".

Expand Down
11 changes: 11 additions & 0 deletions db/migrate/20161218004436_create_country_scores_data.rb
@@ -0,0 +1,11 @@
class CreateCountryScoresData < ActiveRecord::Migration
def change
create_table :country_scores_data do |t|
t.string :name
t.string :data

t.timestamps null: false
end
add_index :country_scores_data, :name, unique: true
end
end
9 changes: 9 additions & 0 deletions db/migrate/20161218043408_add_stats_to_country_scores.rb
@@ -0,0 +1,9 @@
class AddStatsToCountryScores < ActiveRecord::Migration
def change
add_column :country_scores, :initial, :integer
add_column :country_scores, :repeatable, :integer
add_column :country_scores, :defined, :integer
add_column :country_scores, :managed, :integer
add_column :country_scores, :optimising, :integer
end
end
4 changes: 2 additions & 2 deletions lib/organisation_scorer.rb
Expand Up @@ -58,7 +58,7 @@ def read_all_organisations
read_scores_from_statistics( Organisation.all_organisations_group, User.count )
end

def read_all_country_organisations(country)
def read_country_organisations(country)
read_scores_from_statistics( country, country.users_with_organisations.count )
end

Expand All @@ -79,7 +79,7 @@ def read_scores_from_statistics(group, count)
Questionnaire.current.activities.each do |activity|
results[:activities][activity.name] = [0,0,0,0,0]
end
results[:completed] = group.statistics.first.completed_assessments
results[:completed] = group.statistics.first ? group.statistics.first.completed_assessments : 0
group.statistics.each do |organisation_score|
results[:activities][organisation_score.activity.name] =
[organisation_score.initial,
Expand Down

0 comments on commit efb8359

Please sign in to comment.