Passed
Push — master ( bdffc0...7ee269 )
by Marcel
04:00
created

ContextChatManager::removeContentByDataset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
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 bool
48
	 * @throws Exception
49
	 */
50
	public function submitContent(int $datasetId): bool {
51
		$datasetMetadata = $this->DatasetService->read($datasetId);
52
		if ($datasetMetadata['ai_index'] !== 1) return false;
53
54
		$columns = $datasetMetadata['dimension1'] .', '.$datasetMetadata['dimension2'].', '.$datasetMetadata['value'];
55
56
		$storageData = $this->StorageService->read($datasetId, null);
57
		$data = array_map(function ($subArray) {
58
			return implode(",", $subArray);
59
		}, $storageData['data']);
60
		$dataString = implode("\n", $data);
61
62
		$content = 'This is a set of statistical data. The name of the report is: ' . $datasetMetadata['name'] . '. ';
63
		$content .= 'The description of the data is: ' . $datasetMetadata['subheader'] . '. ';
64
		$content .= 'The data comes in multiple rows which 3 columns separated by a comma. ';
65
		$content .= 'The columns are ' . $columns . '. ';
66
		$content .= 'The data is: ' . $dataString;
67
68
		$contentItem = new ContentItem(
69
			$datasetId,
70
			'report',
71
			$datasetMetadata['name'],
72
			$content,
73
			'Report',
74
			new \DateTime(),
75
			[$datasetMetadata['user_id']]
76
		);
77
78
		$contentItems = [$contentItem];
79
		$this->logger->info('Adding Analytics to context chat: ' . json_encode($contentItem));
80
		$this->ContentManager->submitContent('analytics', $contentItems);
81
		return true;
82
	}
83
84
	/**
85
	 * Remove a dataset from context chat
86
	 *
87
	 * @param int $datasetId
88
	 * @return bool
89
	 * @throws Exception
90
	 */
91
	public function removeContentByDataset(int $datasetId): bool {
92
		$datasetMetadata = $this->DatasetService->read($datasetId);
93
		$this->logger->info('Removing Analytics dataset from context chat: ' . $datasetId);
94
		$this->ContentManager->removeContentForUsers('analytics', 'report', $datasetId, [$datasetMetadata['user_id']]);
95
		return true;
96
	}
97
98
	/**
99
	 * Remove a dataset from context chat
100
	 *
101
	 * @param string $user
102
	 * @return bool
103
	 */
104
	public function removeContentByUser(string $user): bool {
105
		$this->logger->info('Removing Analytics from context chat for user: .' . $user);
106
		$this->ContentManager->removeAllContentForUsers('analytics', 'report', [$user]);
107
		return true;
108
	}
109
110
	/**
111
	 * return true if the data set has indexing activated
112
	 *
113
	 * @param int $datasetId
114
	 * @return bool
115
	 * @throws Exception
116
	 */
117
	public function isActiveForDataset(int $datasetId) {
118
		$datasetMetadata = $this->DatasetService->read($datasetId);
119
		return $datasetMetadata['ai_index'] === 1;
120
	}
121
}