Component Details

Deep dive into the configuration and assets associated with this component.

Basic Information
Name log-interpreter
Display Name Log Interpreter
Version 1.0.0
Status Active
Downloads 76
Description Component: Log Interpreter
Created 2025-11-11 20:42:10
Last Updated 2025-11-12 13:28:08
Controller Code
<?php

namespace App\Http\Controllers\Components\LogInterpreter;

use App\Http\Controllers\Controller;
use App\Models\Setting;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\ValidationException;

class ComponentLogInterpreterController extends Controller
{
    /**
     * Display the log interpreter interface.
     */
    public function index()
    {
        return view('components.log-interpreter.index');
    }

    /**
     * Analyze logs using OpenAI
     * POST /dashboard/analyze-logs
     */
    public function analyzeLogs(Request $request)
    {
        // Force JSON response - this is an API endpoint
        if (!$request->wantsJson() && !$request->expectsJson()) {
            $request->headers->set('Accept', 'application/json');
        }

        // Always return JSON for API endpoints
        try {
            $validated = $request->validate([
                'log_content' => 'required|string|max:50000',
                'log_type' => 'nullable|string',
                'log_level' => 'nullable|string',
            ]);
        } catch (ValidationException $e) {
            return response()->json([
                'success' => false,
                'error' => 'Validation failed: ' . implode(', ', $e->validator->errors()->all()),
                'errors' => $e->errors(),
            ], 422);
        }

        $logContent = $validated['log_content'];
        $logType = $validated['log_type'] ?? 'general';
        $logLevel = $validated['log_level'] ?? 'info';

        // Build prompt for OpenAI
        $prompt = "Analyze the following {$logType} log (level: {$logLevel}) and provide:\n\n";
        $prompt .= "1. A summary of what happened\n";
        $prompt .= "2. Identified issues or errors\n";
        $prompt .= "3. Recommended solutions\n\n";
        $prompt .= "Log content:\n{$logContent}";

        // Call OpenAI API directly (controller runs on license server)
        $user = auth()->user();

        if (!$user || !$user->license_key) {
            return response()->json([
                'success' => false,
                'error' => 'License key not found. Please configure your license in settings.',
            ], 403);
        }

        try {
            // Get OpenAI API key from database
            $apiKey = Setting::get('openai_api_key');
            if (!$apiKey) {
                return response()->json([
                    'success' => false,
                    'error' => 'OpenAI API key not configured on license server',
                ], 500);
            }

            $response = Http::withHeaders([
                'Authorization' => 'Bearer ' . $apiKey,
                'Content-Type' => 'application/json',
            ])->timeout(90)->post('https://api.openai.com/v1/chat/completions', [
                'model' => Setting::get('openai_model', 'gpt-4'),
                'messages' => [
                    [
                        'role' => 'user',
                        'content' => $prompt
                    ]
                ],
                'max_tokens' => (int) Setting::get('openai_max_tokens', 2000),
                'temperature' => (float) Setting::get('openai_temperature', 0.1),
            ]);

            if (!$response->successful()) {
                Log::error('OpenAI API error', [
                    'status' => $response->status(),
                    'body' => $response->body(),
                ]);
                return response()->json([
                    'success' => false,
                    'error' => 'OpenAI API request failed: ' . $response->body()
                ], $response->status());
            }

            $data = $response->json();
            
            if (!isset($data['choices'][0]['message']['content'])) {
                return response()->json([
                    'success' => false,
                    'error' => 'Invalid response from OpenAI API'
                ], 500);
            }

            return response()->json([
                'success' => true,
                'data' => $data['choices'][0]['message']['content'],
            ]);

        } catch (\Exception $e) {
            Log::error('Log analysis exception', [
                'error' => $e->getMessage(),
            ]);

            return response()->json([
                'success' => false,
                'error' => 'Analysis failed: ' . $e->getMessage(),
            ], 500);
        }
    }
}

Routes Code
<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Components\LogInterpreter\ComponentLogInterpreterController;

Route::middleware(['auth', 'component.visible'])->group(function () {
    Route::get('/log-interpreter', [ComponentLogInterpreterController::class, 'index'])->name('dashboard.log-interpreter');
    // API route - always returns JSON
    Route::post('/analyze-logs', [ComponentLogInterpreterController::class, 'analyzeLogs'])
        ->name('dashboard.analyze-logs')
        ->middleware('api'); // Ensure JSON response
});

Views (1)
  • index.blade.php Length: 57589 chars