Skip to content

Instantly share code, notes, and snippets.

@abhishekgupta5
Created August 10, 2020 12:08
Show Gist options
  • Select an option

  • Save abhishekgupta5/3e83ffc711d05dc02a32f909b8ba863e to your computer and use it in GitHub Desktop.

Select an option

Save abhishekgupta5/3e83ffc711d05dc02a32f909b8ba863e to your computer and use it in GitHub Desktop.
def check_source_level(collection_class, com)
ac_ids = collection_class.upsert_processor.where(company_id: com.id).ids
ac_id_hashes = ac_ids.map { |x| BaseMongoObject.hash_string(x.to_s) }
chunk_id_hashes = DailyShardingStrategy.date_range_to_chunk_hashes(Date.current - 30.months, Date.current)
res = {}
chunk_id_hashes.each do |chunk_id_hash|
filter = { collection_class.upsert_processor.ad_account_field.to_s =>{"$in"=>ac_ids},
collection_class.upsert_processor.ad_account_id_hash_field => { "$in" => ac_id_hashes },
"chunk_id_hash": { "$in" => [chunk_id_hash]},
"custom_tags.Account.source": { "$exists" => true }}
filter.deep_symbolize_keys!
res[chunk_id_hash] = collection_class.where(filter).count()
end
return com.name, res
end
def run(collection_class)
final = {}
coms = Company.for_cron
coms.each do |com|
com_name, res = check_source_level(collection_class, com)
final[com_name] = res
end
return final
end
x = run(MongoAdwordsAdAccount)
#############
# Before
"Mindvalley" => {
-8626473382729720566 => 36,
-4476903877563359736 => 38,
-8194574768892368174 => 41,
4930267415046325021 => 45,
8536508039064870376 => 50,
620756700196973858 => 57,
-1133216534974650472 => 73,
4801315784669281817 => 78,
-7025915397911870649 => 0,
4275926723378863257 => 0
}
# After
"Mindvalley" => {
-8626473382729720566 => 0,
-4476903877563359736 => 0,
-8194574768892368174 => 0,
4930267415046325021 => 0,
8536508039064870376 => 0,
620756700196973858 => 0,
-1133216534974650472 => 0,
4801315784669281817 => 0,
-7025915397911870649 => 0,
4275926723378863257 => 0
}
@anmolarora
Copy link

Bro, this is not a secret gist

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment