}
return $obj;
}
+
+ /**
+ * Get number object per object category.
+ *
+ * @param string $objecttype object type (usually plugin short name such as 'm365' or 'mysql')
+ * @param string $objectsource object source
+ * @param string $datestart start date
+ * @param string $dateend end date
+ * @return array summary in form [objectcategory => '', objecttype => '', objectsource => '', count => 0, last_job_time => '']
+ */
+ public function getObjectPerCategorySum($objecttype = null, $objectsource = null, $datestart = null, $dateend = null) {
+ $otype = '';
+ if (!is_null($objecttype)) {
+ $otype = ' AND oobj.ObjectType=:objecttype ';
+
+ }
+ $osource = '';
+ if (!is_null($objectsource)) {
+ $osource = ' AND oobj.ObjectSource=:objectsource ';
+ }
+ $dformat = 'Y-m-d H:i:s';
+ if (is_null($datestart)) {
+ $m_ago = new \DateTime('1 month ago');
+ $datestart = $m_ago->format($dformat);
+ }
+ if (is_null($dateend)) {
+ $dateend = date($dformat);
+ }
+
+ $sql = 'SELECT oobj.ObjectCategory AS objectcategory,
+ oobj.ObjectType AS objecttype,
+ oobj.ObjectSource AS objectsource,
+ COUNT(DISTINCT oobj.ObjectUUID) AS count,
+ MAX(Job.StartTime) AS last_job_time
+ FROM Object AS oobj
+ JOIN Job USING(JobId)
+ WHERE
+ Job.StartTime BETWEEN :datestart AND :dateend
+ AND Job.JobStatus IN (\'T\', \'W\')
+ AND oobj.JobId=(
+ SELECT MAX(iobj.JobId) FROM Object AS iobj WHERE iobj.ObjectId=oobj.ObjectId
+ )
+ ' . $otype . $osource . '
+ GROUP BY oobj.ObjectCategory, oobj.ObjectType, oobj.ObjectSource';
+
+ $connection = JobRecord::finder()->getDbConnection();
+ $connection->setActive(true);
+ $pdo = $connection->getPdoInstance();
+ $sth = $pdo->prepare($sql);
+ if (!is_null($objecttype)) {
+ $sth->bindParam(':objecttype', $objecttype, \PDO::PARAM_STR, 100);
+ }
+ if (!is_null($objectsource)) {
+ $sth->bindParam(':objectsource', $objectsource, \PDO::PARAM_STR, 400);
+ }
+ $sth->bindParam(':datestart', $datestart, \PDO::PARAM_STR, 19);
+ $sth->bindParam(':dateend', $dateend, \PDO::PARAM_STR, 19);
+ $sth->execute();
+ return $sth->fetchAll(\PDO::FETCH_ASSOC);
+ }
}
--- /dev/null
+<?php
+/*
+ * Bacula(R) - The Network Backup Solution
+ * Baculum - Bacula web interface
+ *
+ * Copyright (C) 2013-2022 Kern Sibbald
+ *
+ * The main author of Baculum is Marcin Haba.
+ * The original author of Bacula is Kern Sibbald, with contributions
+ * from many others, a complete list can be found in the file AUTHORS.
+ *
+ * You may use this file and others of this release according to the
+ * license defined in the LICENSE file, which includes the Affero General
+ * Public License, v3.0 ("AGPLv3") and some additional permissions and
+ * terms pursuant to its AGPLv3 Section 7.
+ *
+ * This notice must be preserved when any source code is
+ * conveyed and/or propagated.
+ *
+ * Bacula(R) is a registered trademark of Kern Sibbald.
+ */
+
+use Baculum\Common\Modules\Errors\ObjectError;
+
+/**
+ * Objects endpoint.
+ *
+ * @author Marcin Haba <marcin.haba@bacula.pl>
+ * @category API
+ * @package Baculum API
+ */
+class ObjectStatsCategorySum extends BaculumAPIServer {
+
+ public function get() {
+ $misc = $this->getModule('misc');
+ $objecttype = null;
+ if ($this->Request->contains('objecttype') && $misc->isValidName($this->Request['objecttype'])) {
+ $objecttype = $this->Request['objecttype'];
+ }
+ $objectsource = null;
+ if ($this->Request->contains('objectsource') && $misc->isValidName($this->Request['objectsource'])) {
+ $objectsource = $this->Request['objectsource'];
+ }
+ $datestart = null;
+ if ($this->Request->contains('datestart') && $misc->isValidBDateAndTime($this->Request['datestart'])) {
+ $datestart = $this->Request['datestart'];
+ }
+ $dateend = null;
+ if ($this->Request->contains('dateend') && $misc->isValidBDateAndTime($this->Request['dateend'])) {
+ $dateend = $this->Request['dateend'];
+ }
+
+ $objects = $this->getModule('object')->getObjectPerCategorySum(
+ $objecttype,
+ $objectsource,
+ $datestart,
+ $dateend
+ );
+ $this->output = $objects;
+ $this->error = ObjectError::ERROR_NO_ERRORS;
+ }
+}
<!-- event endpoints -->
<url ServiceParameter="Objects" pattern="api/v2/objects/" />
<url ServiceParameter="ObjectClass" pattern="api/v2/objects/{id}/" parameters.id="\d+" />
+ <url ServiceParameter="ObjectStatsCategorySum" pattern="api/v2/objects/stats/category-sum/" />
<!-- @TODO: Separate this endpoint outside 'joblog' -->
<url ServiceParameter="Messages" pattern="api/v2/joblog/messages" />
<!-- fileset endpoints -->
"error": {
"type": "integer",
"description": "Error code",
- "enum": [0, 1, 2, 3, 4, 5, 6, 7, 11, 1000]
+ "enum": [0, 1, 2, 3, 6, 7, 1000]
}
}
}
]
}
},
+ "/api/v2/objects/{objectid}": {
+ "get": {
+ "tags": ["objects"],
+ "summary": "Object by objectid",
+ "description": "Get single object.",
+ "responses": {
+ "200": {
+ "description": "Single object by objectid",
+ "content": {
+ "application/json": {
+ "schema": {
+ "type": "object",
+ "properties": {
+ "output": {
+ "$ref": "#/components/schemas/Object"
+ },
+ "error": {
+ "type": "integer",
+ "description": "Error code",
+ "enum": [0, 1, 2, 3, 6, 7, 1000]
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ },
+ "/api/v2/objects/stats/category-sum/": {
+ "get": {
+ "tags": ["objects"],
+ "summary": "Get object stats per category",
+ "description": "Get object statistics per object category",
+ "responses": {
+ "200": {
+ "description": "Object statistics per category",
+ "content": {
+ "application/json": {
+ "schema": {
+ "type": "object",
+ "properties": {
+ "output": {
+ "type": "array",
+ "items": {
+ "type": "object",
+ "properties": {
+ "objectcategory": {
+ "type": "string",
+ "description": "Object category"
+ },
+ "objecttype": {
+ "type": "string",
+ "description": "Object type, ex. 'm365' or 'PostgreSQL'"
+ },
+ "objectsource": {
+ "type": "string",
+ "description": "Object data source"
+ },
+ "count": {
+ "type": "integer",
+ "description": "Object count"
+ },
+ "last_job_time": {
+ "type": "string",
+ "description": "Last job start time in ISO form, ex: 2022-08-11 12:31:16"
+ }
+ }
+ }
+ },
+ "error": {
+ "type": "integer",
+ "description": "Error code",
+ "enum": [0, 1, 2, 3, 6, 7, 1000]
+ }
+ }
+ }
+ }
+ }
+ }
+ },
+ "parameters": [
+ {
+ "name": "objecttype",
+ "in": "query",
+ "required": false,
+ "description": "Object type, ex. 'm365' or 'PostgreSQL'",
+ "schema": {
+ "type": "string"
+ }
+ },
+ {
+ "name": "objectsource",
+ "in": "query",
+ "required": false,
+ "description": "Object data source",
+ "schema": {
+ "type": "string"
+ }
+ },
+ {
+ "name": "datestart",
+ "in": "query",
+ "required": false,
+ "description": "Start date for job time range. If not given, default is used 1 month ago date.",
+ "schema": {
+ "type": "string"
+ }
+ },
+ {
+ "name": "dateend",
+ "in": "query",
+ "required": false,
+ "description": "End date for job time range. If not given, default is used current date.",
+ "schema": {
+ "type": "string"
+ }
+ }
+ ]
+ }
+ },
"/api/v2/plugins/m365/{clientid}/users": {
"get": {
"tags": ["plugins"],