-
Notifications
You must be signed in to change notification settings - Fork 1
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
Add Right To Know stats #5
Closed
Closed
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
c21ae82
Split up scraper into separate files, to improve maintainability
auxesis ae598a8
Update class names to match scraper
auxesis cd829a2
Make it easier to see if the run worked or not
auxesis 9127de2
Provide stub text to be overridden, so Jacaranda::Runner works standa…
auxesis 2e5ed94
Add tests for run method
auxesis d082969
Add Delorean for time based tests
auxesis 2b04e98
Test posted_in_last_fortnight by changing the clock
auxesis 999163e
Add #posts that filters based on runner type
auxesis 6e505f1
Generate unique URLs
auxesis 1a59f7d
Clean database after each test
auxesis d25cc09
Handle Slack post failures more gracefully
auxesis d58e0c8
Style fixes, per Rubocop
auxesis 0ba4933
Style improvements courtesy Rubocop
auxesis 34de330
Add tests for Right To Know scraper
auxesis edab6d7
Merge branch 'refactor' into righttoknow
auxesis 6d4265c
Merge branch 'master' into righttoknow
auxesis b0526d8
Remove traces of GitHub access tokens, as they're no longer needed
auxesis 427525e
Prepend all output with the name of the scraper
auxesis 6840d96
Display some helper text before the run
auxesis b797486
Print what the runner will do before doing anything
auxesis 965adfc
Add documentation explaining how to add new runners
auxesis 27eb30c
Fix method description
auxesis b4ebe9d
Add ability to filter which runners to run
auxesis 3ed18ba
Refactor tests per feedback from @equivalentideas
auxesis 70cc5e5
Set up and tear down database for every test
auxesis 43d6456
Refactor to use subject syntax
auxesis 25b41c8
Fix merge conflict from master
auxesis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
MORPH_SLACK_CHANNEL_WEBHOOK_URL="https://hooks.slack.com/services/XXXXXXXXXXXXX" | ||
MORPH_GITHUB_OAUTH_ACCESS_TOKEN=XXXXXXXXXXXXXXXXXXXXXXXXXXXXX | ||
MORPH_LIVE_MODE="false" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ Metrics/LineLength: | |
Metrics/BlockLength: | ||
Exclude: | ||
- 'spec/unit_spec.rb' | ||
- 'spec/runner_spec.rb' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ group :development do | |
end | ||
|
||
group :test do | ||
gem 'delorean' | ||
gem 'faker' | ||
gem 'rspec' | ||
gem 'rubocop' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'octokit' | ||
|
||
# PlanningAlerts contributor stats from GitHub | ||
class GitHub | ||
class << self | ||
def commits_text(period:) | ||
puts 'Collect information from GitHub' | ||
if commits_count(period: period).zero? | ||
nil | ||
else | ||
"You shipped #{commits_count(period: period)} commits in the same period." | ||
end | ||
end | ||
|
||
private | ||
|
||
def commits_count(period:) | ||
github = Octokit::Client.new | ||
github.auto_paginate = true | ||
repo = 'openaustralia/planningalerts' | ||
params = { since: period.first, until: period.last } | ||
commits = github.commits(repo, params) | ||
commits.size | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'mechanize' | ||
require 'rest-client' | ||
require 'json' | ||
require 'active_support/all' | ||
|
||
# Duck punches | ||
class Numeric | ||
def percent_of(n) | ||
to_f / n.to_f * 100.0 | ||
end | ||
end | ||
|
||
# PlanningAlerts stats from PlanningAlerts | ||
class PlanningAlerts | ||
class << self | ||
def new_subscribers_text(period:) | ||
before_period = determine_period_before(period) | ||
|
||
puts 'Collect new subscriber information from PlanningAlerts' | ||
period_count = count('new_alert_subscribers', period: period) | ||
period_before_count = count('new_alert_subscribers', period: before_period) | ||
|
||
[ | ||
period_count, | ||
'people signed up for PlanningAlerts last fortnight :revolving_hearts:', | ||
change_sentence(period_count, period_before_count) | ||
].join(' ') | ||
end | ||
|
||
def new_unsubscribers_text(period:) | ||
before_period = determine_period_before(period) | ||
|
||
puts 'Collect new unsubscriber information from PlanningAlerts' | ||
period_count = count('emails_completely_unsubscribed', period: period) | ||
period_before_count = count('emails_completely_unsubscribed', period: before_period) | ||
|
||
[ | ||
period_count, | ||
'people left.', | ||
change_sentence(period_count, period_before_count) | ||
].join(' ') | ||
end | ||
|
||
def total_subscribers_text | ||
puts 'Collect total subscribers information from PlanningAlerts' | ||
number = total_planningalerts_subscribers.round(-2) | ||
format = { precision: 0, delimiter: ',' } | ||
[ | ||
'There are now', | ||
ActiveSupport::NumberHelper.number_to_rounded(number, format), | ||
'PlanningAlerts subscribers! :star2:' | ||
].join(' ') | ||
end | ||
|
||
private | ||
|
||
def determine_period_before(period) | ||
(period.first.advance(weeks: -2)..period.last.advance(weeks: -2)).to_a | ||
end | ||
|
||
def total_planningalerts_subscribers | ||
# Memoize if we have fetched the data before | ||
return @subscribers_count if @subscribers_count | ||
# Otherwise pull the data from the PlanningAlerts website | ||
page = Mechanize.new.get('https://www.planningalerts.org.au/performance') | ||
@subscribers_count = page.at('#content h2').text.split(' ').first.to_i | ||
end | ||
|
||
def percentage_change_in_words(change) | ||
[ | ||
change.to_s.delete('-') + '%', | ||
(change.positive? ? 'more' : 'less') | ||
].join(' ') | ||
end | ||
|
||
def change_sentence(last_fortnight, fortnight_before_last) | ||
percentage_change_from_fortnight_before = last_fortnight.percent_of(fortnight_before_last) - 100 | ||
percentage_change_from_fortnight_before = percentage_change_from_fortnight_before.round(1).floor | ||
|
||
[ | ||
'That’s', | ||
percentage_change_in_words(percentage_change_from_fortnight_before), | ||
'than the fortnight before.' | ||
].join(' ') | ||
end | ||
|
||
def subscribers_data | ||
# Memoize if we have fetched the data before | ||
return @subscribers_data if @subscribers_data | ||
# Otherwise fetch the data, with a _long_ timeout. | ||
url = 'https://www.planningalerts.org.au/performance/alerts.json' | ||
response = RestClient::Request.execute(method: :get, url: url, timeout: 300) | ||
@subscribers_data = JSON.parse(response) | ||
end | ||
|
||
def count(attribute, period:) | ||
count = 0 | ||
|
||
period.each do |date| | ||
subscribers_data.each do |row| | ||
count += row[attribute] if row['date'] == date.to_s | ||
end | ||
end | ||
|
||
count | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'mechanize' | ||
|
||
# RightToKnow stats from RightToKnow | ||
class RightToKnow | ||
class << self | ||
def count(query, period:) | ||
start = period.first.strftime('%D') | ||
finish = period.last.strftime('%D') | ||
base = "https://www.righttoknow.org.au/search/#{query}%20#{start}..#{finish}.html" | ||
agent = Mechanize.new | ||
# TODO: This iterates through pages looking for one with trustworthy | ||
# results. It's guessing that the page number of the last page | ||
# of results is no greater than 10. This is based on Right To Know's current usage, | ||
# with a lot of padding built in. Currently the 3rd page is the last. | ||
# Remove this logic and just get the results, once | ||
# https://github.com/openaustralia/righttoknow/issues/673 is fixed. | ||
(1..10).to_a.reverse.each do |n| | ||
page = agent.get("#{base}?page=#{n}") | ||
return page.at('.foi_results').text.split.last if page.at('.foi_results') | ||
end | ||
end | ||
|
||
def new_requests_text(period:) | ||
[ | ||
':saxophone:', | ||
count('variety:sent', period: period), | ||
'new requests were made through Right To Know last fortnight.' | ||
].join(' ') | ||
end | ||
|
||
def annotations_text(period:) | ||
[ | ||
':heartbeat:', | ||
'Our contributors helped people with', | ||
count('variety:comment', period: period), | ||
'annotations.' | ||
].join(' ') | ||
end | ||
|
||
def success_text(period:) | ||
[ | ||
':trophy:', | ||
count('status:successful', period: period), | ||
'requests were marked successful!' | ||
].join(' ') | ||
end | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This implies you need the
--runners
option to run your runner—which is makes it unclear how this could run on morph.io. I think a sentence explaining that would be helpful.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.