Monday, May 13, 2019

MySql Quey for Rank According to marks

SET @rownum := 0;
SELECT rank, student_marks FROM (
                    SELECT @rownum := @rownum + 1 AS rank, student_marks, student_Id
                    FROM student ORDER BY student_marks DESC
                    ) as result WHERE student_Id=3;


SELECT
 s1.student_name, COUNT(DISTINCT s2.student_marks) AS rank
FROM
  student s1 JOIN student s2 ON (s1.student_marks <= s2.student_marks)
GROUP BY s1.student_Id;

SELECT
  student_Id, student_name, student_marks,
  @prev := @curr,
  @curr := student_marks,
  @rank := IF(@prev = @curr, @rank, @rank+1) AS rank
FROM
  student,
  (SELECT @curr := null, @prev := null, @rank := 0) sel1
ORDER BY student_marks DESC;

Django :: Disable Delete Action


Django :: Disable Delete Action :-

site-packages/django/contrib/admin/options.py :-

class BaseModelAdmin(six.with_metaclass(RenameBaseModelAdminMethods)):
   
     def has_delete_permission(self, request, obj=None):

           return False  # Add this line for diable Delete Action

sites.py :-

class AdminSite(object):

   def __init__(self, name='admin', app_name='admin'):
       #self._actions = {'delete_selected': actions.delete_selected} # Comment this line
        self._actions = {} # add this line

Setup Git Server

Reference :- http://www.jeramysingleton.com/installing-gitolite/ & http://www.jeramysingleton.com/installing-gitolite/
https://github.com/sitaramc/gitolite

Setting up a gitolite server in Ubuntu:-

Run Below cmd on Server:-
sudo apt-get install git

sudo adduser \
  --system \
  --shell /bin/bash \
  --gecos 'git version control' \
  --group \
  --disabled-password \
  --home /home/git \
  git

sudo su git #su - git

cd ~

mkdir ~/bin

git clone git://github.com/sitaramc/gitolite

gitolite/install -ln ~/bin

ls ~/bin

Run below cmd on local machine :-

ssh-keygen or ssh-keygen -C "saurabh"


cp .ssh/id_rsa.pub saurabh.pub

scp saurabh.pub git@gitserver:/home/git

Server :-
export PATH=/home/git/bin:$PATH

gitolite setup -pk saurabh.pub

local System :-
git clone git@gitserver:gitolite-admin

#For Alias

less ~/.ssh/config

Host alias
   Hostname gitserver
    User git
    IdentityFile ~/.ssh/saurabh

##########################

ssh git@gitserver help


svn log -q | awk -F '|' '/^r/ {sub("^ ", "", $2); sub(" $", "", $2); print $2" = "$2" <"$2">"}' | sort -u > authors-transform.txt


git svn clone http://192.168.1.7/cbs_img --trunk=.  --authors-file=users.txt  -s cbs_img


cd cbs_img

#git init
git remote add origin git@gitserver:cbs_img.git

git push origin --all


git svn rebase

git svn dcommit

git pull origin master

git svn dcommit

# for change into svn
git commit -am 'Adding git-svn instructions to the README'

git svn dcommit


####
git update-ref refs/heads/master refs/remotes/git-svn

git config svn.authorsfile users.txt

git svn clone http://192.168.1.7/cbs_pwa --trunk=.  --authors-file=users.txt  -s cbs_pwa --username=saurabh


############# Integrating Jenkins with Gitolite ##################
Ref Url :- https://wiki.jenkins.io/display/JENKINS/Gitolite

cd /var/lib/jenkins
/var/lib/jenkins$ sudo -u jenkins ssh-keygen
sudo -u jenkins cat .ssh/id_rsa.pub
~/gitolite-admin$ vim keydir/jenkins.pub


12:04:37 (master) ~/gitolite-admin$ vim conf/gitolite.conf
@development_team = bob carol ted alice

repo gitolite-admin
    RW+     =   hesco

repo myproject
    RW+     =   hesco
    RW      =   @development_team
    R       =   jenkins

git add keydir/jenkins.pub conf/gitolite.conf

git commit keydir/jenkins.pub conf/gitolite.conf

git push origin

/var/lib/jenkins/workspace/myproject$ sudo -u jenkins git clone git@gitserver:myproject

/var/lib/jenkins/workspace$ sudo -u jenkins rmdir myproject


sudo apt install -f # for install dependency

/var/lib/jenkins/secrets/initialAdminPassword # jenkins admin pwd




rsync -avzh --cvs-exclude /var/lib/jenkins/workspace/newcommunity_dev/ community@192.168.20.140:/home/product/community/saurabh/.


sudo su jenkins

sshpass -p "Devtest12" rsync -avzh --cvs-exclude /var/lib/jenkins/workspace/newcommunity_dev/ community@192.168.20.140:/home/product/community/saurabh/.


**************************************Git-web****************
https://gist.github.com/peter279k/6ac3a8a8ef2e1f24a48679713af50969



