Friday, July 14, 2023

Processing Data in API or Client Application - Pros and Cons Explored

As part of a recent software development project, I had the opportunity to work on a connected ambulance solution. Our goal was to share EPCR (Electronic Patient Care Report) data with a client application developed in Unity language. In this journey, we encountered a scenario that required data processing before displaying it to the client. The team had differing opinions on whether to process the data on the server side or let the client application handle the processing. After careful consideration and evaluation, here are the findings on the pros and cons of each approach.

Processing Data on the Server Side:

Pros:

  1. Network Efficiency: Transmitting processed data over the network is more efficient if the processed data is smaller in size compared to the raw data.
  2. Centralized Processing: Centralizing the processing on the server side ensures consistent and standardized data processing across all clients.
  3. Enhanced Security: Processing data on the server side provides better control and protection of sensitive information, as it remains within a controlled server environment.
  4. Scalability: Server-side processing allows for distributing the processing load across multiple servers, enabling better scalability to handle a large number of concurrent requests.

Cons:

  1. Increased Server Load: Processing data on the server side can increase the load on the server, especially when dealing with a high volume of requests.
  2. Increased Response Time: Server-side processing introduces additional latency as the client application has to wait for the processed data to be returned, potentially impacting user experience.
  3. Dependency on Server Availability: If the server is down or experiencing issues, the client application may not be able to process the data.


Processing Data on the Client Side:

Pros:

  1. Reduced Server Load: Offloading processing to the client side reduces the processing load on the server, leading to improved server performance and scalability.
  2. Faster Response Time: With client-side processing, the processed data can be available immediately, without waiting for the server's processing time, resulting in a better user experience.
  3. Client Control: Client-side processing allows for flexibility and customization, enabling the client application developer to optimize data processing based on specific requirements.
  4. Reduced Network Traffic: Transmitting raw data over the network instead of processed data reduces network traffic and bandwidth usage.


Cons:

  1. Client Device Constraints: Client devices may have limitations in terms of processing power, memory, or software dependencies, which could impact the ability to process complex data efficiently.
  2. Inconsistent Processing: Client-side processing relies on individual client applications to implement the processing logic, potentially leading to inconsistencies in the results.
  3. Data Security: Transmitting raw data over the network may introduce security risks, especially if the data contains sensitive information.
  4. Limited Scalability: Client-side processing relies on the resources and capabilities of individual client devices, which may not scale well for a large number of concurrent users.


Conclusion:

In our connected ambulance project, we faced the decision of whether to process the data on the server side or let the client application handle the processing. After careful consideration, I decided to provide raw data via API and let the client application process it. This choice offered several advantages, such as reduced server load, faster response times, and client control over data processing. However, we acknowledge that this approach may have drawbacks, including client device limitations and potential inconsistencies in processing across different clients.

Ultimately, the decision of processing data in the API or client application depends on various factors, including network efficiency, security requirements, scalability needs, and client capabilities. It's crucial to carefully evaluate these factors in your own projects to determine the best approach that aligns with your specific requirements.

---

Sri Nuwan

Tuesday, July 4, 2023

Retrieving System Information with a Shell Script - OS, php,MySQL, Apache

 This shell script will allows you to retrieve important system information such as the operating system version, PHP version, installed PHP extensions, MySQL/MariaDB version, and Apache version. The script is designed to be compatible with CentOS, RHEL, and Ubuntu distributions of all versions.

 

 
#!/bin/bash

# Function to get OS version for CentOS/RHEL
get_centos_os_version() {
  os_version=$(cat /etc/redhat-release)
}

# Function to get OS version for Ubuntu
get_ubuntu_os_version() {
  os_version=$(lsb_release -ds)
}

# Function to get PHP version
get_php_version() {
  php_version=$(php -v | awk '/^PHP/ {print $2}')
}

# Function to get installed PHP extensions
get_php_extensions() {
  php_extensions=$(php -m)
}

# Function to get MySQL/MariaDB version
get_mysql_version() {
  mysql_version=$(mysql -V | awk '{print $5}')
}

