summaryrefslogtreecommitdiffstats
path: root/application/models/EventreportMapper.php
blob: 89c112459bae6be3df43a7bfef3d9e65139f165e (plain) (blame)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
/*
 * Copyright (c) 2011 - OpenSLX GmbH, RZ Uni Freiburg
 * This program is free software distributed under the GPL version 2.
 * See http://gpl.openslx.org/
 *
 * If you have any feedback please consult http://feedback.openslx.org/ and
 * send your suggestions, praise, or complaints to feedback@openslx.org
 *
 * General information about OpenSLX can be found at http://openslx.org/
 */

class Application_Model_EventreportMapper
{
	protected $_dbTable;

	public function findBy($where, $array=false, $order=false)
	{
		foreach($where as $k => $v){
			if($v != null)
			$where2[] = "$k = '$v'";
			else
			$where2[] = "$k IS NULL";
		}
		$where = implode(" AND " ,$where2);

		try{
			$db = Zend_Db_Table::getDefaultAdapter();
			$select = $this->getDbTable()->select()
			->from($this->_dbTable)
			->where($where);

			if(is_array($order)){
				foreach ($order as $k => $v)
				$a[] = "$k $v";
				$select->order($a);
			}

			$stmt = $select->query();
			$result = $stmt->fetchAll();

			if(!$array){
				$entries = array();
				if(count($result) > 0) {
					foreach ($result as $row) {
						$entry = new Application_Model_Eventreport($row);
						$entry->setID($row['reportID']);
						$entries[] = $entry;
					}
				}
				return $entries;
			}else{
				return $result;
			}

		}catch (Zend_Exception $e) {
			echo "Error message 2: " . $e->getMessage() . "\n";
		}
	}

	public function setDbTable($dbTable)
	{
		if (is_string($dbTable)) {
			$dbTable = new $dbTable();
		}

		if (!$dbTable instanceof Zend_Db_Table_Abstract) {
			throw new Exception('Invalid table data gateway provided');
		}

		$this->_dbTable = $dbTable;

		return $this;
	}

	public function getDbTable()
	{
		if (null === $this->_dbTable) {
			$this->setDbTable('Application_Model_DbTable_Eventreport');
		}

		return $this->_dbTable;
	}

	public function save(Application_Model_Eventreport $eventreport)
	{
		$data = array('reportID'=> $eventreport->getID() ,'eventID'=> $eventreport->getEventID() ,'result'=> $eventreport->getResult(), 'errors' => $eventreport->getErrors(), 'type' => $eventreport->getType(), 'created' => $eventreport->getCreated() );
		if (null === ($id = $eventreport->getID()) ) {
			unset($data['reportID']);
			return $this->getDbTable()->insert($data);
		} else {
			$this->getDbTable()->update($data, array('reportID = ?' => $id));
		}
	}

	public function delete(Application_Model_Eventreport $eventreport)
	{
		if (null === ($id = $eventreport->getID()) ) {
			return;
		} else {
			$this->getDbTable()->delete(array('reportID = ?' => $id));
		}
	}

	public function find($id, Application_Model_Eventreport $eventreport = null)
	{
		$return = false;
		$result = $this->getDbTable()->find($id);
		if (0 == count($result)) {
			return;
		}

		$row = $result->current();

		if($eventreport == null){
			$return = true;

		}

		if($return) {
			$eventreport = new Application_Model_Eventreport();
			$eventreport
			->setID($row->reportID)
			->setEventID($row->eventID)
			->setResult($row->result)
			->setErrors($row->errors)
			->setCreated($row->created)
			->setType($row->type);
			return $eventreport;
		} else {
			$eventreport
			->setID($row->reportID)
			->setEventID($row->eventID)
			->setResult($row->result)
			->setErrors($row->errors)
			->setCreated($row->created)
			->setType($row->type);
		}
	}

	public function fetchAll()
	{
		$resultSet = $this->getDbTable()->fetchAll();
		$entries   = array();
		foreach ($resultSet as $row) {
			$entry = new Application_Model_Eventreport();

			$entry
			->setID($row->reportID)
			->setEventID($row->eventID)
			->setResult($row->result)
			->setErrors($row->errors)
			->setCreated($row->created)
			->setType($row->type);

			$entries[$row->reportID] = $entry;
		}
		return $entries;
	}