###################################33
cd REPONAME
git init
git add .
git commit -m 'initial commit' -a
git remote add origin git@gitserver:.git
git push origin master:refs/heads/master
git push --set-upstream origin master

Mysql :: Delete Duplicate Records

# Delete Duplicate Tag


Topic Table :-
id primary key
topic_slug
topic_name

Tag Table:-
id primary key
tag_slug (not unique)
tag_name

Topic Tag Table (Relation table):-
id primary key
topic_id (Foreign key)
tag_id (Foreign key)
topic_id,tag_id key (not unique)

Due to non unique duplicate records occur , Now we have to remove duplicate records, So Mysql query :-

select min(id),group_concat(id) from tag t2 group by slug having count(slug) > 1;

create table temp_tag as select t1.id as t1_tag_id,t2.id as t2_tag_id FROM  tag t1, tag t2 WHERE t1.id < t2.id AND t1.slug = t2.slug;

update tag,temp_tag set tag_id = t1_tag_id where t2_tag_id = tag_id;

DELETE t2 FROM tag t1, tag t2 WHERE t1.id < t2.id AND t1.slug = t2.slug;

drop table temp_tag;

FCM (Firebase Cloud Messaging) :: User not receive offline message

              FCM (Firebase Cloud Messaging) ::  User not receive offline message 
When we move from GCM (Google Cloud Messaging) to FCM (Firebase Cloud Messaging) . During Testing we find out there is an issue in FCM i.e User not receive offline message . FCM API HTTP Response code is 200 :-

{"multicast_id":********,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"******"}]}

Then , I report this issue to https://firebase.google.com/support  by filling necessary Details on there support URL :- https://support.google.com/firebase/contact/support?page=/fcm/delivery/diagnose/web/data .

During meantime I was checked by manipulating API Params . Then I find out adding time_to_live with some value for example 2419200 #four weeks its working fine . 

Same I communicate to @kat (firebase-help@google.com Support team) because it is bug from FCM . As per FCM document :- https://firebase.google.com/docs/cloud-messaging/http-server-ref#downstream-http-messages-plain-text it is optional and the default value is 4 weeks.
Below mail send by google (firebase-help@google.com) :-

Hi Saurabh, 

Thanks for the update. Happy to hear that adding time_to_live helped fix the issue.

This sounds like a bug, though. As you've noted from our documentation, requests that don't contain this field default to the maximum period of four weeks.

...
Regards, 
Kat


In Node Server we send notification by using below code :-


var request = require('request'); // npm i request                
if (TTL) {
                        TTL = 2419200; // Default TTL is four weeks.
 }      
