Friday, 24 May 2019

Copy Dump from server to local machine


~/Desktop$ scp -i ~/Desktop/permission/h2w/postgres-dump.pem ubuntu@0.0.0.0:./dumpfile_name ./

Friday, 26 October 2018

Create Dynamic CSV with custom data, Stored in tmp directory and upload to AWS S3 Bucket without any rails uploder method.

1. Add gem "aws-sdk-s3" and bundle install
 
  require 'aws-sdk-s3'
  # AWS S3 bucket upload method
  def self.upload_csv_to_s3_bucket
    s3 = Aws::S3::Resource.new(region: REGION_S3, access_key_id: ACCESS_KEY_ID_S3, secret_access_key: SECRECT_ACCESS_KEY_S3)
    file = "#{Rails.root}/tmp/file_name_#{Date.today}.csv"
    name = File.basename(file)
    bucket = BUCKET_S3
    obj = s3.bucket(bucket).object(name)
    metadata = {"answer" => "42"}
    obj.upload_file(file, metadata: metadata)
    # this below line of code used to print file name and S3 File ULR
    bucket = s3.bucket(bucket)
    bucket.objects.limit(50).each do |item|
      puts "Name:  #{item.key}"
      puts "URL:   #{item.presigned_url(:get)}"
    end
  end
 
  def self.csv_generate(header, total_with_times, account_id, file_name)
    begin
    result = CSV.generate(headers: true) do |csv|
      csv << header
      total_with_times.each do |val|
        csv << val
      end
    end
    # Delete old CSV file for creating new one
    File.delete("#{Rails.root}/tmp/#{ file_name }_#{ Date.today - 1.days }.csv") rescue ''
    # Creating new CSV file for all accounts
    File.open("#{Rails.root}/tmp/#{ file_name }_#{ Date.today }.csv", 'w:UTF-8') { |file| file.write(result)}
    upload_csv_to_s3_bucket
  rescue Exception => e
    puts "<<<>>>>>> | #{e.message}| <<<<<<<>>>>>"
  end
  

Friday, 20 April 2018

cassandra configuration setting



Cassandra Configuration setting for initialization session between cassandra and application.

require 'cassandra'
class CassandraPushCluster
  CASSANDRA_KEYSPACE = 'mmm_push_event'
  CASSANDRA_HOSTS = ["127.0.0.1"]

  def self.session
    @@session ||= Cassandra.cluster(hosts: CASSANDRA_HOSTS).connect(CASSANDRA_KEYSPACE)
  end

  def self.validate_date(date)
    formated_date = date.nil? ? Cassandra::Types::Timestamp.new(Time.zone.now) : Cassandra::Types::Timestamp.new(date)
  end
end



-------------query for session prepare--------------------

event_tracking = CassandraPushCluster.session.prepare("INSERT INTO push_event_tracking_#{params[:account_id].to_i} (account_id, push_application_id, push_device_id, name, content_id, title, description, category, total_value, latitude, longitude, created_at, session_id, platform) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")


--------- execute session query -----------

CassandraPushCluster.session.execute(event_tracking, arguments: [params[:account_id].to_i, params[:push_application_id].to_i, params[:push_device_id].to_i, params[:event_name], params[:content_id].to_i, params[:title], params[:description], params[:category], params[:value].to_f, params[:latitude].to_f, params[:longitude].to_f, Time.zone.now, params[:session_id].to_i, params[:platform].to_i])

Elastic Search Index create dynamically as account wise.


-------- step - 1 copy and paste this code in application.rb  -----------------------

require 'elasticsearch'
class ElasticsearchPushCluster
  ELASTICSEARCH_HOSTS = ['http://127.0.0.1:9200']
  def self.session
    @@session ||= Elasticsearch::Client.new hosts: ELASTICSEARCH_HOSTS
  end
end



