-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathappusage_model.php
110 lines (94 loc) · 3.5 KB
/
appusage_model.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
class Appusage_model extends \Model
{
public function __construct($serial = '')
{
parent::__construct('id', 'appusage'); //primary key, tablename
$this->rs['id'] = '';
$this->rs['serial_number'] = $serial;
$this->rs['event'] = "";
$this->rs['bundle_id'] = "";
$this->rs['app_version'] = "";
$this->rs['app_name'] = "";
$this->rs['app_path'] = "";
$this->rs['last_time_epoch'] = 0;
$this->rs['last_time'] = "";
$this->rs['number_times'] = 0;
// Add local config
configAppendFile(__DIR__ . '/config.php');
}
// ------------------------------------------------------------------------
/**
* Get applications names for widget
*
**/
public function get_applaunch()
{
$out = array();
$sql = "SELECT SUM(number_times) AS count, app_name, event
FROM appusage
LEFT JOIN reportdata USING (serial_number)
".get_machine_group_filter()."
AND event = 'launch'
AND app_name <> ''
GROUP BY app_name
ORDER BY count DESC";
foreach ($this->query($sql) as $obj) {
if ("$obj->count" !== "0") {
$obj->app_name = $obj->app_name ? $obj->app_name : 'Unknown';
$out[] = $obj;
}
}
return $out;
}
/**
* Process data sent by postflight
*
* @param string data
*
**/
public function process($data)
{
// Delete previous entries
$this->deleteWhere('serial_number=?', $this->serial_number);
// List of bundle IDs to ignore
$bundleid_ignorelist = is_array(conf('appusage_ignorelist')) ? conf('appusage_ignorelist') : array();
$regex = '/^'.implode('|', $bundleid_ignorelist).'$/';
// Check if we have data
if(is_null($data) || ! $data || $data == ""){
print_r("Error processing appusage module request: No data found");
} else {
// Split into lines
foreach(str_getcsv($data, "\n") as $line)
{
// Skip if empty line
if(!is_null($line) && trim($line) === ''){
continue;
}
// Split line
$appusage_line = str_getcsv($line);
if ( count($appusage_line) > 1)
{
// Check if we should skip this bundle ID
if (preg_match($regex, $appusage_line[1])) {
continue;
}
$this->event = $appusage_line[0];
$this->bundle_id = $appusage_line[1];
$this->app_version = $appusage_line[2];
$this->app_path = $appusage_line[3];
$app_array_explode = explode('/',$appusage_line[3]);
$app_array = array_pop($app_array_explode);
$app_name_full = substr($app_array, 0, -4);
$this->app_name = $app_name_full;
$this->number_times = $appusage_line[4];
$dt = new DateTime('@'.$appusage_line[5]);
$this->last_time = ($dt->format('Y-m-d H:i:s'));
$this->last_time_epoch = $appusage_line[5];
$this->id = '';
$this->create();
}
}
}
} // end process()
}