// Before Background notificatin fix
//var postData ='{"data":{"title":"'+title+'","body":"'+body+'","icon":"'+ImageIcon+'","click_action":"'+weburl+'","requireInteraction":true,"tag":"'+Math.random()+'"},"to":"'+endpoint+'"}'; 

 var postData ='{"data":{"title":"'+title+'","body":"'+body+'","icon":"'+ImageIcon+'","click_action":"'+weburl+'","requireInteraction":true,"tag":"'+Math.random()+'"},"to":"'+endpoint+'","priority":10,"time_to_live": '+TTL+'}';
              var url = 'https://fcm.googleapis.com/fcm/send';
                var options = {
                        method: 'post',
                        body: JSON.parse(postData),
                        json: true,
                        url: url,
                        headers: {'content-type' : 'application/json',
                        'Authorization':'key=*********,'Urgency':'high'}
                }
                request(options, function (err, res, body) {
                        if (err) {
                                console.error('error posting json: ', err)
                                throw err
                        }
                                //console.log(res);
                        var headers = res.headers;
                        var statusCode = res.statusCode;


Friday, April 29, 2016

MySql Query - find records from one table which don't exist in another

MySql Query - find records from one table which don't exist in another:-

Table 1:-

social_auth_email

email varchar(75)

Table 2 :-

 studentdetail

email varchar(75)

Then below query for find non duplicate records :-

SELECT sa.email  FROM   social_auth_email as sa LEFT OUTER JOIN studentdetail as htc   ON (sa.email=htc.email)   WHERE htc.email IS NULL

Wednesday, December 23, 2015

Django :: IOError: encoder error -2 when writing image file


exceptions:IOError: encoder error -2 when writing image file

Traceback (most recent call last):
 File "/django_virtalenv//lib/python2.7/site-packages/newrelic-2.14.0.11/newrelic/hooks/framework_django.py", line 492, in wrapper return wrapped(*args, **kwargs)
 File "/django_virtalenv//lib/python2.7/site-packages/django/contrib/admin/options.py", line 465, in wrapper return self.admin_site.admin_view(view)(*args, **kwargs)
 File "/django_virtalenv//lib/python2.7/site-packages/django/utils/decorators.py", line 99, in _wrapped_view response = view_func(request, *args, **kwargs)
 File "/django_virtalenv//lib/python2.7/site-packages/django/views/decorators/cache.py", line 52, in _wrapped_view_func response = view_func(request, *args, **kwargs)
 File "/django_virtalenv//lib/python2.7/site-packages/django/contrib/admin/sites.py", line 198, in inner return view(request, *args, **kwargs)
 File "/django_virtalenv//lib/python2.7/site-packages/django/utils/decorators.py", line 29, in _wrapper return bound_func(*args, **kwargs)
 File "/django_virtalenv//lib/python2.7/site-packages/django/utils/decorators.py", line 99, in _wrapped_view response = view_func(request, *args, **kwargs)
 File "/django_virtalenv//lib/python2.7/site-packages/django/utils/decorators.py", line 25, in bound_func return func(self, *args2, **kwargs2)
 File "/django_virtalenv//lib/python2.7/site-packages/django/db/transaction.py", line 371, in inner return func(*args, **kwargs)
 File "/django_virtalenv//lib/python2.7/site-packages/django/contrib/admin/options.py", line 1263, in change_view self.save_model(request, new_object, form, True)
 File "/data/cms/trunk/cms/articles/admin.py", line 125, in save_model obj.save()
 File "/django_virtalenv//lib/python2.7/site-packages/django/db/models/base.py", line 545, in save force_update=force_update, update_fields=update_fields)
 File "/django_virtalenv//lib/python2.7/site-packages/django/db/models/base.py", line 573, in save_base updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
 File "/django_virtalenv//lib/python2.7/site-packages/django/db/models/base.py", line 632, in _save_table for f in non_pks]
 File "/django_virtalenv//lib/python2.7/site-packages/django/db/models/fields/files.py", line 252, in pre_save file.save(file.name, file, save=False)
 File "/data/cms/trunk/cms/utils/thumbs.py", line 93, in save thumb_content = generate_thumb( content, size, split[1] )
 File "/data/cms/trunk/cms/utils/thumbs.py", line 60, in generate_thumb image2.save( io, "JPEG", quality = 90, optimize=True, progressive=True)
 File "/django_virtalenv//lib/python2.7/site-packages/PIL/Image.py", line 1439, in save save_handler(self, fp, filename)
 File "/django_virtalenv//lib/python2.7/site-packages/PIL/JpegImagePlugin.py", line 471, in _save Image
 File._save(im, fp, [("jpeg", (0,0)+im.size, 0, rawmode)])
 File "/django_virtalenv//lib/python2.7/site-packages/PIL/Image
 File.py", line 491, in _save raise IOError("encoder error %d when writing image file" % s)IOError: encoder error -2 when writing image file

Soln :- pip install Pillow

Thursday, December 17, 2015

Performance Guidelines for making webpages load more efficiently / Page Speed

Sno Activity Definition
1 Minimize HTTP Requests Each image, script file, css and other external files are counted as 1 HTTP request individually
2 Use a Content Delivery Network CDNs allow faster downloading of files at Users end, as they have tie ups with ISPs directly.
Similar to LAN transfer. Eg. Akamai
3 Avoid empty src or href IE - Makes a request to directory in which the page is located
Chrome/Safari/Firefox - Make request to original page
This can create Errors and useless Extra Traffic at Web Server
4 Add an Expires or a Cache-Control Header For static components: implement "Never expire" policy by setting far future Expires header
For dynamic components: use an appropriate Cache-Control header to help the browser with conditional requests
5 Gzip Components Gzip is a compression algorithum which can reduce ~70% file size of static/text based content.

Smaller file size, faster loading and lesser bandwidth consumed.
6 Put StyleSheets at the Top Stylesheets (CSS) files by nature can be parallely loaded along with other static content like images.
Loading them early also helps in faster rendering of page.
7 Put Scripts at the Bottom Browsers open 2 connections per Hostname (aka Parallel Downloading.)
While a script is downloading the browser won't start any other downloads, even on different hostnames.
8 Avoid CSS Expressions Used to set CSS properties dynamically. Especially for IE. Powerful and dangerous. As they are recalculated on every mouse movement and keyboard input.
9 Make JavaScript and CSS External Reduces size of main HTML page at the cost of extra HTTP requests. However, if CSS/JS are cached properly then HTTP requests can also be saved.
10 Reduce DNS Lookups DNS maps hostnames to IP addresses, just as phonebooks map people's names to their phone numbers. Takes 20-120 mil secs to resolve a hostname to IP. Browser cannot download anything uptill the DNS is not resolved.
11 Minify JavaScript and CSS Minification is the practice of removing unnecessary characters from code to reduce its size thereby improving load times.
12 Avoid Redirects Redirects slow down the user experience.
Inserting a redirect between the user and the HTML document delays everything in the page since nothing in the page can be rendered and no components can start being downloaded until the HTML document has arrived.
13 Remove Duplicate Scripts If a JS file is referred to twice for same page:
IE - Will download it again, even if JS is cached
However, all browsers even if they don't download it, will always execute it twice, that is unwated time spent on processing.
14 Configure ETags Entity tags (ETags) are a mechanism that web servers and browsers use to determine whether the component in the browser's cache matches the one on the origin server.
Can be a boon or a curse.
Single web server - Definitely a boon
Multiple web servers - Needs evaluation, can even degrade performance
15 Make AJAX Cacheable Ajax responses should be made cacheable, this will significantly improve User Experience
16 Use GET for AJAX Requests Ajax requests (XMLHttpRequest)
POST - Sends headers then sends Data.
GET - Sends Data directly. However, cap on URL length is 2,000 chars, data larger than that should be sent using POST method.
17 Reduce the Number of DOM Elements A complex page design means more bytes to download and it also means slower DOM access in JavaScript.
It makes a difference if you loop through 500 or 5000 DOM (design/HTML) elements on the page when you want to add an event handler for example.
To get count of DOM elements FireBug > Console >
document.getElementsByTagName('*').length
Ideal size - 500 to 700 elements for a very busy page
18 No 404s HTTP requests are costly and getting useless response will further bring down user experience.
1) Some sites keep helpful 404 pages, however this can lead to unwanted DB hits eg. "Did you mean X".
2) When external JS files return 404.
a) First it blocks browser from loading anything else,
b) Secondly the browser might try to parse the error page to find something useable.
19 Reduce Cookie Size Bloating cookies with unwanted information can also slow down user responses. As cookies travel with every request user makes.
Cookie size - <400 Bytes for any single cookie
20 Use Cookie-Free Domains for Components As cookies travel with each request, static content (images/css/etc) that don't require them should be served from cookie free domains as this will speed up trasmission.
21 Avoid Filters The IE-proprietary AlphaImageLoader filter aims to fix a problem with semi-transparent true color PNGs in IE versions < 7.
Blocks rendering
Freezes the browser
Increases memory comsumption
Applied per element, not per image, so the problem is multiplied.
Solution -
1) Use gracefully degrading PNG8 instead
2) If you absolutely need AlphaImageLoader, use the underscore hack _filter as to not penalize your IE7+ users.
22 Do Not Scale Images in HTML Optimize image resolution to match front end requirement don't scale down a large size image in HTML.
23 Make favicon.ico Small and Cacheable It's a necessary evil and Interferes with the download sequence. To set it right
Size - <1 Kb
Cache - Long term
Serving - Cookie-less domain
24 Avoid bad requests Same as No 404, bad requests are generated by User (4xx) and Server (5xx) - So all 4xx and 5xx series errors should be addressed
25 Avoid CSS @import Changes default nature of CSS download from Parallel to Serial. This is similar to loading CSS at the end of the page, which is a bad practice.
26 Avoid CSS expressions As mentioned earlier
27 Avoid document.write If external resources (JS/CSS/Images/etc) are invoked via document.write they cannot be prefetched.
28 Combine external CSS Club all CSS into 1 file
29 Combine external JavaScript Club all JS into 1
30 Combine images using CSS sprites Each request incurs a fixed amount of request overhead. Reduce this overhead from one request per image to one request for the entire sprite.
31 Defer loading of JavaScript Identify JS functions not used by document before onload event. Load them seperately using event handler.