-----------step 2 copy and paste this code  inside model ----------------------

 def self.elastic_index_record(params, hash_record)
     elastic_hash  = {
          account_id: params[:account_id].to_i,
          push_application_id: params[:push_application_id].to_i,
          push_device_id: params[:push_device_id].to_i,
          event_name: params[:event_name],
          custom_attributes: "#{ hash_record }",
          latitude: params[:latitude],
          longitude: params[:longitude],
          created_at: Time.zone.now,
          updated_at: Time.zone.now,
          session_id: params[:session_id]
        }
    client = ElasticsearchPushCluster.session
    response_record = client.index  index: "push_event_tracking_#{ params[:account_id] }", type: "push_event_tracking", body: elastic_hash
    elastic_search  =  ElasticsearchPushCluster.session.search index: "push_event_tracking_#{ params[:account_id].to_i }", body: { query: { match: { event_name: "#{params[:event_name]}" } }, sort: [{ "created_at": { "order": "desc" } } ] }
    elastic_response = elastic_search["hits"]["hits"].first["_source"]
    return elastic_response
end

Monday, 26 March 2018

Create migration with account wise

  self.table_name = "push_event_trackings1"

  def self.for account_id
    tname = "push_event_trackings#{account_id}"
    model_name = tname.classify
    if Object.const_defined?(model_name)
      fields_class = model_name.constantize
    else
      fields_class = Object.const_set(model_name, Class.new(PushTracking){
        self.table_name = tname
      })
    end
    return fields_class.using(:sms)
  end
 
  def self.create_table account_id
    fields_table_name = "push_event_trackings#{account_id}"
    Octopus.using(:sms) do
      ActiveRecord::Migration.execute "CREATE TABLE `#{fields_table_name}` (
        `id` bigint(20) NOT NULL AUTO_INCREMENT,
        `push_device_id` int(11) DEFAULT NULL,
        `push_event_id` int(11) DEFAULT NULL,
        `cart_id`  int(11) DEFAULT NULL,
        `sku` varchar(255) DEFAULT NULL,
        `description` varchar(255) DEFAULT NULL,
        `quantity` int(11) DEFAULT NULL,
        `value` float DEFAULT NULL,
        `product_category` varchar(255) DEFAULT NULL,
        `brand` varchar(255) DEFAULT NULL,
        `model_number` varchar(255) DEFAULT NULL,
        `order_id` varchar(255) DEFAULT NULL,
        `order_date` datetime DEFAULT NULL,
        `order_status` varchar(255) DEFAULT NULL,
        `coupon_code` varchar(255) DEFAULT NULL,
        `campaign_id` int(11) DEFAULT NULL,
        `session_id` int(11) DEFAULT NULL,
        `platform` int(11) DEFAULT NULL,
        `price` float DEFAULT NULL,
        `content_id` int(11) DEFAULT NULL,
        `title` varchar(255) DEFAULT NULL,
        `category` varchar(255) DEFAULT NULL,
        `event_data` text COLLATE utf8_unicode_ci,
        `latitude` float DEFAULT NULL,
        `longitude` float DEFAULT NULL,
        `created_at` datetime NOT NULL,
        `updated_at` datetime NOT NULL,
        PRIMARY KEY (`id`)
      ) ENGINE=InnoDB DEFAULT CHARSET=utf8"
      ActiveRecord::Migration.add_index(fields_table_name, :push_device_id)
      ActiveRecord::Migration.add_index(fields_table_name, :push_event_id)
    end
  end

  run migration on console according to account id
  account = Account.first
  PushEvenTracking.create account.id

truncate word with screen resolution

.wrap{
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 100%;
  }

Check Query Execution Time in spark

def time[A](f: => A) = {
  val s = System.nanoTime
  val ret = f
  println("time: " + (System.nanoTime - s) / 1e9 + " seconds")
  ret
  }
val test = time('statement')


val test = time(spark.sql("SELECT * from mmc_push_event.push_devices"))

Revert last commit or second last and more....

 Git revert commit_id -m 1 this command willl revert last commit  Git revert commit_id -m 2 this command will revert second commit with same...