From 1996751d963f6ae367c73e2294bfc0ae71297fd4 Mon Sep 17 00:00:00 2001 From: Arjen van Bochoven Date: Fri, 12 Jul 2019 21:26:47 +0200 Subject: [PATCH] New machine model --- machine_controller.php | 180 ++++++++------ machine_model.php | 219 +++--------------- ...ssor.disabled.php => machine_processor.php | 22 +- 3 files changed, 146 insertions(+), 275 deletions(-) rename machine_processor.disabled.php => machine_processor.php (71%) diff --git a/machine_controller.php b/machine_controller.php index 393788c..f66c59d 100644 --- a/machine_controller.php +++ b/machine_controller.php @@ -38,9 +38,16 @@ public function index() **/ public function get_duplicate_computernames() { - $machine = new Machine_model(); + $machine = Machine_model::selectRaw('computer_name, COUNT(*) AS count') + ->filter() + ->groupBy('computer_name') + ->having('count', '>', 1) + ->orderBy('count', 'desc') + ->get() + ->toArray(); + $obj = new View(); - $obj->view('json', array('msg' => $machine->get_duplicate_computernames())); + $obj->view('json', ['msg' => $machine]); } /** @@ -49,9 +56,59 @@ public function get_duplicate_computernames() **/ public function get_model_stats($summary="") { - $machine = new Machine_model(); + $machine = Machine_model::selectRaw('count(*) AS count, machine_desc AS label') + ->filter() + ->groupBy('machine_desc') + ->orderBy('count', 'desc') + ->get() + ->toArray(); + + $out = array(); + foreach ($machine as $obj) { + $obj['label'] = $obj['label'] ? $obj['label'] : 'Unknown'; + $out[] = $obj; + } + + // Check if we need to convert to summary (Model + screen size) + if($summary){ + $model_list = array(); + foreach ($out as $key => $obj) { + // Mac mini Server (Late 2012) + // + $suffix = ""; + if(preg_match('/^(.+) \((.+)\)/', $obj['label'], $matches)) + { + $name = $matches[1]; + // Find suffix + if(preg_match('/([\d\.]+-inch)/', $matches[2], $matches)) + { + $suffix = ' ('.$matches[1].')'; + } + } + else + { + $name = $obj['label']; + + } + if(! isset($model_list[$name.$suffix])) + { + $model_list[$name.$suffix] = 0; + } + $model_list[$name.$suffix] += $obj['count']; + + } + // Erase out + $out = array(); + // Sort model list + arsort($model_list); + // Add entries to $out + foreach ($model_list as $key => $count) + { + $out[] = array('label' => $key, 'count' => $count); + } + } $obj = new View(); - $obj->view('json', array('msg' => $machine->get_model_stats($summary))); + $obj->view('json', ['msg' => $out]); } @@ -63,9 +120,11 @@ public function get_model_stats($summary="") **/ public function report($serial_number = '') { - $machine = new Machine_model($serial_number); + $machine = Machine_model::where('machine.serial_number', $serial_number) + ->filter() + ->first(); $obj = new View(); - $obj->view('json', array('msg' => $machine->rs)); + $obj->view('json', array('msg' => $machine)); } /** @@ -77,21 +136,12 @@ public function report($serial_number = '') public function new_clients() { $lastweek = time() - 60 * 60 * 24 * 7; - $out = array(); - $machine = new Machine_model(); - - $filter = get_machine_group_filter('AND'); - - $sql = "SELECT machine.serial_number, computer_name, reg_timestamp - FROM machine - LEFT JOIN reportdata USING (serial_number) - WHERE reg_timestamp > $lastweek - $filter - ORDER BY reg_timestamp DESC"; - - foreach ($machine->query($sql) as $obj) { - $out[] = $obj; - } + $out = Machine_model::select('machine.serial_number', 'computer_name', 'reg_timestamp') + ->where('reg_timestamp', '>', $lastweek) + ->filter() + ->orderBy('reg_timestamp', 'desc') + ->get() + ->toArray(); $obj = new View(); $obj->view('json', array('msg' => $out)); @@ -109,13 +159,19 @@ public function get_memory_stats($format = 'none') // Legacy loop to do sort in php $tmp = array(); - $machine = new Machine_model(); - foreach ($machine->get_memory_stats() as $obj) { + $machine = Machine_model::selectRaw('physical_memory, count(1) as count') + ->filter() + ->groupBy('physical_memory') + ->orderBy('physical_memory', 'desc') + ->get() + ->toArray(); + + foreach ($machine as $obj) { // Take care of mixed entries (string or int) - if (isset($tmp[$obj->physical_memory])) { - $tmp[$obj->physical_memory] += $obj->count; + if (isset($tmp[$obj['physical_memory']])) { + $tmp[$obj['physical_memory']] += $obj['count']; } else { - $tmp[$obj->physical_memory] = $obj->count; + $tmp[$obj['physical_memory']] = $obj['count']; } } @@ -146,17 +202,15 @@ public function get_memory_stats($format = 'none') **/ public function hw() { - $out = array(); - $machine = new Machine_model(); - $sql = "SELECT machine_name, count(1) as count - FROM machine - LEFT JOIN reportdata USING (serial_number) - ".get_machine_group_filter()." - GROUP BY machine_name - ORDER BY count DESC"; - $cnt = 0; - foreach ($machine->query($sql) as $obj) { - $out[] = array('label' => $obj->machine_name, 'count' => intval($obj->count)); + $out = []; + $machine = Machine_model::selectRaw('machine_name, count(1) as count') + ->filter() + ->groupBy('machine_name') + ->orderBy('count', 'desc') + ->get() + ->toArray(); + foreach ($machine as $obj) { + $out[] = array('label' => $obj['machine_name'], 'count' => intval($obj['count'])); } $obj = new View(); @@ -170,23 +224,10 @@ public function hw() **/ public function os() { - $out = array(); - $machine = new Machine_model(); - $sql = "SELECT count(1) as count, os_version - FROM machine - LEFT JOIN reportdata USING (serial_number) - ".get_machine_group_filter()." - GROUP BY os_version - ORDER BY os_version DESC"; - - foreach ($machine->query($sql) as $obj) { - $obj->os_version = $obj->os_version ? $obj->os_version : '0'; - $out[] = array('label' => $obj->os_version, 'count' => intval($obj->count)); - } - - $obj = new View(); - $obj->view('json', array('msg' => $out)); + $obj->view('json', [ + 'msg' => $this->_trait_stats('os_version') + ]); } /** * Return json array with os build breakdown @@ -195,22 +236,25 @@ public function os() **/ public function osbuild() { - $out = array(); - $machine = new Machine_model(); - $sql = "SELECT count(1) as count, buildversion - FROM machine - LEFT JOIN reportdata USING (serial_number) - ".get_machine_group_filter()." - GROUP BY buildversion - ORDER BY buildversion DESC"; - - foreach ($machine->query($sql) as $obj) { - $obj->buildversion = $obj->buildversion ? $obj->buildversion : '0'; - $out[] = array('label' => $obj->buildversion, 'count' => intval($obj->count)); - } + $obj = new View(); + $obj->view('json', [ + 'msg' => $this->_trait_stats('buildversion') + ]); + } + private function _trait_stats($what = 'os_version'){ + $out = []; + $machine = Machine_model::selectRaw("count(1) as count, $what") + ->filter() + ->groupBy($what) + ->orderBy($what, 'desc') + ->get() + ->toArray(); - $obj = new View(); - $obj->view('json', array('msg' => $out)); + foreach ($machine as $obj) { + $obj[$what] = $obj[$what] ? $obj[$what] : '0'; + $out[] = ['label' => $obj[$what], 'count' => intval($obj['count'])]; + } + return $out; } } // END class Machine_controller diff --git a/machine_model.php b/machine_model.php index 7708d43..a891eaf 100644 --- a/machine_model.php +++ b/machine_model.php @@ -1,196 +1,33 @@ rs['id'] = ''; - $this->rs['serial_number'] = $serial; - $this->rs['hostname'] = ''; - $this->rs['machine_model'] = ''; - $this->rs['machine_desc'] = ''; - $this->rs['img_url'] = ''; - $this->rs['cpu'] = ''; - $this->rs['current_processor_speed'] = ''; - $this->rs['cpu_arch'] = ''; - $this->rs['os_version'] = 0; - $this->rs['physical_memory'] = 0; - $this->rs['platform_UUID'] = ''; - $this->rs['number_processors'] = 0; - $this->rs['SMC_version_system'] = ''; - $this->rs['boot_rom_version'] = ''; - $this->rs['bus_speed'] = ''; - $this->rs['computer_name'] = ''; - $this->rs['l2_cache'] = ''; - $this->rs['machine_name'] = ''; - $this->rs['packages'] = ''; - $this->rs['buildversion'] = ''; - - if ($serial) { - $this->retrieve_record($serial); - } - - $this->serial = $serial; - } - - /** - * Get duplicate computernames - * - * - **/ - public function get_duplicate_computernames() - { - $out = array(); - $filter = get_machine_group_filter(); - $sql = "SELECT computer_name, COUNT(*) AS count - FROM machine - LEFT JOIN reportdata USING (serial_number) - $filter - GROUP BY computer_name - HAVING count > 1 - ORDER BY count DESC"; - - foreach ($this->query($sql) as $obj) { - $out[] = $obj; - } - - return $out; - } - - /** - * Get model statistics - * - **/ - public function get_model_stats($summary) - { - $out = array(); - $filter = get_machine_group_filter(); - $sql = "SELECT count(*) AS count, machine_desc AS label - FROM machine - LEFT JOIN reportdata USING (serial_number) - $filter - GROUP BY machine_desc - ORDER BY count DESC"; - - foreach ($this->query($sql) as $obj) { - $obj->label = $obj->label ? $obj->label : 'Unknown'; - $out[] = $obj; - } - - // Check if we need to convert to summary (Model + screen size) - if($summary){ - $model_list = array(); - foreach ($out as $key => $obj) { - // Mac mini Server (Late 2012) - // - $suffix = ""; - if(preg_match('/^(.+) \((.+)\)/', $obj->label, $matches)) - { - $name = $matches[1]; - // Find suffix - if(preg_match('/([\d\.]+-inch)/', $matches[2], $matches)) - { - $suffix = ' ('.$matches[1].')'; - } - } - else - { - $name = $obj->label; - - } - if(! isset($model_list[$name.$suffix])) - { - $model_list[$name.$suffix] = 0; - } - $model_list[$name.$suffix] += $obj->count; - - } - // Erase out - $out = array(); - // Sort model list - arsort($model_list); - // Add entries to $out - foreach ($model_list as $key => $count) - { - $out[] = array('label' => $key, 'count' => $count); - } - } - - return $out; - } - - /** - * Get memory statistics - * - * - **/ - public function get_memory_stats() - { - $out = array(); - $sql = "SELECT physical_memory, count(1) as count - FROM machine - LEFT JOIN reportdata USING (serial_number) - ".get_machine_group_filter()." - GROUP BY physical_memory - ORDER BY physical_memory DESC"; - - foreach ($this->query($sql) as $obj) { - $obj->physical_memory = intval($obj->physical_memory); - $obj->count = intval($obj->count); - $out[] = $obj; - } - - return $out; - } - - // ------------------------------------------------------------------------ - - /** - * Process data sent by postflight - * - * @param string data - * @author abn290 - **/ - public function process($plist) - { - $parser = new CFPropertyList(); - $parser->parse($plist, CFPropertyList::FORMAT_XML); - $mylist = $parser->toArray(); - - // Remove serial_number from mylist, use the cleaned serial that was provided in the constructor. - unset($mylist['serial_number']); - - // Set default computer_name - if (! isset($mylist['computer_name']) or trim($mylist['computer_name']) == '') { - $mylist['computer_name'] = 'No name'; - } - - // Convert memory string (4 GB) to int - if (isset($mylist['physical_memory'])) { - $mylist['physical_memory'] = intval($mylist['physical_memory']); - } - - // Convert OS version to int - if (isset($mylist['os_version'])) { - $digits = explode('.', $mylist['os_version']); - $mult = 10000; - $mylist['os_version'] = 0; - foreach ($digits as $digit) { - $mylist['os_version'] += $digit * $mult; - $mult = $mult / 100; - } - } - - // Dirify buildversion - if (isset($mylist['buildversion'])) { - $mylist['buildversion'] = preg_replace('/[^A-Za-z0-9]/', '', $mylist['buildversion']); - } - - $this->timestamp = time(); - $this->merge($mylist)->save(); - } + protected $table = 'machine'; + + protected $fillable = [ + 'serial_number', + 'hostname', + 'machine_model', + 'machine_desc', + 'img_url', + 'cpu', + 'current_processor_speed', + 'cpu_arch', + 'os_version', + 'physical_memory', + 'platform_UUID', + 'number_processors', + 'SMC_version_system', + 'boot_rom_version', + 'bus_speed', + 'computer_name', + 'l2_cache', + 'machine_name', + 'packages', + 'buildversion', + ]; + + public $timestamps = false; } diff --git a/machine_processor.disabled.php b/machine_processor.php similarity index 71% rename from machine_processor.disabled.php rename to machine_processor.php index 1c13b54..be0852a 100644 --- a/machine_processor.disabled.php +++ b/machine_processor.php @@ -1,30 +1,23 @@ serial_number = $serial_number; - } - /** * Process data sent by postflight * * @param string data * @author abn290 **/ - public function process($plist) + public function run($plist) { $parser = new CFPropertyList(); $parser->parse($plist, CFPropertyList::FORMAT_XML); $mylist = $parser->toArray(); - // Remove serial_number from mylist, use the cleaned serial that was provided in the constructor. - unset($mylist['serial_number']); + $mylist['serial_number'] = $this->serial_number; // Set default computer_name if (! isset($mylist['computer_name']) or trim($mylist['computer_name']) == '') { @@ -52,10 +45,7 @@ public function process($plist) $mylist['buildversion'] = preg_replace('/[^A-Za-z0-9]/', '', $mylist['buildversion']); } - $model = Machine_model::updateOrCreate( - ['serial_number' => $this->serial_number], - $mylist - ); + $model = Machine_model::firstOrNew(['serial_number' => $this->serial_number]); + $model->fill($mylist)->save(); } - } \ No newline at end of file