Alternately "defer" can be used as a mode in HTML4/5
<script type="text/javascript" defer="defer">
alert(document.getElementById("p1").firstChild.nodeValue);
</script>
32 Defer parsing of JavaScript
33 Enable compression Same as Gzip components
34 Leverage browser caching Setting an Expiry/Max-age header for a resource helps browsers to cache items locally in user machines
35 Leverage proxy caching Enabling public caching in the HTTP headers for static resources allows the browser to download resources from a nearby proxy server rather than from a remote origin server.
This means that even first-time users to your site can benefit from caching. Similar to CDN.
36 Make landing page redirects cacheable Redirects slow down the user experience. If they are not cached to do so:
Expires or Cache-Control header must be added to response
Mobile phone redirects should be handled seperately
37 Minify CSS Similar to Minify JavaScript and CSS
38 Minify HTML Compacting HTML code, including any inline JavaScript and CSS contained in it, can save many bytes of data and speed up downloading, parsing, and execution time.
39 Minify JavaScript Similar to Minify JavaScript and CSS
40 Minimize request size HTTP request should not go beyond 1 packet. That is ~1500 bytes.
Cookie size - < 400 Bytes.
URL length - < 500 Bytes including parameters
Browser-fields - Request/Response headers, only set required ones
41 Minimize DNS lookups Similar to Reduce DNS lookups
42 Minimize redirects Similar to Avoid redirects
43 Optimize images Choose an appropriate image file format
PNGs are almost always superior to GIFs
GIFs to be used for very small or simple graphics
JPG for photographic style images
BMP and Tiff to be avoided
Use an image compressor
JPG - JPEGtran or JPEGoptim
PNG - OptiPNG or PNGout
44 Optimize the order of styles and scripts <head>
<!-- Title -->
<!-- Meta -->
<!-- CSS -->
<!-- Favicon -->
<!-- JS if required (inline/external) -->
</head>
45 Parallelize downloads across hostnames 100 resources
4 hosts
each host should serve 25 resources
no one host should serve more than 38 resources

