MCPcopy Create free account
hub / github.com/Rello/analytics / read

Method read

lib/Controller/DatasourceController.php:147–224  ·  view source on GitHub ↗

* Get the data from a data source; * * @param int $datasourceId * @param $datasetMetadata * @param bool $allowCacheValidation * @return array|NotFoundException */

(int $datasourceId, $datasetMetadata, bool $allowCacheValidation = true)

Source from the content-addressed store, hash-verified

145 * @return array|NotFoundException
146 */
147 #[NoAdminRequired]
148 public function read(int $datasourceId, $datasetMetadata, bool $allowCacheValidation = true) {
149 if (!$this->getDatasources()[$datasourceId]) {
150 $result['error'] = $this->l10n->t('Data source not available anymore');
151 return $result;
152 }
153
154 $option = array();
155 // before 3.1.0, the options were in another format. as of 3.1.0 the standard option array is used
156 if ($datasetMetadata['link'][0] !== '{') {
157 $option['link'] = $datasetMetadata['link'];
158 } else {
159 $option = json_decode($datasetMetadata['link'], true);
160 }
161 unset($option['cacheKey']);
162 $option['user_id'] = $datasetMetadata['user_id'];
163 if ($allowCacheValidation && isset($datasetMetadata['cacheKey']) && $datasetMetadata['cacheKey'] !== '') {
164 $option['cacheKey'] = $datasetMetadata['cacheKey'];
165 }
166
167 try {
168 // read the data from the source
169 $result = $this->getDatasources()[$datasourceId]->readData($option);
170 if (isset($result['cache']['notModified']) && $result['cache']['notModified'] === true) {
171 return $result;
172 }
173
174 // if data source should be timestamped/snapshoted
175 if (isset($option['timestamp']) && ($option['timestamp'] === 'true' || $option['timestamp'] === 'filename')) {
176 $timestamp = $this->getDatasourceTimestamp($option);
177 if ($timestamp === null) {
178 return [
179 'error' => $this->l10n->t('No valid timestamp found in datasource filename'),
180 'data' => [],
181 'status' => 'nodata',
182 ];
183 }
184
185 $result['data'] = array_map(function ($tag) use ($timestamp) {
186 $columns = count($tag);
187 if ($columns > 1) {
188 // shift values by one dimension and stores date in second column
189 return array($tag[$columns - 2], $timestamp, $tag[$columns - 1]);
190 } else {
191 // just return 2 columns if the original data only has one column
192 return array($timestamp, $tag[$columns - 1]);
193 }
194 }, $result['data']);
195 }
196
197 $originalFilterOptions = $datasetMetadata['filteroptions'] ?? null;
198 $datasetMetadata['filteroptions'] = $this->normalizeFilterOptionsForData(
199 $originalFilterOptions,
200 $result['header'] ?? []
201 );
202 if (is_string($originalFilterOptions)
203 && trim($originalFilterOptions) !== ''
204 && $datasetMetadata['filteroptions'] !== $originalFilterOptions) {

Callers

nothing calls this directly

Calls 7

getDatasourcesMethod · 0.95
filterDataMethod · 0.95
aggregateDataMethod · 0.95
tMethod · 0.45
readDataMethod · 0.45

Tested by

no test coverage detected