Skip to content

Commit

Permalink
v1.1.19
Browse files Browse the repository at this point in the history
  • Loading branch information
andiwijaya-dev committed Apr 26, 2021
1 parent b0ca023 commit 94e71e9
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/Console/Commands/ScheduledTaskRun.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ public function handle()
else
ScheduledTask::check($this);

$this->info("Completed in " . (microtime(1) - LARAVEL_START));
//$this->info("Completed in " . (microtime(1) - LARAVEL_START));
}
}
93 changes: 93 additions & 0 deletions src/Facades/Log.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace Andiwijaya\AppCore\Facades;

use Andiwijaya\AppCore\Models\ScheduledTask;
use Andiwijaya\AppCore\Notifications\SlackNotification;
use App\Notifications\LogToSlackNotification;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Notification;

class Log{

private static $settings = [];

public static function make($message, $type = 'info', $settings = null){

$detail = '';
if(is_array($message)){
$detail = $message[1] ?? '';
$message = $message[0] ?? '';
}

$formattedMessage = implode("\t", [
"[" . Carbon::now()->format('Y-m-d H:i:s') . "]",
$message
]);

if(isset(self::$settings['output']) || isset($settings['output'])){
$output = $settings['output'] ?? self::$settings['output'];
if(is_callable([ $output, $type ]))
call_user_func_array([ $output, $type ], [ $formattedMessage ]);
}

if(isset(self::$settings['path']) || isset($settings['path'])){
$path = $settings['path'] ?? self::$settings['path'];
$fp = fopen(self::$settings['path'], 'a+');
if($fp){
fwrite($fp, $formattedMessage . "\n");
fclose($fp);
}
}

if(isset(self::$settings['table']) || isset($settings['table'])){
$table = $settings['table'] ?? self::$settings['table'];
DB::table($table)->insert([
'type'=>1,
'data'=>json_encode([ 'type'=>$type, 'message'=>$message ]),
'created_at'=>Carbon::now()->format('Y-m-d H:i:s')
]);
}

if(isset(self::$settings['slack']) || isset($settings['slack'])){
$slack = $settings['slack'] ?? self::$settings['slack'];
ScheduledTask::runOnce(function($result) use($slack, $type, $message, $detail){
Notification::route('slack', $slack)
->notify(new SlackNotification($message, $type, $detail));
});
}
}

public static function error($message)
{
return self::make($message, 'error');
}

public static function warning($message)
{
return self::make($message, 'warning');
}

public static function info($message)
{
return self::make($message);
}

public static function set(array $settings, $overwrite = false){

foreach($settings as $key=>$value){
switch($key){

case 'path':
case 'table':
case 'output':
case 'slack':
if(!isset(self::$settings[$key]) || $overwrite)
self::$settings[$key] = $value;
break;
}
}
}

}
4 changes: 3 additions & 1 deletion src/Http/Controllers/ChatAdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,8 @@ function(){
'Nama Pelanggan',
'Topic',
'Date',
'Type',
'Tipe',
'Channel',
'Message',
'Context',
'Otomatis'
Expand All @@ -353,6 +354,7 @@ function(){
$message->discussion->title,
$message->created_at,
$message->direction == ChatMessage::DIRECTION_IN ? 'In' : 'Out',
$message->type == ChatMessage::TYPE_WHATSAPP ? 'WhatsApp' : 'Web',
$message->text,
__('models.chat-context-' . ($message->context ?? '')),
$message->is_system ? 'Ya' : 'Tidak'
Expand Down
7 changes: 5 additions & 2 deletions src/Models/ChatMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ class ChatMessage extends Model

protected $table = 'chat_message';

protected $fillable = [ 'discussion_id', 'session_id', 'reply_of', 'initial', 'first_reply_at', 'first_reply_after',
const TYPE_WEB = 1;
const TYPE_WHATSAPP = 2;

protected $fillable = [ 'type', 'discussion_id', 'session_id', 'reply_of', 'initial', 'first_reply_at', 'first_reply_after',
'unread', 'direction', 'text', 'images', 'extra', 'notified', 'unsent',
'context', 'is_bot', 'is_system', 'notified_at' ];
'context', 'context_id', 'is_bot', 'is_system', 'notified_at', 'ref_id' ];

protected $attributes = [
'unread'=>1,
Expand Down
18 changes: 9 additions & 9 deletions src/Models/ScheduledTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@ public function calculate()
" then 1 else 0 end) as `error`"));
//exc($model->toSql());
$res = $model->first();

$this->count = $res->count ?? 0;
$this->error = $res->error ?? 0;
parent::save();
}