*** Also many browsers do not download JavaScript files in parallel, so there is no benefit from serving them from multiple hostnames
46 Prefer asynchronous resources Fetching resources asynchronously prevents those resources from blocking the page load.

Example
<script>
var node = document.createElement('script');
node.type = 'text/javascript';
node.async = true;
node.src = 'example.js';
// Now insert the node into the DOM, perhaps using insertBefore()
</script>
47 Put CSS in the document head Similar to Put stylesheets at the top
48 Remove unused CSS Removing or deferring style rules that are not used by a document avoid downloads unnecessary bytes and allow the browser to start rendering sooner
49 Serve resources from a consistent URL Shared resources across multiple pages in a site such as images, .css and .js files should always be served from a consistent URL. This greatly helps in reducing bandwidth comsumption for both User and Server.
50 Serve scaled images If a page contains a large image and thumbnail of the same is to displayed, the same large image can be scaled down and reused, provided the aspect ratio for thumbnail is maintained.
51 Serve static content from a cookieless domain Similar to Use Cookie-Free Domains for Components
52 Specify a character set Add Content-Type with charset response header
Content-Type: text/html; charset=utf-8
This brings extra buffering to the page, as the first thing a browser does is to try and obtain most suitable charset to display information on a page

NOTE : Both charset header and <meta> charset should be identical for a page
53 Specify image dimensions Specify height and width at <img> element level
This helps in reserving space for image before it loads and boosts page rendering
54 Use efficient CSS selectors 1) Avoid a universal key selector.
Allow elements to inherit from ancestors, or use a class to apply a style to multiple elements.

2) Make your rules as specific as possible.
Prefer class and ID selectors over tag selectors.

3) Remove redundant qualifiers.
These qualifiers are redundant:
a) ID selectors qualified by class and/or tag selectors
b) Class selectors qualified by tag selectors (when a class is only used for one tag, which is a good design practice anyway).

4) Avoid using descendant selectors, especially those that specify redundant ancestors.
For example, the rule body ul li a {...} specifies a redundant body selector, since all elements are descendants of the body tag.

5) Use class selectors instead of descendant selectors.

instead of using 2 style rules to display ordered list and ordered list
ul li {color: blue;}
ol li {color: red;}

encode the styles into two class names and use those in your rules
.unordered-list-item {color: blue;}
.ordered-list-item {color: red;}
55 Implementing mod pagespeed for apache Apache module to bring page speed recommendations aboard
56 PHP flush(); Putting PHP Flush after the </head> segment can improve performance significantly
... <!-- css, js -->
</head>
<?php flush(); ?>
<body>
... <!-- content -->
57 Implement Xcache / ZendOpcache The Xcache / Zend OPcache provides faster PHP execution through opcode caching and
optimization. It improves PHP performance by storing precompiled script
bytecode in the shared memory. This eliminates the stages of reading code from
the disk and compiling it on future access. In addition, it applies a few
bytecode optimization patterns that make code execution faster.
58 Post-load content What's absolutely required in order to render the page initially?
The rest of the content and components can wait.
YUI Image Loader - Loads images in the visible scroll
YUI GET Utility
59 Pre-load content Unconditional Preload - If you visit Google's homepage, it preloads all required files for Search Result page.
Conditional Preload - Yahoo search preloads extra components basis search terms
Anticipated Preload - When planning a redesign, preload all new design requisite on older version of pages
60 Minimize Iframes Costly even if blank
Affects initialization time of onload event
61 Optimize css sprites Smaller size - Arrange images horizontally instead of vertical
Optimize - Combining similar colors in a sprite helps you keep the color count low, ideally under 256 colors so to fit in a PNG8.
Mobile friendly - Don't leave big gaps between images
62 iPhone caching iPhone doesn't cache components larger than 25KB in size
63 Mobile phone redirects Mobile-specific redirects must be privately cacheable.
Else proxies will serve mobile redirect to non-mobile users. 1) Use a 302 redirect with a cache lifetime of one day.
2) To avoid mobile redirect to non-mobile user redirect should contain
a) Vary: User-Agent header
b) Cache-Control: private header.
c) Expires header in the past, to prevent old HTTP/1.0 proxies from caching these redirects
64 W3C Validation of CSS & HTML This speeds up page drawing and rendering in the browser as there is no extra computation done from browsers end to display the page properly
Ref :- http://yslow.org/ http://yslow.org/ruleset-matrix/ https://developers.google.com/speed/

Python :: Spammed word Validator or Blocked Spammed Word

Python :: Spammed word Validator or Blocked Spammed Word :-

def is_spammed(content):
    content = " "+content.lower()+" "
    spamword_list = ['call girls','Escort Service','sex','spam']
    for x in spamword_list:
        if " "+x+" " in content:
            return True
    return False

is_spammed("this is spam word") // return True

Wednesday, December 16, 2015

Django :: ...(remaining elements truncated)... Issue

Django :: ...(remaining elements truncated)... Issue :-

Problem :: ...(remaining elements truncated)... data was saved into mysql db ?