# Function to get Apache version for CentOS/RHEL
get_centos_apache_version() {
  apache_version=$(httpd -v | awk '/^Server version/ {print $3}')
}

# Function to get Apache version for Ubuntu
get_ubuntu_apache_version() {
  apache_version=$(apache2 -v | awk '/^Server version/ {print $3}')
}

# Detect the OS type and call appropriate functions
if [ -f "/etc/redhat-release" ]; then
  get_centos_os_version
  get_php_version
  get_php_extensions
  get_mysql_version
  get_centos_apache_version
elif [ -f "/etc/lsb-release" ]; then
  get_ubuntu_os_version
  get_php_version
  get_php_extensions
  get_mysql_version
  get_ubuntu_apache_version
else
  echo "Unsupported OS. Exiting..."
  exit 1
fi

# Print the gathered information
echo "OS Version: $os_version"
echo "PHP Version: $php_version"
echo "Installed PHP Extensions: $php_extensions"
echo "MySQL/MariaDB Version: $mysql_version"
echo "Apache Version: $apache_version"

Monday, July 15, 2019

Enable MySQL Log Rotation on CentOS 6.6

Create /root/.my.cnf configuration file with the following content

Content:
[mysqladmin]
password = <password>
user= root

Change the access permissions

chmod 600 /root/.my.cnf


Create mysql file under /etc/logrotate.d (Or edit file if exists)

vim /etc/logrotate.d/mysql 

Content:

