Search files from the terminal on Linux using locate

sudo apt-get install locate

Using locate Command

locate LAMP-Setup.odt
locate "*.html"

Ignore Case Sensitive Locate Outputs

locate -i *text.txt*

Refresh mlocate Database

Since locate command relies on a database called mlocate. The said database needs to be updated regularly for the command utility to work efficiently

sudo updatedb

Display Only Files Present in Your System

locate -i -e *text.txt*

Review Your Locate Database

locate -S

Limit Search Queries to a Specific Number

locate "*.html" -n 20

Display The Number of Matching Entries

locate -c [tecmint]*

References
https://www.howtoforge.com/tutorial/linux-search-files-from-the-terminal/
https://www.tecmint.com/linux-locate-command-practical-examples/

Scheduled backup MongoDB database using mongodump and cron

mkdir backup
touch monitoring.sh
#!/bin/bash
 
currentDate=`date +"%Y%m%d%H%M"`
dirName="monitoring_$currentDate"
filePath=/home/mahmood/backup/$dirName
mkdir $filePath
mongodump --db monitoring --out $filePath
sudo chmod +x monitoring.sh
sudo crontab -e
30 4 * * * /home/mahmood/backup/monitoring.sh

References
https://stackoverflow.com/questions/35574603/run-cron-job-everyday-at-specific-time
https://tecadmin.net/get-current-date-and-time-in-bash/
https://askubuntu.com/questions/350861/how-to-set-a-cron-job-to-run-a-shell-script
https://coderwall.com/p/hdmmnq/easy-automatic-nightly-backups-for-mongodb

Backup MongoDB using mongodump

Backup a Whole Database

mongodump --db mydb
sudo mongodump --db mydb --out /var/backups/mongo

Backup a Single Collection

mongodump -d mydb -o /var/backups/mongo --collection users

Restore a Whole Database

sudo mongorestore --db mydb /var/backups/mongo/mydb

Restore a whole collection

mongorestore -d mydb -c users mydb/users.bson

Taking Regular Backups Using Mongodump/Mongorestore

sudo crontab -e

backup every day at 4:30 AM

30 4 * * * mongodump --db mydb

Archive output

mongodump --db monitoring --archive=monitoring_20190604.archive

Gzip output

mongodump --db monitoring --archive=monitoring_20190604.archive.gz --gzip

References
https://severalnines.com/blog/review-mongodb-backup-options
https://docs.mongodb.com/manual/reference/program/mongodump/

Measuring a 4-20ma Input With a Voltage Input Device

we will assume that a 0-10Vdc input will be used to measure 4-20ma signal.

Ohms law states: R=V/I where V is the Voltage, I is the current and R is the resistance

R=10V/.020A = 500 Ohms

When 20ma flows through a 500 Ohm resistor, it will drop 10 volts

When 4ma flows through a 500 Ohm resistor, it will drop 2 volts

Therefore, 4-20ma signal through a 500 ohm resister will drop 2 to 10 volts

PLC
if you’re using instrument with 4-20mA and connect it to:

1. range 0-20mA – your value of the instrument will be from that 5529 to 27648, value from 0 to cca 5500 will be not showed, but it depends on instrument.

2. range 4-20mA – your value of instrument will be from 0 to 27648.

References
https://www.omega.co.uk/techref/das/4-20ma.html
https://support.industry.siemens.com/tf/WW/en/posts/analog-input-evaluation/146749?page=0&pageSize=10
https://www.omega.com/en-us/resources/data-acquisition-measuring-420-input-with-voltage-device

Detect when an image fails to load in Javascript

function testImage(URL) {
    var tester=new Image();
    tester.onload=imageFound;
    tester.onerror=imageNotFound;
    tester.src=URL;
}

function imageFound() {
    alert('That image is found and loaded');
}

function imageNotFound() {
    alert('That image was not found.');
}

testImage("http://foo.com/bar.jpg");

References
https://stackoverflow.com/questions/9815762/detect-when-an-image-fails-to-load-in-javascript
https://www.w3schools.com/jsref/event_onerror.asp

Deploy Flask with Gevent Pywsgi Server using Multiprocessing

import sys
from gevent import server
from gevent.baseserver import _tcp_listener
from gevent import pywsgi
from gevent.monkey import patch_all; patch_all()
from multiprocessing import Process, current_process, cpu_count

def hello_world(env, start_response):
    if env['PATH_INFO'] == '/':
        start_response('200 OK', [('Content-Type', 'text/html')])
        return ["<b>hello world</b>"]
    else:
        start_response('404 Not Found', [('Content-Type', 'text/html')])
        return ['<h1>Not Found</h1>']

listener = _tcp_listener(('127.0.0.1', 8001))

def serve_forever(listener):
    pywsgi.WSGIServer(listener, hello_world).serve_forever()

number_of_processes = 5
print 'Starting %s processes' % number_of_processes
for i in range(number_of_processes):
    Process(target=serve_forever, args=(listener,)).start()

serve_forever(listener)

References
https://stackoverflow.com/questions/7407868/gevent-pywsgi-server-multiprocessing

Detect scroll to bottom of html element in Angular

@HostListener('window:scroll', ['$event'])
onWindowScroll() {
  // In chrome and some browser scroll is given to body tag
  const pos = (document.documentElement.scrollTop || document.body.scrollTop) + document.documentElement.offsetHeight;
  const max = document.documentElement.scrollHeight;
  const fixedPos = pos + 10;
  console.log(max, fixedPos);
  // pos/max will give you the distance between scroll bottom and and bottom of screen in percentage.
  if (fixedPos >= max) {
    if (!this.isFetching) {
      this.nextHistory().subscribe();
    }
  }
}

References
https://stackoverflow.com/questions/40664766/how-to-detect-scroll-to-bottom-of-html-element

Pagination and Sorting using Spring Data JPA

Creating Repository

public interface ProductRepository extends PagingAndSortingRepository<Product, Integer> {
 
    List<Product> findAllByPrice(double price, Pageable pageable);
}

Pagination

Pageable firstPageWithTwoElements = PageRequest.of(0, 2);
 
Pageable secondPageWithFiveElements = PageRequest.of(1, 5);
Page<Product> allProducts = productRepository.findAll(firstPageWithTwoElements);
 
List<Product> allTenDollarProducts = 
  productRepository.findAllByPrice(10, secondPageWithFiveElements);

Pagination and Sorting

Pageable sortedByName = 
  PageRequest.of(0, 3, Sort.by("name"));
 
Pageable sortedByPriceDesc = 
  PageRequest.of(0, 3, Sort.by("price").descending());
 
Pageable sortedByPriceDescNameAsc = 
  PageRequest.of(0, 5, Sort.by("price").descending().and(Sort.by("name")));

References
https://www.baeldung.com/spring-data-jpa-pagination-sorting
https://www.baeldung.com/queries-in-spring-data-mongodb