public function run(){

if(in_array($this->status, [ self::STATUS_DISABLED ])) return;
if(in_array($this->status, [ self::STATUS_DISABLED, self::STATUS_RUNNING ])) return;

if($this->status == self::STATUS_RUNNING){
foreach($this->results->where('status', ScheduledTask::STATUS_RUNNING) as $result)
Expand Down Expand Up @@ -142,19 +142,19 @@ public function run(){
}
catch(\Exception $ex){
$exitCode = 1;
$output = $ex->getMessage() . "@" . $ex->getFile() . ":" . $ex->getLine();
$output = $ex->getMessage() . "@" . $ex->getFile() . ":" . $ex->getLine() . PHP_EOL;
}
}
else{
$exitCode = Artisan::call($this->command);
$output = Artisan::output();
}

if($this->remove_after_completed)
if($exitCode == 0 && $this->remove_after_completed)
$this->delete();
else{
$result->status = $exitCode > 0 ? self::STATUS_ERROR : self::STATUS_COMPLETED;
$result->verbose .= $output . PHP_EOL;
$result->verbose .= $output;
$result->ellapsed = microtime(1) - $t1;
$result->completed_at = Carbon::now()->format('Y-m-d H:i:s');
$result->save();
Expand All @@ -168,10 +168,10 @@ public function run(){
public function runInBackground($delay = 0){

chdir(base_path());

$log_path = storage_path('logs/laravel.log');

exec("php artisan scheduled-task:run --id={$this->id} --delay={$delay} > {$log_path} 2>&1 &", $output, $return_var);
$log_path = storage_path('logs/scheduled-task.log');
exec("php artisan scheduled-task:run --id={$this->id} --delay={$delay} >> {$log_path} 2>&1 &", $output, $return_var);
if($output) file_put_contents("[" . Carbon::now()->format('Y-m-d H:i:s') . "] output:" . json_encode($output) . "\n", FILE_APPEND);
if($return_var) file_put_contents("[" . Carbon::now()->format('Y-m-d H:i:s') . "] return:" . json_encode($return_var) . "\n", FILE_APPEND);
}

public static function check(Command $cmd = null){
Expand Down
51 changes: 51 additions & 0 deletions src/Notifications/SlackNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Andiwijaya\AppCore\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\SlackMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

class SlackNotification extends Notification
{
use Queueable;

protected $message;
protected $type;
protected $detail;

public function __construct($message, $type = 'info', $detail = '')
{
$this->message = $message;
$this->type = $type;
$this->detail = $detail;
}

public function via($notifiable)
{
return ['slack'];
}

public function toSlack($notifiable)
{
$message = (new SlackMessage)
->content($this->message);

switch($this->type){
case 'info': $message->info(); break;
case 'error': $message->error(); break;
case 'warning': $message->warning(); break;
}

if($this->detail)
$message
->attachment(function($attachment){
$attachment->title('Detail')
->content($this->detail);
});

return $message;
}
}
21 changes: 16 additions & 5 deletions src/views/components/chat-admin-message-foot.blade.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
@if($discussion->handled_by == $user->id)
<div class="srow pad-1">

@php $chat_message_templates = App\Models\ChatMessageTemplate::whereDirection(\Andiwijaya\AppCore\Models\ChatMessage::DIRECTION_OUT)->orderBy('id')->get() @endphp

@if(count($chat_message_templates) > 0)
<div class="chat-message-templates nowrap hmar-1">
@foreach($chat_message_templates as $chat_message_template)
<span class="chat-message-template" onclick="chatadmin_set_template.apply(this)">{{ $chat_message_template->text }}</span>
@endforeach
</div>
@endif

<div class="srow pad-1 vpadt-0">
<div>
<div class="textarea" data-validation="required">
<textarea rows="1" placeholder="ENTER utk kirim, CTRL+ENTER utk tambahan baris" name="text" onkeydown="chatadmin_keyup.apply(this, arguments)"></textarea>
<div class="textarea" style="height:2.78rem">
<textarea rows="1" name="text"></textarea>
</div>
</div>
<span>
<input type="hidden" name="last_id" />
<button type="button" class="hmarl-1" onclick="$('.chat-admin').chatadmin_add_file()"><label><span class="fa fa-image"></span></label></button>
<button class="hpad-2"><label>Kirim</label></button>
<button type="button" class="hmarl-05" onclick="$('.chat-admin').chatadmin_add_file()"><label><span class="fa fa-image"></span></label></button>
<button class="hpad-1 btn-send-message"><label>Kirim</label></button><button class="hpad-1 btn-send-whatsapp" name="action" value="send-whatsapp"><label><span class="fab fa-whatsapp"></span></label></button>
</span>
</div>
<div class="images repeater hpad-1">
Expand Down
2 changes: 1 addition & 1 deletion src/views/layouts/minimal.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ function c(){
<script type="text/javascript" src="{{ env('APP_CDN_HOST') }}/js/{{ $js ?? 'default' }}.js?v={{ assets_version() }}" defer></script>
@endif

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.14.0/css/all.min.css" media="print" onload="c.apply(this)"/>
<link rel="stylesheet" href="/css/all.min.css" media="print" onload="c.apply(this)"/>
@if(isset($debug) && $debug)
@foreach(glob(public_path('/css/' . ($css ?? 'clean') . '/*.css')) as $path)
<link rel="stylesheet" href="{{ '/css/' . ($css ?? 'clean') . '/' . basename($path) }}?v={{ assets_version () }}" media="print" onload="c.apply(this)"/>
Expand Down

0 comments on commit 94e71e9

Please sign in to comment.