/var/log/mysqld/*.log {
        # create 600 mysql mysql
        notifempty
    daily
        rotate 30
        missingok
        compress
        postrotate
    # just if mysqld is really running
    if test -x /usr/bin/mysqladmin && \
       /usr/bin/mysqladmin ping &>/dev/null
    then
       /usr/bin/mysqladmin  flush-general-log flush-slow-log flush-error-log
    fi
    endscript
}





Debug:

logrotate -df /etc/logrotate.d/mysql

Try:

logrotate -df /etc/logrotate.d/mysql



Ref:









Tuesday, December 12, 2017

CentOS Timezone Configuration

/usr/share/zoneinfo/ - The system time-zone information directory.
/etc/localtime - The symlink to the timezone file

Image: Update Timezone to Asia/Colombo

Friday, March 27, 2015

Create a web application using Yii2

The latest version of Yii 2 is 2.0.3, released on March 1, 2015.

Related Links


Environment

  • OS: Ubuntu 14.04

Installation of an application template

  • Find / Create a suitable directory in your hard drive.
  • Install Composer 
    • $ curl -sS https://getcomposer.org/installer | php
  • Install the Composer Asset Plugin
    • $ php composer.phar global require "fxp/composer-asset-plugin:1.0.0"
  • To install the basic application template, run the command below: 
    •  php composer.phar create-project yiisoft/yii2-app-basic basic 2.0.3
  •  To install the advanced application template, run the command below: 
    •  php composer.phar create-project yiisoft/yii2-app-advanced advanced 2.0.3
$ curl -sS https://getcomposer.org/installer | php
#!/usr/bin/env php
All settings correct for using Composer
Downloading...

Composer successfully installed to: /Data/nuwan/Research/yii2/app1/composer.phar
Use it: php composer.phar
$ php composer.phar global require "fxp/composer-asset-plugin:1.0.0"
Changed current directory to /home/nuwan/.composer
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Removing fxp/composer-asset-plugin (1.0.x-dev dc24e62)
  - Installing fxp/composer-asset-plugin (v1.0.0)
    Downloading: 100%         

Writing lock file
Generating autoload files
$ php composer.phar create-project yiisoft/yii2-app-basic basic 2.0.3
Installing yiisoft/yii2-app-basic (2.0.3)
  - Installing yiisoft/yii2-app-basic (2.0.3)
    Downloading: 100%         

Created project in basic
Loading composer repositories with package information
Installing dependencies (including require-dev)
Reading bower.json of bower-asset/jquery.inputmask (3.1.51)
Could not fetch https://api.github.com/repos/RobinHerbots/jquery.inputmask/contents/bower.json?ref=f29e2b535028bde8e1135deb65cb5ed2e081cf39, enter your GitHub credentials to go over the API rate limit
A token will be created and stored in "/home/nuwan/.composer/auth.json", your password will never be stored
To revoke access to this token you can visit https://github.com/settings/applications
Username: srinuwanperera@gmail.com
Password: 
Token successfully created
Reading bower.json of bower-asset/bootstrap (v3.1.1)       
Reading bower.json of bower-asset/bootstrap (v3.3.1)
  - Installing yiisoft/yii2-composer (2.0.3)            
    Downloading: 100%         

  - Installing ezyang/htmlpurifier (v4.6.0)
    Loading from cache

  - Installing swiftmailer/swiftmailer (v5.4.0)
    Downloading: 100%         

  - Installing bower-asset/jquery (2.1.3)
    Downloading: 100%         

  - Installing bower-asset/yii2-pjax (v2.0.4)
    Downloading: 100%         

  - Installing bower-asset/punycode (v1.3.2)
    Downloading: 100%         

  - Installing bower-asset/jquery.inputmask (3.1.62)
    Downloading: 100%         

  - Installing cebe/markdown (1.0.2)
    Downloading: 100%         

  - Installing yiisoft/yii2 (2.0.3)
    Downloading: 100%         

  - Installing yiisoft/yii2-swiftmailer (2.0.3)
    Downloading: 100%         

  - Installing yiisoft/yii2-codeception (2.0.3)
    Downloading: 100%         

  - Installing bower-asset/bootstrap (v3.3.4)
    Downloading: 100%         

  - Installing yiisoft/yii2-bootstrap (2.0.3)
    Downloading: 100%         

  - Installing yiisoft/yii2-debug (2.0.3)
    Downloading: 100%         

  - Installing bower-asset/typeahead.js (v0.10.5)
    Downloading: 100%         

  - Installing phpspec/php-diff (v1.0.2)
    Loading from cache

  - Installing yiisoft/yii2-gii (2.0.3)
    Downloading: 100%         

  - Installing fzaninotto/faker (v1.4.0)
    Downloading: 100%         

  - Installing yiisoft/yii2-faker (2.0.3)
    Downloading: 100%         

Writing lock file
Generating autoload files
chmod('runtime', 0777)...done.
chmod('web/assets', 0777)...done.
chmod('yii', 0755)...done.
$
$ php composer.phar create-project yiisoft/yii2-app-advanced advanced 2.0.3
Installing yiisoft/yii2-app-advanced (2.0.3)
  - Installing yiisoft/yii2-app-advanced (2.0.3)
    Downloading: 100%         

Created project in advanced
Loading composer repositories with package information
Installing dependencies (including require-dev)
  - Installing yiisoft/yii2-composer (2.0.3)               
    Loading from cache

  - Installing ezyang/htmlpurifier (v4.6.0)
    Loading from cache

  - Installing swiftmailer/swiftmailer (v5.4.0)
    Loading from cache

  - Installing bower-asset/jquery (2.1.3)
    Loading from cache

  - Installing bower-asset/yii2-pjax (v2.0.4)
    Loading from cache

  - Installing bower-asset/punycode (v1.3.2)
    Loading from cache

  - Installing bower-asset/jquery.inputmask (3.1.62)
    Loading from cache

  - Installing cebe/markdown (1.0.2)
    Loading from cache

  - Installing yiisoft/yii2 (2.0.3)
    Loading from cache

  - Installing yiisoft/yii2-swiftmailer (2.0.3)
    Loading from cache

  - Installing yiisoft/yii2-codeception (2.0.3)
    Loading from cache

  - Installing bower-asset/bootstrap (v3.3.4)
    Loading from cache

  - Installing yiisoft/yii2-bootstrap (2.0.3)
    Loading from cache

  - Installing yiisoft/yii2-debug (2.0.3)
    Loading from cache

  - Installing bower-asset/typeahead.js (v0.10.5)
    Loading from cache

  - Installing phpspec/php-diff (v1.0.2)
    Loading from cache

  - Installing yiisoft/yii2-gii (2.0.3)
    Loading from cache

  - Installing fzaninotto/faker (v1.4.0)
    Loading from cache

  - Installing yiisoft/yii2-faker (2.0.3)
    Loading from cache

Writing lock file
Generating autoload files
$
$ ll
total 1068
drwxrwxr-x  4 nuwan nuwan    4096 මාර්  27 13:03 ./
drwxrwxr-x  3 nuwan nuwan    4096 මාර්  27 12:10 ../
drwxrwxr-x  9 nuwan nuwan    4096 මාර්  27 13:08 advanced/
drwxrwxr-x 13 nuwan nuwan    4096 මාර්  27 12:50 basic/
-rwxr-xr-x  1 nuwan nuwan 1075141 මාර්  27 12:12 composer.phar*
$

Initialize the application (For advanced template)

  • ./init
$ ./init
Yii Application Initialization Tool v1.0

Which environment do you want the application to be initialized in?

  [0] Development
  [1] Production

  Your choice [0-1, or "q" to quit] 0

  Initialize the application under 'Development' environment? [yes|no] yes

  Start initialization ...

   generate common/config/main-local.php
   generate common/config/params-local.php
   generate frontend/web/index-test.php
   generate frontend/web/index.php
   generate frontend/config/main-local.php
   generate frontend/config/params-local.php
   generate backend/web/index-test.php
   generate backend/web/index.php
   generate backend/config/main-local.php
   generate backend/config/params-local.php
   generate yii
   generate console/config/main-local.php
   generate console/config/params-local.php
   generate cookie validation key in backend/config/main-local.php
   generate cookie validation key in frontend/config/main-local.php
      chmod 0777 backend/runtime
      chmod 0777 backend/web/assets
      chmod 0777 frontend/runtime
      chmod 0777 frontend/web/assets
      chmod 0755 yii

  ... initialization completed.
$

Test the application using php server.

  • Basic Application
    • php -S localhost:8000 -t basic/web
  • Advanced Application
    • php -S localhost:8000 -t advanced/frontend/web/
    •  php -S localhost:8000 -t advanced/backend/web/
  • Once the php server started, open a web browser and visit:
    •  http://localhost:8000/
$ php -S localhost:8000 -t advanced/frontend/web/
PHP 5.5.9-1ubuntu4.7 Development Server started at Fri Mar 27 13:28:22 2015
Listening on http://localhost:8000
Document root is /*******/advanced/frontend/web
Press Ctrl-C to quit.





