8 Commits

  1. 8
      app/Channels/Messages/SmsMessage.php
  2. 26
      app/Channels/SmsChannel.php
  3. 12
      app/Enums/sms.php
  4. 8
      app/Listeners/NotifHandler.php
  5. 4
      app/Models/Business.php
  6. 2
      app/Notifications/DBNotification.php
  7. 6
      app/Notifications/SmsNotification.php
  8. 4
      app/Notifications/SocketNotification.php
  9. 13
      app/Providers/AppServiceProvider.php
  10. 6
      app/Utilities/HelperClass/NotificationHelper.php
  11. 1
      database/migrations/2021_03_08_114700_create_notifications_table.php
  12. 19
      docker-compose.yml
  13. 2521
      filebeat.reference.yml
  14. 232
      filebeat.yml
  15. 19
      my.cnf
  16. 4
      routes/api.php

8
app/Channels/Messages/SmsMessage.php

@ -75,12 +75,12 @@ class SmsMessage
/**
* Set the params of the sms message when we use ultraFastSend method
*
* @param array $params
* @param array|mixed $params
* @return $this
*/
public function params(array $params)
public function params($params)
{
foreach ($parameters as $key => $value) {
foreach ($params as $key => $value) {
$this->params[] = ['Parameter' => $key, 'ParameterValue' => $value];
}
return $this;
@ -92,7 +92,7 @@ class SmsMessage
* @param string $template_id
* @return $this
*/
public function templateId(string $template_id)
public function templateId($template_id)
{
$this->template_id = $template_id;

26
app/Channels/SmsChannel.php

@ -10,12 +10,26 @@ use GuzzleHttp\Client as HttpClient;
class SmsChannel
{
/**
* The API URL for Socket.
* The API URL for sms.
*
* @var string
*/
protected $sms_url;
/**
* The api key for sms inside sms.ir panel.
*
* @var string
*/
protected $api_key;
/**
* The secret key for sms inside sms.ir panel.
*
* @var string
*/
protected $secret_key;
/**
* The HTTP client instance.
*
@ -29,10 +43,12 @@ class SmsChannel
* @param \GuzzleHttp\Client $http
* @return void
*/
public function __construct(HttpClient $http, string $sms_url)
public function __construct(HttpClient $http, string $sms_url, string $api_key, string $secret_key)
{
$this->http = $http;
$this->sms_url = $sms_url;
$this->api_key = $api_key;
$this->secret_key = $secret_key;
}
/**
@ -49,7 +65,7 @@ class SmsChannel
$message->to($notifiable->routeNotificationFor('sms', $notification));
if (! $message->to || ! ($message->params && $message->template_id) || ! $message->verification_code) {
if (! $message->to || (! ($message->params && $message->template_id) && ! $message->verification_code)) {
return;
}
@ -75,8 +91,8 @@ class SmsChannel
protected function getToken()
{
$body = [
'UserApiKey' => config('smsirlaravel.api-key'),
'SecretKey' => config('smsirlaravel.secret-key'),
'UserApiKey' => $this->api_key,
'SecretKey' => $this->secret_key,
'System' => 'laravel_v_1_4'
];

12
app/Enums/sms.php

@ -0,0 +1,12 @@
<?php
return [
'types' => [
'verification_code' => [
'name' => 'VerificationCode'
],
'ultra_fast_send' => [
'name' => 'ultraFastSend'
]
],
];

8
app/Listeners/NotifHandler.php

@ -3,8 +3,10 @@
namespace App\Listeners;
use App\Events\ModelSaved;
use App\Notifications\SocketNotification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Notification;
class NotifHandler
{
@ -29,8 +31,12 @@ class NotifHandler
$message = json_decode($event->message);
$event_class = 'App\Events\\'.enum('tables.'.$message->data->table_name.'.singular_name').enum('cruds.inverse.'.$message->data->crud_id.'.name');
if (class_exists($event_class)) {
// event(new ('App\Events\\'.$event_class($message)));
$event_class::dispatch($message);
}
Notification::send(auth()->user(), new SocketNotification(
[
'message' => enum('tables.'.$message->data->table_name.'.singular_name').enum('cruds.inverse.'.$message->data->crud_id.'.name'),
'payload'=>$business_info
]));
}
}

4
app/Models/Business.php

@ -5,6 +5,8 @@ namespace App\Models;
use App\Models\File;
use App\Models\Model;
use App\Models\SoftDeletes;
use App\Notifications\SocketNotification;
use Illuminate\Support\Facades\Notification;
use Illuminate\Validation\Rule;
use Illuminate\Http\UploadedFile;
use Spatie\MediaLibrary\HasMedia;
@ -234,6 +236,8 @@ class Business extends Model implements HasMedia
Cache::put('business_info'.$businessId , $business_info, config('app.cache_ttl'));
Notification::send(auth()->user(), new SocketNotification(['message' => 'business info update','payload'=>$business_info]));
return $business_info;
}

2
app/Notifications/DBNotification.php

@ -29,7 +29,7 @@ class DBNotification extends Notification
*/
public function via($notifiable)
{
return ['db'];
return ['database'];
}
/**

6
app/Notifications/SmsNotification.php

@ -28,10 +28,10 @@ class SmsNotification extends Notification
*
* @return void
*/
public function __construct($message, $type = 'sendVerification')
public function __construct($message, $type = null)
{
$this->message = $message;
$this->type = $type;
$this->type = $type ?? enum('sms.types.verification_code.name');
}
/**
@ -54,7 +54,7 @@ class SmsNotification extends Notification
public function toSms($notifiable)
{
return (new SmsMessage())
->params($this->message['params'] ?? null)
->params($this->message['params'] ?? [])
->templateId($this->message['template_id'] ?? null)
->verificationCode($this->message['verification_code'] ?? null);

4
app/Notifications/SocketNotification.php

@ -45,8 +45,8 @@ class SocketNotification extends Notification
{
return (new SocketMessage())
->data([
'title' => $this->message['title'],
'body' => $this->message['body'],
'message' => $this->message['message'],
'payload' => $this->message['payload'],
]);
}
}

13
app/Providers/AppServiceProvider.php

@ -27,11 +27,16 @@ class AppServiceProvider extends ServiceProvider
$service->extend('socket', function ($app) {
return new SocketChannel(new HttpClient, config('socket.url'));
});
$service->extend('db', function ($app) {
return new DBChannel();
});
// $service->extend('db', function ($app) {
// return new DBChannel();
// });
$service->extend('sms', function ($app) {
return new SmsChannel(new HttpClient, config('smsirlaravel.webservice-url'));
return new SmsChannel(
new HttpClient,
config('smsirlaravel.webservice-url'),
config('smsirlaravel.api-key'),
config('smsirlaravel.secret-key'),
);
});
});
}

6
app/Utilities/HelperClass/NotificationHelper.php

@ -65,7 +65,7 @@ class NotificationHelper
public function sendNotifications($users, $level = null) {
switch ($level) {
case "emergency":
Notification::send($users, new SmsNotification($this->sms_notif, 'ultraFastSend'));
Notification::send($users, new SmsNotification($this->sms_notif, enum('sms.types.ultra_fast_send.name')));
case "critical":
Notification::send($users, new MailNotification($this->notif));
case "high":
@ -73,10 +73,10 @@ class NotificationHelper
case "medium":
Notification::send($users, new FcmNotification($this->notif));
case "low":
Notification::send($users, new SocketNotification($this->notif));
// Notification::send($users, new SocketNotification($this->notif));
break;
default:
Notification::send($users, new SocketNotification($this->notif));
// Notification::send($users, new SocketNotification($this->notif));
}
}

1
database/migrations/2021_03_08_114700_create_notifications_table.php

@ -17,7 +17,6 @@ class CreateNotificationsTable extends Migration
$table->uuid('id')->primary();
$table->string('type');
$table->morphs('notifiable');
$table->unsignedBigInteger('business_id');
$table->text('data');
$table->timestamp('read_at')->nullable();
$table->timestamps();

19
docker-compose.yml

@ -73,6 +73,7 @@ services:
depends_on:
- mysql
mysql:
user: root
image: 'mysql:8.0'
ports:
- '${FORWARD_DB_PORT:-3306}:3306'
@ -83,7 +84,9 @@ services:
MYSQL_PASSWORD: '${DB_PASSWORD}'
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
volumes:
- 'sailmysql:/var/lib/mysql'
# - 'sailmysql:/var/lib/mysql'
- ./my.cnf:/etc/mysql/conf.d/my.cnf
- ./storage/logs/mysql:/var/lib/mysql
networks:
- hi-user
healthcheck:
@ -144,6 +147,20 @@ services:
- 9000:9000
- 12201:12201
- 1514:1514
- 5044:5044
networks:
- hi-user
filebeat:
restart: always
depends_on:
- graylog
user: root
container_name: filebeat
image: docker.elastic.co/beats/filebeat:7.11.2
volumes:
- ./storage/logs/mysql:/var/log/mysql
- ./filebeat.yml:/usr/share/filebeat/filebeat.yml
- ./filebeat.reference.yml:/usr/share/filebeat/filebeat.reference.yml
networks:
- hi-user
networks:

2521
filebeat.reference.yml
File diff suppressed because it is too large
View File

232
filebeat.yml

@ -0,0 +1,232 @@
filebeat.config:
modules:
path: ${path.config}/modules.d/*.yml
reload.enabled: false
processors:
- add_cloud_metadata: ~
- add_docker_metadata: ~
filebeat.inputs:
#------------------------------ Log input --------------------------------
- type: log
# Change to true to enable this input configuration.
enabled: true
# Paths that should be crawled and fetched. Glob based paths.
# To fetch all ".log" files from a specific level of subdirectories
# /var/log/*/*.log can be used.
# For each file found under this path, a harvester is started.
# Make sure not file is defined twice as this can lead to unexpected behaviour.
paths:
- /var/log/mysql/*.log
#- c:\programdata\elasticsearch\logs\*
# Configure the file encoding for reading files with international characters
# following the W3C recommendation for HTML5 (http://www.w3.org/TR/encoding).
# Some sample encodings:
# plain, utf-8, utf-16be-bom, utf-16be, utf-16le, big5, gb18030, gbk,
# hz-gb-2312, euc-kr, euc-jp, iso-2022-jp, shift-jis, ...
#encoding: plain
# Exclude lines. A list of regular expressions to match. It drops the lines that are
# matching any regular expression from the list. The include_lines is called before
# exclude_lines. By default, no lines are dropped.
#exclude_lines: ['^DBG']
# Include lines. A list of regular expressions to match. It exports the lines that are
# matching any regular expression from the list. The include_lines is called before
# exclude_lines. By default, all the lines are exported.
#include_lines: ['^ERR', '^WARN']
# Exclude files. A list of regular expressions to match. Filebeat drops the files that
# are matching any regular expression from the list. By default, no files are dropped.
#exclude_files: ['.gz$']
# Method to determine if two files are the same or not. By default
# the Beat considers two files the same if their inode and device id are the same.
#file_identity.native: ~
# Optional additional fields. These fields can be freely picked
# to add additional information to the crawled log files for filtering
#fields:
# level: debug
# review: 1
# Set to true to store the additional fields as top level fields instead
# of under the "fields" sub-dictionary. In case of name conflicts with the
# fields added by Filebeat itself, the custom fields overwrite the default
# fields.
#fields_under_root: false
# Set to true to publish fields with null values in events.
#keep_null: false
# By default, all events contain `host.name`. This option can be set to true
# to disable the addition of this field to all events. The default value is
# false.
#publisher_pipeline.disable_host: false
# Ignore files which were modified more then the defined timespan in the past.
# ignore_older is disabled by default, so no files are ignored by setting it to 0.
# Time strings like 2h (2 hours), 5m (5 minutes) can be used.
#ignore_older: 0
# How often the input checks for new files in the paths that are specified
# for harvesting. Specify 1s to scan the directory as frequently as possible
# without causing Filebeat to scan too frequently. Default: 10s.
#scan_frequency: 10s
# Defines the buffer size every harvester uses when fetching the file
#harvester_buffer_size: 16384
# Maximum number of bytes a single log event can have
# All bytes after max_bytes are discarded and not sent. The default is 10MB.
# This is especially useful for multiline log messages which can get large.
#max_bytes: 10485760
# Characters which separate the lines. Valid values: auto, line_feed, vertical_tab, form_feed,
# carriage_return, carriage_return_line_feed, next_line, line_separator, paragraph_separator.
#line_terminator: auto
### Recursive glob configuration
# Expand "**" patterns into regular glob patterns.
#recursive_glob.enabled: true
### JSON configuration
# Decode JSON options. Enable this if your logs are structured in JSON.
# JSON key on which to apply the line filtering and multiline settings. This key
# must be top level and its value must be string, otherwise it is ignored. If
# no text key is defined, the line filtering and multiline features cannot be used.
#json.message_key:
# By default, the decoded JSON is placed under a "json" key in the output document.
# If you enable this setting, the keys are copied top level in the output document.
#json.keys_under_root: false
# If keys_under_root and this setting are enabled, then the values from the decoded
# JSON object overwrite the fields that Filebeat normally adds (type, source, offset, etc.)
# in case of conflicts.
#json.overwrite_keys: false
# If this setting is enabled, then keys in the decoded JSON object will be recursively
# de-dotted, and expanded into a hierarchical object structure.
# For example, `{"a.b.c": 123}` would be expanded into `{"a":{"b":{"c":123}}}`.
#json.expand_keys: false
# If this setting is enabled, Filebeat adds a "error.message" and "error.key: json" key in case of JSON
# unmarshaling errors or when a text key is defined in the configuration but cannot
# be used.
#json.add_error_key: false
### Multiline options
# Multiline can be used for log messages spanning multiple lines. This is common
# for Java Stack Traces or C-Line Continuation
# The regexp Pattern that has to be matched. The example pattern matches all lines starting with [
# multiline.pattern: ^\[
# Defines if the pattern set under pattern should be negated or not. Default is false.
#multiline.negate: false
# Match can be set to "after" or "before". It is used to define if lines should be append to a pattern
# that was (not) matched before or after or as long as a pattern is not matched based on negate.
# Note: After is the equivalent to previous and before is the equivalent to to next in Logstash
#multiline.match: after
# The maximum number of lines that are combined to one event.
# In case there are more the max_lines the additional lines are discarded.
# Default is 500
#multiline.max_lines: 500
# After the defined timeout, an multiline event is sent even if no new pattern was found to start a new event
# Default is 5s.
#multiline.timeout: 5s
# To aggregate constant number of lines into a single event use the count mode of multiline.
#multiline.type: count
# The number of lines to aggregate into a single event.
#multiline.count_lines: 3
# Do not add new line character when concatenating lines.
#multiline.skip_newline: false
# Setting tail_files to true means filebeat starts reading new files at the end
# instead of the beginning. If this is used in combination with log rotation
# this can mean that the first entries of a new file are skipped.
#tail_files: false
# The Ingest Node pipeline ID associated with this input. If this is set, it
# overwrites the pipeline option from the Elasticsearch output.
#pipeline:
# If symlinks is enabled, symlinks are opened and harvested. The harvester is opening the
# original for harvesting but will report the symlink name as source.
#symlinks: false
# Backoff values define how aggressively filebeat crawls new files for updates
# The default values can be used in most cases. Backoff defines how long it is waited
# to check a file again after EOF is reached. Default is 1s which means the file
# is checked every second if new lines were added. This leads to a near real time crawling.
# Every time a new line appears, backoff is reset to the initial value.
#backoff: 1s
# Max backoff defines what the maximum backoff time is. After having backed off multiple times
# from checking the files, the waiting time will never exceed max_backoff independent of the
# backoff factor. Having it set to 10s means in the worst case a new line can be added to a log
# file after having backed off multiple times, it takes a maximum of 10s to read the new line
#max_backoff: 10s
# The backoff factor defines how fast the algorithm backs off. The bigger the backoff factor,
# the faster the max_backoff value is reached. If this value is set to 1, no backoff will happen.
# The backoff value will be multiplied each time with the backoff_factor until max_backoff is reached
#backoff_factor: 2
# Max number of harvesters that are started in parallel.
# Default is 0 which means unlimited
#harvester_limit: 0
### Harvester closing options
# Close inactive closes the file handler after the predefined period.
# The period starts when the last line of the file was, not the file ModTime.
# Time strings like 2h (2 hours), 5m (5 minutes) can be used.
#close_inactive: 5m
# Close renamed closes a file handler when the file is renamed or rotated.
# Note: Potential data loss. Make sure to read and understand the docs for this option.
#close_renamed: false
# When enabling this option, a file handler is closed immediately in case a file can't be found
# any more. In case the file shows up again later, harvesting will continue at the last known position
# after scan_frequency.
#close_removed: true
# Closes the file handler as soon as the harvesters reaches the end of the file.
# By default this option is disabled.
# Note: Potential data loss. Make sure to read and understand the docs for this option.
#close_eof: false
### State options
# Files for the modification data is older then clean_inactive the state from the registry is removed
# By default this is disabled.
#clean_inactive: 0
# Removes the state for file which cannot be found on disk anymore immediately
#clean_removed: true
# Close timeout closes the harvester after the predefined time.
# This is independent if the harvester did finish reading the file or not.
# By default this option is disabled.
# Note: Potential data loss. Make sure to read and understand the docs for this option.
#close_timeout: 0
output.logstash:
hosts: ["graylog:5044"]

19
my.cnf

@ -0,0 +1,19 @@
[mysqld]
sync_binlog = 1
innodb_buffer_pool_size = 1G
innodb_log_file_size = 2047M
innodb_flush_log_at_trx_commit = 0
innodb_flush_method = O_DIRECT
innodb_buffer_pool_instances = 8
innodb_thread_concurrency = 8
innodb_io_capacity = 1000
innodb_io_capacity_max = 3000
innodb_buffer_pool_dump_pct = 75
innodb_adaptive_hash_index_parts = 16
innodb_read_io_threads = 16
innodb_write_io_threads = 16
innodb_flush_neighbors = 0
innodb_flushing_avg_loops = 100
innodb_page_cleaners = 8
long_query_time = 0.2
slow_query_log = ON

4
routes/api.php

@ -9,8 +9,8 @@ $router->get('/lab', function () {
$router->get('/ntest', function () {
$user = \App\Models\User::find(1);
\Illuminate\Support\Facades\Notification::send($user, new \App\Notifications\SmsNotification(['verification_code' => "1234"]));
(new \App\Utilities\HelperClass\NotificationHelper())
->makeSmsNotif('template_name', ['user' => 'myUser', 'business' => 'myBusiness']);
// (new \App\Utilities\HelperClass\NotificationHelper())
// ->makeSmsNotif('template_name', ['user' => 'myUser', 'business' => 'myBusiness']);
})->middleware('bindBusiness');
$router->group(['prefix' => 'actions'], function () use ($router) {
$router->group(['prefix' => 'businesses'], function () use ($router) {

Loading…
Cancel
Save