Passed
Push — master ( 42cc8f...094e4b )
by Marcel
05:15
created

ContextChatManager   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 66
rs 10
c 0
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A isActiveForDataset() 0 3 1
A submitContent() 0 23 1
1
<?php
2
/**
3
 * Analytics
4
 *
5
 * SPDX-FileCopyrightText: 2024 Marcel Scherello
6
 * SPDX-License-Identifier: AGPL-3.0-or-later
7
 */
8
9
namespace OCA\Analytics\ContextChat;
10
11
use OCP\DB\Exception;
0 ignored issues
show
Bug introduced by
The type OCP\DB\Exception was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use OCP\IL10N;
0 ignored issues
show
Bug introduced by
The type OCP\IL10N was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Psr\Log\LoggerInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Log\LoggerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use OCA\ContextChat\Public\ContentItem;
0 ignored issues
show
Bug introduced by
The type OCA\ContextChat\Public\ContentItem was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use OCA\ContextChat\Public\ContentManager;
0 ignored issues
show
Bug introduced by
The type OCA\ContextChat\Public\ContentManager was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use OCA\Analytics\Service\StorageService;
17
use OCA\Analytics\Service\DatasetService;
18
19
class ContextChatManager {
20
	private $userId;
21
	private $logger;
22
	private $l10n;
23
	private $StorageService;
24
	private $DatasetService;
25
	private $ContentManager;
26
27
	public function __construct(
28
		$userId,
29
		IL10N $l10n,
30
		LoggerInterface $logger,
31
		StorageService $StorageService,
32
		DatasetService $DatasetService,
33
		ContentManager $ContentManager
34
	) {
35
		$this->userId = $userId;
36
		$this->l10n = $l10n;
37
		$this->logger = $logger;
38
		$this->StorageService = $StorageService;
39
		$this->DatasetService = $DatasetService;
40
		$this->ContentManager = $ContentManager;
41
	}
42
43
	/**
44
	 * Providers can use this to submit content for indexing in context chat
45
	 *
46
	 * @param int $datasetId
47
	 * @return void
48
	 * @throws Exception
49
	 */
50
	public function submitContent(int $datasetId): void {
51
		$storageData = $this->StorageService->read($datasetId, null);
52
		$datasetMetadata = $this->DatasetService->read($datasetId);
53
54
		$columns = $datasetMetadata['dimension1'] .', '.$datasetMetadata['dimension2'].', '.$datasetMetadata['value'];
55
56
		$data = array_map(function ($subArray) {
57
			return implode(",", $subArray);
58
		}, $storageData['data']);
59
		$dataString = implode("\n", $data);
60
61
		$content = 'This is a report of statistical data. ' . $datasetMetadata['subheader'] . '. ';
62
		$content .= 'The data comes in multiple rows which 3 columns separated by a comma. ';
63
		$content .= 'The columns are ' . $columns . '. ';
64
		$content .= 'The data is: ' . $dataString;
65
66
		$contentItem = new ContentItem($datasetId, 'report', $datasetMetadata['name'], $content, 'Report', new \DateTime(), ['admin']);
67
68
		$contentItems = [$contentItem];
69
		//$this->ContentManager->removeContentForUsers('analytics', 'report', $dataset, ['admin']);
70
		$this->logger->debug('adding item: ' . json_encode($contentItem));
71
		$this->ContentManager->submitContent('analytics', $contentItems);
72
		return;
73
	}
74
75
	/**
76
	 * return true if the data set has indexing activated
77
	 *
78
	 * @param int $datasetId
79
	 * @return bool
80
	 * @throws Exception
81
	 */
82
	public function isActiveForDataset(int $datasetId) {
83
		$datasetMetadata = $this->DatasetService->read($datasetId);
84
		return $datasetMetadata['ai_index'] === 1;
85
	}
86
}