Tuesday, September 16, 2014

cannot enable executable stack as shared object requires: Permission denied

Story:

Today, One of our team member has been installed a clean installation of RHEL 6.3 (RedHat) and once we try to start the Apache web server, it went bad due to following error.

Error:


Starting httpd: httpd: Syntax error on line 218 of /etc/httpd/conf/httpd.conf: Cannot load /etc/httpd/modules/libphp5.so into server: /etc/httpd/modules/libphp5.so: cannot enable executable stack as shared object requires: Permission denied 

Solution:


  1. Log into the CLI - Command Line interface (As root user)
  2. Run Following Command
[root@new ~]# execstack -c /etc/httpd/modules/libphp5.so

Image:


Tuesday, August 5, 2014

Abstract classes in PHP

Related Links

  • http://php.net/manual/en/language.oop5.interfaces.php
  • http://www.techflirt.com/tutorials/oop-in-php/abstract-classes-interface.html

What is class abstraction?

  • PHP 5 introduces abstract classes and methods.
  • Usually abstract class are also known as base class.
  • Abstract classes are those classes which can not be directly initialized. Or in other word we can say that you can not create object of abstract classes.
  • Any class that contains at least one abstract method must also be abstract.
  • Abstract classes always created for inheritance purpose. You can only inherit abstract class in your child class.
  • It can only act as parent class of any normal class. You can use abstract class in class hierarchy. Mean one abstract class can inherit another abstract class also.
  • A protected abstract function can be implemented as either protected or public but not private.