Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add comments and fix issues #33

Merged
merged 3 commits into from
Apr 29, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 42 additions & 4 deletions modules/ocha_ai_chat/src/Controller/OchaAiChatController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,48 @@

use Drupal\Core\Access\AccessResult;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Database\Connection;
use Drupal\Core\Logger\LoggerChannelTrait;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

/**
* Class OchaAiChat Controller.
*
* @package Drupal\ocha_ai_chat\Controller
*/
class OchaAiChatController extends ControllerBase {
use LoggerChannelTrait;

/**
* The database connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected $connection;

/**
* The current request.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected $requestStack;

/**
* Constructs controller.
*
* @param \Drupal\Core\Database\Connection $connection
* The Connection object.
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* The RequestStack object.
*/
public function __construct(Connection $connection, RequestStack $request_stack) {
$this->connection = $connection;
$this->requestStack = $request_stack;
}

/**
* Generate chat log statistics.
Expand All @@ -22,15 +56,14 @@ class OchaAiChatController extends ControllerBase {
public function statistics(RouteMatchInterface $route_match, Request $request) {
$response = [];

cafuego marked this conversation as resolved.
Show resolved Hide resolved
// @codingStandardsIgnoreLine
$database = \Drupal::database();
$database = $this->connection;

// Number of interactions.
$query = $database->query("SELECT FROM_UNIXTIME(timestamp, '%Y-%m - Week %V') AS week, COUNT(id) AS interactions FROM ocha_ai_chat_logs GROUP BY week ORDER BY week ASC");
$result = $query->fetchAll();
$response['interactions'] = $result;

// Number of interactions.
// Average satisfaction.
$query = $database->query("SELECT FROM_UNIXTIME(timestamp, '%Y-%m - Week %V') AS week, AVG(satisfaction) AS average_satisfaction FROM ocha_ai_chat_logs GROUP BY week ORDER BY week ASC");
$result = $query->fetchAll();
$response['average_satisfaction'] = $result;
Expand All @@ -55,18 +88,22 @@ public function statistics(RouteMatchInterface $route_match, Request $request) {
$result = $query->fetchAll();
$response['thumbs'] = $result;

// Users asking less than five questions.
$query = $database->query("SELECT week, COUNT(uid) AS users_under_five_questions FROM (SELECT FROM_UNIXTIME(timestamp, '%Y-%m - Week %V') AS week, uid, count(id) as conversations FROM ocha_ai_chat_logs GROUP BY week, uid HAVING conversations < 5) A GROUP BY week ORDER BY week ASC");
$result = $query->fetchAll();
$response['users_under_five_questions'] = $result;

// Users asking between five and ten questions.
$query = $database->query("SELECT week, COUNT(uid) AS users_five_to_ten_questions FROM (SELECT FROM_UNIXTIME(timestamp, '%Y-%m - Week %V') AS week, uid, count(id) as conversations FROM ocha_ai_chat_logs GROUP BY week, uid HAVING conversations >= 5 AND conversations <= 10) A GROUP BY week ORDER BY week ASC");
$result = $query->fetchAll();
$response['users_five_to_ten_questions'] = $result;

// Users asking between ten and twenty questions.
$query = $database->query("SELECT week, COUNT(uid) AS users_ten_to_twenty_questions FROM (SELECT FROM_UNIXTIME(timestamp, '%Y-%m - Week %V') AS week, uid, count(id) as conversations FROM ocha_ai_chat_logs GROUP BY week, uid HAVING conversations > 10 AND conversations <= 20) A GROUP BY week ORDER BY week ASC");
$result = $query->fetchAll();
$response['users_ten_to_twenty_questions'] = $result;

// Users asking more than twenty questions.
$query = $database->query("SELECT week, COUNT(uid) AS users_over_twenty_questions FROM (SELECT FROM_UNIXTIME(timestamp, '%Y-%m - Week %V') AS week, uid, count(id) as conversations FROM ocha_ai_chat_logs GROUP BY week, uid HAVING conversations > 20) A GROUP BY week ORDER BY week ASC");
$result = $query->fetchAll();
$response['users_over_twenty_questions'] = $result;
Expand All @@ -89,7 +126,8 @@ public function access(AccountInterface $account) {
}
else {
$access_result = AccessResult::forbidden();
$this->logger->warning('Unauthorized access to statistics denied.');
$logger = $this->getLogger('ocha_ai');
$logger->warning('Unauthorized access to statistics denied');
}
$access_result
->setCacheMaxAge(0)
Expand Down