Skip to content

Commit

Permalink
chore: change the chat logs download file type to TSV to ease import …
Browse files Browse the repository at this point in the history
…since some fields contain commas

Refs: RW-1033
orakili committed Jul 25, 2024
1 parent e67f4cf commit 83bf0dc
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions modules/ocha_ai_chat/src/Form/OchaAiChatLogsForm.php
Original file line number Diff line number Diff line change
@@ -300,12 +300,12 @@ public function buildForm(array $form, FormStateInterface $form_state): array {
$form['download']['submit'] = [
'#type' => 'submit',
'#name' => 'download',
'#value' => $this->t('Download as CSV'),
'#value' => $this->t('Download as TSV'),
'#limit_validation_errors' => [
['download'],
],
'#ajax' => [
'callback' => [$this, 'downloadCsv'],
'callback' => [$this, 'downloadTsv'],
'wrapper' => 'download',
'disable-refocus' => TRUE,
],
@@ -410,7 +410,7 @@ protected function formatPassages(array $passages): array {
public function submitForm(array &$form, FormStateInterface $form_state): void {}

/**
* Remove elements from being submitted as GET variables.
* Download the logs as a TSV file.
*
* @param array $form
* From.
@@ -422,15 +422,15 @@ public function submitForm(array &$form, FormStateInterface $form_state): void {
*
* @todo see if can just send a response to download the file.
*/
public function downloadCsv(array &$form, FormStateInterface $form_state): AjaxResponse {
public function downloadTsv(array &$form, FormStateInterface $form_state): AjaxResponse {
$response = new AjaxResponse();
$selector = '#download';
$directory = 'private://ocha_ai_chat_logs/';

// Create a temporary managed file so it can be deleted on cron.
$file = $this->entityTypeManager->getStorage('file')->create();
$file->setFileName($file->uuid() . '.csv');
$file->setFileUri($directory . $file->uuid() . '.csv');
$file->setFileName($file->uuid() . '.tsv');
$file->setFileUri($directory . $file->uuid() . '.tsv');
$file->setTemporary();
$file->setOwnerId($this->currentUser->id());
$file->save();
@@ -480,13 +480,13 @@ public function downloadCsv(array &$form, FormStateInterface $form_state): AjaxR
}

if (!isset($last_id)) {
if (fputcsv($handle, array_keys(reset($results))) === FALSE) {
if (fputcsv($handle, array_keys(reset($results)), "\t") === FALSE) {
throw new \Exception('Unable to write headers to file for the log export');
}
}

foreach ($results as $result) {
if (fputcsv($handle, $result) === FALSE) {
if (fputcsv($handle, $result, "\t") === FALSE) {
throw new \Exception('Unable to write rows to file for the log export');
}
}

0 comments on commit 83bf0dc

Please sign in to comment.