input('isuser'); if ($type == 'user') { $usercount = Users::count(); $devicecount = Device::where('uid','=',Auth::id())->count(); $devicepolice = Device::where('uid','=',Auth::id())->where('devicepolice', '>', '1')->count(); // 设备在线数量 $device_online = Device::where('uid','=',Auth::id())->whereNotIn('devicepolice', [9, 15])->count(); // 设备报警数量 $device_alarm = Device::where('uid','=',Auth::id())->whereIn('devicepolice', [4, 5, 6, 16])->count(); // 未报警设备数量 $device_normal = Device::where('uid','=',Auth::id())->where('devicepolice', 1)->count(); // 故障设备 $device_offline = Device::where('uid','=',Auth::id())->whereNotIn('devicepolice', [1, 4, 5, 6, 16])->count(); $t = time();//当前时间 $start = mktime(0, 0, 0, date("m", $t), date("d", $t), date("Y", $t));//今天的开始 $end = mktime(23, 59, 59, date("m", $t), date("d", $t), date("Y", $t));//今天的结束 $timedevicepolice = DB::table('reportpolice')->where('status', '=', '1')->whereBetween('starttime', [$start, $end])->count(); } else { $usercount = Users::count(); $devicecount = Device::count(); $devicepolice = Device::where('devicepolice', '>', '1')->count(); // 设备在线数量 $device_online = Device::whereNotIn('devicepolice', [9, 15])->count(); // 设备报警数量 $device_alarm = Device::whereIn('devicepolice', [4, 5, 6, 16])->count(); // 未报警设备数量 $device_normal = Device::where('devicepolice', 1)->count(); // 故障设备 $device_offline = Device::whereNotIn('devicepolice', [1, 4, 5, 6, 16])->count(); $t = time();//当前时间 $start = mktime(0, 0, 0, date("m", $t), date("d", $t), date("Y", $t));//今天的开始 $end = mktime(23, 59, 59, date("m", $t), date("d", $t), date("Y", $t));//今天的结束 $timedevicepolice = DB::table('reportpolice')->where('status', '=', '1')->whereBetween('starttime', [$start, $end])->count(); } return $this->jsonSuccessData([ 'usercount' => $usercount, 'devicecount' => $devicecount, 'devicepolice' => $devicepolice, 'timedevicepolice' => $timedevicepolice, 'device_online' => $device_online, 'device_alarm' => $device_alarm, 'device_normal' => $device_normal, 'device_offline' => $device_offline, 'percent_alarm' => (float)number_format(($device_alarm/$devicecount) * 100, '2'), 'percent_online' => (float)number_format(($device_online/$devicecount) * 100, '2'), ]); } public function gettimeline() { return $this->jsonSuccessData(DB::table('timeline')->orderByDesc('id')->get()); } //返回一年当前月份 public function devicemonthcount(Request $request) { $type = $request->input('isuser'); if ($type == 'user') { $year_start = strtotime(date("Y") . "-01-01"); // 获取当前的1月份的时间戳 $endThismonth = mktime(23, 59, 59, date('m'), date('t'), date('Y')); //获取当月的时间戳 $devicedata = Device::where('uid','=',Auth::id())->whereBetween('deviceaddtime', [$year_start, $endThismonth]) ->selectRaw('from_unixtime(deviceaddtime,"%Y-%m") as date,COUNT(id) as value') ->groupBy('date') ->get() ->toArray(); $policedata = Users::whereBetween('created_at', [date('Y-m-d H:i:s', $year_start), date('Y-m-d H:i:s', $endThismonth)]) ->selectRaw('from_unixtime(unix_timestamp(created_at),"%Y-%m") as date,COUNT(id) as value') ->groupBy('date') ->get() ->toArray(); $devicepolice = Device::where('uid','=',Auth::id())->whereBetween('deviceaddtime', [$year_start, $endThismonth]) ->where('devicepolice', '>', 1) ->selectRaw('from_unixtime(deviceaddtime,"%Y-%m") as date,COUNT(id) as value') ->groupBy('date') ->get() ->toArray(); }else{ $year_start = strtotime(date("Y") . "-01-01"); // 获取当前的1月份的时间戳 $endThismonth = mktime(23, 59, 59, date('m'), date('t'), date('Y')); //获取当月的时间戳 $devicedata = Device::whereBetween('deviceaddtime', [$year_start, $endThismonth]) ->selectRaw('from_unixtime(deviceaddtime,"%Y-%m") as date,COUNT(id) as value') ->groupBy('date') ->get() ->toArray(); $policedata = Users::whereBetween('created_at', [date('Y-m-d H:i:s', $year_start), date('Y-m-d H:i:s', $endThismonth)]) ->selectRaw('from_unixtime(unix_timestamp(created_at),"%Y-%m") as date,COUNT(id) as value') ->groupBy('date') ->get() ->toArray(); $devicepolice = Device::whereBetween('deviceaddtime', [$year_start, $endThismonth]) ->where('devicepolice', '>', 1) ->selectRaw('from_unixtime(deviceaddtime,"%Y-%m") as date,COUNT(id) as value') ->groupBy('date') ->get() ->toArray(); } return $this->jsonSuccessData(['devicedata' => $this->datamonth($devicedata), 'policedata' => $this->datamonth($policedata), 'devicepolice' => $this->datamonth($devicepolice)]); } //用户统计 public function UserStatistics() { $devicecount = Device::where('uid', '=', Auth::id())->count(); $devicepolice = Device::where('devicepolice', '>', '1')->where('uid', '=', Auth::id())->count(); $t = time();//当前时间 $start = mktime(0, 0, 0, date("m", $t), date("d", $t), date("Y", $t));//今天的开始 $end = mktime(23, 59, 59, date("m", $t), date("d", $t), date("Y", $t));//今天的结束 $timedevicepolice = DB::table('reportpolice as r') ->join('device as d', 'd.devicenum', '=', 'r.devicenumber') ->where('d.uid', '=', Auth::id()) ->where('r.status', '=', '1') ->whereBetween('r.starttime', [$start, $end]) ->count(); return $this->jsonSuccessData(['devicecount' => $devicecount, 'devicepolice' => $devicepolice, 'timedevicepolice' => $timedevicepolice]); } //返回一年当前月份 public function userdevicemonthcount(Request $request) { \Session::put(['loginuid' => Auth::id()]); $year_start = strtotime(date("Y") . "-01-01"); // 获取当前的1月份的时间戳 $endThismonth = mktime(23, 59, 59, date('m'), date('t'), date('Y')); //获取当月的时间戳 $devicedata = Device::where('uid', '=', Auth::id())->whereBetween('deviceaddtime', [$year_start, $endThismonth]) ->selectRaw('from_unixtime(deviceaddtime,"%Y-%m") as date,COUNT(id) as value') ->groupBy('date') ->get() ->toArray(); $devicepolice = Device::where('uid', '=', Auth::id())->whereBetween('deviceaddtime', [$year_start, $endThismonth]) ->where('devicepolice', '>', 1) ->selectRaw('from_unixtime(deviceaddtime,"%Y-%m") as date,COUNT(id) as value') ->groupBy('date') ->get() ->toArray(); return $this->jsonSuccessData(['devicedata' => $this->datamonth($devicedata), 'devicepolice' => $this->datamonth($devicepolice)]); } // 项目统计 public function Project_statistics() { $start_time = microtime(true); $memory = (!function_exists('memory_get_usage')) ? '0' : round(memory_get_usage() / 1024 / 1024, 2); $unit = array('B', 'KB', 'MB', 'GB', 'TB', 'PD'); $phay = base_path(); $size = $this->getDirSize($phay); $base_path = @round($size / pow(1024, ($i = floor(log($size, 1024)))), 2); $end_time = microtime(true); return $this->jsonSuccessData(['memory' => round($memory), 'base_path' => round($base_path) / 10, 'end_time' => round($end_time - $start_time), 'dabatime' => ceil($this->dabadas()), 'rand' => rand(1, 100)]); } //获取接口访问时间 private function datamonth($monthdata = []): array { $year = date('Y', time()); $month = date('m', time()); $data = []; foreach ($monthdata as $value) { $data[$value['date']] = $value['value']; } $out = []; for ($i = 1; $i <= $month; $i++) { $mont = $i < 10 ? "0{$i}" : $i; $val = isset($data["{$year}-{$mont}"]) ? $data["{$year}-{$mont}"] : 0; array_push($out, $val); } return $out; } private function getDirSize($dir) { @$dh = opendir($dir); $size = 0; while ($file = @readdir($dh)) { if ($file != "." and $file != "..") { $path = $dir . "/" . $file; if (is_dir($path)) { $size += $this->getDirSize($path); } elseif (is_file($path)) { $size += filesize($path); } } } @closedir($dh); return $size; } private function dabadas() { $start_time = microtime(true); DB::table('device')->select(); DB::table('timeline')->select(); DB::table('BackgroundUser')->select(); $end_time = microtime(true); return $end_time - $start_time; } }