Skip to content

Instantly share code, notes, and snippets.

View cmorss's full-sized avatar

Charlie Morss cmorss

View GitHub Profile
@cmorss
cmorss / response_summary.rb
Created July 14, 2016 16:40
Create answer summaries on survey responses
Mongoid.default_session["survey_responses"].find.each_with_index do |response, index|
summary = {}
other_text_summary = {}
(response["answers"] || []).each do |answer|
if answer["_type"] == "Survey::Answers::Multiselect"
choices = (answer["selections"] || []).map { |s| s && s["answer_choice_id"] && s["answer_choice_id"].to_s }
summary[answer["question_id"].to_s] = choices if choices.present?
other = (answer["selections"] || []).detect { |s| s && s["text"].present? }
@cmorss
cmorss / mongo-connect-production.log
Created July 15, 2016 16:22
Simple mongo query with connection issues then a very slow query.
MOPED: 127.0.0.1:27017 COMMAND database=admin command={:ismaster=>1} (0.3254ms)
MOPED: Could not connect to any node in replica set <Moped::Cluster nodes=[<Moped::Node resolved_address="127.0.0.1:27017">]>, refreshing list.
MOPED: 127.0.0.1:27017 COMMAND database=admin command={:ismaster=>1} (0.9255ms)
MOPED: 127.0.0.1:27017 COMMAND database=apptentive_production command={:count=>"survey_responses", :query=>{"deleted_at"=>nil, "survey_id"=>"572bdf52fd7381d6ee000004", "summary.572bdf52fd7381d6ee000008"=>{:$exists=>true}}} (555.3071ms)
=> 4838
@cmorss
cmorss / reviews_report.rb
Created August 3, 2016 17:46
Start_date changes
rails.logger.debug "[reviews report] building review data for app (#{app.id})"
token = make_jwt_token(user, app)
start_date = if user.last_review_email.present? && !env["force_emails"]
(user.last_review_email - 1.day).to_date
else
30.days.ago.to_date
end
start_date = [start_date, app.organization.active_entitlement.days_of_reviews_history.days.ago.to_date].max
@cmorss
cmorss / daily_reviews_emails.html.haml
Created August 3, 2016 17:47
change to start date
- for id, app in @email[:app_reviews]
- reviews_link = "#{root_url}apps/#{id}/reviews?start_date=#{app[:start_date].to_s}&end_date=#{@email[:end_date].to_s}&#{ga_suffix}"
- store_name = app[:store].downcase == 'itunes' ? 'App Store' : 'Google Play'
@cmorss
cmorss / response.rb
Created August 16, 2016 23:04
Response.rb. Updated to include error subclass.
class Response
attr_accessor :data
delegate :[], :map, :select, :each, :first, :inject, to: :data
def initialize(data = nil)
self.data = data
end
def value
module PullUpErrors
extend ActiveSupport::Concern
# Used to pull up errors that are on an associated collection or object
included do
after_validation :pull_up_errors
def pull_up_errors(*attrs)
@pull_up_attrs = Array(attrs)
@cmorss
cmorss / dau.rb
Last active February 3, 2017 17:06
This will use Stuart's retention service (which pulls from Redis and stores in Cassandra) to get DAU for the specified dates and apps.
require 'faraday'
require 'faraday_middleware'
require 'active_support/all'
def period(app_id, start_date, end_date)
date = start_date
results = []
while date <= end_date do
results << { date: date.strftime("%F"), dau: day(app_id, date) }
date = (date + 1)
@cmorss
cmorss / orm.dsl.js
Last active November 7, 2019 22:52
// In express middleware when a request comes in we start a transaction
// and store it in continuation local store (think thread local store)
// Create a new user in the DB and return the newly created User instance
let user = await User.create({name: "Sally Smith", email: "sally@gmail.com"});
user.setAddressStreet1("123 Main St."); // With no Typescript
user.addressStreet1 = "123 Main St."; // If we introduce Typescript this would be okay too
def create
self.resource = resource_class.send_confirmation_instructions(resource_params)
yield resource if block_given?
render_resource(resource, blueprint: :admin)
end
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
def google_oauth2
auth = request.env['omniauth.auth']
manager = Manager.where(provider: auth['provider'], uid: auth['uid'])
.first_or_initialize(email: auth['info']['email'])
manager.first_name = auth['info']['first_name']
manager.last_name = auth['info']['last_name']
manager.save!