我有三个表,分别命名为hostgroup
、host
和services
。
例如,我有一个这样的数据库。
Hostgroup:
id, name
1 GroupA
2 GroupB
Host:
id, name, hostgroup_id
1 HostA 1
2 HostB 1
3 HostC 2
4 HostD 2
5 HostE 2
//2 = critical, 1 = warning
Service:
id, host_id, state
1 1 2
2 1 2
3 2 1
4 2 1
5 3 2
最后的表格应该是这样的:
Name Total Hosts Total Critical Total Warning
GroupA 2 2 2
GroupB 3 1 0
我可以通过在我的Hostgroup
模型上添加以下功能来完成第二列,即Total Hosts
:
public function getHostcount()
{
return Host::find()->where(['hostgroup_id' => $this->id])->count();
}
我如何计算总警告和严重警告?
- 总关键状态(统计与主机组相关的主机的总关键状态数)
- 警告总数(统计与主机组相关的主机的警告状态总数)
编辑:添加源代码
主机组搜索型号:
<?php
class HostgroupSearch extends Hostgroup
{
public function rules()
{
return [
[['id', 'check_interval', 'retry_interval', 'max_check_attempts', 'timeout', 'freshness_threshold', 'is_auto_recovery', 'reflect_flg', 'created_user_id'], 'integer'],
[['object_name', 'name', 'name_search', 'remarks', 'snmp_community', 'timestamp'], 'safe'],
];
}
public function search($params)
{
$query = Hostgroup::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
$query->andFilterWhere([
'id' => $this->id,
'check_interval' => $this->check_interval,
'retry_interval' => $this->retry_interval,
'max_check_attempts' => $this->max_check_attempts,
'timeout' => $this->timeout,
'freshness_threshold' => $this->freshness_threshold,
'is_auto_recovery' => $this->is_auto_recovery,
'reflect_flg' => $this->reflect_flg,
'created_user_id' => $this->created_user_id,
'timestamp' => $this->timestamp,
]);
$query->andFilterWhere(['like', 'object_name', $this->object_name])
->andFilterWhere(['like', 'name', $this->name])
->andFilterWhere(['like', 'name_search', $this->name_search])
->andFilterWhere(['like', 'remarks', $this->remarks])
->andFilterWhere(['like', 'snmp_community', $this->snmp_community]);
return $dataProvider;
}
}
此HostlistController控制表中所有内容的显示。
<?
class HostlistController extends Controller
{
public $layout = "default";
public function actionIndex()
{
$searchModel = new HostgroupSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
}
?>
这就是我的视图页面index.php:
'columns' => [
[
'label' => 'Name',
'attribute' => 'name',
'format' => 'raw',
'value' => function ($data, $id) {
return Html::a($data->name, '/index.php/hostlistbygroup/'. $id);
},
],
[
'label' => 'Total Host',
'attribute' => 'hostcount',
],
[
'label' => 'Total Critical',
'attribute' => 'host.criticalcount',
],
[
'label' => 'Total Warning',
'attribute' => 'host.warningcount',
],
永不终止。通过将这些函数添加到Host
模型中,解决了这一问题。
public function getCriticalcount()
{
return ServiceStatuses::find()->where(['host_id' => $this->id, 'last_hard_state' => '2'])->count();
}
public function getWarningcount()
{
return ServiceStatuses::find()->where(['host_id' => $this->id, 'last_hard_state' => '1'])->count();
}
并将其添加到Hostgroup
型号中
public function getHost()
{
return $this->hasOne(Host::className(), ['hostgroup_id' => 'id']);
}
用这个修改了index.php
'columns' => [
[
'label' => 'Name',
'attribute' => 'name',
'format' => 'raw',
'value' => function ($data, $id) {
return Html::a($data->name, '/index.php/hostlistbygroup/'. $id);
},
],
[
'label' => 'Total Host',
'attribute' => 'hostcount',
],
[
'label' => 'Total Critical',
'attribute' => 'host.criticalcount',
],
[
'label' => 'Total Warning',
'attribute' => 'host.warningcount',
],