Why Occurs :: Because of List object was trying to saved into field of mysql db by python str() function due to which it truncate list and replace larger list data to ...(remaining elements truncated)...

Soln :- Replace str() to list()

Mysql :: How can I dump a specific table or set of tables without including the rest of the db tables?

Mysql :: How can I dump a specific table or set of tables without including the rest of the db tables?

Soln:-

mysql -u -p -e 'show tables like "_%"' | grep -v Tables_in | xargs mysqldump -u -p 

mysql dump data only :-
mysqldump --no-create-info

mysql dump structure only :-
mysqldump --no-data

Tuesday, November 17, 2015

Ubantu 14.10 : apache2 not starting after purge and re-install

Problem :- I am trying to install and run apache2, but after apt-get purge apache2;apt-get install apache2 (as root), I am still missing the file /usr/sbin/apache2.

It show me error :- /usr/sbin/apache2 not found 

Soln :- sudo apt-get install --reinstall apache2 apache2-bin

Thursday, January 15, 2015

#2006 - MySQL server has gone away

Error : #2006 - MySQL server has gone away
 
Soln :-
 
This error is occur due to due to expire of wait_timeout .
 
Just go to mysql server check its wait_timeout :
 
mysql> SHOW  VARIABLES  LIKE  'wait_timeout'
 
mysql> set global wait_timeout = 600 # 10 minute or maximum wait time out you need  

Monday, November 10, 2014

django.core.exceptions:ImproperlyConfigured: Error importing module django.contrib.auth.middleware: "cannot import name utils"

When I deploy my code My site stop working properly from One of my server below error occurs . I try to redeploy my code restart Apache 2-3 times .But still below error show in my error log :-

django.core.exceptions:ImproperlyConfigured

/django.core.handlers.wsgi:WSGIHandler.__call__

Traceback (most recent call last):
File "/opt/.virtualenv/mysite/lib/python2.7/site-packages/newrelic-2.14.0.11/newrelic/api/web_transaction.py", line 853, in __call__ result = application(environ, _start_response)
File "/opt/.virtualenv/mysite/lib/python2.7/site-packages/newrelic-2.14.0.11/newrelic/api/function_trace.py", line 90, in literal_wrapper return wrapped(*args, **kwargs)
File "/opt/.virtualenv/mysite/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 187, in __call__ self.load_middleware()
File "/opt/.virtualenv/mysite/lib/python2.7/site-packages/newrelic-2.14.0.11/newrelic/common/object_wrapper.py", line 277, in _wrapper result = wrapped(*args, **kwargs)
File "/opt/.virtualenv/mysite/lib/python2.7/site-packages/django/core/handlers/base.py", line 47, in load_middleware mw_class = import_by_path(middleware_path)
File "/opt/.virtualenv/mysite/lib/python2.7/site-packages/django/utils/module_loading.py", line 26, in import_by_path sys.exc_info()[2])
File "/opt/.virtualenv/mysite/lib/python2.7/site-packages/django/utils/module_loading.py", line 21, in import_by_path module = import_module(module_path)
File "/opt/.virtualenv/mysite/lib/python2.7/site-packages/django/utils/importlib.py", line 40, in import_module __import__(name)
File "/opt/.virtualenv/mysite/lib/python2.7/site-packages/django/contrib/auth/middleware.py", line 3, in from django.contrib.auth.backends import RemoteUserBackend
File "/opt/.virtualenv/mysite/lib/python2.7/site-packages/django/contrib/auth/backends.py", line 3, in from django.contrib.auth.models import Permission
File "/opt/.virtualenv/mysite/lib/python2.7/site-packages/django/contrib/auth/models.py", line 48, in class Permission(models.Model):
File "/opt/.virtualenv/mysite/lib/python2.7/site-packages/django/db/models/base.py", line 96, in __new__ new_class.add_to_class('_meta', Options(meta, **kwargs))
File "/opt/.virtualenv/mysite/lib/python2.7/site-packages/django/db/models/base.py", line 264, in add_to_class value.contribute_to_class(cls, name)
File "/opt/.virtualenv/mysite/lib/python2.7/site-packages/django/db/models/options.py", line 124, in contribute_to_class self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "/opt/.virtualenv/mysite/lib/python2.7/site-packages/django/db/__init__.py", line 34, in __getattr__ return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/opt/.virtualenv/mysite/lib/python2.7/site-packages/django/db/utils.py", line 198, in __getitem__ backend = load_backend(db['ENGINE'])
File "/opt/.virtualenv/mysite/lib/python2.7/site-packages/django/db/utils.py", line 113, in load_backend return import_module('%s.base' % backend_name)
File "/opt/.virtualenv/mysite/lib/python2.7/site-packages/django/utils/importlib.py", line 40, in import_module __import__(name)
File "/opt/.virtualenv/mysite/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 39, in from django.db import utils
ImproperlyConfigured: Error importing module django.contrib.auth.middleware: "cannot import name utils"


To solve this problem I reinstall django 1.6 & then resart apache.