	public function compare(Application_Model_Eventreport $v1,Application_Model_Eventreport $v2){
		$vv1 = $v1->toArray();
		$vv2 = $v2->toArray();
		return array_diff_assoc($vv1,$vv2);
	}

	public function getSuccessBarCount($poolID) {

		$db = Zend_Db_Table::getDefaultAdapter();
		if($poolID != 'all'){
			$select = $this->getDbTable()->select()
			->setIntegrityCheck(false)
			->from(array('pcr' => 'poolctrl_eventreport'),
			array('count1' => 'SUM(IF(pcr.result="successful",1,0))', 'count0' => 'SUM(IF(pcr.result="failed",1,0))')
			)
			->join(array('pce' => 'poolctrl_event'),
    'pce.eventID = pcr.eventID',
			array())
			->where('pce.pbs_poolID = ?', $poolID);}
			else {
				$select = $this->getDbTable()->select()
				->setIntegrityCheck(false)
				->from(array('pcr' => 'poolctrl_eventreport'),
				array('count1' => 'SUM(IF(pcr.result="successful",1,0))', 'count0' => 'SUM(IF(pcr.result="failed",1,0))')
				)
				->join(array('pce' => 'poolctrl_event'),
    'pce.eventID = pcr.eventID',
				array());
			}

			$stmt = $db->query($select);
			$result = $stmt->fetchAll(PDO::FETCH_NUM);

			foreach ($result as $r) {
				$ret1 = array((int)$r[0],'Successful');
				$ret2 = array((int)$r[1],'Failed');
				$return[] = array($ret1,$ret2);
			}

			return $return;
	}

	public function getSuccessPieCount($poolID) {

		$db = Zend_Db_Table::getDefaultAdapter();
		if($poolID != 'all'){
			$select = $this->getDbTable()->select()
			->setIntegrityCheck(false)
			->from(array('pcr' => 'poolctrl_eventreport'),
			array('count1' => 'SUM(IF(pcr.result="successful",1,0))', 'count0' => 'SUM(IF(pcr.result="failed",1,0))')
			)
			->join(array('pce' => 'poolctrl_event'),
    'pce.eventID = pcr.eventID',
			array())
			->where('pce.pbs_poolID = ?', $poolID);}
			else {
				$select = $this->getDbTable()->select()
				->setIntegrityCheck(false)
				->from(array('pcr' => 'poolctrl_eventreport'),
				array('count1' => 'SUM(IF(pcr.result="successful",1,0))', 'count0' => 'SUM(IF(pcr.result="failed",1,0))')
				)
				->join(array('pce' => 'poolctrl_event'),
    'pce.eventID = pcr.eventID',
				array());
			}

			$stmt = $db->query($select);
			$result = $stmt->fetchAll(PDO::FETCH_NUM);

			foreach ($result as $r) {
				$ret1 = array('Successful', (int)$r[0]);
				$ret2 = array('Failed', (int)$r[1]);
				$return[] = array($ret1,$ret2);
			}

			return $return;
	}

	public function getSuccessTypePieCount($poolID) {

		$db = Zend_Db_Table::getDefaultAdapter();
		if($poolID != 'all'){
			$select = $this->getDbTable()->select()
			->setIntegrityCheck(false)
			->from(array('pcr' => 'poolctrl_eventreport'),
			array('errortype' => 'pcr.errors', 'count' => 'COUNT(*)'))
			->join(array('pce' => 'poolctrl_event'),
    'pce.eventID = pcr.eventID',
			array())
			->where('pce.pbs_poolID = ?', $poolID)
			->group('pcr.errors')
			->order('pce.category ASC');}
			else {
				$select = $this->getDbTable()->select()
				->setIntegrityCheck(false)
				->from(array('pcr' => 'poolctrl_eventreport'),
				array('errortype' => 'pcr.errors', 'count' => 'COUNT(*)'))
				->join(array('pce' => 'poolctrl_event'),
    'pce.eventID = pcr.eventID',
				array())
				->group('pcr.errors')
				->order('pce.category ASC');
			}

			$stmt = $db->query($select);
			$result = $stmt->fetchAll(PDO::FETCH_NUM);
			$ret = null;

			foreach ($result as $r) {
				$ret[] = array($r[0], (int)$r[1]);
			}

			return array($ret);
	}

}