This commit is contained in:
Flatlogic Bot 2025-10-19 03:32:56 +00:00
parent d2b41a238e
commit 119d467b75
7 changed files with 348 additions and 33 deletions

View File

@ -1,13 +1,50 @@
<?php
require_once __DIR__ . '/db/config.php';
// Fetch submissions
try {
$pdo = db();
$stmt = $pdo->query("SELECT id, name, status, created_at FROM submissions ORDER BY created_at DESC");
$stmt = $pdo->query("SELECT * FROM submissions ORDER BY created_at DESC");
$submissions = $stmt->fetchAll();
} catch (PDOException $e) {
$submissions = [];
// error_log($e->getMessage()); // It's good practice to log errors
// In a real app, log this error to a file or service
// error_log($e->getMessage());
}
// Handle notifications from query parameters
$notification = null;
if (isset($_GET['success'])) {
$count = (int)$_GET['success'];
$notification = [
'type' => 'success',
'message' => "Successfully imported {$count} records from the CSV."
];
} elseif (isset($_GET['error'])) {
$notification = [
'type' => 'danger',
'message' => "An error occurred: " . htmlspecialchars($_GET['error'])
];
} elseif (isset($_GET['refreshed'])) {
$notification = [
'type' => 'info',
'message' => "Submission has been queued for refresh."
];
}
// Helper to determine badge color based on status
function get_status_badge_class($status) {
switch (strtolower($status)) {
case 'completed':
return 'bg-success-subtle text-success-emphasis';
case 'processing':
return 'bg-primary-subtle text-primary-emphasis';
case 'error':
return 'bg-danger-subtle text-danger-emphasis';
case 'pending':
default:
return 'bg-secondary-subtle text-secondary-emphasis';
}
}
?>
<!DOCTYPE html>
@ -16,7 +53,7 @@ try {
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Analyst Dashboard</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
</head>
@ -42,15 +79,22 @@ try {
</nav>
<main class="container mt-5">
<?php if ($notification): ?>
<div class="alert alert-<?= $notification['type'] ?> alert-dismissible fade show" role="alert">
<?= $notification['message'] ?>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<?php endif; ?>
<div class="d-flex justify-content-between align-items-center mb-4">
<h1 class="h2">Analyst Dashboard</h1>
<div class="d-flex gap-2">
<button class="btn btn-primary">
<button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#uploadCsvModal">
<i class="bi bi-upload me-2"></i>Upload CSV
</button>
<button class="btn btn-outline-secondary">
<a href="worker.php" class="btn btn-outline-secondary" onclick="this.innerHTML = '<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span> Refreshing...';">
<i class="bi bi-arrow-clockwise me-2"></i>Refresh All
</button>
</a>
</div>
</div>
@ -61,9 +105,10 @@ try {
<thead class="table-light">
<tr>
<th scope="col">Name</th>
<th scope="col">Company</th>
<th scope="col">Location</th>
<th scope="col">Social Profiles</th>
<th scope="col">Industry</th>
<th scope="col">Geo-Location</th>
<th scope="col">Status</th>
<th scope="col" class="text-end">Actions</th>
</tr>
@ -71,28 +116,34 @@ try {
<tbody>
<?php if (empty($submissions)): ?>
<tr>
<td colspan="6" class="text-center text-muted pt-4 pb-4">
<td colspan="7" class="text-center text-muted pt-4 pb-4">
<i class="bi bi-inbox fs-2 d-block mb-2"></i>
No data submitted yet.
</td>
</tr>
<?php else: ?>
<?php foreach ($submissions as $submission): ?>
<?php foreach ($submissions as $sub): ?>
<tr>
<td><?= htmlspecialchars($submission['name']) ?></td>
<td class="fw-medium"><?= htmlspecialchars($sub['name']) ?></td>
<td><?= htmlspecialchars($sub['company'] ?? 'N/A') ?></td>
<td><?= htmlspecialchars($sub['location'] ?? 'N/A') ?></td>
<td>
<!-- Placeholder for social profiles -->
<?php if (!empty($sub['linkedin_url'])): ?><a href="<?= htmlspecialchars($sub['linkedin_url']) ?>" target="_blank" class="text-body me-2"><i class="bi bi-linkedin"></i></a><?php endif; ?>
<?php if (!empty($sub['twitter_url'])): ?><a href="<?= htmlspecialchars($sub['twitter_url']) ?>" target="_blank" class="text-body me-2"><i class="bi bi-twitter-x"></i></a><?php endif; ?>
<?php if (!empty($sub['facebook_url'])): ?><a href="<?= htmlspecialchars($sub['facebook_url']) ?>" target="_blank" class="text-body me-2"><i class="bi bi-facebook"></i></a><?php endif; ?>
<?php if (!empty($sub['instagram_url'])): ?><a href="<?= htmlspecialchars($sub['instagram_url']) ?>" target="_blank" class="text-body me-2"><i class="bi bi-instagram"></i></a><?php endif; ?>
<?php if (!empty($sub['youtube_url'])): ?><a href="<?= htmlspecialchars($sub['youtube_url']) ?>" target="_blank" class="text-body"><i class="bi bi-youtube"></i></a><?php endif; ?>
</td>
<td><!-- Placeholder for Industry --></td>
<td><!-- Placeholder for Geo-Location --></td>
<td><?= htmlspecialchars($sub['industry'] ?? 'N/A') ?></td>
<td>
<span class="badge bg-light text-dark rounded-pill">
<?= htmlspecialchars($submission['status']) ?>
<span class="badge rounded-pill <?= get_status_badge_class($sub['status']) ?>">
<?= htmlspecialchars($sub['status']) ?>
</span>
</td>
<td class="text-end">
<button class="btn btn-sm btn-outline-secondary"><i class="bi bi-eye"></i></button>
<button class="btn btn-sm btn-outline-primary"><i class="bi bi-pencil-square"></i></button>
<a href="refresh_submission.php?id=<?= $sub['id'] ?>" class="btn btn-sm btn-outline-primary" data-bs-toggle="tooltip" title="Refresh Data">
<i class="bi bi-arrow-clockwise"></i>
</a>
</td>
</tr>
<?php endforeach; ?>
@ -104,6 +155,38 @@ try {
</div>
</main>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
<!-- CSV Upload Modal -->
<div class="modal fade" id="uploadCsvModal" tabindex="-1" aria-labelledby="uploadCsvModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="uploadCsvModalLabel">Upload CSV File</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<form action="upload_csv.php" method="post" enctype="multipart/form-data" id="csvUploadForm">
<div class="mb-3">
<label for="csvFile" class="form-label">Select a CSV file to upload. The file should contain one column with the header "Name".</label>
<input class="form-control" type="file" id="csvFile" name="csvFile" accept=".csv" required>
</div>
<p class="text-muted small">Maximum rows: 1000.</p>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="submit" form="csvUploadForm" class="btn btn-primary">Upload and Process</button>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<script>
// Enable tooltips
var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'))
var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
return new bootstrap.Tooltip(tooltipTriggerEl)
})
</script>
</body>
</html>

View File

@ -2,35 +2,55 @@
require_once __DIR__ . '/config.php';
try {
// Connect without specifying a database
// Connect without specifying a database to create it if it doesn't exist
$pdo = new PDO('mysql:host='.DB_HOST, DB_USER, DB_PASS, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
]);
$pdo->exec('CREATE DATABASE IF NOT EXISTS '.DB_NAME);
echo "Database ".DB_NAME." created or already exists.\n";
} catch (PDOException $e) {
echo "Error creating database: " . $e->getMessage() . "\n";
// If the user already exists, we might not have permission to create a database.
// Let's assume the DB is there and try to connect anyway for the next step.
// Suppress error if DB exists but user lacks CREATE DB permissions
}
// Now, connect to the database and run migrations
// Now, connect to the specific database
$pdo = db();
// 1. Create migrations table if it doesn't exist
try {
$pdo->exec("\n CREATE TABLE IF NOT EXISTS `migrations` (\n `id` INT AUTO_INCREMENT PRIMARY KEY,\n `migration` VARCHAR(255) NOT NULL,\n `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n )\n ");
} catch (PDOException $e) {
echo "Error creating migrations table: " . $e->getMessage() . "\n";
exit(1);
}
// 2. Get all migrations that have been run
$stmt = $pdo->query("SELECT `migration` FROM `migrations`");
$runMigrations = $stmt->fetchAll(PDO::FETCH_COLUMN);
// 3. Get all available migration files
$migrationsDir = __DIR__ . '/migrations';
$files = glob($migrationsDir . '/*.sql');
sort($files);
// 4. Run migrations that have not been run yet
foreach ($files as $file) {
echo "Applying migration: " . basename($file) . "\n";
$sql = file_get_contents($file);
try {
$pdo->exec($sql);
echo "Success\n";
} catch (PDOException $e) {
echo "Error applying migration: " . $e->getMessage() . "\n";
exit(1);
$migrationName = basename($file);
if (!in_array($migrationName, $runMigrations)) {
echo "Applying migration: " . $migrationName . "\n";
$sql = file_get_contents($file);
try {
$pdo->exec($sql);
// Add the migration to the migrations table
$insertStmt = $pdo->prepare("INSERT INTO `migrations` (`migration`) VALUES (?)");
$insertStmt->execute([$migrationName]);
echo "Success\n";
} catch (PDOException $e) {
echo "Error applying migration: " . $e->getMessage() . "\n";
// Don't exit, allow other migrations to run
}
}
}
echo "All migrations applied successfully.\n";
echo "Migration check complete.\n";

View File

@ -0,0 +1,13 @@
ALTER TABLE `submissions`
ADD COLUMN `status` VARCHAR(255) NOT NULL DEFAULT 'Pending',
ADD COLUMN `company` VARCHAR(255) NULL,
ADD COLUMN `profile_image_url` VARCHAR(2048) NULL,
ADD COLUMN `location` VARCHAR(255) NULL,
ADD COLUMN `linkedin_url` VARCHAR(2048) NULL,
ADD COLUMN `twitter_url` VARCHAR(2048) NULL,
ADD COLUMN `facebook_url` VARCHAR(2048) NULL,
ADD COLUMN `instagram_url` VARCHAR(2048) NULL,
ADD COLUMN `youtube_url` VARCHAR(2048) NULL,
ADD COLUMN `industry` VARCHAR(255) NULL,
ADD COLUMN `geo_location` VARCHAR(255) NULL,
ADD COLUMN `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;

View File

@ -10,6 +10,8 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($_POST['name'])) {
$stmt = $pdo->prepare("INSERT INTO submissions (name) VALUES (?)");
$stmt->execute([$name]);
$success_toast = true;
// Trigger the background worker
shell_exec('php /home/ubuntu/executor/workspace/worker.php > /dev/null 2>&1 &');
} catch (PDOException $e) {
// For now, we'll just prevent the success toast on db error.
}

28
refresh_submission.php Normal file
View File

@ -0,0 +1,28 @@
<?php
require_once __DIR__ . '/db/config.php';
// Check if ID is provided
if (!isset($_GET['id']) || empty($_GET['id'])) {
header('Location: analyst_dashboard.php?error=missing_id');
exit;
}
$submissionId = (int)$_GET['id'];
// Update the status to 'Pending'
try {
$pdo = db();
$stmt = $pdo->prepare("UPDATE submissions SET status = 'Pending' WHERE id = ?");
$stmt->execute([$submissionId]);
// Trigger the background worker
shell_exec('php /home/ubuntu/executor/workspace/worker.php > /dev/null 2>&1 &');
} catch (PDOException $e) {
// In a real app, log this error
header('Location: analyst_dashboard.php?error=' . urlencode($e->getMessage()));
exit;
}
// Redirect back with a success message
header('Location: analyst_dashboard.php?refreshed=1');
exit;

76
upload_csv.php Normal file
View File

@ -0,0 +1,76 @@
<?php
require_once __DIR__ . '/db/config.php';
// Basic validation
if ($_SERVER['REQUEST_METHOD'] !== 'POST' || !isset($_FILES['csvFile'])) {
header('Location: analyst_dashboard.php?error=' . urlencode('Invalid request.'));
exit;
}
$file = $_FILES['csvFile'];
// Check for upload errors
if ($file['error'] !== UPLOAD_ERR_OK) {
header('Location: analyst_dashboard.php?error=' . urlencode('File upload error: ' . $file['error']));
exit;
}
// Check file type
$mime_type = mime_content_type($file['tmp_name']);
if ($mime_type !== 'text/csv' && $mime_type !== 'text/plain') {
header('Location: analyst_dashboard.php?error=' . urlencode('Invalid file type. Please upload a CSV file.'));
exit;
}
$handle = fopen($file['tmp_name'], 'r');
if ($handle === false) {
header('Location: analyst_dashboard.php?error=' . urlencode('Could not open the uploaded file.'));
exit;
}
// Validate header
$header = fgetcsv($handle);
if ($header === false || count($header) !== 1 || strtolower(trim($header[0])) !== 'name') {
fclose($handle);
header('Location: analyst_dashboard.php?error=' . urlencode('Invalid CSV header. The first column must be "Name".'));
exit;
}
$pdo = db();
$stmt = $pdo->prepare("INSERT INTO submissions (name) VALUES (:name)");
$importedCount = 0;
$rowCount = 0;
while (($data = fgetcsv($handle)) !== false) {
$rowCount++;
if ($rowCount > 1000) {
// Stop processing if the file exceeds the row limit
break;
}
if (isset($data[0]) && !empty(trim($data[0]))) {
try {
$stmt->execute(['name' => trim($data[0])]);
$importedCount++;
} catch (PDOException $e) {
// You might want to log this error instead of ignoring it
// error_log("CSV Import Error: " . $e->getMessage());
continue; // Continue to the next row
}
}
}
fclose($handle);
// Trigger the background worker now that new submissions are in
if ($importedCount > 0) {
shell_exec('php /home/ubuntu/executor/workspace/worker.php > /dev/null 2>&1 &');
}
if ($rowCount > 1000) {
header('Location: analyst_dashboard.php?error=' . urlencode('CSV file exceeds the 1000-row limit. Only the first 1000 rows were processed.') . '&success=' . $importedCount);
} else {
header('Location: analyst_dashboard.php?success=' . $importedCount);
}
exit;

93
worker.php Normal file
View File

@ -0,0 +1,93 @@
<?php
require_once __DIR__ . '/db/config.php';
// Set a longer execution time, as this can be a long-running script.
ini_set('max_execution_time', 300); // 5 minutes
// --- Helper Functions ---
/**
* Updates the status of a submission.
*/
function update_status($pdo, $id, $status, $message = null) {
$sql = "UPDATE submissions SET status = ? WHERE id = ?";
if ($message) {
// For now, we don't have a column for error messages, but we could add one.
// For this version, we'll just log it to the server's error log.
error_log("Submission ID: $id, Status: $status, Message: $message");
}
$stmt = $pdo->prepare($sql);
$stmt->execute([$status, $id]);
}
/**
* A simple function to extract a domain from a URL.
*/
function get_domain($url) {
$pieces = parse_url($url);
if (isset($pieces['host'])) {
return preg_replace('/^www\./', '', $pieces['host']);
}
return null;
}
// --- Main Worker Logic ---
$pdo = db();
// 1. Find submissions that are 'Pending' or those that have been 'Processing' for a while (e.g., > 15 mins)
// This helps recover from script crashes.
$stmt = $pdo->query("SELECT * FROM submissions WHERE status = 'Pending' OR (status = 'Processing' AND updated_at < NOW() - INTERVAL 15 MINUTE)");
$pending_submissions = $stmt->fetchAll();
if (empty($pending_submissions)) {
// No work to do, just redirect back.
header('Location: analyst_dashboard.php');
exit;
}
foreach ($pending_submissions as $sub) {
$id = $sub['id'];
$name = $sub['name'];
// 2. Mark as 'Processing'
update_status($pdo, $id, 'Processing');
// 3. Simulate fetching data (This is where the Gemini API calls will go)
// For now, we'll use a mock implementation that just populates some data.
try {
// --- MOCK DATA IMPLEMENTATION ---
// In a real scenario, you would use a proper web scraping or API service.
// This is a placeholder to simulate finding profiles.
$social_profiles = [
'linkedin_url' => 'https://linkedin.com/in/' . strtolower(str_replace(' ', '', $name)) . rand(1, 99),
'twitter_url' => 'https://twitter.com/' . strtolower(str_replace(' ', '', $name)),
];
$update_data = array_merge($social_profiles, [
'company' => 'Mock Company ' . rand(100, 999),
'location' => 'Mock Location',
'industry' => 'Mock Industry',
'geo_location' => 'Mock Geo',
'status' => 'Completed'
]);
$sql_parts = [];
foreach ($update_data as $key => $value) {
$sql_parts[] = "`$key` = :$key";
}
$sql = "UPDATE submissions SET " . implode(', ', $sql_parts) . " WHERE id = :id";
$stmt = $pdo->prepare($sql);
$update_data['id'] = $id;
$stmt->execute($update_data);
} catch (Exception $e) {
// If something goes wrong, mark as 'Error'
update_status($pdo, $id, 'Error', $e->getMessage());
}
}
// 4. Redirect back to the dashboard
header('Location: analyst_dashboard.php?refreshed=all');
exit;