pip uninstall django
pip install django==1.6.7
service apache2 restart

 
 

Wednesday, December 5, 2012

Saving cookie as an array


Every Browser have limit of setting cookie by domain And it is best practice  if save your cookie in form of array .Browser support saving cookie in form :-
1) name,value
2) Json
3) Serialized

So I have Create a class Cookie Manager in Php for saving , getting & checking cookie in form of array using serialized - unserialized.


/**
 * Description of CookieManager
 *
 * @author saurabh goyal
 */
class CookieManager {
    protected  $path;
    protected  $domain;
    protected  $expire;
    protected  $secure;
   
    /**
     * __construct
     * @param mixed $path,$domain,$expire
     * @param boolean $secure,$httpOnly
     * @param mixed $objResumeUpdateManager
     * @return void
     */
  
    public function __construct($path='/',$domain='.localhost',$expire='',$secure=false,$httpOnly=false) {       
        $this->path = $path;
        $this->domain = $domain;
        $this->expire = $expire;
        $this->secure = $secure;
        $this->httpOnly = $httpOnly;
    }
   
    public function setCookie($name,$value){
        setcookie($name, $value, $this->expire, $this->path, $this->domain, $this->secure,$this->httpOnly);       
    }
      /*
     * Set setCookie
     * @param string $name
     * @param string $value
     * return boolean
     */
    public function setSerializeCookie($name,$value) {
        try {           
            $this->setCookie($name,serialize($value));
            return true;
        } catch (Exception $e) {
            return false;
        }
    }

    public function getUnSerializeCookie($name) {
        return (isset($_COOKIE[$name]) && $_COOKIE[$name]!='')?unserialize($_COOKIE[$name]):false;
    }

     /*
     * Set setCookieArray
     * @param string $name
     * @param array $arrayValue
     * return boolean
     */

    public function setCookieArray($name,$arrayValue) {
        $cookieArray = $this->getUnSerializeCookie($name);
        if($cookieArray) {
             $arrayValue = $arrayValue+$cookieArray;
        }
        $this->setSerializeCookie($name,$arrayValue);
        return true;
    }
  
    /*
     * Set unsetCookieArray
     * @param string $name
     * @param string $value key of array
     * return boolean
     */
    public function unsetCookieArrayByKey($name,$key) {
         $cookieArray = $this->getUnSerializeCookie($name);
         if($cookieArray && (array_key_exists($key,$cookieArray) !== false)) {
             unset($cookieArray[$key]);            
             $this->setSerializeCookie($name,$cookieArray);
         }
         return true;
    }

    public function getCookieValueByKey($name,$key) {
        $cookieArray = $this->getUnSerializeCookie($name);
        if($cookieArray) {
           return isset($cookieArray[$key])?$cookieArray[$key]:false;
        }
        return false;
    } 
      
}

?>





And Javascript code for same :-



Tuesday, August 23, 2011

Funtion of Sorting an Array using Php

$arr = array(1,7,6,8,4,5,2);

function sg_array_sort($arr) {
    for($i=1;$i      for($j=$i-1;$j>=0;$j--) {   
         if($arr[$i] < $arr[$j]) {
            $temp = $arr[$i];
            $arr[$i]= $arr[$j];
            $arr[$j] = $temp;
            $i = $j;
         } 
      }
    }
}


$sort_arr =  sg_array_sort($arr);


Many-to-many MySql relation: how to retrieve all related entries in one query?

Suppose there are 3 Table 1) Products , 2)Category & 3)Pro_Cat :- Many to Many RelationShip B/w Products & Category Table :-

1) Products Table :-                                  


Product_Id
Product_Name
1
Samsung
2
Relience
3
Micromax

2) Category Table :-


Category_Id
Category_Name
1
Mobile
2
Retails
3
Usb

3) Product_Category Many into Many Relationship Table :-


Product_Id
Category_Id
1
1
2
1
2
2
3
1
3
3


Result Query :-

SELECT
  product.product_name,
  GROUP_CONCAT(category.category_name) AS category_name
FROM product,category,pro_cat
Where product.product_id = pro_cat.product_id
and category.category_id = pro_cat.category_id
GROUP BY product.product_id;

Result :-


Product_Name
Category_Names
Samsung
Mobile
Relience
Mobile,Retails
Micromax
Mobile ,Usb

Tuesday, July 26, 2011

The Flipkart Story (Case Study)

It’s been about a year since Flipkart started moving from being a pure bookstore to selling mobiles/DVDs etc. Then we had cribbed about a possible brand dilution and investor pressure. Probably we were never sure about the bigger picture. Since then, Flipkart went for a major brand makeover, making it look more ‘upmarket’. There has been large newspaper ads, TVCs and a lot of web ads. But unlike other eCommerce companies the inorganic marketing kicked in only when the product was strong.
Flipkart already had a proven model execution with books and extending to other verticals did not need infrastructural changes. Flipkart’s real achievement has been in solving the pain points in Indian eCommerce that most well funded players are still complaining  about.
Here are some of the things that Flipkart has done well in solving each of these problems.

1. Discoverability:

It is the case with any venture on the web, “How does the customer find us?” Answer: Organically!
Flipkart has been the “baap” of SEO. This has been the most important contributor to their success. I say this, because only when you see people coming to you, you get encouraged to deliver more and keep adding. There is no fun (motivation) in adding features to a product that no one is using.
Though from what I had noticed, SEO did not come the straight way. There were particularly 2 things that are worth mentioning.
a. Yahoo News: Until last year Flipkart had a feed of Yahoo News on its product pages. From what I understand of SEO, this is to increase the keyword density and introduce ‘original’ content on the page, as the product description across all books sites is same. This was removed later as it was violating the Yahoo’s ToS on using the service for a commercial site. Check the Waybackmachine here. I loved the risk they took for this.
b. We Do Not Sell Used Books: This one is my favorite. If you check the Waybackmachine in the last line you will notice the following text.

  • We DO NOT sell old books or used books. All the books listed at Flipkart.com are new books.

  • The books listed at Flipkart.com are NOT available for free download in ebook or PDF format.

  • The magic of this text is that if you search for free ebook or pdf download” you would always get Flipkart among the top results. These are very popular search queries and Flipkart had nothing to do with it but still they cashed in. This was also the time when Flipkart had Adsense embedded. People would come to the site, see nothing like a “PDF download” button, and then see an ad for PDF download and click. This meant more revenue for Flipkart. I have done this atleast twice myself. Given that the margin on books are very small after the discount, Flipkart was probably earning more by saying what they did not do than by doing what they actually were suppose to do.

    2. Payments:

    No credit card/netbanking, fear to transact online, repeat transaction failures, no access to web – these are the common problems with online payments. What Flipkart is doing to overcome these?
    From what I last counted, Flipkart had atleast 4 different Payment Gateways integrated. They introduced Cash-on-Delivery. Then they are also doing order on phone. Payment via DD/Cheque is also accepted.
    2 basic things that they are currently doing that takes little tech. effort but quite some product management ‘will’:
    a. Auto redirection to banking site: Unlike most other ecommerce sites, Flipkart never lands you on CCavenue page, you are auto redirected to the banks page where the info is required to be filled. Flipkart by-passes 1 unnecessary page by passing the required parameters directly to CCavenue and not through a user interface.
    b. Banks Status: Flipkart maintains its own real time status if the bank’s netbanking is working or not. So there are no surprises after you have chosen the bank and then go to the netbanking page.
    And if you think Flipkart gets very good rates from Payment Gateways, not really. Atleast 1 big PG that I am aware of charges quite high rates to Flipkart, atleast 40-50% more than to lesser known players.

    3. Inventory:

    I come from a traditional business family where we believe in selling what we have. The world of eCommerce really amazes me when I see the players keeping a standard list of products and then go out procuring it only when there is an order placed. Imagine if you go to a brick & mortar shop and after billing the manager sends a boy to a nearby store to get the goods. This is where the problem starts. There is no inventory on their end and there is no live status of inventory from their supplier. Remember The Alchemist, “Never Promise something that you don’t have“.
    After placing an order, they would keep looking for the product at multiple places. After a week you might get a call saying that either the product is not available and we will do a ‘favor’ by refunding your money or if the product is there, it is not the color/size that you asked for.
    Flipkart was no different in 2009, a couple my friends used to get similar calls after days of ordering. But for the last 1 year atleast Flipkart maintains its own inventory(or atleast it seems so). They are selling what they have. From pure hearsay, Flipkart is taking up a big warehouse in Bangalore and is in talks for one in NCR as well. One of the few companies that is using the funding to build a business and not spend it like a FMCG company on ads.

    4. Delivery:

    I have dealt with courier companies in my last startup and am quite aware of the ‘fcukall’ standards they have. Most similar looking envelopes are never delivered thinking that it is a marketing package and no one would track it. They would be willing to bargain on rates but would never say anything about the service. Paying a premium may not solve the problem always.
    Flipkart is exploiting this problem as a cashable need gap and building its own delivery backendFlipkart is seen delivering through their own delivery boys in Bangalore and at times within 12hrs from order.
    Flipkart has started putting fliers in newspapers in Bangalore with a product listing, call-to-order phone number and a promise of delivering ‘tomorrow’. This means more discoverability, no payment problem and no delivery delay. The way it is actually suppose to be.
    From what I heard from multiple sources, Flipkart is looking to build its own courier company. The recent $20Mn funding from Tiger Global was only part of a larger sum they are known to be raising. Flipkart is looking to raise $100Mn at a valuation of $200Mn.
    Recently, Flipkart has started selling everything from cameras, laptops to gaming consoles to personal and health care electronic products. There are major talks about Amazon acquiring Flipkart but it would only make sense to grow it bigger and better from here. A handover wouldn’t see the same product management ‘will’.
    Flipkart is a story that is come from smart work and an ‘it is possible’ attitude. There is a need to for a couple of more stories like these and there would be no cribbing about Indian eCommerce not working.