Skip to content

Commit

Permalink
Cleaned up client tab
Browse files Browse the repository at this point in the history
  • Loading branch information
tuxudo committed Aug 17, 2022
1 parent 66de805 commit 8037708
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 406 deletions.
10 changes: 0 additions & 10 deletions provides.php

This file was deleted.

8 changes: 8 additions & 0 deletions provides.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
client_tabs:
usage_stats-tab:
view: usage_stats_tab
i18n: usage_stats.usage_stats
listings:
usage_stats:
view: usage_stats_listing
i18n: usage_stats.usage_stats
46 changes: 18 additions & 28 deletions scripts/usage_stats
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,23 @@

import os, sys, plistlib, subprocess, platform


def getOsVersion():
"""Returns the minor OS version."""
os_version_tuple = platform.mac_ver()[0].split('.')
return int(os_version_tuple[1])

def get_usage_metrics():
cmd = ['/usr/bin/powermetrics', '--show-initial-usage', ' -s',
'network,disk', '-f', 'plist', '-n', '0']
cmd = ['/usr/bin/powermetrics', '--show-initial-usage', ' -s', 'network,disk', '-f', 'plist', '-n', '0']
proc = subprocess.Popen(cmd, shell=False, bufsize=-1,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, err = proc.communicate()
plist = plistlib.readPlistFromString(output.rstrip(' \t\r\n\0'))

cmd = ['/usr/bin/powermetrics', '-s',
'cpu_power,gpu_power,thermal,battery', '-n', '1', '-f', 'plist']
cmd = ['/usr/bin/powermetrics', '-s', 'cpu_power,gpu_power,thermal,battery', '-n', '1', '-f', 'plist']
proc = subprocess.Popen(cmd, shell=False, bufsize=-1,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, err = proc.communicate()
plist.update(plistlib.readPlistFromString(output.rstrip(' \t\r\n\0')))

return plist

def parse_usage_plist(plist):
usage_info = {}
for item in plist:
Expand Down Expand Up @@ -167,41 +159,39 @@ def parse_usage_plist(plist):
try:
usage_info['kern_bootargs'] = plist[item]
except:
usage_info['error'] = ''
usage_info['error'] = ''
return usage_info

def getDarwinVersion():
"""Returns the Darwin version."""
# Catalina -> 10.15.7 -> 19.6.0 -> 19
# os_version_tuple = platform.mac_ver()[0].split('.')
# return int(os_version_tuple[1])
darwin_version_tuple = platform.release().split('.')
return int(darwin_version_tuple[0])

def main():
"""Main"""
# Create cache dir if it does not exist
cachedir = '%s/cache' % os.path.dirname(os.path.realpath(__file__))
if not os.path.exists(cachedir):
os.makedirs(cachedir)

# Skip manual check
if len(sys.argv) > 1:
if sys.argv[1] == 'manualcheck':
print 'Manual check: skipping'
exit(0)

# If less than 10.10, skip and write empty file
if getOsVersion() < 10:

# If less than macOS 10.10 (Darwin) 14, skip and write empty file
if getDarwinVersion() < 14:
result = dict()

# Write results to cache
cachedir = '%s/cache' % os.path.dirname(os.path.realpath(__file__))
output_plist = os.path.join(cachedir, 'usage_stats.plist')
plistlib.writePlist(result, output_plist)

else :

# Get results
result = dict()
info = get_usage_metrics()
result = parse_usage_plist(info)

# Write results to cache
cachedir = '%s/cache' % os.path.dirname(os.path.realpath(__file__))
output_plist = os.path.join(cachedir, 'usage_stats.plist')
plistlib.writePlist(result, output_plist)


if __name__ == "__main__":
main()
5 changes: 0 additions & 5 deletions usage_stats_controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ function index()
public function get_data($serial_number = '')
{
$obj = new View();

if (! $this->authorized()) {
$obj->view('json', array('msg' => 'Not authorized'));
}

$usage = new Usage_stats_model($serial_number);
$obj->view('json', array('msg' => $usage->rs));
}
Expand Down
44 changes: 2 additions & 42 deletions usage_stats_model.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Usage_stats_model extends \Model {

function __construct($serial='')
{
parent::__construct('id', 'usage_stats'); //primary key, tablename
parent::__construct('id', 'usage_stats'); // Primary key, tablename
$this->rs['id'] = '';
$this->rs['serial_number'] = $serial; $this->rt['serial_number'] = 'VARCHAR(255) UNIQUE';
$this->rs['timestamp'] = 0; $this->rt['timestamp'] = 'BIGINT';
Expand Down Expand Up @@ -42,46 +42,6 @@ function __construct($serial='')
$this->rs['gpu_busy'] = 0.0;
$this->rs['kern_bootargs'] = "";

// Schema version, increment when creating a db migration
$this->schema_version = 0;

// Add indexes
$this->idx[] = array('timestamp');
$this->idx[] = array('backlight_max');
$this->idx[] = array('backlight_min');
$this->idx[] = array('backlight');
$this->idx[] = array('keyboard_backlight');
$this->idx[] = array('ibyte_rate');
$this->idx[] = array('ibytes');
$this->idx[] = array('ipacket_rate');
$this->idx[] = array('ipackets');
$this->idx[] = array('obyte_rate');
$this->idx[] = array('obytes');
$this->idx[] = array('opacket_rate');
$this->idx[] = array('opackets');
$this->idx[] = array('rbytes_per_s');
$this->idx[] = array('rops_per_s');
$this->idx[] = array('wbytes_per_s');
$this->idx[] = array('wops_per_s');
$this->idx[] = array('rbytes_diff');
$this->idx[] = array('rops_diff');
$this->idx[] = array('wbytes_diff');
$this->idx[] = array('wops_diff');
$this->idx[] = array('thermal_pressure');
$this->idx[] = array('package_watts');
$this->idx[] = array('package_joules');
$this->idx[] = array('freq_hz');
$this->idx[] = array('freq_ratio');
$this->idx[] = array('gpu_name');
$this->idx[] = array('gpu_freq_hz');
$this->idx[] = array('gpu_freq_mhz');
$this->idx[] = array('gpu_freq_ratio');
$this->idx[] = array('gpu_busy');
$this->idx[] = array('kern_bootargs');

// Create table if it does not exist
//$this->create_table();

if ($serial) {
$this->retrieve_record($serial);
}
Expand All @@ -100,7 +60,7 @@ function __construct($serial='')
**/
function process($plist)
{

// Check if we have data
if ( ! $plist){
throw new Exception("Error Processing Request: No property list found", 1);
}
Expand Down
Loading

0 comments on commit 8037708

Please sign in to comment.