Skip to content

Instantly share code, notes, and snippets.

@djamol
Created April 18, 2023 05:53
Show Gist options
  • Save djamol/80e60f691c9bcd495f9f06dd9ca022ac to your computer and use it in GitHub Desktop.
Save djamol/80e60f691c9bcd495f9f06dd9ca022ac to your computer and use it in GitHub Desktop.
<?php
namespace App\Models\mongo;
use Carbon\Carbon;
use MongoDB\BSON\UTCDateTime;
use DB;
use Jenssegers\Mongodb\Eloquent\Model;
class BotHistory extends Model
{
protected $connection = 'mongodb';
protected $collection = 'bot_history';
// set the fillable properties
protected $fillable = [
'bot_id',
'input',
'output',
'intent',
'customer_id',
'website_id',
'ip'
];
protected $dates = ['created_at', 'updated_at'];
/*
public function getLog() {
return $this->belongsTo(Log::class);
}
*/
public static function rawQuery($query,$REQmatch=null,$start=null,$end=null)
{
if($start!=null && $end!=null){
$date = Carbon::parse($start);
$utcDate = new UTCDateTime($date->timestamp * 1000);
$date2 = Carbon::parse($end);
$utcDate2 = new UTCDateTime($date2->timestamp * 1000);
$match =['created_at' => [
'$gte' => $utcDate,
'$lte' => $utcDate2
]];
if($REQmatch!=null){
$match=array_merge($match,$REQmatch);
}
$query2 = [
[
'$match' => $match
],
];
$nw = array_merge($query,$query2);
}else{
$nw =$query;
}
$history = self::raw(function($collection) use($nw) {
return $collection->aggregate($nw);
});
return $history;
}
}
@djamol
Copy link
Author

djamol commented Apr 18, 2023

GET/POST API REQUEST INPUT DATA

{
    "api": "chatbox_api",
    "created_at":["2023-04-12 02:42:42","2023-04-12 09:42:42"],
    "match":{"customer_id":{"$eq":"wa_919545553061"},"cust_msg":{"$eq":"0"}},
    "query": [  {
        "$project": {
          "myTimestamp": {
            "$dateToString": {
              "format": "%Y-%m-%d %H:%M:%S",
              "date": {
                "$add": [
                  {"$toDate": "$created_at"},
                  19800000
                ]
              },
              "timezone": "+05:30"
            }
          },
          "created_at": 1,
          "output": 1,
          "customer_id":1,
          "unix_time":{"$toDate":"$created_at"},
          "check_customer":{"$eq":["$customer_id","wa_919545553061"]},
          "cust_msg": "$input"
        }
      }
        
    ]
    
}
 $query = $request->get('query', null);
		$nm =$request->get('created_at', null);
		$match =$request->get('match', []);
		if(count($nm)>0){
			$data= BotHistory::rawQuery($query,$match,$nm[0],$nm[1]);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment