Skip to content

Commit e75dc39

Browse files
committed
record the times of embed and maintenance runs
This makes it easier to debug when something with the cronjob goes wrong. Currently the data is only exposed in the cli info command. We might want to use it somewhere in the UI to warn about outdated data
1 parent 6ea4c85 commit e75dc39

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

cli.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ protected function setup(Options $options)
5757

5858
$options->registerCommand('chat', 'Start an interactive chat session');
5959

60-
$options->registerCommand('info', 'Get Info about the vector storage');
60+
$options->registerCommand('info', 'Get Info about the vector storage and other stats');
6161

6262
$options->registerCommand('split', 'Split a page into chunks (for debugging)');
6363
$options->registerArgument('page', 'The page to split', true, 'split');
@@ -120,7 +120,11 @@ protected function showinfo()
120120
$stats = [
121121
'model' => $this->getConf('model'),
122122
];
123-
$stats = array_merge($stats, $this->helper->getStorage()->statistics());
123+
$stats = array_merge(
124+
$stats,
125+
array_map('dformat', $this->helper->getRunData()),
126+
$this->helper->getStorage()->statistics()
127+
);
124128
$this->printTable($stats);
125129
}
126130

@@ -137,14 +141,14 @@ protected function printTable($data, $level = 0)
137141
foreach ($data as $key => $value) {
138142
if (is_array($value)) {
139143
echo $tf->format(
140-
[$level * 2, 15, '*'],
144+
[$level * 2, 20, '*'],
141145
['', $key, ''],
142146
[Colors::C_LIGHTBLUE, Colors::C_LIGHTBLUE, Colors::C_LIGHTBLUE]
143147
);
144148
$this->printTable($value, $level + 1);
145149
} else {
146150
echo $tf->format(
147-
[$level * 2, 15, '*'],
151+
[$level * 2, 20, '*'],
148152
['', $key, $value],
149153
[Colors::C_LIGHTBLUE, Colors::C_LIGHTBLUE, Colors::C_LIGHTGRAY]
150154
);
@@ -259,6 +263,10 @@ protected function runMaintenance()
259263
$this->helper->getStorage()->runMaintenance();
260264
$this->notice('Peak memory used: {memory}', ['memory' => filesize_h(memory_get_peak_usage(true))]);
261265
$this->notice('Spent time: {time}min', ['time' => round((time() - $start) / 60, 2)]);
266+
267+
$data = $this->helper->getRunData();
268+
$data['maintenance ran at'] = time();
269+
$this->helper->setRunData($data);
262270
}
263271

264272
/**
@@ -274,6 +282,10 @@ protected function createEmbeddings($clear)
274282
$this->helper->getEmbeddings()->createNewIndex($skipRE, $matchRE, $clear);
275283
$this->notice('Peak memory used: {memory}', ['memory' => filesize_h(memory_get_peak_usage(true))]);
276284
$this->notice('Spent time: {time}min', ['time' => round((time() - $start) / 60, 2)]);
285+
286+
$data = $this->helper->getRunData();
287+
$data['embed ran at'] = time();
288+
$this->helper->setRunData($data);
277289
}
278290

279291
/**

helper.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,17 @@ class helper_plugin_aichat extends Plugin
3030
/** @var AbstractStorage */
3131
protected $storage;
3232

33+
/** @var array where to store meta data on the last run */
34+
protected $runDataFile;
35+
3336
/**
3437
* Constructor. Initializes vendor autoloader
3538
*/
3639
public function __construct()
3740
{
38-
require_once __DIR__ . '/vendor/autoload.php';
41+
require_once __DIR__ . '/vendor/autoload.php'; // FIXME obsolete from Kaos onwards
42+
global $conf;
43+
$this->runDataFile = $conf['metadir'] . '/aichat__run.json';
3944
}
4045

4146
/**
@@ -295,4 +300,28 @@ public function getLanguageLimit()
295300
return '';
296301
}
297302
}
303+
304+
/**
305+
* Store info about the last run
306+
*
307+
* @param array $data
308+
* @return void
309+
*/
310+
public function setRunData(array $data)
311+
{
312+
file_put_contents($this->runDataFile, json_encode($data, JSON_PRETTY_PRINT));
313+
}
314+
315+
/**
316+
* Get info about the last run
317+
*
318+
* @return array
319+
*/
320+
public function getRunData()
321+
{
322+
if (!file_exists($this->runDataFile)) {
323+
return [];
324+
}
325+
return json_decode(file_get_contents($this->runDataFile), true);
326+
}
298327
}

0 commit comments

Comments
 (0)