Compare commits
49 Commits
feat/task-
...
feat/ui-po
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3f958fbff3 | ||
|
|
c84ef0396b | ||
|
|
6cd1f55119 | ||
|
|
e918234928 | ||
|
|
888a608485 | ||
|
|
b5c3b05246 | ||
|
|
fdce5e0302 | ||
|
|
4679b245de | ||
|
|
a837070f54 | ||
|
|
5a929e9803 | ||
|
|
52b0fad410 | ||
|
|
9944031eea | ||
|
|
2babaa7136 | ||
|
|
90567511dd | ||
|
|
beb16ad0cb | ||
|
|
fc7fc5ea85 | ||
|
|
ab8956b14b | ||
|
|
1d9c90641f | ||
|
|
6126b907f2 | ||
|
|
cc93d2d483 | ||
|
|
7642c17ec0 | ||
|
|
cb60dcf352 | ||
|
|
5ffe05d519 | ||
|
|
8e2f07c941 | ||
|
|
0b6e615075 | ||
|
|
be251c6fb3 | ||
|
|
efb1e89e33 | ||
|
|
529c447413 | ||
|
|
1eaf95c06b | ||
|
|
138ed17d8b | ||
|
|
a880c41d89 | ||
|
|
2a9ae61dce | ||
|
|
1f21911fa1 | ||
|
|
6f0a58f5d2 | ||
|
|
8206dce821 | ||
|
|
ced1afaa8a | ||
|
|
d6c602c567 | ||
|
|
a252a7fefd | ||
|
|
83b06c21cc | ||
|
|
f5214da54c | ||
|
|
e3d4dd0127 | ||
|
|
d0ee0d72f5 | ||
|
|
521f0550cd | ||
|
|
459ad7d9c9 | ||
|
|
d102d27731 | ||
|
|
b7d33e1cbf | ||
|
|
5b34b5a78c | ||
|
|
c091d2316b | ||
|
|
e8862b8a8b |
@@ -1,6 +1,3 @@
|
|||||||
when:
|
|
||||||
- event: [push, pull_request]
|
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
# ===========================================
|
# ===========================================
|
||||||
# PR VALIDATION: Parallel type checks (PRs only)
|
# PR VALIDATION: Parallel type checks (PRs only)
|
||||||
@@ -89,6 +86,10 @@ steps:
|
|||||||
from_secret: registry_password
|
from_secret: registry_password
|
||||||
platforms: linux/amd64
|
platforms: linux/amd64
|
||||||
provenance: false
|
provenance: false
|
||||||
|
cache_from:
|
||||||
|
- "type=registry,ref=code.cannabrands.app/creationshop/dispensary-scraper:cache"
|
||||||
|
cache_to:
|
||||||
|
- "type=registry,ref=code.cannabrands.app/creationshop/dispensary-scraper:cache,mode=max"
|
||||||
build_args:
|
build_args:
|
||||||
APP_BUILD_VERSION: ${CI_COMMIT_SHA:0:8}
|
APP_BUILD_VERSION: ${CI_COMMIT_SHA:0:8}
|
||||||
APP_GIT_SHA: ${CI_COMMIT_SHA}
|
APP_GIT_SHA: ${CI_COMMIT_SHA}
|
||||||
@@ -115,6 +116,10 @@ steps:
|
|||||||
from_secret: registry_password
|
from_secret: registry_password
|
||||||
platforms: linux/amd64
|
platforms: linux/amd64
|
||||||
provenance: false
|
provenance: false
|
||||||
|
cache_from:
|
||||||
|
- "type=registry,ref=code.cannabrands.app/creationshop/cannaiq-frontend:cache"
|
||||||
|
cache_to:
|
||||||
|
- "type=registry,ref=code.cannabrands.app/creationshop/cannaiq-frontend:cache,mode=max"
|
||||||
depends_on: []
|
depends_on: []
|
||||||
when:
|
when:
|
||||||
branch: master
|
branch: master
|
||||||
@@ -136,6 +141,10 @@ steps:
|
|||||||
from_secret: registry_password
|
from_secret: registry_password
|
||||||
platforms: linux/amd64
|
platforms: linux/amd64
|
||||||
provenance: false
|
provenance: false
|
||||||
|
cache_from:
|
||||||
|
- "type=registry,ref=code.cannabrands.app/creationshop/findadispo-frontend:cache"
|
||||||
|
cache_to:
|
||||||
|
- "type=registry,ref=code.cannabrands.app/creationshop/findadispo-frontend:cache,mode=max"
|
||||||
depends_on: []
|
depends_on: []
|
||||||
when:
|
when:
|
||||||
branch: master
|
branch: master
|
||||||
@@ -157,38 +166,17 @@ steps:
|
|||||||
from_secret: registry_password
|
from_secret: registry_password
|
||||||
platforms: linux/amd64
|
platforms: linux/amd64
|
||||||
provenance: false
|
provenance: false
|
||||||
|
cache_from:
|
||||||
|
- "type=registry,ref=code.cannabrands.app/creationshop/findagram-frontend:cache"
|
||||||
|
cache_to:
|
||||||
|
- "type=registry,ref=code.cannabrands.app/creationshop/findagram-frontend:cache,mode=max"
|
||||||
depends_on: []
|
depends_on: []
|
||||||
when:
|
when:
|
||||||
branch: master
|
branch: master
|
||||||
event: push
|
event: push
|
||||||
|
|
||||||
# ===========================================
|
# ===========================================
|
||||||
# STAGE 3: Run Database Migrations (before deploy)
|
# STAGE 3: Deploy and Run Migrations
|
||||||
# ===========================================
|
|
||||||
migrate:
|
|
||||||
image: code.cannabrands.app/creationshop/dispensary-scraper:${CI_COMMIT_SHA:0:8}
|
|
||||||
environment:
|
|
||||||
CANNAIQ_DB_HOST:
|
|
||||||
from_secret: db_host
|
|
||||||
CANNAIQ_DB_PORT:
|
|
||||||
from_secret: db_port
|
|
||||||
CANNAIQ_DB_NAME:
|
|
||||||
from_secret: db_name
|
|
||||||
CANNAIQ_DB_USER:
|
|
||||||
from_secret: db_user
|
|
||||||
CANNAIQ_DB_PASS:
|
|
||||||
from_secret: db_pass
|
|
||||||
commands:
|
|
||||||
- cd /app
|
|
||||||
- node dist/db/migrate.js
|
|
||||||
depends_on:
|
|
||||||
- docker-backend
|
|
||||||
when:
|
|
||||||
branch: master
|
|
||||||
event: push
|
|
||||||
|
|
||||||
# ===========================================
|
|
||||||
# STAGE 4: Deploy (after migrations)
|
|
||||||
# ===========================================
|
# ===========================================
|
||||||
deploy:
|
deploy:
|
||||||
image: bitnami/kubectl:latest
|
image: bitnami/kubectl:latest
|
||||||
@@ -199,15 +187,20 @@ steps:
|
|||||||
- mkdir -p ~/.kube
|
- mkdir -p ~/.kube
|
||||||
- echo "$KUBECONFIG_CONTENT" | tr -d '[:space:]' | base64 -d > ~/.kube/config
|
- echo "$KUBECONFIG_CONTENT" | tr -d '[:space:]' | base64 -d > ~/.kube/config
|
||||||
- chmod 600 ~/.kube/config
|
- chmod 600 ~/.kube/config
|
||||||
|
# Deploy backend first
|
||||||
- kubectl set image deployment/scraper scraper=code.cannabrands.app/creationshop/dispensary-scraper:${CI_COMMIT_SHA:0:8} -n dispensary-scraper
|
- kubectl set image deployment/scraper scraper=code.cannabrands.app/creationshop/dispensary-scraper:${CI_COMMIT_SHA:0:8} -n dispensary-scraper
|
||||||
|
- kubectl rollout status deployment/scraper -n dispensary-scraper --timeout=300s
|
||||||
|
# Note: Migrations run automatically at startup via auto-migrate
|
||||||
|
# Deploy remaining services
|
||||||
|
# Resilience: ensure workers are scaled up if at 0
|
||||||
|
- REPLICAS=$(kubectl get deployment scraper-worker -n dispensary-scraper -o jsonpath='{.spec.replicas}'); if [ "$REPLICAS" = "0" ]; then echo "Scaling workers from 0 to 5"; kubectl scale deployment/scraper-worker --replicas=5 -n dispensary-scraper; fi
|
||||||
- kubectl set image deployment/scraper-worker worker=code.cannabrands.app/creationshop/dispensary-scraper:${CI_COMMIT_SHA:0:8} -n dispensary-scraper
|
- kubectl set image deployment/scraper-worker worker=code.cannabrands.app/creationshop/dispensary-scraper:${CI_COMMIT_SHA:0:8} -n dispensary-scraper
|
||||||
- kubectl set image deployment/cannaiq-frontend cannaiq-frontend=code.cannabrands.app/creationshop/cannaiq-frontend:${CI_COMMIT_SHA:0:8} -n dispensary-scraper
|
- kubectl set image deployment/cannaiq-frontend cannaiq-frontend=code.cannabrands.app/creationshop/cannaiq-frontend:${CI_COMMIT_SHA:0:8} -n dispensary-scraper
|
||||||
- kubectl set image deployment/findadispo-frontend findadispo-frontend=code.cannabrands.app/creationshop/findadispo-frontend:${CI_COMMIT_SHA:0:8} -n dispensary-scraper
|
- kubectl set image deployment/findadispo-frontend findadispo-frontend=code.cannabrands.app/creationshop/findadispo-frontend:${CI_COMMIT_SHA:0:8} -n dispensary-scraper
|
||||||
- kubectl set image deployment/findagram-frontend findagram-frontend=code.cannabrands.app/creationshop/findagram-frontend:${CI_COMMIT_SHA:0:8} -n dispensary-scraper
|
- kubectl set image deployment/findagram-frontend findagram-frontend=code.cannabrands.app/creationshop/findagram-frontend:${CI_COMMIT_SHA:0:8} -n dispensary-scraper
|
||||||
- kubectl rollout status deployment/scraper -n dispensary-scraper --timeout=300s
|
|
||||||
- kubectl rollout status deployment/cannaiq-frontend -n dispensary-scraper --timeout=120s
|
- kubectl rollout status deployment/cannaiq-frontend -n dispensary-scraper --timeout=120s
|
||||||
depends_on:
|
depends_on:
|
||||||
- migrate
|
- docker-backend
|
||||||
- docker-cannaiq
|
- docker-cannaiq
|
||||||
- docker-findadispo
|
- docker-findadispo
|
||||||
- docker-findagram
|
- docker-findagram
|
||||||
191
.woodpecker/ci.yml
Normal file
191
.woodpecker/ci.yml
Normal file
@@ -0,0 +1,191 @@
|
|||||||
|
steps:
|
||||||
|
# ===========================================
|
||||||
|
# PR VALIDATION: Only typecheck changed projects
|
||||||
|
# ===========================================
|
||||||
|
typecheck-backend:
|
||||||
|
image: code.cannabrands.app/creationshop/node:20
|
||||||
|
commands:
|
||||||
|
- npm config set cache /npm-cache/backend --global
|
||||||
|
- cd backend
|
||||||
|
- npm ci --prefer-offline
|
||||||
|
- npx tsc --noEmit
|
||||||
|
volumes:
|
||||||
|
- npm-cache:/npm-cache
|
||||||
|
depends_on: []
|
||||||
|
when:
|
||||||
|
event: pull_request
|
||||||
|
path:
|
||||||
|
include: ['backend/**']
|
||||||
|
|
||||||
|
typecheck-cannaiq:
|
||||||
|
image: code.cannabrands.app/creationshop/node:20
|
||||||
|
commands:
|
||||||
|
- npm config set cache /npm-cache/cannaiq --global
|
||||||
|
- cd cannaiq
|
||||||
|
- npm ci --prefer-offline
|
||||||
|
- npx tsc --noEmit
|
||||||
|
volumes:
|
||||||
|
- npm-cache:/npm-cache
|
||||||
|
depends_on: []
|
||||||
|
when:
|
||||||
|
event: pull_request
|
||||||
|
path:
|
||||||
|
include: ['cannaiq/**']
|
||||||
|
|
||||||
|
# findadispo/findagram typechecks skipped - they have || true anyway
|
||||||
|
|
||||||
|
# ===========================================
|
||||||
|
# AUTO-MERGE: Merge PR after all checks pass
|
||||||
|
# ===========================================
|
||||||
|
auto-merge:
|
||||||
|
image: alpine:latest
|
||||||
|
environment:
|
||||||
|
GITEA_TOKEN:
|
||||||
|
from_secret: gitea_token
|
||||||
|
commands:
|
||||||
|
- apk add --no-cache curl
|
||||||
|
- |
|
||||||
|
echo "Merging PR #${CI_COMMIT_PULL_REQUEST}..."
|
||||||
|
curl -s -X POST \
|
||||||
|
-H "Authorization: token $GITEA_TOKEN" \
|
||||||
|
-H "Content-Type: application/json" \
|
||||||
|
-d '{"Do":"merge"}' \
|
||||||
|
"https://code.cannabrands.app/api/v1/repos/Creationshop/dispensary-scraper/pulls/${CI_COMMIT_PULL_REQUEST}/merge"
|
||||||
|
depends_on:
|
||||||
|
- typecheck-backend
|
||||||
|
- typecheck-cannaiq
|
||||||
|
when:
|
||||||
|
event: pull_request
|
||||||
|
|
||||||
|
# ===========================================
|
||||||
|
# MASTER DEPLOY: Parallel Docker builds
|
||||||
|
# ===========================================
|
||||||
|
docker-backend:
|
||||||
|
image: woodpeckerci/plugin-docker-buildx
|
||||||
|
settings:
|
||||||
|
registry: code.cannabrands.app
|
||||||
|
repo: code.cannabrands.app/creationshop/dispensary-scraper
|
||||||
|
tags:
|
||||||
|
- latest
|
||||||
|
- ${CI_COMMIT_SHA:0:8}
|
||||||
|
dockerfile: backend/Dockerfile
|
||||||
|
context: backend
|
||||||
|
username:
|
||||||
|
from_secret: registry_username
|
||||||
|
password:
|
||||||
|
from_secret: registry_password
|
||||||
|
platforms: linux/amd64
|
||||||
|
provenance: false
|
||||||
|
cache_from: type=registry,ref=code.cannabrands.app/creationshop/dispensary-scraper:cache
|
||||||
|
cache_to: type=registry,ref=code.cannabrands.app/creationshop/dispensary-scraper:cache,mode=max
|
||||||
|
build_args:
|
||||||
|
APP_BUILD_VERSION: ${CI_COMMIT_SHA:0:8}
|
||||||
|
APP_GIT_SHA: ${CI_COMMIT_SHA}
|
||||||
|
APP_BUILD_TIME: ${CI_PIPELINE_CREATED}
|
||||||
|
CONTAINER_IMAGE_TAG: ${CI_COMMIT_SHA:0:8}
|
||||||
|
depends_on: []
|
||||||
|
when:
|
||||||
|
branch: master
|
||||||
|
event: push
|
||||||
|
|
||||||
|
docker-cannaiq:
|
||||||
|
image: woodpeckerci/plugin-docker-buildx
|
||||||
|
settings:
|
||||||
|
registry: code.cannabrands.app
|
||||||
|
repo: code.cannabrands.app/creationshop/cannaiq-frontend
|
||||||
|
tags:
|
||||||
|
- latest
|
||||||
|
- ${CI_COMMIT_SHA:0:8}
|
||||||
|
dockerfile: cannaiq/Dockerfile
|
||||||
|
context: cannaiq
|
||||||
|
username:
|
||||||
|
from_secret: registry_username
|
||||||
|
password:
|
||||||
|
from_secret: registry_password
|
||||||
|
platforms: linux/amd64
|
||||||
|
provenance: false
|
||||||
|
cache_from: type=registry,ref=code.cannabrands.app/creationshop/cannaiq-frontend:cache
|
||||||
|
cache_to: type=registry,ref=code.cannabrands.app/creationshop/cannaiq-frontend:cache,mode=max
|
||||||
|
depends_on: []
|
||||||
|
when:
|
||||||
|
branch: master
|
||||||
|
event: push
|
||||||
|
|
||||||
|
docker-findadispo:
|
||||||
|
image: woodpeckerci/plugin-docker-buildx
|
||||||
|
settings:
|
||||||
|
registry: code.cannabrands.app
|
||||||
|
repo: code.cannabrands.app/creationshop/findadispo-frontend
|
||||||
|
tags:
|
||||||
|
- latest
|
||||||
|
- ${CI_COMMIT_SHA:0:8}
|
||||||
|
dockerfile: findadispo/frontend/Dockerfile
|
||||||
|
context: findadispo/frontend
|
||||||
|
username:
|
||||||
|
from_secret: registry_username
|
||||||
|
password:
|
||||||
|
from_secret: registry_password
|
||||||
|
platforms: linux/amd64
|
||||||
|
provenance: false
|
||||||
|
cache_from: type=registry,ref=code.cannabrands.app/creationshop/findadispo-frontend:cache
|
||||||
|
cache_to: type=registry,ref=code.cannabrands.app/creationshop/findadispo-frontend:cache,mode=max
|
||||||
|
depends_on: []
|
||||||
|
when:
|
||||||
|
branch: master
|
||||||
|
event: push
|
||||||
|
|
||||||
|
docker-findagram:
|
||||||
|
image: woodpeckerci/plugin-docker-buildx
|
||||||
|
settings:
|
||||||
|
registry: code.cannabrands.app
|
||||||
|
repo: code.cannabrands.app/creationshop/findagram-frontend
|
||||||
|
tags:
|
||||||
|
- latest
|
||||||
|
- ${CI_COMMIT_SHA:0:8}
|
||||||
|
dockerfile: findagram/frontend/Dockerfile
|
||||||
|
context: findagram/frontend
|
||||||
|
username:
|
||||||
|
from_secret: registry_username
|
||||||
|
password:
|
||||||
|
from_secret: registry_password
|
||||||
|
platforms: linux/amd64
|
||||||
|
provenance: false
|
||||||
|
cache_from: type=registry,ref=code.cannabrands.app/creationshop/findagram-frontend:cache
|
||||||
|
cache_to: type=registry,ref=code.cannabrands.app/creationshop/findagram-frontend:cache,mode=max
|
||||||
|
depends_on: []
|
||||||
|
when:
|
||||||
|
branch: master
|
||||||
|
event: push
|
||||||
|
|
||||||
|
# ===========================================
|
||||||
|
# STAGE 3: Deploy and Run Migrations
|
||||||
|
# ===========================================
|
||||||
|
deploy:
|
||||||
|
image: bitnami/kubectl:latest
|
||||||
|
environment:
|
||||||
|
KUBECONFIG_CONTENT:
|
||||||
|
from_secret: kubeconfig_data
|
||||||
|
commands:
|
||||||
|
- mkdir -p ~/.kube
|
||||||
|
- echo "$KUBECONFIG_CONTENT" | tr -d '[:space:]' | base64 -d > ~/.kube/config
|
||||||
|
- chmod 600 ~/.kube/config
|
||||||
|
# Deploy backend first
|
||||||
|
- kubectl set image deployment/scraper scraper=code.cannabrands.app/creationshop/dispensary-scraper:${CI_COMMIT_SHA:0:8} -n dispensary-scraper
|
||||||
|
- kubectl rollout status deployment/scraper -n dispensary-scraper --timeout=300s
|
||||||
|
# Note: Migrations run automatically at startup via auto-migrate
|
||||||
|
# Deploy remaining services
|
||||||
|
# Resilience: ensure workers are scaled up if at 0
|
||||||
|
- REPLICAS=$(kubectl get deployment scraper-worker -n dispensary-scraper -o jsonpath='{.spec.replicas}'); if [ "$REPLICAS" = "0" ]; then echo "Scaling workers from 0 to 5"; kubectl scale deployment/scraper-worker --replicas=5 -n dispensary-scraper; fi
|
||||||
|
- kubectl set image deployment/scraper-worker worker=code.cannabrands.app/creationshop/dispensary-scraper:${CI_COMMIT_SHA:0:8} -n dispensary-scraper
|
||||||
|
- kubectl set image deployment/cannaiq-frontend cannaiq-frontend=code.cannabrands.app/creationshop/cannaiq-frontend:${CI_COMMIT_SHA:0:8} -n dispensary-scraper
|
||||||
|
- kubectl set image deployment/findadispo-frontend findadispo-frontend=code.cannabrands.app/creationshop/findadispo-frontend:${CI_COMMIT_SHA:0:8} -n dispensary-scraper
|
||||||
|
- kubectl set image deployment/findagram-frontend findagram-frontend=code.cannabrands.app/creationshop/findagram-frontend:${CI_COMMIT_SHA:0:8} -n dispensary-scraper
|
||||||
|
- kubectl rollout status deployment/cannaiq-frontend -n dispensary-scraper --timeout=120s
|
||||||
|
depends_on:
|
||||||
|
- docker-backend
|
||||||
|
- docker-cannaiq
|
||||||
|
- docker-findadispo
|
||||||
|
- docker-findagram
|
||||||
|
when:
|
||||||
|
branch: master
|
||||||
|
event: push
|
||||||
@@ -939,7 +939,8 @@ export default defineConfig({
|
|||||||
|
|
||||||
20) **Crawler Architecture**
|
20) **Crawler Architecture**
|
||||||
- **Scraper pod (1 replica)**: Runs the Express API server + scheduler.
|
- **Scraper pod (1 replica)**: Runs the Express API server + scheduler.
|
||||||
- **Scraper-worker pods (5 replicas)**: Each worker runs `dist/dutchie-az/services/worker.js`, polling the job queue.
|
- **Scraper-worker pods (25 replicas)**: Each runs `dist/tasks/task-worker.js`, polling the job queue.
|
||||||
|
- **Worker naming**: Pods use fantasy names (Aethelgard, Xylos, Kryll, Coriolis, etc.) - see `k8s/scraper-worker.yaml` ConfigMap. Worker IDs: `{PodName}-worker-{n}`
|
||||||
- **Job types**: `menu_detection`, `menu_detection_single`, `dutchie_product_crawl`
|
- **Job types**: `menu_detection`, `menu_detection_single`, `dutchie_product_crawl`
|
||||||
- **Job schedules** (managed in `job_schedules` table):
|
- **Job schedules** (managed in `job_schedules` table):
|
||||||
- `dutchie_az_menu_detection`: Runs daily with 60-min jitter
|
- `dutchie_az_menu_detection`: Runs daily with 60-min jitter
|
||||||
|
|||||||
@@ -362,6 +362,148 @@ SET status = 'pending', retry_count = retry_count + 1
|
|||||||
WHERE status = 'failed' AND retry_count < max_retries;
|
WHERE status = 'failed' AND retry_count < max_retries;
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Concurrent Task Processing (Added 2024-12)
|
||||||
|
|
||||||
|
Workers can now process multiple tasks concurrently within a single worker instance. This improves throughput by utilizing async I/O efficiently.
|
||||||
|
|
||||||
|
### Architecture
|
||||||
|
|
||||||
|
```
|
||||||
|
┌─────────────────────────────────────────────────────────────┐
|
||||||
|
│ Pod (K8s) │
|
||||||
|
│ │
|
||||||
|
│ ┌─────────────────────────────────────────────────────┐ │
|
||||||
|
│ │ TaskWorker │ │
|
||||||
|
│ │ │ │
|
||||||
|
│ │ ┌─────────┐ ┌─────────┐ ┌─────────┐ │ │
|
||||||
|
│ │ │ Task 1 │ │ Task 2 │ │ Task 3 │ (concurrent)│ │
|
||||||
|
│ │ └─────────┘ └─────────┘ └─────────┘ │ │
|
||||||
|
│ │ │ │
|
||||||
|
│ │ Resource Monitor │ │
|
||||||
|
│ │ ├── Memory: 65% (threshold: 85%) │ │
|
||||||
|
│ │ ├── CPU: 45% (threshold: 90%) │ │
|
||||||
|
│ │ └── Status: Normal │ │
|
||||||
|
│ └─────────────────────────────────────────────────────┘ │
|
||||||
|
└─────────────────────────────────────────────────────────────┘
|
||||||
|
```
|
||||||
|
|
||||||
|
### Environment Variables
|
||||||
|
|
||||||
|
| Variable | Default | Description |
|
||||||
|
|----------|---------|-------------|
|
||||||
|
| `MAX_CONCURRENT_TASKS` | 3 | Maximum tasks a worker will run concurrently |
|
||||||
|
| `MEMORY_BACKOFF_THRESHOLD` | 0.85 | Back off when heap memory exceeds 85% |
|
||||||
|
| `CPU_BACKOFF_THRESHOLD` | 0.90 | Back off when CPU exceeds 90% |
|
||||||
|
| `BACKOFF_DURATION_MS` | 10000 | How long to wait when backing off (10s) |
|
||||||
|
|
||||||
|
### How It Works
|
||||||
|
|
||||||
|
1. **Main Loop**: Worker continuously tries to fill up to `MAX_CONCURRENT_TASKS`
|
||||||
|
2. **Resource Monitoring**: Before claiming a new task, worker checks memory and CPU
|
||||||
|
3. **Backoff**: If resources exceed thresholds, worker pauses and stops claiming new tasks
|
||||||
|
4. **Concurrent Execution**: Tasks run in parallel using `Promise` - they don't block each other
|
||||||
|
5. **Graceful Shutdown**: On SIGTERM/decommission, worker stops claiming but waits for active tasks
|
||||||
|
|
||||||
|
### Resource Monitoring
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// ResourceStats interface
|
||||||
|
interface ResourceStats {
|
||||||
|
memoryPercent: number; // Current heap usage as decimal (0.0-1.0)
|
||||||
|
memoryMb: number; // Current heap used in MB
|
||||||
|
memoryTotalMb: number; // Total heap available in MB
|
||||||
|
cpuPercent: number; // CPU usage as percentage (0-100)
|
||||||
|
isBackingOff: boolean; // True if worker is in backoff state
|
||||||
|
backoffReason: string; // Why the worker is backing off
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Heartbeat Data
|
||||||
|
|
||||||
|
Workers report the following in their heartbeat:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"worker_id": "worker-abc123",
|
||||||
|
"current_task_id": 456,
|
||||||
|
"current_task_ids": [456, 457, 458],
|
||||||
|
"active_task_count": 3,
|
||||||
|
"max_concurrent_tasks": 3,
|
||||||
|
"status": "active",
|
||||||
|
"resources": {
|
||||||
|
"memory_mb": 256,
|
||||||
|
"memory_total_mb": 512,
|
||||||
|
"memory_rss_mb": 320,
|
||||||
|
"memory_percent": 50,
|
||||||
|
"cpu_user_ms": 12500,
|
||||||
|
"cpu_system_ms": 3200,
|
||||||
|
"cpu_percent": 45,
|
||||||
|
"is_backing_off": false,
|
||||||
|
"backoff_reason": null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Backoff Behavior
|
||||||
|
|
||||||
|
When resources exceed thresholds:
|
||||||
|
|
||||||
|
1. Worker logs the backoff reason:
|
||||||
|
```
|
||||||
|
[TaskWorker] MyWorker backing off: Memory at 87.3% (threshold: 85%)
|
||||||
|
```
|
||||||
|
|
||||||
|
2. Worker stops claiming new tasks but continues existing tasks
|
||||||
|
|
||||||
|
3. After `BACKOFF_DURATION_MS`, worker rechecks resources
|
||||||
|
|
||||||
|
4. When resources return to normal:
|
||||||
|
```
|
||||||
|
[TaskWorker] MyWorker resuming normal operation
|
||||||
|
```
|
||||||
|
|
||||||
|
### UI Display
|
||||||
|
|
||||||
|
The Workers Dashboard shows:
|
||||||
|
|
||||||
|
- **Tasks Column**: `2/3 tasks` (active/max concurrent)
|
||||||
|
- **Resources Column**: Memory % and CPU % with color coding
|
||||||
|
- Green: < 50%
|
||||||
|
- Yellow: 50-74%
|
||||||
|
- Amber: 75-89%
|
||||||
|
- Red: 90%+
|
||||||
|
- **Backing Off**: Orange warning badge when worker is in backoff state
|
||||||
|
|
||||||
|
### Task Count Badge Details
|
||||||
|
|
||||||
|
```
|
||||||
|
┌─────────────────────────────────────────────┐
|
||||||
|
│ Worker: "MyWorker" │
|
||||||
|
│ Tasks: 2/3 tasks #456, #457 │
|
||||||
|
│ Resources: 🧠 65% 💻 45% │
|
||||||
|
│ Status: ● Active │
|
||||||
|
└─────────────────────────────────────────────┘
|
||||||
|
```
|
||||||
|
|
||||||
|
### Best Practices
|
||||||
|
|
||||||
|
1. **Start Conservative**: Use `MAX_CONCURRENT_TASKS=3` initially
|
||||||
|
2. **Monitor Resources**: Watch for frequent backoffs in logs
|
||||||
|
3. **Tune Per Workload**: I/O-bound tasks benefit from higher concurrency
|
||||||
|
4. **Scale Horizontally**: Add more pods rather than cranking concurrency too high
|
||||||
|
|
||||||
|
### Code References
|
||||||
|
|
||||||
|
| File | Purpose |
|
||||||
|
|------|---------|
|
||||||
|
| `src/tasks/task-worker.ts:68-71` | Concurrency environment variables |
|
||||||
|
| `src/tasks/task-worker.ts:104-111` | ResourceStats interface |
|
||||||
|
| `src/tasks/task-worker.ts:149-179` | getResourceStats() method |
|
||||||
|
| `src/tasks/task-worker.ts:184-196` | shouldBackOff() method |
|
||||||
|
| `src/tasks/task-worker.ts:462-516` | mainLoop() with concurrent claiming |
|
||||||
|
| `src/routes/worker-registry.ts:148-195` | Heartbeat endpoint handling |
|
||||||
|
| `cannaiq/src/pages/WorkersDashboard.tsx:233-305` | UI components for resources |
|
||||||
|
|
||||||
## Monitoring
|
## Monitoring
|
||||||
|
|
||||||
### Logs
|
### Logs
|
||||||
|
|||||||
27
backend/migrations/074_worker_commands.sql
Normal file
27
backend/migrations/074_worker_commands.sql
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
-- Migration: Worker Commands Table
|
||||||
|
-- Purpose: Store commands for workers (decommission, etc.)
|
||||||
|
-- Workers poll this table after each task to check for commands
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS worker_commands (
|
||||||
|
id SERIAL PRIMARY KEY,
|
||||||
|
worker_id TEXT NOT NULL,
|
||||||
|
command TEXT NOT NULL, -- 'decommission', 'pause', 'resume'
|
||||||
|
reason TEXT,
|
||||||
|
issued_by TEXT,
|
||||||
|
issued_at TIMESTAMPTZ DEFAULT NOW(),
|
||||||
|
acknowledged_at TIMESTAMPTZ,
|
||||||
|
executed_at TIMESTAMPTZ,
|
||||||
|
status TEXT DEFAULT 'pending' -- 'pending', 'acknowledged', 'executed', 'cancelled'
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Index for worker lookups
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_worker_commands_worker_id ON worker_commands(worker_id);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_worker_commands_pending ON worker_commands(worker_id, status) WHERE status = 'pending';
|
||||||
|
|
||||||
|
-- Add decommission_requested column to worker_registry for quick checks
|
||||||
|
ALTER TABLE worker_registry ADD COLUMN IF NOT EXISTS decommission_requested BOOLEAN DEFAULT FALSE;
|
||||||
|
ALTER TABLE worker_registry ADD COLUMN IF NOT EXISTS decommission_reason TEXT;
|
||||||
|
ALTER TABLE worker_registry ADD COLUMN IF NOT EXISTS decommission_requested_at TIMESTAMPTZ;
|
||||||
|
|
||||||
|
-- Comment
|
||||||
|
COMMENT ON TABLE worker_commands IS 'Commands issued to workers (decommission after task, pause, etc.)';
|
||||||
88
backend/migrations/083_discovery_runs.sql
Normal file
88
backend/migrations/083_discovery_runs.sql
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
-- Migration 083: Discovery Run Tracking
|
||||||
|
-- Tracks progress of store discovery runs step-by-step
|
||||||
|
|
||||||
|
-- Main discovery runs table
|
||||||
|
CREATE TABLE IF NOT EXISTS discovery_runs (
|
||||||
|
id SERIAL PRIMARY KEY,
|
||||||
|
platform VARCHAR(50) NOT NULL DEFAULT 'dutchie',
|
||||||
|
status VARCHAR(20) NOT NULL DEFAULT 'running', -- running, completed, failed
|
||||||
|
started_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||||
|
finished_at TIMESTAMPTZ,
|
||||||
|
task_id INTEGER REFERENCES worker_task_queue(id),
|
||||||
|
|
||||||
|
-- Totals
|
||||||
|
states_total INTEGER DEFAULT 0,
|
||||||
|
states_completed INTEGER DEFAULT 0,
|
||||||
|
locations_discovered INTEGER DEFAULT 0,
|
||||||
|
locations_promoted INTEGER DEFAULT 0,
|
||||||
|
new_store_ids INTEGER[] DEFAULT '{}',
|
||||||
|
|
||||||
|
-- Error info
|
||||||
|
error_message TEXT,
|
||||||
|
|
||||||
|
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Per-state progress within a run
|
||||||
|
CREATE TABLE IF NOT EXISTS discovery_run_states (
|
||||||
|
id SERIAL PRIMARY KEY,
|
||||||
|
run_id INTEGER NOT NULL REFERENCES discovery_runs(id) ON DELETE CASCADE,
|
||||||
|
state_code VARCHAR(2) NOT NULL,
|
||||||
|
status VARCHAR(20) NOT NULL DEFAULT 'pending', -- pending, running, completed, failed
|
||||||
|
started_at TIMESTAMPTZ,
|
||||||
|
finished_at TIMESTAMPTZ,
|
||||||
|
|
||||||
|
-- Results
|
||||||
|
cities_found INTEGER DEFAULT 0,
|
||||||
|
locations_found INTEGER DEFAULT 0,
|
||||||
|
locations_upserted INTEGER DEFAULT 0,
|
||||||
|
new_dispensary_ids INTEGER[] DEFAULT '{}',
|
||||||
|
|
||||||
|
-- Error info
|
||||||
|
error_message TEXT,
|
||||||
|
|
||||||
|
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||||
|
|
||||||
|
UNIQUE(run_id, state_code)
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Step-by-step log for detailed progress tracking
|
||||||
|
CREATE TABLE IF NOT EXISTS discovery_run_steps (
|
||||||
|
id SERIAL PRIMARY KEY,
|
||||||
|
run_id INTEGER NOT NULL REFERENCES discovery_runs(id) ON DELETE CASCADE,
|
||||||
|
state_code VARCHAR(2),
|
||||||
|
step_name VARCHAR(100) NOT NULL,
|
||||||
|
status VARCHAR(20) NOT NULL DEFAULT 'started', -- started, completed, failed
|
||||||
|
started_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||||
|
finished_at TIMESTAMPTZ,
|
||||||
|
|
||||||
|
-- Details (JSON for flexibility)
|
||||||
|
details JSONB DEFAULT '{}',
|
||||||
|
|
||||||
|
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Indexes for querying
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_discovery_runs_status ON discovery_runs(status);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_discovery_runs_platform ON discovery_runs(platform);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_discovery_runs_started_at ON discovery_runs(started_at DESC);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_discovery_run_states_run_id ON discovery_run_states(run_id);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_discovery_run_steps_run_id ON discovery_run_steps(run_id);
|
||||||
|
|
||||||
|
-- View for latest run status per platform
|
||||||
|
CREATE OR REPLACE VIEW v_latest_discovery_runs AS
|
||||||
|
SELECT DISTINCT ON (platform)
|
||||||
|
id,
|
||||||
|
platform,
|
||||||
|
status,
|
||||||
|
started_at,
|
||||||
|
finished_at,
|
||||||
|
states_total,
|
||||||
|
states_completed,
|
||||||
|
locations_discovered,
|
||||||
|
locations_promoted,
|
||||||
|
array_length(new_store_ids, 1) as new_stores_count,
|
||||||
|
error_message,
|
||||||
|
EXTRACT(EPOCH FROM (COALESCE(finished_at, NOW()) - started_at)) as duration_seconds
|
||||||
|
FROM discovery_runs
|
||||||
|
ORDER BY platform, started_at DESC;
|
||||||
@@ -146,6 +146,7 @@ import tasksRoutes from './routes/tasks';
|
|||||||
import workerRegistryRoutes from './routes/worker-registry';
|
import workerRegistryRoutes from './routes/worker-registry';
|
||||||
// Per TASK_WORKFLOW_2024-12-10.md: Raw payload access API
|
// Per TASK_WORKFLOW_2024-12-10.md: Raw payload access API
|
||||||
import payloadsRoutes from './routes/payloads';
|
import payloadsRoutes from './routes/payloads';
|
||||||
|
import k8sRoutes from './routes/k8s';
|
||||||
|
|
||||||
// Mark requests from trusted domains (cannaiq.co, findagram.co, findadispo.com)
|
// Mark requests from trusted domains (cannaiq.co, findagram.co, findadispo.com)
|
||||||
// These domains can access the API without authentication
|
// These domains can access the API without authentication
|
||||||
@@ -230,6 +231,10 @@ console.log('[WorkerRegistry] Routes registered at /api/worker-registry');
|
|||||||
app.use('/api/payloads', payloadsRoutes);
|
app.use('/api/payloads', payloadsRoutes);
|
||||||
console.log('[Payloads] Routes registered at /api/payloads');
|
console.log('[Payloads] Routes registered at /api/payloads');
|
||||||
|
|
||||||
|
// K8s control routes - worker scaling from admin UI
|
||||||
|
app.use('/api/k8s', k8sRoutes);
|
||||||
|
console.log('[K8s] Routes registered at /api/k8s');
|
||||||
|
|
||||||
// Phase 3: Analytics V2 - Enhanced analytics with rec/med state segmentation
|
// Phase 3: Analytics V2 - Enhanced analytics with rec/med state segmentation
|
||||||
try {
|
try {
|
||||||
const analyticsV2Router = createAnalyticsV2Router(getPool());
|
const analyticsV2Router = createAnalyticsV2Router(getPool());
|
||||||
|
|||||||
@@ -47,4 +47,27 @@ router.post('/refresh', authMiddleware, async (req: AuthRequest, res) => {
|
|||||||
res.json({ token });
|
res.json({ token });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Verify password for sensitive actions (requires current user to be authenticated)
|
||||||
|
router.post('/verify-password', authMiddleware, async (req: AuthRequest, res) => {
|
||||||
|
try {
|
||||||
|
const { password } = req.body;
|
||||||
|
|
||||||
|
if (!password) {
|
||||||
|
return res.status(400).json({ error: 'Password required' });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Re-authenticate the current user with the provided password
|
||||||
|
const user = await authenticateUser(req.user!.email, password);
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
return res.status(401).json({ error: 'Invalid password', verified: false });
|
||||||
|
}
|
||||||
|
|
||||||
|
res.json({ verified: true });
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Password verification error:', error);
|
||||||
|
res.status(500).json({ error: 'Internal server error' });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
export default router;
|
export default router;
|
||||||
|
|||||||
140
backend/src/routes/k8s.ts
Normal file
140
backend/src/routes/k8s.ts
Normal file
@@ -0,0 +1,140 @@
|
|||||||
|
/**
|
||||||
|
* Kubernetes Control Routes
|
||||||
|
*
|
||||||
|
* Provides admin UI control over k8s resources like worker scaling.
|
||||||
|
* Uses in-cluster config when running in k8s, or kubeconfig locally.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { Router, Request, Response } from 'express';
|
||||||
|
import * as k8s from '@kubernetes/client-node';
|
||||||
|
|
||||||
|
const router = Router();
|
||||||
|
|
||||||
|
// K8s client setup - lazy initialization
|
||||||
|
let appsApi: k8s.AppsV1Api | null = null;
|
||||||
|
let k8sError: string | null = null;
|
||||||
|
|
||||||
|
function getK8sClient(): k8s.AppsV1Api | null {
|
||||||
|
if (appsApi) return appsApi;
|
||||||
|
if (k8sError) return null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const kc = new k8s.KubeConfig();
|
||||||
|
|
||||||
|
// Try in-cluster config first (when running in k8s)
|
||||||
|
try {
|
||||||
|
kc.loadFromCluster();
|
||||||
|
console.log('[K8s] Loaded in-cluster config');
|
||||||
|
} catch {
|
||||||
|
// Fall back to default kubeconfig (local dev)
|
||||||
|
try {
|
||||||
|
kc.loadFromDefault();
|
||||||
|
console.log('[K8s] Loaded default kubeconfig');
|
||||||
|
} catch (e) {
|
||||||
|
k8sError = 'No k8s config available';
|
||||||
|
console.log('[K8s] No config available - k8s routes disabled');
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
appsApi = kc.makeApiClient(k8s.AppsV1Api);
|
||||||
|
return appsApi;
|
||||||
|
} catch (e: any) {
|
||||||
|
k8sError = e.message;
|
||||||
|
console.error('[K8s] Failed to initialize client:', e.message);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const NAMESPACE = process.env.K8S_NAMESPACE || 'dispensary-scraper';
|
||||||
|
const WORKER_DEPLOYMENT = 'scraper-worker';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* GET /api/k8s/workers
|
||||||
|
* Get current worker deployment status
|
||||||
|
*/
|
||||||
|
router.get('/workers', async (_req: Request, res: Response) => {
|
||||||
|
const client = getK8sClient();
|
||||||
|
|
||||||
|
if (!client) {
|
||||||
|
return res.json({
|
||||||
|
success: true,
|
||||||
|
available: false,
|
||||||
|
error: k8sError || 'K8s not available',
|
||||||
|
replicas: 0,
|
||||||
|
readyReplicas: 0,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const deployment = await client.readNamespacedDeployment({
|
||||||
|
name: WORKER_DEPLOYMENT,
|
||||||
|
namespace: NAMESPACE,
|
||||||
|
});
|
||||||
|
|
||||||
|
res.json({
|
||||||
|
success: true,
|
||||||
|
available: true,
|
||||||
|
replicas: deployment.spec?.replicas || 0,
|
||||||
|
readyReplicas: deployment.status?.readyReplicas || 0,
|
||||||
|
availableReplicas: deployment.status?.availableReplicas || 0,
|
||||||
|
updatedReplicas: deployment.status?.updatedReplicas || 0,
|
||||||
|
});
|
||||||
|
} catch (e: any) {
|
||||||
|
console.error('[K8s] Error getting deployment:', e.message);
|
||||||
|
res.status(500).json({
|
||||||
|
success: false,
|
||||||
|
error: e.message,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* POST /api/k8s/workers/scale
|
||||||
|
* Scale worker deployment
|
||||||
|
* Body: { replicas: number }
|
||||||
|
*/
|
||||||
|
router.post('/workers/scale', async (req: Request, res: Response) => {
|
||||||
|
const client = getK8sClient();
|
||||||
|
|
||||||
|
if (!client) {
|
||||||
|
return res.status(503).json({
|
||||||
|
success: false,
|
||||||
|
error: k8sError || 'K8s not available',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const { replicas } = req.body;
|
||||||
|
|
||||||
|
if (typeof replicas !== 'number' || replicas < 0 || replicas > 50) {
|
||||||
|
return res.status(400).json({
|
||||||
|
success: false,
|
||||||
|
error: 'replicas must be a number between 0 and 50',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Patch the deployment to set replicas
|
||||||
|
await client.patchNamespacedDeploymentScale({
|
||||||
|
name: WORKER_DEPLOYMENT,
|
||||||
|
namespace: NAMESPACE,
|
||||||
|
body: { spec: { replicas } },
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log(`[K8s] Scaled ${WORKER_DEPLOYMENT} to ${replicas} replicas`);
|
||||||
|
|
||||||
|
res.json({
|
||||||
|
success: true,
|
||||||
|
replicas,
|
||||||
|
message: `Scaled to ${replicas} workers`,
|
||||||
|
});
|
||||||
|
} catch (e: any) {
|
||||||
|
console.error('[K8s] Error scaling deployment:', e.message);
|
||||||
|
res.status(500).json({
|
||||||
|
success: false,
|
||||||
|
error: e.message,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
export default router;
|
||||||
@@ -291,6 +291,107 @@ router.get('/stores/:id/summary', async (req: Request, res: Response) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* GET /api/markets/stores/:id/crawl-history
|
||||||
|
* Get crawl history for a specific store
|
||||||
|
*/
|
||||||
|
router.get('/stores/:id/crawl-history', async (req: Request, res: Response) => {
|
||||||
|
try {
|
||||||
|
const { id } = req.params;
|
||||||
|
const { limit = '50' } = req.query;
|
||||||
|
const dispensaryId = parseInt(id, 10);
|
||||||
|
const limitNum = Math.min(parseInt(limit as string, 10), 100);
|
||||||
|
|
||||||
|
// Get crawl history from crawl_orchestration_traces
|
||||||
|
const { rows: historyRows } = await pool.query(`
|
||||||
|
SELECT
|
||||||
|
id,
|
||||||
|
run_id,
|
||||||
|
profile_key,
|
||||||
|
crawler_module,
|
||||||
|
state_at_start,
|
||||||
|
state_at_end,
|
||||||
|
total_steps,
|
||||||
|
duration_ms,
|
||||||
|
success,
|
||||||
|
error_message,
|
||||||
|
products_found,
|
||||||
|
started_at,
|
||||||
|
completed_at
|
||||||
|
FROM crawl_orchestration_traces
|
||||||
|
WHERE dispensary_id = $1
|
||||||
|
ORDER BY started_at DESC
|
||||||
|
LIMIT $2
|
||||||
|
`, [dispensaryId, limitNum]);
|
||||||
|
|
||||||
|
// Get next scheduled crawl if available
|
||||||
|
const { rows: scheduleRows } = await pool.query(`
|
||||||
|
SELECT
|
||||||
|
js.id as schedule_id,
|
||||||
|
js.job_name,
|
||||||
|
js.enabled,
|
||||||
|
js.base_interval_minutes,
|
||||||
|
js.jitter_minutes,
|
||||||
|
js.next_run_at,
|
||||||
|
js.last_run_at,
|
||||||
|
js.last_status
|
||||||
|
FROM job_schedules js
|
||||||
|
WHERE js.enabled = true
|
||||||
|
AND js.job_config->>'dispensaryId' = $1::text
|
||||||
|
ORDER BY js.next_run_at
|
||||||
|
LIMIT 1
|
||||||
|
`, [dispensaryId.toString()]);
|
||||||
|
|
||||||
|
// Get dispensary info for slug
|
||||||
|
const { rows: dispRows } = await pool.query(`
|
||||||
|
SELECT
|
||||||
|
id,
|
||||||
|
name,
|
||||||
|
dba_name,
|
||||||
|
slug,
|
||||||
|
state,
|
||||||
|
city,
|
||||||
|
menu_type,
|
||||||
|
platform_dispensary_id,
|
||||||
|
last_menu_scrape
|
||||||
|
FROM dispensaries
|
||||||
|
WHERE id = $1
|
||||||
|
`, [dispensaryId]);
|
||||||
|
|
||||||
|
res.json({
|
||||||
|
dispensary: dispRows[0] || null,
|
||||||
|
history: historyRows.map(row => ({
|
||||||
|
id: row.id,
|
||||||
|
runId: row.run_id,
|
||||||
|
profileKey: row.profile_key,
|
||||||
|
crawlerModule: row.crawler_module,
|
||||||
|
stateAtStart: row.state_at_start,
|
||||||
|
stateAtEnd: row.state_at_end,
|
||||||
|
totalSteps: row.total_steps,
|
||||||
|
durationMs: row.duration_ms,
|
||||||
|
success: row.success,
|
||||||
|
errorMessage: row.error_message,
|
||||||
|
productsFound: row.products_found,
|
||||||
|
startedAt: row.started_at?.toISOString() || null,
|
||||||
|
completedAt: row.completed_at?.toISOString() || null,
|
||||||
|
})),
|
||||||
|
nextSchedule: scheduleRows[0] ? {
|
||||||
|
scheduleId: scheduleRows[0].schedule_id,
|
||||||
|
jobName: scheduleRows[0].job_name,
|
||||||
|
enabled: scheduleRows[0].enabled,
|
||||||
|
baseIntervalMinutes: scheduleRows[0].base_interval_minutes,
|
||||||
|
jitterMinutes: scheduleRows[0].jitter_minutes,
|
||||||
|
nextRunAt: scheduleRows[0].next_run_at?.toISOString() || null,
|
||||||
|
lastRunAt: scheduleRows[0].last_run_at?.toISOString() || null,
|
||||||
|
lastStatus: scheduleRows[0].last_status,
|
||||||
|
} : null,
|
||||||
|
});
|
||||||
|
} catch (error: any) {
|
||||||
|
console.error('[Markets] Error fetching crawl history:', error.message);
|
||||||
|
res.status(500).json({ error: error.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* GET /api/markets/stores/:id/products
|
* GET /api/markets/stores/:id/products
|
||||||
* Get products for a store with filtering and pagination
|
* Get products for a store with filtering and pagination
|
||||||
|
|||||||
@@ -70,21 +70,20 @@ router.post('/register', async (req: Request, res: Response) => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (existing.rows.length > 0) {
|
if (existing.rows.length > 0) {
|
||||||
// Re-activate existing worker
|
// Re-activate existing worker - keep existing pod_name (fantasy name), don't overwrite with K8s name
|
||||||
const { rows } = await pool.query(`
|
const { rows } = await pool.query(`
|
||||||
UPDATE worker_registry
|
UPDATE worker_registry
|
||||||
SET status = 'active',
|
SET status = 'active',
|
||||||
role = $1,
|
role = $1,
|
||||||
pod_name = $2,
|
hostname = $2,
|
||||||
hostname = $3,
|
ip_address = $3,
|
||||||
ip_address = $4,
|
|
||||||
last_heartbeat_at = NOW(),
|
last_heartbeat_at = NOW(),
|
||||||
started_at = NOW(),
|
started_at = NOW(),
|
||||||
metadata = $5,
|
metadata = $4,
|
||||||
updated_at = NOW()
|
updated_at = NOW()
|
||||||
WHERE worker_id = $6
|
WHERE worker_id = $5
|
||||||
RETURNING id, worker_id, friendly_name, role
|
RETURNING id, worker_id, friendly_name, pod_name, role
|
||||||
`, [role, pod_name, finalHostname, clientIp, metadata, finalWorkerId]);
|
`, [role, finalHostname, clientIp, metadata, finalWorkerId]);
|
||||||
|
|
||||||
const worker = rows[0];
|
const worker = rows[0];
|
||||||
const roleMsg = role ? `for ${role}` : 'as role-agnostic';
|
const roleMsg = role ? `for ${role}` : 'as role-agnostic';
|
||||||
@@ -105,13 +104,13 @@ router.post('/register', async (req: Request, res: Response) => {
|
|||||||
const nameResult = await pool.query('SELECT assign_worker_name($1) as name', [finalWorkerId]);
|
const nameResult = await pool.query('SELECT assign_worker_name($1) as name', [finalWorkerId]);
|
||||||
const friendlyName = nameResult.rows[0].name;
|
const friendlyName = nameResult.rows[0].name;
|
||||||
|
|
||||||
// Register the worker
|
// Register the worker - use friendlyName as pod_name (not K8s name)
|
||||||
const { rows } = await pool.query(`
|
const { rows } = await pool.query(`
|
||||||
INSERT INTO worker_registry (
|
INSERT INTO worker_registry (
|
||||||
worker_id, friendly_name, role, pod_name, hostname, ip_address, status, metadata
|
worker_id, friendly_name, role, pod_name, hostname, ip_address, status, metadata
|
||||||
) VALUES ($1, $2, $3, $4, $5, $6, 'active', $7)
|
) VALUES ($1, $2, $3, $4, $5, $6, 'active', $7)
|
||||||
RETURNING id, worker_id, friendly_name, role
|
RETURNING id, worker_id, friendly_name, pod_name, role
|
||||||
`, [finalWorkerId, friendlyName, role, pod_name, finalHostname, clientIp, metadata]);
|
`, [finalWorkerId, friendlyName, role, friendlyName, finalHostname, clientIp, metadata]);
|
||||||
|
|
||||||
const worker = rows[0];
|
const worker = rows[0];
|
||||||
const roleMsg = role ? `for ${role}` : 'as role-agnostic';
|
const roleMsg = role ? `for ${role}` : 'as role-agnostic';
|
||||||
@@ -138,17 +137,36 @@ router.post('/register', async (req: Request, res: Response) => {
|
|||||||
*
|
*
|
||||||
* Body:
|
* Body:
|
||||||
* - worker_id: string (required)
|
* - worker_id: string (required)
|
||||||
* - current_task_id: number (optional) - task currently being processed
|
* - current_task_id: number (optional) - task currently being processed (primary task)
|
||||||
|
* - current_task_ids: number[] (optional) - all tasks currently being processed (concurrent)
|
||||||
|
* - active_task_count: number (optional) - number of tasks currently running
|
||||||
|
* - max_concurrent_tasks: number (optional) - max concurrent tasks this worker can handle
|
||||||
* - status: string (optional) - 'active', 'idle'
|
* - status: string (optional) - 'active', 'idle'
|
||||||
|
* - resources: object (optional) - memory_mb, cpu_user_ms, cpu_system_ms, etc.
|
||||||
*/
|
*/
|
||||||
router.post('/heartbeat', async (req: Request, res: Response) => {
|
router.post('/heartbeat', async (req: Request, res: Response) => {
|
||||||
try {
|
try {
|
||||||
const { worker_id, current_task_id, status = 'active', resources } = req.body;
|
const {
|
||||||
|
worker_id,
|
||||||
|
current_task_id,
|
||||||
|
current_task_ids,
|
||||||
|
active_task_count,
|
||||||
|
max_concurrent_tasks,
|
||||||
|
status = 'active',
|
||||||
|
resources
|
||||||
|
} = req.body;
|
||||||
|
|
||||||
if (!worker_id) {
|
if (!worker_id) {
|
||||||
return res.status(400).json({ success: false, error: 'worker_id is required' });
|
return res.status(400).json({ success: false, error: 'worker_id is required' });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Build metadata object with all the new fields
|
||||||
|
const metadata: Record<string, unknown> = {};
|
||||||
|
if (resources) Object.assign(metadata, resources);
|
||||||
|
if (current_task_ids) metadata.current_task_ids = current_task_ids;
|
||||||
|
if (active_task_count !== undefined) metadata.active_task_count = active_task_count;
|
||||||
|
if (max_concurrent_tasks !== undefined) metadata.max_concurrent_tasks = max_concurrent_tasks;
|
||||||
|
|
||||||
// Store resources in metadata jsonb column
|
// Store resources in metadata jsonb column
|
||||||
const { rows } = await pool.query(`
|
const { rows } = await pool.query(`
|
||||||
UPDATE worker_registry
|
UPDATE worker_registry
|
||||||
@@ -159,7 +177,7 @@ router.post('/heartbeat', async (req: Request, res: Response) => {
|
|||||||
updated_at = NOW()
|
updated_at = NOW()
|
||||||
WHERE worker_id = $3
|
WHERE worker_id = $3
|
||||||
RETURNING id, friendly_name, status
|
RETURNING id, friendly_name, status
|
||||||
`, [current_task_id || null, status, worker_id, resources ? JSON.stringify(resources) : null]);
|
`, [current_task_id || null, status, worker_id, Object.keys(metadata).length > 0 ? JSON.stringify(metadata) : null]);
|
||||||
|
|
||||||
if (rows.length === 0) {
|
if (rows.length === 0) {
|
||||||
return res.status(404).json({ success: false, error: 'Worker not found - please register first' });
|
return res.status(404).json({ success: false, error: 'Worker not found - please register first' });
|
||||||
@@ -330,12 +348,21 @@ router.get('/workers', async (req: Request, res: Response) => {
|
|||||||
tasks_completed,
|
tasks_completed,
|
||||||
tasks_failed,
|
tasks_failed,
|
||||||
current_task_id,
|
current_task_id,
|
||||||
|
-- Concurrent task fields from metadata
|
||||||
|
(metadata->>'current_task_ids')::jsonb as current_task_ids,
|
||||||
|
(metadata->>'active_task_count')::int as active_task_count,
|
||||||
|
(metadata->>'max_concurrent_tasks')::int as max_concurrent_tasks,
|
||||||
|
-- Decommission fields
|
||||||
|
COALESCE(decommission_requested, false) as decommission_requested,
|
||||||
|
decommission_reason,
|
||||||
|
-- Full metadata for resources
|
||||||
metadata,
|
metadata,
|
||||||
EXTRACT(EPOCH FROM (NOW() - last_heartbeat_at)) as seconds_since_heartbeat,
|
EXTRACT(EPOCH FROM (NOW() - last_heartbeat_at)) as seconds_since_heartbeat,
|
||||||
CASE
|
CASE
|
||||||
WHEN status = 'offline' OR status = 'terminated' THEN status
|
WHEN status = 'offline' OR status = 'terminated' THEN status
|
||||||
WHEN last_heartbeat_at < NOW() - INTERVAL '2 minutes' THEN 'stale'
|
WHEN last_heartbeat_at < NOW() - INTERVAL '2 minutes' THEN 'stale'
|
||||||
WHEN current_task_id IS NOT NULL THEN 'busy'
|
WHEN current_task_id IS NOT NULL THEN 'busy'
|
||||||
|
WHEN (metadata->>'active_task_count')::int > 0 THEN 'busy'
|
||||||
ELSE 'ready'
|
ELSE 'ready'
|
||||||
END as health_status,
|
END as health_status,
|
||||||
created_at
|
created_at
|
||||||
@@ -672,4 +699,163 @@ router.get('/capacity', async (_req: Request, res: Response) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// ============================================================
|
||||||
|
// WORKER LIFECYCLE MANAGEMENT
|
||||||
|
// ============================================================
|
||||||
|
|
||||||
|
/**
|
||||||
|
* POST /api/worker-registry/workers/:workerId/decommission
|
||||||
|
* Request graceful decommission of a worker (will stop after current task)
|
||||||
|
*/
|
||||||
|
router.post('/workers/:workerId/decommission', async (req: Request, res: Response) => {
|
||||||
|
try {
|
||||||
|
const { workerId } = req.params;
|
||||||
|
const { reason, issued_by } = req.body;
|
||||||
|
|
||||||
|
// Update worker_registry to flag for decommission
|
||||||
|
const result = await pool.query(
|
||||||
|
`UPDATE worker_registry
|
||||||
|
SET decommission_requested = true,
|
||||||
|
decommission_reason = $2,
|
||||||
|
decommission_requested_at = NOW()
|
||||||
|
WHERE worker_id = $1
|
||||||
|
RETURNING friendly_name, status, current_task_id`,
|
||||||
|
[workerId, reason || 'Manual decommission from admin']
|
||||||
|
);
|
||||||
|
|
||||||
|
if (result.rows.length === 0) {
|
||||||
|
return res.status(404).json({ success: false, error: 'Worker not found' });
|
||||||
|
}
|
||||||
|
|
||||||
|
const worker = result.rows[0];
|
||||||
|
|
||||||
|
// Also log to worker_commands for audit trail
|
||||||
|
await pool.query(
|
||||||
|
`INSERT INTO worker_commands (worker_id, command, reason, issued_by)
|
||||||
|
VALUES ($1, 'decommission', $2, $3)
|
||||||
|
ON CONFLICT DO NOTHING`,
|
||||||
|
[workerId, reason || 'Manual decommission', issued_by || 'admin']
|
||||||
|
).catch(() => {
|
||||||
|
// Table might not exist yet - ignore
|
||||||
|
});
|
||||||
|
|
||||||
|
res.json({
|
||||||
|
success: true,
|
||||||
|
message: worker.current_task_id
|
||||||
|
? `Worker ${worker.friendly_name} will stop after completing task #${worker.current_task_id}`
|
||||||
|
: `Worker ${worker.friendly_name} will stop on next poll`,
|
||||||
|
worker: {
|
||||||
|
friendly_name: worker.friendly_name,
|
||||||
|
status: worker.status,
|
||||||
|
current_task_id: worker.current_task_id,
|
||||||
|
decommission_requested: true
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (error: any) {
|
||||||
|
res.status(500).json({ success: false, error: error.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* POST /api/worker-registry/workers/:workerId/cancel-decommission
|
||||||
|
* Cancel a pending decommission request
|
||||||
|
*/
|
||||||
|
router.post('/workers/:workerId/cancel-decommission', async (req: Request, res: Response) => {
|
||||||
|
try {
|
||||||
|
const { workerId } = req.params;
|
||||||
|
|
||||||
|
const result = await pool.query(
|
||||||
|
`UPDATE worker_registry
|
||||||
|
SET decommission_requested = false,
|
||||||
|
decommission_reason = NULL,
|
||||||
|
decommission_requested_at = NULL
|
||||||
|
WHERE worker_id = $1
|
||||||
|
RETURNING friendly_name`,
|
||||||
|
[workerId]
|
||||||
|
);
|
||||||
|
|
||||||
|
if (result.rows.length === 0) {
|
||||||
|
return res.status(404).json({ success: false, error: 'Worker not found' });
|
||||||
|
}
|
||||||
|
|
||||||
|
res.json({
|
||||||
|
success: true,
|
||||||
|
message: `Decommission cancelled for ${result.rows[0].friendly_name}`
|
||||||
|
});
|
||||||
|
} catch (error: any) {
|
||||||
|
res.status(500).json({ success: false, error: error.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* POST /api/worker-registry/spawn
|
||||||
|
* Spawn a new worker in the current pod (only works in multi-worker-per-pod mode)
|
||||||
|
* For now, this is a placeholder - actual spawning requires the pod supervisor
|
||||||
|
*/
|
||||||
|
router.post('/spawn', async (req: Request, res: Response) => {
|
||||||
|
try {
|
||||||
|
const { pod_name, role } = req.body;
|
||||||
|
|
||||||
|
// For now, we can't actually spawn workers from the API
|
||||||
|
// This would require a supervisor process in each pod that listens for spawn commands
|
||||||
|
// Instead, return instructions for how to scale
|
||||||
|
res.json({
|
||||||
|
success: false,
|
||||||
|
error: 'Direct worker spawning not yet implemented',
|
||||||
|
instructions: 'To add workers, scale the K8s deployment: kubectl scale deployment/scraper-worker --replicas=N'
|
||||||
|
});
|
||||||
|
} catch (error: any) {
|
||||||
|
res.status(500).json({ success: false, error: error.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* GET /api/worker-registry/pods
|
||||||
|
* Get workers grouped by pod
|
||||||
|
*/
|
||||||
|
router.get('/pods', async (_req: Request, res: Response) => {
|
||||||
|
try {
|
||||||
|
const { rows } = await pool.query(`
|
||||||
|
SELECT
|
||||||
|
COALESCE(pod_name, 'Unknown') as pod_name,
|
||||||
|
COUNT(*) as worker_count,
|
||||||
|
COUNT(*) FILTER (WHERE current_task_id IS NOT NULL) as busy_count,
|
||||||
|
COUNT(*) FILTER (WHERE current_task_id IS NULL) as idle_count,
|
||||||
|
SUM(tasks_completed) as total_completed,
|
||||||
|
SUM(tasks_failed) as total_failed,
|
||||||
|
SUM((metadata->>'memory_rss_mb')::int) as total_memory_mb,
|
||||||
|
array_agg(json_build_object(
|
||||||
|
'worker_id', worker_id,
|
||||||
|
'friendly_name', friendly_name,
|
||||||
|
'status', status,
|
||||||
|
'current_task_id', current_task_id,
|
||||||
|
'tasks_completed', tasks_completed,
|
||||||
|
'tasks_failed', tasks_failed,
|
||||||
|
'decommission_requested', COALESCE(decommission_requested, false),
|
||||||
|
'last_heartbeat_at', last_heartbeat_at
|
||||||
|
)) as workers
|
||||||
|
FROM worker_registry
|
||||||
|
WHERE status NOT IN ('offline', 'terminated')
|
||||||
|
GROUP BY pod_name
|
||||||
|
ORDER BY pod_name
|
||||||
|
`);
|
||||||
|
|
||||||
|
res.json({
|
||||||
|
success: true,
|
||||||
|
pods: rows.map(row => ({
|
||||||
|
pod_name: row.pod_name,
|
||||||
|
worker_count: parseInt(row.worker_count),
|
||||||
|
busy_count: parseInt(row.busy_count),
|
||||||
|
idle_count: parseInt(row.idle_count),
|
||||||
|
total_completed: parseInt(row.total_completed) || 0,
|
||||||
|
total_failed: parseInt(row.total_failed) || 0,
|
||||||
|
total_memory_mb: parseInt(row.total_memory_mb) || 0,
|
||||||
|
workers: row.workers
|
||||||
|
}))
|
||||||
|
});
|
||||||
|
} catch (error: any) {
|
||||||
|
res.status(500).json({ success: false, error: error.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
export default router;
|
export default router;
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ const router = Router();
|
|||||||
// ============================================================
|
// ============================================================
|
||||||
|
|
||||||
const K8S_NAMESPACE = process.env.K8S_NAMESPACE || 'dispensary-scraper';
|
const K8S_NAMESPACE = process.env.K8S_NAMESPACE || 'dispensary-scraper';
|
||||||
const K8S_STATEFULSET_NAME = process.env.K8S_WORKER_STATEFULSET || 'scraper-worker';
|
const K8S_DEPLOYMENT_NAME = process.env.K8S_WORKER_DEPLOYMENT || 'scraper-worker';
|
||||||
|
|
||||||
// Initialize K8s client - uses in-cluster config when running in K8s,
|
// Initialize K8s client - uses in-cluster config when running in K8s,
|
||||||
// or kubeconfig when running locally
|
// or kubeconfig when running locally
|
||||||
@@ -70,7 +70,7 @@ function getK8sClient(): k8s.AppsV1Api | null {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* GET /api/workers/k8s/replicas - Get current worker replica count
|
* GET /api/workers/k8s/replicas - Get current worker replica count
|
||||||
* Returns current and desired replica counts from the StatefulSet
|
* Returns current and desired replica counts from the Deployment
|
||||||
*/
|
*/
|
||||||
router.get('/k8s/replicas', async (_req: Request, res: Response) => {
|
router.get('/k8s/replicas', async (_req: Request, res: Response) => {
|
||||||
const client = getK8sClient();
|
const client = getK8sClient();
|
||||||
@@ -84,21 +84,21 @@ router.get('/k8s/replicas', async (_req: Request, res: Response) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await client.readNamespacedStatefulSet({
|
const response = await client.readNamespacedDeployment({
|
||||||
name: K8S_STATEFULSET_NAME,
|
name: K8S_DEPLOYMENT_NAME,
|
||||||
namespace: K8S_NAMESPACE,
|
namespace: K8S_NAMESPACE,
|
||||||
});
|
});
|
||||||
|
|
||||||
const statefulSet = response;
|
const deployment = response;
|
||||||
res.json({
|
res.json({
|
||||||
success: true,
|
success: true,
|
||||||
replicas: {
|
replicas: {
|
||||||
current: statefulSet.status?.readyReplicas || 0,
|
current: deployment.status?.readyReplicas || 0,
|
||||||
desired: statefulSet.spec?.replicas || 0,
|
desired: deployment.spec?.replicas || 0,
|
||||||
available: statefulSet.status?.availableReplicas || 0,
|
available: deployment.status?.availableReplicas || 0,
|
||||||
updated: statefulSet.status?.updatedReplicas || 0,
|
updated: deployment.status?.updatedReplicas || 0,
|
||||||
},
|
},
|
||||||
statefulset: K8S_STATEFULSET_NAME,
|
deployment: K8S_DEPLOYMENT_NAME,
|
||||||
namespace: K8S_NAMESPACE,
|
namespace: K8S_NAMESPACE,
|
||||||
});
|
});
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
@@ -112,7 +112,7 @@ router.get('/k8s/replicas', async (_req: Request, res: Response) => {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* POST /api/workers/k8s/scale - Scale worker replicas
|
* POST /api/workers/k8s/scale - Scale worker replicas
|
||||||
* Body: { replicas: number } - desired replica count (1-20)
|
* Body: { replicas: number } - desired replica count (0-20)
|
||||||
*/
|
*/
|
||||||
router.post('/k8s/scale', async (req: Request, res: Response) => {
|
router.post('/k8s/scale', async (req: Request, res: Response) => {
|
||||||
const client = getK8sClient();
|
const client = getK8sClient();
|
||||||
@@ -136,21 +136,21 @@ router.post('/k8s/scale', async (req: Request, res: Response) => {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
// Get current state first
|
// Get current state first
|
||||||
const currentResponse = await client.readNamespacedStatefulSetScale({
|
const currentResponse = await client.readNamespacedDeploymentScale({
|
||||||
name: K8S_STATEFULSET_NAME,
|
name: K8S_DEPLOYMENT_NAME,
|
||||||
namespace: K8S_NAMESPACE,
|
namespace: K8S_NAMESPACE,
|
||||||
});
|
});
|
||||||
const currentReplicas = currentResponse.spec?.replicas || 0;
|
const currentReplicas = currentResponse.spec?.replicas || 0;
|
||||||
|
|
||||||
// Update scale using replaceNamespacedStatefulSetScale
|
// Update scale using replaceNamespacedDeploymentScale
|
||||||
await client.replaceNamespacedStatefulSetScale({
|
await client.replaceNamespacedDeploymentScale({
|
||||||
name: K8S_STATEFULSET_NAME,
|
name: K8S_DEPLOYMENT_NAME,
|
||||||
namespace: K8S_NAMESPACE,
|
namespace: K8S_NAMESPACE,
|
||||||
body: {
|
body: {
|
||||||
apiVersion: 'autoscaling/v1',
|
apiVersion: 'autoscaling/v1',
|
||||||
kind: 'Scale',
|
kind: 'Scale',
|
||||||
metadata: {
|
metadata: {
|
||||||
name: K8S_STATEFULSET_NAME,
|
name: K8S_DEPLOYMENT_NAME,
|
||||||
namespace: K8S_NAMESPACE,
|
namespace: K8S_NAMESPACE,
|
||||||
},
|
},
|
||||||
spec: {
|
spec: {
|
||||||
@@ -159,14 +159,14 @@ router.post('/k8s/scale', async (req: Request, res: Response) => {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log(`[Workers] Scaled ${K8S_STATEFULSET_NAME} from ${currentReplicas} to ${replicas} replicas`);
|
console.log(`[Workers] Scaled ${K8S_DEPLOYMENT_NAME} from ${currentReplicas} to ${replicas} replicas`);
|
||||||
|
|
||||||
res.json({
|
res.json({
|
||||||
success: true,
|
success: true,
|
||||||
message: `Scaled from ${currentReplicas} to ${replicas} replicas`,
|
message: `Scaled from ${currentReplicas} to ${replicas} replicas`,
|
||||||
previous: currentReplicas,
|
previous: currentReplicas,
|
||||||
desired: replicas,
|
desired: replicas,
|
||||||
statefulset: K8S_STATEFULSET_NAME,
|
deployment: K8S_DEPLOYMENT_NAME,
|
||||||
namespace: K8S_NAMESPACE,
|
namespace: K8S_NAMESPACE,
|
||||||
});
|
});
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
@@ -178,6 +178,73 @@ router.post('/k8s/scale', async (req: Request, res: Response) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* POST /api/workers/k8s/scale-up - Scale up worker replicas by 1
|
||||||
|
* Convenience endpoint for adding a single worker
|
||||||
|
*/
|
||||||
|
router.post('/k8s/scale-up', async (_req: Request, res: Response) => {
|
||||||
|
const client = getK8sClient();
|
||||||
|
|
||||||
|
if (!client) {
|
||||||
|
return res.status(503).json({
|
||||||
|
success: false,
|
||||||
|
error: 'K8s client not available (not running in cluster or no kubeconfig)',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Get current replica count
|
||||||
|
const currentResponse = await client.readNamespacedDeploymentScale({
|
||||||
|
name: K8S_DEPLOYMENT_NAME,
|
||||||
|
namespace: K8S_NAMESPACE,
|
||||||
|
});
|
||||||
|
const currentReplicas = currentResponse.spec?.replicas || 0;
|
||||||
|
const newReplicas = currentReplicas + 1;
|
||||||
|
|
||||||
|
// Cap at 20 replicas
|
||||||
|
if (newReplicas > 20) {
|
||||||
|
return res.status(400).json({
|
||||||
|
success: false,
|
||||||
|
error: 'Maximum replica count (20) reached',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Scale up by 1
|
||||||
|
await client.replaceNamespacedDeploymentScale({
|
||||||
|
name: K8S_DEPLOYMENT_NAME,
|
||||||
|
namespace: K8S_NAMESPACE,
|
||||||
|
body: {
|
||||||
|
apiVersion: 'autoscaling/v1',
|
||||||
|
kind: 'Scale',
|
||||||
|
metadata: {
|
||||||
|
name: K8S_DEPLOYMENT_NAME,
|
||||||
|
namespace: K8S_NAMESPACE,
|
||||||
|
},
|
||||||
|
spec: {
|
||||||
|
replicas: newReplicas,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log(`[Workers] Scaled up ${K8S_DEPLOYMENT_NAME} from ${currentReplicas} to ${newReplicas} replicas`);
|
||||||
|
|
||||||
|
res.json({
|
||||||
|
success: true,
|
||||||
|
message: `Added worker (${currentReplicas} → ${newReplicas} replicas)`,
|
||||||
|
previous: currentReplicas,
|
||||||
|
desired: newReplicas,
|
||||||
|
deployment: K8S_DEPLOYMENT_NAME,
|
||||||
|
namespace: K8S_NAMESPACE,
|
||||||
|
});
|
||||||
|
} catch (err: any) {
|
||||||
|
console.error('[Workers] K8s scale-up error:', err.body?.message || err.message);
|
||||||
|
res.status(500).json({
|
||||||
|
success: false,
|
||||||
|
error: err.body?.message || err.message,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// ============================================================
|
// ============================================================
|
||||||
// STATIC ROUTES (must come before parameterized routes)
|
// STATIC ROUTES (must come before parameterized routes)
|
||||||
// ============================================================
|
// ============================================================
|
||||||
|
|||||||
@@ -9,3 +9,4 @@ export { handleProductDiscovery } from './product-discovery';
|
|||||||
export { handleStoreDiscovery } from './store-discovery';
|
export { handleStoreDiscovery } from './store-discovery';
|
||||||
export { handleEntryPointDiscovery } from './entry-point-discovery';
|
export { handleEntryPointDiscovery } from './entry-point-discovery';
|
||||||
export { handleAnalyticsRefresh } from './analytics-refresh';
|
export { handleAnalyticsRefresh } from './analytics-refresh';
|
||||||
|
export { handleProxyTest } from './proxy-test';
|
||||||
|
|||||||
51
backend/src/tasks/handlers/proxy-test.ts
Normal file
51
backend/src/tasks/handlers/proxy-test.ts
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
/**
|
||||||
|
* Proxy Test Handler
|
||||||
|
* Tests proxy connectivity by fetching public IP via ipify
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { TaskContext, TaskResult } from '../task-worker';
|
||||||
|
import { execSync } from 'child_process';
|
||||||
|
|
||||||
|
export async function handleProxyTest(ctx: TaskContext): Promise<TaskResult> {
|
||||||
|
const { pool } = ctx;
|
||||||
|
|
||||||
|
console.log('[ProxyTest] Testing proxy connection...');
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Get active proxy from DB
|
||||||
|
const proxyResult = await pool.query(`
|
||||||
|
SELECT host, port, username, password
|
||||||
|
FROM proxies
|
||||||
|
WHERE is_active = true
|
||||||
|
LIMIT 1
|
||||||
|
`);
|
||||||
|
|
||||||
|
if (proxyResult.rows.length === 0) {
|
||||||
|
return { success: false, error: 'No active proxy configured' };
|
||||||
|
}
|
||||||
|
|
||||||
|
const p = proxyResult.rows[0];
|
||||||
|
const proxyUrl = p.username
|
||||||
|
? `http://${p.username}:${p.password}@${p.host}:${p.port}`
|
||||||
|
: `http://${p.host}:${p.port}`;
|
||||||
|
|
||||||
|
console.log(`[ProxyTest] Using proxy: ${p.host}:${p.port}`);
|
||||||
|
|
||||||
|
// Fetch IP via proxy
|
||||||
|
const cmd = `curl -s --proxy '${proxyUrl}' 'https://api.ipify.org?format=json'`;
|
||||||
|
const output = execSync(cmd, { timeout: 30000 }).toString().trim();
|
||||||
|
const data = JSON.parse(output);
|
||||||
|
|
||||||
|
console.log(`[ProxyTest] Proxy IP: ${data.ip}`);
|
||||||
|
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
proxyIp: data.ip,
|
||||||
|
proxyHost: p.host,
|
||||||
|
proxyPort: p.port,
|
||||||
|
};
|
||||||
|
} catch (error: any) {
|
||||||
|
console.error('[ProxyTest] Error:', error.message);
|
||||||
|
return { success: false, error: error.message };
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,10 +6,12 @@
|
|||||||
* task-service.ts and routes/tasks.ts.
|
* task-service.ts and routes/tasks.ts.
|
||||||
*
|
*
|
||||||
* State is in-memory and resets on server restart.
|
* State is in-memory and resets on server restart.
|
||||||
* By default, the pool is OPEN (not paused).
|
* By default, the pool is PAUSED (closed) - admin must explicitly start it.
|
||||||
|
* This prevents workers from immediately grabbing tasks on deploy before
|
||||||
|
* the system is ready.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
let taskPoolPaused = false;
|
let taskPoolPaused = true;
|
||||||
|
|
||||||
export function isTaskPoolPaused(): boolean {
|
export function isTaskPoolPaused(): boolean {
|
||||||
return taskPoolPaused;
|
return taskPoolPaused;
|
||||||
|
|||||||
@@ -31,7 +31,8 @@ export type TaskRole =
|
|||||||
| 'product_discovery'
|
| 'product_discovery'
|
||||||
| 'payload_fetch' // NEW: Fetches from API, saves to disk
|
| 'payload_fetch' // NEW: Fetches from API, saves to disk
|
||||||
| 'product_refresh' // CHANGED: Now reads from local payload
|
| 'product_refresh' // CHANGED: Now reads from local payload
|
||||||
| 'analytics_refresh';
|
| 'analytics_refresh'
|
||||||
|
| 'proxy_test'; // Tests proxy connectivity via ipify
|
||||||
|
|
||||||
export type TaskStatus =
|
export type TaskStatus =
|
||||||
| 'pending'
|
| 'pending'
|
||||||
|
|||||||
@@ -59,11 +59,53 @@ import { handleProductDiscovery } from './handlers/product-discovery';
|
|||||||
import { handleStoreDiscovery } from './handlers/store-discovery';
|
import { handleStoreDiscovery } from './handlers/store-discovery';
|
||||||
import { handleEntryPointDiscovery } from './handlers/entry-point-discovery';
|
import { handleEntryPointDiscovery } from './handlers/entry-point-discovery';
|
||||||
import { handleAnalyticsRefresh } from './handlers/analytics-refresh';
|
import { handleAnalyticsRefresh } from './handlers/analytics-refresh';
|
||||||
|
import { handleProxyTest } from './handlers/proxy-test';
|
||||||
|
|
||||||
const POLL_INTERVAL_MS = parseInt(process.env.POLL_INTERVAL_MS || '5000');
|
const POLL_INTERVAL_MS = parseInt(process.env.POLL_INTERVAL_MS || '5000');
|
||||||
const HEARTBEAT_INTERVAL_MS = parseInt(process.env.HEARTBEAT_INTERVAL_MS || '30000');
|
const HEARTBEAT_INTERVAL_MS = parseInt(process.env.HEARTBEAT_INTERVAL_MS || '30000');
|
||||||
const API_BASE_URL = process.env.API_BASE_URL || 'http://localhost:3010';
|
const API_BASE_URL = process.env.API_BASE_URL || 'http://localhost:3010';
|
||||||
|
|
||||||
|
// =============================================================================
|
||||||
|
// CONCURRENT TASK PROCESSING SETTINGS
|
||||||
|
// =============================================================================
|
||||||
|
// Workers can process multiple tasks simultaneously using async I/O.
|
||||||
|
// This improves throughput for I/O-bound tasks (network calls, DB queries).
|
||||||
|
//
|
||||||
|
// Resource thresholds trigger "backoff" - the worker stops claiming new tasks
|
||||||
|
// but continues processing existing ones until resources return to normal.
|
||||||
|
//
|
||||||
|
// See: docs/WORKER_TASK_ARCHITECTURE.md#concurrent-task-processing
|
||||||
|
// =============================================================================
|
||||||
|
|
||||||
|
// Maximum number of tasks this worker will run concurrently
|
||||||
|
// Tune based on workload: I/O-bound tasks benefit from higher concurrency
|
||||||
|
const MAX_CONCURRENT_TASKS = parseInt(process.env.MAX_CONCURRENT_TASKS || '3');
|
||||||
|
|
||||||
|
// When heap memory usage exceeds this threshold (as decimal 0.0-1.0), stop claiming new tasks
|
||||||
|
// Default 85% - gives headroom before OOM
|
||||||
|
const MEMORY_BACKOFF_THRESHOLD = parseFloat(process.env.MEMORY_BACKOFF_THRESHOLD || '0.85');
|
||||||
|
|
||||||
|
// Parse max heap size from NODE_OPTIONS (--max-old-space-size=1500)
|
||||||
|
// This is used as the denominator for memory percentage calculation
|
||||||
|
// V8's heapTotal is dynamic and stays small when idle, causing false high percentages
|
||||||
|
function getMaxHeapSizeMb(): number {
|
||||||
|
const nodeOptions = process.env.NODE_OPTIONS || '';
|
||||||
|
const match = nodeOptions.match(/--max-old-space-size=(\d+)/);
|
||||||
|
if (match) {
|
||||||
|
return parseInt(match[1], 10);
|
||||||
|
}
|
||||||
|
// Fallback: use 512MB if not specified
|
||||||
|
return 512;
|
||||||
|
}
|
||||||
|
const MAX_HEAP_SIZE_MB = getMaxHeapSizeMb();
|
||||||
|
|
||||||
|
// When CPU usage exceeds this threshold (as decimal 0.0-1.0), stop claiming new tasks
|
||||||
|
// Default 90% - allows some burst capacity
|
||||||
|
const CPU_BACKOFF_THRESHOLD = parseFloat(process.env.CPU_BACKOFF_THRESHOLD || '0.90');
|
||||||
|
|
||||||
|
// How long to wait (ms) when in backoff state before rechecking resources
|
||||||
|
const BACKOFF_DURATION_MS = parseInt(process.env.BACKOFF_DURATION_MS || '10000');
|
||||||
|
|
||||||
export interface TaskContext {
|
export interface TaskContext {
|
||||||
pool: Pool;
|
pool: Pool;
|
||||||
workerId: string;
|
workerId: string;
|
||||||
@@ -92,8 +134,28 @@ const TASK_HANDLERS: Record<TaskRole, TaskHandler> = {
|
|||||||
store_discovery: handleStoreDiscovery,
|
store_discovery: handleStoreDiscovery,
|
||||||
entry_point_discovery: handleEntryPointDiscovery,
|
entry_point_discovery: handleEntryPointDiscovery,
|
||||||
analytics_refresh: handleAnalyticsRefresh,
|
analytics_refresh: handleAnalyticsRefresh,
|
||||||
|
proxy_test: handleProxyTest, // Tests proxy via ipify
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resource usage stats reported to the registry and used for backoff decisions.
|
||||||
|
* These values are included in worker heartbeats and displayed in the UI.
|
||||||
|
*/
|
||||||
|
interface ResourceStats {
|
||||||
|
/** Current heap memory usage as decimal (0.0 to 1.0) */
|
||||||
|
memoryPercent: number;
|
||||||
|
/** Current heap used in MB */
|
||||||
|
memoryMb: number;
|
||||||
|
/** Total heap available in MB */
|
||||||
|
memoryTotalMb: number;
|
||||||
|
/** CPU usage percentage since last check (0 to 100) */
|
||||||
|
cpuPercent: number;
|
||||||
|
/** True if worker is currently in backoff state */
|
||||||
|
isBackingOff: boolean;
|
||||||
|
/** Reason for backoff (e.g., "Memory at 87.3% (threshold: 85%)") */
|
||||||
|
backoffReason: string | null;
|
||||||
|
}
|
||||||
|
|
||||||
export class TaskWorker {
|
export class TaskWorker {
|
||||||
private pool: Pool;
|
private pool: Pool;
|
||||||
private workerId: string;
|
private workerId: string;
|
||||||
@@ -102,14 +164,110 @@ export class TaskWorker {
|
|||||||
private isRunning: boolean = false;
|
private isRunning: boolean = false;
|
||||||
private heartbeatInterval: NodeJS.Timeout | null = null;
|
private heartbeatInterval: NodeJS.Timeout | null = null;
|
||||||
private registryHeartbeatInterval: NodeJS.Timeout | null = null;
|
private registryHeartbeatInterval: NodeJS.Timeout | null = null;
|
||||||
private currentTask: WorkerTask | null = null;
|
|
||||||
private crawlRotator: CrawlRotator;
|
private crawlRotator: CrawlRotator;
|
||||||
|
|
||||||
|
// ==========================================================================
|
||||||
|
// CONCURRENT TASK TRACKING
|
||||||
|
// ==========================================================================
|
||||||
|
// activeTasks: Map of task ID -> task object for all currently running tasks
|
||||||
|
// taskPromises: Map of task ID -> Promise for cleanup when task completes
|
||||||
|
// maxConcurrentTasks: How many tasks this worker will run in parallel
|
||||||
|
// ==========================================================================
|
||||||
|
private activeTasks: Map<number, WorkerTask> = new Map();
|
||||||
|
private taskPromises: Map<number, Promise<void>> = new Map();
|
||||||
|
private maxConcurrentTasks: number = MAX_CONCURRENT_TASKS;
|
||||||
|
|
||||||
|
// ==========================================================================
|
||||||
|
// RESOURCE MONITORING FOR BACKOFF
|
||||||
|
// ==========================================================================
|
||||||
|
// CPU tracking uses differential measurement - we track last values and
|
||||||
|
// calculate percentage based on elapsed time since last check.
|
||||||
|
// ==========================================================================
|
||||||
|
private lastCpuUsage: { user: number; system: number } = { user: 0, system: 0 };
|
||||||
|
private lastCpuCheck: number = Date.now();
|
||||||
|
private isBackingOff: boolean = false;
|
||||||
|
private backoffReason: string | null = null;
|
||||||
|
|
||||||
constructor(role: TaskRole | null = null, workerId?: string) {
|
constructor(role: TaskRole | null = null, workerId?: string) {
|
||||||
this.pool = getPool();
|
this.pool = getPool();
|
||||||
this.role = role;
|
this.role = role;
|
||||||
this.workerId = workerId || `worker-${uuidv4().slice(0, 8)}`;
|
this.workerId = workerId || `worker-${uuidv4().slice(0, 8)}`;
|
||||||
this.crawlRotator = new CrawlRotator(this.pool);
|
this.crawlRotator = new CrawlRotator(this.pool);
|
||||||
|
|
||||||
|
// Initialize CPU tracking
|
||||||
|
const cpuUsage = process.cpuUsage();
|
||||||
|
this.lastCpuUsage = { user: cpuUsage.user, system: cpuUsage.system };
|
||||||
|
this.lastCpuCheck = Date.now();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get current resource usage
|
||||||
|
* Memory percentage is calculated against MAX_HEAP_SIZE_MB (from --max-old-space-size)
|
||||||
|
* NOT against V8's dynamic heapTotal which stays small when idle
|
||||||
|
*/
|
||||||
|
private getResourceStats(): ResourceStats {
|
||||||
|
const memUsage = process.memoryUsage();
|
||||||
|
const heapUsedMb = memUsage.heapUsed / 1024 / 1024;
|
||||||
|
// Use MAX_HEAP_SIZE_MB as ceiling, not dynamic heapTotal
|
||||||
|
// V8's heapTotal stays small when idle (e.g., 36MB) causing false 95%+ readings
|
||||||
|
// With --max-old-space-size=1500, we should calculate against 1500MB
|
||||||
|
const memoryPercent = heapUsedMb / MAX_HEAP_SIZE_MB;
|
||||||
|
|
||||||
|
// Calculate CPU usage since last check
|
||||||
|
const cpuUsage = process.cpuUsage();
|
||||||
|
const now = Date.now();
|
||||||
|
const elapsed = now - this.lastCpuCheck;
|
||||||
|
|
||||||
|
let cpuPercent = 0;
|
||||||
|
if (elapsed > 0) {
|
||||||
|
const userDiff = (cpuUsage.user - this.lastCpuUsage.user) / 1000; // microseconds to ms
|
||||||
|
const systemDiff = (cpuUsage.system - this.lastCpuUsage.system) / 1000;
|
||||||
|
cpuPercent = ((userDiff + systemDiff) / elapsed) * 100;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update last values
|
||||||
|
this.lastCpuUsage = { user: cpuUsage.user, system: cpuUsage.system };
|
||||||
|
this.lastCpuCheck = now;
|
||||||
|
|
||||||
|
return {
|
||||||
|
memoryPercent,
|
||||||
|
memoryMb: Math.round(heapUsedMb),
|
||||||
|
memoryTotalMb: MAX_HEAP_SIZE_MB, // Use max-old-space-size, not dynamic heapTotal
|
||||||
|
cpuPercent: Math.min(100, cpuPercent), // Cap at 100%
|
||||||
|
isBackingOff: this.isBackingOff,
|
||||||
|
backoffReason: this.backoffReason,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if we should back off from taking new tasks
|
||||||
|
*/
|
||||||
|
private shouldBackOff(): { backoff: boolean; reason: string | null } {
|
||||||
|
const stats = this.getResourceStats();
|
||||||
|
|
||||||
|
if (stats.memoryPercent > MEMORY_BACKOFF_THRESHOLD) {
|
||||||
|
return { backoff: true, reason: `Memory at ${(stats.memoryPercent * 100).toFixed(1)}% (threshold: ${MEMORY_BACKOFF_THRESHOLD * 100}%)` };
|
||||||
|
}
|
||||||
|
|
||||||
|
if (stats.cpuPercent > CPU_BACKOFF_THRESHOLD * 100) {
|
||||||
|
return { backoff: true, reason: `CPU at ${stats.cpuPercent.toFixed(1)}% (threshold: ${CPU_BACKOFF_THRESHOLD * 100}%)` };
|
||||||
|
}
|
||||||
|
|
||||||
|
return { backoff: false, reason: null };
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get count of currently running tasks
|
||||||
|
*/
|
||||||
|
get activeTaskCount(): number {
|
||||||
|
return this.activeTasks.size;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if we can accept more tasks
|
||||||
|
*/
|
||||||
|
private canAcceptMoreTasks(): boolean {
|
||||||
|
return this.activeTasks.size < this.maxConcurrentTasks;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -252,21 +410,32 @@ export class TaskWorker {
|
|||||||
const memUsage = process.memoryUsage();
|
const memUsage = process.memoryUsage();
|
||||||
const cpuUsage = process.cpuUsage();
|
const cpuUsage = process.cpuUsage();
|
||||||
const proxyLocation = this.crawlRotator.getProxyLocation();
|
const proxyLocation = this.crawlRotator.getProxyLocation();
|
||||||
|
const resourceStats = this.getResourceStats();
|
||||||
|
|
||||||
|
// Get array of active task IDs
|
||||||
|
const activeTaskIds = Array.from(this.activeTasks.keys());
|
||||||
|
|
||||||
await fetch(`${API_BASE_URL}/api/worker-registry/heartbeat`, {
|
await fetch(`${API_BASE_URL}/api/worker-registry/heartbeat`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
worker_id: this.workerId,
|
worker_id: this.workerId,
|
||||||
current_task_id: this.currentTask?.id || null,
|
current_task_id: activeTaskIds[0] || null, // Primary task for backwards compat
|
||||||
status: this.currentTask ? 'active' : 'idle',
|
current_task_ids: activeTaskIds, // All active tasks
|
||||||
|
active_task_count: this.activeTasks.size,
|
||||||
|
max_concurrent_tasks: this.maxConcurrentTasks,
|
||||||
|
status: this.activeTasks.size > 0 ? 'active' : 'idle',
|
||||||
resources: {
|
resources: {
|
||||||
memory_mb: Math.round(memUsage.heapUsed / 1024 / 1024),
|
memory_mb: Math.round(memUsage.heapUsed / 1024 / 1024),
|
||||||
memory_total_mb: Math.round(memUsage.heapTotal / 1024 / 1024),
|
memory_total_mb: Math.round(memUsage.heapTotal / 1024 / 1024),
|
||||||
memory_rss_mb: Math.round(memUsage.rss / 1024 / 1024),
|
memory_rss_mb: Math.round(memUsage.rss / 1024 / 1024),
|
||||||
|
memory_percent: Math.round(resourceStats.memoryPercent * 100),
|
||||||
cpu_user_ms: Math.round(cpuUsage.user / 1000),
|
cpu_user_ms: Math.round(cpuUsage.user / 1000),
|
||||||
cpu_system_ms: Math.round(cpuUsage.system / 1000),
|
cpu_system_ms: Math.round(cpuUsage.system / 1000),
|
||||||
|
cpu_percent: Math.round(resourceStats.cpuPercent),
|
||||||
proxy_location: proxyLocation,
|
proxy_location: proxyLocation,
|
||||||
|
is_backing_off: this.isBackingOff,
|
||||||
|
backoff_reason: this.backoffReason,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
@@ -328,20 +497,85 @@ export class TaskWorker {
|
|||||||
this.startRegistryHeartbeat();
|
this.startRegistryHeartbeat();
|
||||||
|
|
||||||
const roleMsg = this.role ? `for role: ${this.role}` : '(role-agnostic - any task)';
|
const roleMsg = this.role ? `for role: ${this.role}` : '(role-agnostic - any task)';
|
||||||
console.log(`[TaskWorker] ${this.friendlyName} starting ${roleMsg}`);
|
console.log(`[TaskWorker] ${this.friendlyName} starting ${roleMsg} (max ${this.maxConcurrentTasks} concurrent tasks)`);
|
||||||
|
|
||||||
while (this.isRunning) {
|
while (this.isRunning) {
|
||||||
try {
|
try {
|
||||||
await this.processNextTask();
|
await this.mainLoop();
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
console.error(`[TaskWorker] Loop error:`, error.message);
|
console.error(`[TaskWorker] Loop error:`, error.message);
|
||||||
await this.sleep(POLL_INTERVAL_MS);
|
await this.sleep(POLL_INTERVAL_MS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Wait for any remaining tasks to complete
|
||||||
|
if (this.taskPromises.size > 0) {
|
||||||
|
console.log(`[TaskWorker] Waiting for ${this.taskPromises.size} active tasks to complete...`);
|
||||||
|
await Promise.allSettled(this.taskPromises.values());
|
||||||
|
}
|
||||||
|
|
||||||
console.log(`[TaskWorker] Worker ${this.workerId} stopped`);
|
console.log(`[TaskWorker] Worker ${this.workerId} stopped`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Main loop - tries to fill up to maxConcurrentTasks
|
||||||
|
*/
|
||||||
|
private async mainLoop(): Promise<void> {
|
||||||
|
// Check resource usage and backoff if needed
|
||||||
|
const { backoff, reason } = this.shouldBackOff();
|
||||||
|
if (backoff) {
|
||||||
|
if (!this.isBackingOff) {
|
||||||
|
console.log(`[TaskWorker] ${this.friendlyName} backing off: ${reason}`);
|
||||||
|
}
|
||||||
|
this.isBackingOff = true;
|
||||||
|
this.backoffReason = reason;
|
||||||
|
await this.sleep(BACKOFF_DURATION_MS);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clear backoff state
|
||||||
|
if (this.isBackingOff) {
|
||||||
|
console.log(`[TaskWorker] ${this.friendlyName} resuming normal operation`);
|
||||||
|
this.isBackingOff = false;
|
||||||
|
this.backoffReason = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for decommission signal
|
||||||
|
const shouldDecommission = await this.checkDecommission();
|
||||||
|
if (shouldDecommission) {
|
||||||
|
console.log(`[TaskWorker] ${this.friendlyName} received decommission signal - waiting for ${this.activeTasks.size} tasks to complete`);
|
||||||
|
// Stop accepting new tasks, wait for current to finish
|
||||||
|
this.isRunning = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try to claim more tasks if we have capacity
|
||||||
|
if (this.canAcceptMoreTasks()) {
|
||||||
|
const task = await taskService.claimTask(this.role, this.workerId);
|
||||||
|
|
||||||
|
if (task) {
|
||||||
|
console.log(`[TaskWorker] ${this.friendlyName} claimed task ${task.id} (${task.role}) [${this.activeTasks.size + 1}/${this.maxConcurrentTasks}]`);
|
||||||
|
this.activeTasks.set(task.id, task);
|
||||||
|
|
||||||
|
// Start task in background (don't await)
|
||||||
|
const taskPromise = this.executeTask(task);
|
||||||
|
this.taskPromises.set(task.id, taskPromise);
|
||||||
|
|
||||||
|
// Clean up when done
|
||||||
|
taskPromise.finally(() => {
|
||||||
|
this.activeTasks.delete(task.id);
|
||||||
|
this.taskPromises.delete(task.id);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Immediately try to claim more tasks (don't wait for poll interval)
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// No task claimed or at capacity - wait before next poll
|
||||||
|
await this.sleep(POLL_INTERVAL_MS);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stop the worker
|
* Stop the worker
|
||||||
*/
|
*/
|
||||||
@@ -354,23 +588,10 @@ export class TaskWorker {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Process the next available task
|
* Execute a single task (runs concurrently with other tasks)
|
||||||
*/
|
*/
|
||||||
private async processNextTask(): Promise<void> {
|
private async executeTask(task: WorkerTask): Promise<void> {
|
||||||
// Try to claim a task
|
console.log(`[TaskWorker] ${this.friendlyName} starting task ${task.id} (${task.role}) for dispensary ${task.dispensary_id || 'N/A'}`);
|
||||||
const task = await taskService.claimTask(this.role, this.workerId);
|
|
||||||
|
|
||||||
if (!task) {
|
|
||||||
// No tasks available, wait and retry
|
|
||||||
await this.sleep(POLL_INTERVAL_MS);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.currentTask = task;
|
|
||||||
console.log(`[TaskWorker] Claimed task ${task.id} (${task.role}) for dispensary ${task.dispensary_id || 'N/A'}`);
|
|
||||||
|
|
||||||
// Start heartbeat
|
|
||||||
this.startHeartbeat(task.id);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Mark as running
|
// Mark as running
|
||||||
@@ -399,7 +620,7 @@ export class TaskWorker {
|
|||||||
// Mark as completed
|
// Mark as completed
|
||||||
await taskService.completeTask(task.id, result);
|
await taskService.completeTask(task.id, result);
|
||||||
await this.reportTaskCompletion(true);
|
await this.reportTaskCompletion(true);
|
||||||
console.log(`[TaskWorker] ${this.friendlyName} completed task ${task.id}`);
|
console.log(`[TaskWorker] ${this.friendlyName} completed task ${task.id} [${this.activeTasks.size}/${this.maxConcurrentTasks} active]`);
|
||||||
|
|
||||||
// Chain next task if applicable
|
// Chain next task if applicable
|
||||||
const chainedTask = await taskService.chainNextTask({
|
const chainedTask = await taskService.chainNextTask({
|
||||||
@@ -421,9 +642,35 @@ export class TaskWorker {
|
|||||||
await taskService.failTask(task.id, error.message);
|
await taskService.failTask(task.id, error.message);
|
||||||
await this.reportTaskCompletion(false);
|
await this.reportTaskCompletion(false);
|
||||||
console.error(`[TaskWorker] ${this.friendlyName} task ${task.id} error:`, error.message);
|
console.error(`[TaskWorker] ${this.friendlyName} task ${task.id} error:`, error.message);
|
||||||
} finally {
|
}
|
||||||
this.stopHeartbeat();
|
// Note: cleanup (removing from activeTasks) is handled in mainLoop's finally block
|
||||||
this.currentTask = null;
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if this worker has been flagged for decommission
|
||||||
|
* Returns true if worker should stop after current task
|
||||||
|
*/
|
||||||
|
private async checkDecommission(): Promise<boolean> {
|
||||||
|
try {
|
||||||
|
// Check worker_registry for decommission flag
|
||||||
|
const result = await this.pool.query(
|
||||||
|
`SELECT decommission_requested, decommission_reason
|
||||||
|
FROM worker_registry
|
||||||
|
WHERE worker_id = $1`,
|
||||||
|
[this.workerId]
|
||||||
|
);
|
||||||
|
|
||||||
|
if (result.rows.length > 0 && result.rows[0].decommission_requested) {
|
||||||
|
const reason = result.rows[0].decommission_reason || 'No reason provided';
|
||||||
|
console.log(`[TaskWorker] Decommission requested: ${reason}`);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
} catch (error: any) {
|
||||||
|
// If we can't check, continue running
|
||||||
|
console.warn(`[TaskWorker] Could not check decommission status: ${error.message}`);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -460,12 +707,25 @@ export class TaskWorker {
|
|||||||
/**
|
/**
|
||||||
* Get worker info
|
* Get worker info
|
||||||
*/
|
*/
|
||||||
getInfo(): { workerId: string; role: TaskRole | null; isRunning: boolean; currentTaskId: number | null } {
|
getInfo(): {
|
||||||
|
workerId: string;
|
||||||
|
role: TaskRole | null;
|
||||||
|
isRunning: boolean;
|
||||||
|
activeTaskIds: number[];
|
||||||
|
activeTaskCount: number;
|
||||||
|
maxConcurrentTasks: number;
|
||||||
|
isBackingOff: boolean;
|
||||||
|
backoffReason: string | null;
|
||||||
|
} {
|
||||||
return {
|
return {
|
||||||
workerId: this.workerId,
|
workerId: this.workerId,
|
||||||
role: this.role,
|
role: this.role,
|
||||||
isRunning: this.isRunning,
|
isRunning: this.isRunning,
|
||||||
currentTaskId: this.currentTask?.id || null,
|
activeTaskIds: Array.from(this.activeTasks.keys()),
|
||||||
|
activeTaskCount: this.activeTasks.size,
|
||||||
|
maxConcurrentTasks: this.maxConcurrentTasks,
|
||||||
|
isBackingOff: this.isBackingOff,
|
||||||
|
backoffReason: this.backoffReason,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
4
cannaiq/dist/index.html
vendored
4
cannaiq/dist/index.html
vendored
@@ -7,8 +7,8 @@
|
|||||||
<title>CannaIQ - Cannabis Menu Intelligence Platform</title>
|
<title>CannaIQ - Cannabis Menu Intelligence Platform</title>
|
||||||
<meta name="description" content="CannaIQ provides real-time cannabis dispensary menu data, product tracking, and analytics for dispensaries across Arizona." />
|
<meta name="description" content="CannaIQ provides real-time cannabis dispensary menu data, product tracking, and analytics for dispensaries across Arizona." />
|
||||||
<meta name="keywords" content="cannabis, dispensary, menu, products, analytics, Arizona" />
|
<meta name="keywords" content="cannabis, dispensary, menu, products, analytics, Arizona" />
|
||||||
<script type="module" crossorigin src="/assets/index-BXmp5CSY.js"></script>
|
<script type="module" crossorigin src="/assets/index-Dq9S0rVi.js"></script>
|
||||||
<link rel="stylesheet" crossorigin href="/assets/index-4959QN4j.css">
|
<link rel="stylesheet" crossorigin href="/assets/index-DhM09B-d.css">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div id="root"></div>
|
<div id="root"></div>
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8" />
|
<meta charset="UTF-8" />
|
||||||
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
|
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
<title>CannaIQ - Cannabis Menu Intelligence Platform</title>
|
<title>CannaIQ - Cannabis Menu Intelligence Platform</title>
|
||||||
<meta name="description" content="CannaIQ provides real-time cannabis dispensary menu data, product tracking, and analytics for dispensaries across Arizona." />
|
<meta name="description" content="CannaIQ provides real-time cannabis dispensary menu data, product tracking, and analytics for dispensaries across Arizona." />
|
||||||
|
|||||||
5
cannaiq/public/favicon.svg
Normal file
5
cannaiq/public/favicon.svg
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
<svg viewBox="0 0 32 32" xmlns="http://www.w3.org/2000/svg">
|
||||||
|
<rect width="32" height="32" rx="6" fill="#059669"/>
|
||||||
|
<path d="M16 6C12.5 6 9.5 7.5 7.5 10L16 16L24.5 10C22.5 7.5 19.5 6 16 6Z" fill="white"/>
|
||||||
|
<path d="M7.5 10C6 12 5 14.5 5 17C5 22.5 10 26 16 26C22 26 27 22.5 27 17C27 14.5 26 12 24.5 10L16 16L7.5 10Z" fill="white" fill-opacity="0.7"/>
|
||||||
|
</svg>
|
||||||
|
After Width: | Height: | Size: 360 B |
@@ -8,6 +8,7 @@ import { ProductDetail } from './pages/ProductDetail';
|
|||||||
import { Stores } from './pages/Stores';
|
import { Stores } from './pages/Stores';
|
||||||
import { Dispensaries } from './pages/Dispensaries';
|
import { Dispensaries } from './pages/Dispensaries';
|
||||||
import { DispensaryDetail } from './pages/DispensaryDetail';
|
import { DispensaryDetail } from './pages/DispensaryDetail';
|
||||||
|
import { DispensarySchedule } from './pages/DispensarySchedule';
|
||||||
import { StoreDetail } from './pages/StoreDetail';
|
import { StoreDetail } from './pages/StoreDetail';
|
||||||
import { StoreBrands } from './pages/StoreBrands';
|
import { StoreBrands } from './pages/StoreBrands';
|
||||||
import { StoreSpecials } from './pages/StoreSpecials';
|
import { StoreSpecials } from './pages/StoreSpecials';
|
||||||
@@ -46,7 +47,6 @@ import CrossStateCompare from './pages/CrossStateCompare';
|
|||||||
import StateDetail from './pages/StateDetail';
|
import StateDetail from './pages/StateDetail';
|
||||||
import { Discovery } from './pages/Discovery';
|
import { Discovery } from './pages/Discovery';
|
||||||
import { WorkersDashboard } from './pages/WorkersDashboard';
|
import { WorkersDashboard } from './pages/WorkersDashboard';
|
||||||
import { JobQueue } from './pages/JobQueue';
|
|
||||||
import TasksDashboard from './pages/TasksDashboard';
|
import TasksDashboard from './pages/TasksDashboard';
|
||||||
import { ScraperOverviewDashboard } from './pages/ScraperOverviewDashboard';
|
import { ScraperOverviewDashboard } from './pages/ScraperOverviewDashboard';
|
||||||
import { SeoOrchestrator } from './pages/admin/seo/SeoOrchestrator';
|
import { SeoOrchestrator } from './pages/admin/seo/SeoOrchestrator';
|
||||||
@@ -66,6 +66,7 @@ export default function App() {
|
|||||||
<Route path="/stores" element={<PrivateRoute><Stores /></PrivateRoute>} />
|
<Route path="/stores" element={<PrivateRoute><Stores /></PrivateRoute>} />
|
||||||
<Route path="/dispensaries" element={<PrivateRoute><Dispensaries /></PrivateRoute>} />
|
<Route path="/dispensaries" element={<PrivateRoute><Dispensaries /></PrivateRoute>} />
|
||||||
<Route path="/dispensaries/:state/:city/:slug" element={<PrivateRoute><DispensaryDetail /></PrivateRoute>} />
|
<Route path="/dispensaries/:state/:city/:slug" element={<PrivateRoute><DispensaryDetail /></PrivateRoute>} />
|
||||||
|
<Route path="/dispensaries/:state/:city/:slug/schedule" element={<PrivateRoute><DispensarySchedule /></PrivateRoute>} />
|
||||||
<Route path="/stores/:state/:storeName/:slug/brands" element={<PrivateRoute><StoreBrands /></PrivateRoute>} />
|
<Route path="/stores/:state/:storeName/:slug/brands" element={<PrivateRoute><StoreBrands /></PrivateRoute>} />
|
||||||
<Route path="/stores/:state/:storeName/:slug/specials" element={<PrivateRoute><StoreSpecials /></PrivateRoute>} />
|
<Route path="/stores/:state/:storeName/:slug/specials" element={<PrivateRoute><StoreSpecials /></PrivateRoute>} />
|
||||||
<Route path="/stores/:state/:storeName/:slug" element={<PrivateRoute><StoreDetail /></PrivateRoute>} />
|
<Route path="/stores/:state/:storeName/:slug" element={<PrivateRoute><StoreDetail /></PrivateRoute>} />
|
||||||
@@ -123,8 +124,6 @@ export default function App() {
|
|||||||
<Route path="/discovery" element={<PrivateRoute><Discovery /></PrivateRoute>} />
|
<Route path="/discovery" element={<PrivateRoute><Discovery /></PrivateRoute>} />
|
||||||
{/* Workers Dashboard */}
|
{/* Workers Dashboard */}
|
||||||
<Route path="/workers" element={<PrivateRoute><WorkersDashboard /></PrivateRoute>} />
|
<Route path="/workers" element={<PrivateRoute><WorkersDashboard /></PrivateRoute>} />
|
||||||
{/* Job Queue Management */}
|
|
||||||
<Route path="/job-queue" element={<PrivateRoute><JobQueue /></PrivateRoute>} />
|
|
||||||
{/* Task Queue Dashboard */}
|
{/* Task Queue Dashboard */}
|
||||||
<Route path="/tasks" element={<PrivateRoute><TasksDashboard /></PrivateRoute>} />
|
<Route path="/tasks" element={<PrivateRoute><TasksDashboard /></PrivateRoute>} />
|
||||||
{/* Scraper Overview Dashboard (new primary) */}
|
{/* Scraper Overview Dashboard (new primary) */}
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
import { ReactNode, useEffect, useState } from 'react';
|
import { ReactNode, useEffect, useState, useRef } from 'react';
|
||||||
import { useNavigate, useLocation } from 'react-router-dom';
|
import { useNavigate, useLocation, Link } from 'react-router-dom';
|
||||||
import { useAuthStore } from '../store/authStore';
|
import { useAuthStore } from '../store/authStore';
|
||||||
import { api } from '../lib/api';
|
import { api } from '../lib/api';
|
||||||
import { StateSelector } from './StateSelector';
|
|
||||||
import {
|
import {
|
||||||
LayoutDashboard,
|
LayoutDashboard,
|
||||||
Building2,
|
Building2,
|
||||||
@@ -48,8 +47,8 @@ interface NavLinkProps {
|
|||||||
|
|
||||||
function NavLink({ to, icon, label, isActive }: NavLinkProps) {
|
function NavLink({ to, icon, label, isActive }: NavLinkProps) {
|
||||||
return (
|
return (
|
||||||
<a
|
<Link
|
||||||
href={to}
|
to={to}
|
||||||
className={`flex items-center gap-3 px-3 py-2 rounded-lg text-sm font-medium transition-colors ${
|
className={`flex items-center gap-3 px-3 py-2 rounded-lg text-sm font-medium transition-colors ${
|
||||||
isActive
|
isActive
|
||||||
? 'bg-emerald-50 text-emerald-700'
|
? 'bg-emerald-50 text-emerald-700'
|
||||||
@@ -58,7 +57,7 @@ function NavLink({ to, icon, label, isActive }: NavLinkProps) {
|
|||||||
>
|
>
|
||||||
<span className={`flex-shrink-0 ${isActive ? 'text-emerald-600' : 'text-gray-400'}`}>{icon}</span>
|
<span className={`flex-shrink-0 ${isActive ? 'text-emerald-600' : 'text-gray-400'}`}>{icon}</span>
|
||||||
<span>{label}</span>
|
<span>{label}</span>
|
||||||
</a>
|
</Link>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -86,6 +85,8 @@ export function Layout({ children }: LayoutProps) {
|
|||||||
const { user, logout } = useAuthStore();
|
const { user, logout } = useAuthStore();
|
||||||
const [versionInfo, setVersionInfo] = useState<VersionInfo | null>(null);
|
const [versionInfo, setVersionInfo] = useState<VersionInfo | null>(null);
|
||||||
const [sidebarOpen, setSidebarOpen] = useState(false);
|
const [sidebarOpen, setSidebarOpen] = useState(false);
|
||||||
|
const navRef = useRef<HTMLElement>(null);
|
||||||
|
const scrollPositionRef = useRef<number>(0);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const fetchVersion = async () => {
|
const fetchVersion = async () => {
|
||||||
@@ -111,16 +112,34 @@ export function Layout({ children }: LayoutProps) {
|
|||||||
return location.pathname.startsWith(path);
|
return location.pathname.startsWith(path);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Close sidebar on route change (mobile)
|
// Save scroll position before route change
|
||||||
|
useEffect(() => {
|
||||||
|
const nav = navRef.current;
|
||||||
|
if (nav) {
|
||||||
|
const handleScroll = () => {
|
||||||
|
scrollPositionRef.current = nav.scrollTop;
|
||||||
|
};
|
||||||
|
nav.addEventListener('scroll', handleScroll);
|
||||||
|
return () => nav.removeEventListener('scroll', handleScroll);
|
||||||
|
}
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
// Restore scroll position after route change and close mobile sidebar
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setSidebarOpen(false);
|
setSidebarOpen(false);
|
||||||
|
// Restore scroll position after render
|
||||||
|
requestAnimationFrame(() => {
|
||||||
|
if (navRef.current) {
|
||||||
|
navRef.current.scrollTop = scrollPositionRef.current;
|
||||||
|
}
|
||||||
|
});
|
||||||
}, [location.pathname]);
|
}, [location.pathname]);
|
||||||
|
|
||||||
const sidebarContent = (
|
const sidebarContent = (
|
||||||
<>
|
<>
|
||||||
{/* Logo/Brand */}
|
{/* Logo/Brand */}
|
||||||
<div className="px-6 py-5 border-b border-gray-200">
|
<div className="px-6 py-5 border-b border-gray-200">
|
||||||
<div className="flex items-center gap-3">
|
<Link to="/dashboard" className="flex items-center gap-3 hover:opacity-80 transition-opacity">
|
||||||
<div className="w-8 h-8 bg-emerald-600 rounded-lg flex items-center justify-center">
|
<div className="w-8 h-8 bg-emerald-600 rounded-lg flex items-center justify-center">
|
||||||
<svg viewBox="0 0 24 24" className="w-5 h-5 text-white" fill="currentColor">
|
<svg viewBox="0 0 24 24" className="w-5 h-5 text-white" fill="currentColor">
|
||||||
<path d="M12 2C8.5 2 5.5 3.5 3.5 6L12 12L20.5 6C18.5 3.5 15.5 2 12 2Z" />
|
<path d="M12 2C8.5 2 5.5 3.5 3.5 6L12 12L20.5 6C18.5 3.5 15.5 2 12 2Z" />
|
||||||
@@ -131,21 +150,17 @@ export function Layout({ children }: LayoutProps) {
|
|||||||
<span className="text-lg font-bold text-gray-900">CannaIQ</span>
|
<span className="text-lg font-bold text-gray-900">CannaIQ</span>
|
||||||
{versionInfo && (
|
{versionInfo && (
|
||||||
<p className="text-xs text-gray-400">
|
<p className="text-xs text-gray-400">
|
||||||
v{versionInfo.version} ({versionInfo.git_sha}) {versionInfo.build_time !== 'unknown' && `- ${new Date(versionInfo.build_time).toLocaleDateString()}`}
|
{versionInfo.git_sha || 'dev'}
|
||||||
</p>
|
</p>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</Link>
|
||||||
<p className="text-xs text-gray-500 mt-2 truncate">{user?.email}</p>
|
<p className="text-xs text-gray-500 mt-2 truncate">{user?.email}</p>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* State Selector */}
|
|
||||||
<div className="px-4 py-3 border-b border-gray-200 bg-gray-50">
|
|
||||||
<StateSelector showLabel={false} />
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{/* Navigation */}
|
{/* Navigation */}
|
||||||
<nav className="flex-1 px-3 py-4 space-y-6 overflow-y-auto">
|
<nav ref={navRef} className="flex-1 px-3 py-4 space-y-6 overflow-y-auto">
|
||||||
<NavSection title="Main">
|
<NavSection title="Main">
|
||||||
<NavLink to="/dashboard" icon={<LayoutDashboard className="w-4 h-4" />} label="Dashboard" isActive={isActive('/dashboard', true)} />
|
<NavLink to="/dashboard" icon={<LayoutDashboard className="w-4 h-4" />} label="Dashboard" isActive={isActive('/dashboard', true)} />
|
||||||
<NavLink to="/dispensaries" icon={<Building2 className="w-4 h-4" />} label="Dispensaries" isActive={isActive('/dispensaries')} />
|
<NavLink to="/dispensaries" icon={<Building2 className="w-4 h-4" />} label="Dispensaries" isActive={isActive('/dispensaries')} />
|
||||||
@@ -164,8 +179,7 @@ export function Layout({ children }: LayoutProps) {
|
|||||||
<NavLink to="/admin/orchestrator" icon={<Activity className="w-4 h-4" />} label="Orchestrator" isActive={isActive('/admin/orchestrator')} />
|
<NavLink to="/admin/orchestrator" icon={<Activity className="w-4 h-4" />} label="Orchestrator" isActive={isActive('/admin/orchestrator')} />
|
||||||
<NavLink to="/users" icon={<UserCog className="w-4 h-4" />} label="Users" isActive={isActive('/users')} />
|
<NavLink to="/users" icon={<UserCog className="w-4 h-4" />} label="Users" isActive={isActive('/users')} />
|
||||||
<NavLink to="/workers" icon={<Users className="w-4 h-4" />} label="Workers" isActive={isActive('/workers')} />
|
<NavLink to="/workers" icon={<Users className="w-4 h-4" />} label="Workers" isActive={isActive('/workers')} />
|
||||||
<NavLink to="/job-queue" icon={<ListOrdered className="w-4 h-4" />} label="Job Queue" isActive={isActive('/job-queue')} />
|
<NavLink to="/tasks" icon={<ListChecks className="w-4 h-4" />} label="Tasks" isActive={isActive('/tasks')} />
|
||||||
<NavLink to="/tasks" icon={<ListChecks className="w-4 h-4" />} label="Task Queue" isActive={isActive('/tasks')} />
|
|
||||||
<NavLink to="/admin/seo" icon={<FileText className="w-4 h-4" />} label="SEO Pages" isActive={isActive('/admin/seo')} />
|
<NavLink to="/admin/seo" icon={<FileText className="w-4 h-4" />} label="SEO Pages" isActive={isActive('/admin/seo')} />
|
||||||
<NavLink to="/proxies" icon={<Shield className="w-4 h-4" />} label="Proxies" isActive={isActive('/proxies')} />
|
<NavLink to="/proxies" icon={<Shield className="w-4 h-4" />} label="Proxies" isActive={isActive('/proxies')} />
|
||||||
<NavLink to="/api-permissions" icon={<Key className="w-4 h-4" />} label="API Keys" isActive={isActive('/api-permissions')} />
|
<NavLink to="/api-permissions" icon={<Key className="w-4 h-4" />} label="API Keys" isActive={isActive('/api-permissions')} />
|
||||||
@@ -214,7 +228,7 @@ export function Layout({ children }: LayoutProps) {
|
|||||||
<button onClick={() => setSidebarOpen(true)} className="p-2 -ml-2 rounded-lg hover:bg-gray-100">
|
<button onClick={() => setSidebarOpen(true)} className="p-2 -ml-2 rounded-lg hover:bg-gray-100">
|
||||||
<Menu className="w-5 h-5 text-gray-600" />
|
<Menu className="w-5 h-5 text-gray-600" />
|
||||||
</button>
|
</button>
|
||||||
<div className="flex items-center gap-2">
|
<Link to="/dashboard" className="flex items-center gap-2 hover:opacity-80 transition-opacity">
|
||||||
<div className="w-6 h-6 bg-emerald-600 rounded flex items-center justify-center">
|
<div className="w-6 h-6 bg-emerald-600 rounded flex items-center justify-center">
|
||||||
<svg viewBox="0 0 24 24" className="w-4 h-4 text-white" fill="currentColor">
|
<svg viewBox="0 0 24 24" className="w-4 h-4 text-white" fill="currentColor">
|
||||||
<path d="M12 2C8.5 2 5.5 3.5 3.5 6L12 12L20.5 6C18.5 3.5 15.5 2 12 2Z" />
|
<path d="M12 2C8.5 2 5.5 3.5 3.5 6L12 12L20.5 6C18.5 3.5 15.5 2 12 2Z" />
|
||||||
@@ -222,7 +236,7 @@ export function Layout({ children }: LayoutProps) {
|
|||||||
</svg>
|
</svg>
|
||||||
</div>
|
</div>
|
||||||
<span className="font-semibold text-gray-900">CannaIQ</span>
|
<span className="font-semibold text-gray-900">CannaIQ</span>
|
||||||
</div>
|
</Link>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Page content */}
|
{/* Page content */}
|
||||||
|
|||||||
138
cannaiq/src/components/PasswordConfirmModal.tsx
Normal file
138
cannaiq/src/components/PasswordConfirmModal.tsx
Normal file
@@ -0,0 +1,138 @@
|
|||||||
|
import { useState, useEffect, useRef } from 'react';
|
||||||
|
import { api } from '../lib/api';
|
||||||
|
import { Shield, X, Loader2 } from 'lucide-react';
|
||||||
|
|
||||||
|
interface PasswordConfirmModalProps {
|
||||||
|
isOpen: boolean;
|
||||||
|
onClose: () => void;
|
||||||
|
onConfirm: () => void;
|
||||||
|
title: string;
|
||||||
|
description: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function PasswordConfirmModal({
|
||||||
|
isOpen,
|
||||||
|
onClose,
|
||||||
|
onConfirm,
|
||||||
|
title,
|
||||||
|
description,
|
||||||
|
}: PasswordConfirmModalProps) {
|
||||||
|
const [password, setPassword] = useState('');
|
||||||
|
const [error, setError] = useState('');
|
||||||
|
const [loading, setLoading] = useState(false);
|
||||||
|
const inputRef = useRef<HTMLInputElement>(null);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (isOpen) {
|
||||||
|
setPassword('');
|
||||||
|
setError('');
|
||||||
|
// Focus the input when modal opens
|
||||||
|
setTimeout(() => inputRef.current?.focus(), 100);
|
||||||
|
}
|
||||||
|
}, [isOpen]);
|
||||||
|
|
||||||
|
const handleSubmit = async (e: React.FormEvent) => {
|
||||||
|
e.preventDefault();
|
||||||
|
if (!password.trim()) {
|
||||||
|
setError('Password is required');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setLoading(true);
|
||||||
|
setError('');
|
||||||
|
|
||||||
|
try {
|
||||||
|
const result = await api.verifyPassword(password);
|
||||||
|
if (result.verified) {
|
||||||
|
onConfirm();
|
||||||
|
onClose();
|
||||||
|
} else {
|
||||||
|
setError('Invalid password');
|
||||||
|
}
|
||||||
|
} catch (err: any) {
|
||||||
|
setError(err.message || 'Verification failed');
|
||||||
|
} finally {
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!isOpen) return null;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="fixed inset-0 z-50 flex items-center justify-center">
|
||||||
|
{/* Backdrop */}
|
||||||
|
<div
|
||||||
|
className="absolute inset-0 bg-black bg-opacity-50"
|
||||||
|
onClick={onClose}
|
||||||
|
/>
|
||||||
|
|
||||||
|
{/* Modal */}
|
||||||
|
<div className="relative bg-white rounded-lg shadow-xl max-w-md w-full mx-4">
|
||||||
|
{/* Header */}
|
||||||
|
<div className="flex items-center justify-between px-6 py-4 border-b border-gray-200">
|
||||||
|
<div className="flex items-center gap-3">
|
||||||
|
<div className="p-2 bg-amber-100 rounded-lg">
|
||||||
|
<Shield className="w-5 h-5 text-amber-600" />
|
||||||
|
</div>
|
||||||
|
<h3 className="text-lg font-semibold text-gray-900">{title}</h3>
|
||||||
|
</div>
|
||||||
|
<button
|
||||||
|
onClick={onClose}
|
||||||
|
className="p-1 hover:bg-gray-100 rounded-lg transition-colors"
|
||||||
|
>
|
||||||
|
<X className="w-5 h-5 text-gray-500" />
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Body */}
|
||||||
|
<form onSubmit={handleSubmit}>
|
||||||
|
<div className="px-6 py-4">
|
||||||
|
<p className="text-gray-600 mb-4">{description}</p>
|
||||||
|
|
||||||
|
<div className="space-y-2">
|
||||||
|
<label
|
||||||
|
htmlFor="password"
|
||||||
|
className="block text-sm font-medium text-gray-700"
|
||||||
|
>
|
||||||
|
Enter your password to continue
|
||||||
|
</label>
|
||||||
|
<input
|
||||||
|
ref={inputRef}
|
||||||
|
type="password"
|
||||||
|
id="password"
|
||||||
|
value={password}
|
||||||
|
onChange={(e) => setPassword(e.target.value)}
|
||||||
|
className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-emerald-500 focus:border-emerald-500"
|
||||||
|
placeholder="Password"
|
||||||
|
disabled={loading}
|
||||||
|
/>
|
||||||
|
{error && (
|
||||||
|
<p className="text-sm text-red-600">{error}</p>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Footer */}
|
||||||
|
<div className="flex justify-end gap-3 px-6 py-4 border-t border-gray-200 bg-gray-50 rounded-b-lg">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={onClose}
|
||||||
|
disabled={loading}
|
||||||
|
className="px-4 py-2 text-gray-700 hover:bg-gray-100 rounded-lg transition-colors"
|
||||||
|
>
|
||||||
|
Cancel
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
type="submit"
|
||||||
|
disabled={loading}
|
||||||
|
className="px-4 py-2 bg-emerald-600 text-white rounded-lg hover:bg-emerald-700 transition-colors disabled:opacity-50 flex items-center gap-2"
|
||||||
|
>
|
||||||
|
{loading && <Loader2 className="w-4 h-4 animate-spin" />}
|
||||||
|
Confirm
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -84,6 +84,13 @@ class ApiClient {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async verifyPassword(password: string) {
|
||||||
|
return this.request<{ verified: boolean; error?: string }>('/api/auth/verify-password', {
|
||||||
|
method: 'POST',
|
||||||
|
body: JSON.stringify({ password }),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
async getMe() {
|
async getMe() {
|
||||||
return this.request<{ user: any }>('/api/auth/me');
|
return this.request<{ user: any }>('/api/auth/me');
|
||||||
}
|
}
|
||||||
@@ -983,6 +990,47 @@ class ApiClient {
|
|||||||
}>(`/api/markets/stores/${id}/categories`);
|
}>(`/api/markets/stores/${id}/categories`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getStoreCrawlHistory(id: number, limit = 50) {
|
||||||
|
return this.request<{
|
||||||
|
dispensary: {
|
||||||
|
id: number;
|
||||||
|
name: string;
|
||||||
|
dba_name: string | null;
|
||||||
|
slug: string;
|
||||||
|
state: string;
|
||||||
|
city: string;
|
||||||
|
menu_type: string | null;
|
||||||
|
platform_dispensary_id: string | null;
|
||||||
|
last_menu_scrape: string | null;
|
||||||
|
} | null;
|
||||||
|
history: Array<{
|
||||||
|
id: number;
|
||||||
|
runId: string | null;
|
||||||
|
profileKey: string | null;
|
||||||
|
crawlerModule: string | null;
|
||||||
|
stateAtStart: string | null;
|
||||||
|
stateAtEnd: string | null;
|
||||||
|
totalSteps: number;
|
||||||
|
durationMs: number | null;
|
||||||
|
success: boolean;
|
||||||
|
errorMessage: string | null;
|
||||||
|
productsFound: number | null;
|
||||||
|
startedAt: string | null;
|
||||||
|
completedAt: string | null;
|
||||||
|
}>;
|
||||||
|
nextSchedule: {
|
||||||
|
scheduleId: number;
|
||||||
|
jobName: string;
|
||||||
|
enabled: boolean;
|
||||||
|
baseIntervalMinutes: number;
|
||||||
|
jitterMinutes: number;
|
||||||
|
nextRunAt: string | null;
|
||||||
|
lastRunAt: string | null;
|
||||||
|
lastStatus: string | null;
|
||||||
|
} | null;
|
||||||
|
}>(`/api/markets/stores/${id}/crawl-history?limit=${limit}`);
|
||||||
|
}
|
||||||
|
|
||||||
// Global Brands/Categories (from v_brands/v_categories views)
|
// Global Brands/Categories (from v_brands/v_categories views)
|
||||||
async getMarketBrands(params?: { limit?: number; offset?: number }) {
|
async getMarketBrands(params?: { limit?: number; offset?: number }) {
|
||||||
const searchParams = new URLSearchParams();
|
const searchParams = new URLSearchParams();
|
||||||
@@ -2909,6 +2957,25 @@ class ApiClient {
|
|||||||
{ method: 'POST' }
|
{ method: 'POST' }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// K8s Worker Control
|
||||||
|
async getK8sWorkers() {
|
||||||
|
return this.request<{
|
||||||
|
success: boolean;
|
||||||
|
available: boolean;
|
||||||
|
replicas: number;
|
||||||
|
readyReplicas: number;
|
||||||
|
availableReplicas?: number;
|
||||||
|
error?: string;
|
||||||
|
}>('/api/k8s/workers');
|
||||||
|
}
|
||||||
|
|
||||||
|
async scaleK8sWorkers(replicas: number) {
|
||||||
|
return this.request<{ success: boolean; replicas: number; message?: string; error?: string }>(
|
||||||
|
'/api/k8s/workers/scale',
|
||||||
|
{ method: 'POST', body: JSON.stringify({ replicas }) }
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const api = new ApiClient(API_URL);
|
export const api = new ApiClient(API_URL);
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import { useEffect, useState } from 'react';
|
import { useEffect, useState } from 'react';
|
||||||
import { Layout } from '../components/Layout';
|
import { Layout } from '../components/Layout';
|
||||||
import { HealthPanel } from '../components/HealthPanel';
|
|
||||||
import { api } from '../lib/api';
|
import { api } from '../lib/api';
|
||||||
import { useNavigate } from 'react-router-dom';
|
import { useNavigate } from 'react-router-dom';
|
||||||
import {
|
import {
|
||||||
@@ -42,7 +41,6 @@ export function Dashboard() {
|
|||||||
const [activity, setActivity] = useState<any>(null);
|
const [activity, setActivity] = useState<any>(null);
|
||||||
const [nationalStats, setNationalStats] = useState<any>(null);
|
const [nationalStats, setNationalStats] = useState<any>(null);
|
||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
const [refreshing, setRefreshing] = useState(false);
|
|
||||||
const [pendingChangesCount, setPendingChangesCount] = useState(0);
|
const [pendingChangesCount, setPendingChangesCount] = useState(0);
|
||||||
const [showNotification, setShowNotification] = useState(false);
|
const [showNotification, setShowNotification] = useState(false);
|
||||||
const [taskCounts, setTaskCounts] = useState<Record<string, number> | null>(null);
|
const [taskCounts, setTaskCounts] = useState<Record<string, number> | null>(null);
|
||||||
@@ -93,10 +91,7 @@ export function Dashboard() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const loadData = async (isRefresh = false) => {
|
const loadData = async () => {
|
||||||
if (isRefresh) {
|
|
||||||
setRefreshing(true);
|
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
// Fetch dashboard data (primary data source)
|
// Fetch dashboard data (primary data source)
|
||||||
const dashboard = await api.getMarketDashboard();
|
const dashboard = await api.getMarketDashboard();
|
||||||
@@ -158,7 +153,6 @@ export function Dashboard() {
|
|||||||
console.error('Failed to load dashboard:', error);
|
console.error('Failed to load dashboard:', error);
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
setRefreshing(false);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -271,24 +265,11 @@ export function Dashboard() {
|
|||||||
|
|
||||||
<div className="space-y-8">
|
<div className="space-y-8">
|
||||||
{/* Header */}
|
{/* Header */}
|
||||||
<div className="flex flex-col sm:flex-row sm:justify-between sm:items-center gap-4">
|
<div>
|
||||||
<div>
|
<h1 className="text-xl sm:text-2xl font-semibold text-gray-900">Dashboard</h1>
|
||||||
<h1 className="text-xl sm:text-2xl font-semibold text-gray-900">Dashboard</h1>
|
<p className="text-sm text-gray-500 mt-1">Monitor your dispensary data aggregation</p>
|
||||||
<p className="text-sm text-gray-500 mt-1">Monitor your dispensary data aggregation</p>
|
|
||||||
</div>
|
|
||||||
<button
|
|
||||||
onClick={() => loadData(true)}
|
|
||||||
disabled={refreshing}
|
|
||||||
className="inline-flex items-center justify-center gap-2 px-4 py-2 bg-white border border-gray-200 rounded-lg hover:bg-gray-50 transition-colors text-sm font-medium text-gray-700 self-start sm:self-auto disabled:opacity-50 disabled:cursor-not-allowed"
|
|
||||||
>
|
|
||||||
<RefreshCw className={`w-4 h-4 ${refreshing ? 'animate-spin' : ''}`} />
|
|
||||||
{refreshing ? 'Refreshing...' : 'Refresh'}
|
|
||||||
</button>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* System Health */}
|
|
||||||
<HealthPanel showQueues={false} refreshInterval={60000} />
|
|
||||||
|
|
||||||
{/* Stats Grid */}
|
{/* Stats Grid */}
|
||||||
<div className="grid grid-cols-2 lg:grid-cols-3 gap-3 sm:gap-6">
|
<div className="grid grid-cols-2 lg:grid-cols-3 gap-3 sm:gap-6">
|
||||||
{/* Products */}
|
{/* Products */}
|
||||||
|
|||||||
@@ -161,23 +161,6 @@ export function Dispensaries() {
|
|||||||
))}
|
))}
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
|
||||||
<label className="block text-sm font-medium text-gray-700 mb-2">
|
|
||||||
Filter by Status
|
|
||||||
</label>
|
|
||||||
<select
|
|
||||||
value={filterStatus}
|
|
||||||
onChange={(e) => handleStatusFilter(e.target.value)}
|
|
||||||
className={`w-full px-3 py-2 border rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 ${
|
|
||||||
filterStatus === 'dropped' ? 'border-red-300 bg-red-50' : 'border-gray-300'
|
|
||||||
}`}
|
|
||||||
>
|
|
||||||
<option value="">All Statuses</option>
|
|
||||||
<option value="open">Open</option>
|
|
||||||
<option value="dropped">Dropped (Needs Review)</option>
|
|
||||||
<option value="closed">Closed</option>
|
|
||||||
</select>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
@@ -204,47 +204,6 @@ export function DispensaryDetail() {
|
|||||||
Back to Dispensaries
|
Back to Dispensaries
|
||||||
</button>
|
</button>
|
||||||
|
|
||||||
{/* Update Dropdown */}
|
|
||||||
<div className="relative">
|
|
||||||
<button
|
|
||||||
onClick={() => setShowUpdateDropdown(!showUpdateDropdown)}
|
|
||||||
disabled={isUpdating}
|
|
||||||
className="flex items-center gap-2 px-4 py-2 text-sm font-medium text-white bg-blue-600 hover:bg-blue-700 rounded-lg disabled:opacity-50 disabled:cursor-not-allowed"
|
|
||||||
>
|
|
||||||
<RefreshCw className={`w-4 h-4 ${isUpdating ? 'animate-spin' : ''}`} />
|
|
||||||
{isUpdating ? 'Updating...' : 'Update'}
|
|
||||||
{!isUpdating && <ChevronDown className="w-4 h-4" />}
|
|
||||||
</button>
|
|
||||||
|
|
||||||
{showUpdateDropdown && !isUpdating && (
|
|
||||||
<div className="absolute right-0 mt-2 w-48 bg-white rounded-lg shadow-lg border border-gray-200 z-10">
|
|
||||||
<button
|
|
||||||
onClick={() => handleUpdate('products')}
|
|
||||||
className="w-full text-left px-4 py-2 text-sm text-gray-700 hover:bg-gray-100 rounded-t-lg"
|
|
||||||
>
|
|
||||||
Products
|
|
||||||
</button>
|
|
||||||
<button
|
|
||||||
onClick={() => handleUpdate('brands')}
|
|
||||||
className="w-full text-left px-4 py-2 text-sm text-gray-700 hover:bg-gray-100"
|
|
||||||
>
|
|
||||||
Brands
|
|
||||||
</button>
|
|
||||||
<button
|
|
||||||
onClick={() => handleUpdate('specials')}
|
|
||||||
className="w-full text-left px-4 py-2 text-sm text-gray-700 hover:bg-gray-100"
|
|
||||||
>
|
|
||||||
Specials
|
|
||||||
</button>
|
|
||||||
<button
|
|
||||||
onClick={() => handleUpdate('all')}
|
|
||||||
className="w-full text-left px-4 py-2 text-sm text-gray-700 hover:bg-gray-100 rounded-b-lg border-t border-gray-200"
|
|
||||||
>
|
|
||||||
All
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Dispensary Header */}
|
{/* Dispensary Header */}
|
||||||
@@ -266,7 +225,7 @@ export function DispensaryDetail() {
|
|||||||
<div className="flex items-center gap-2 text-sm text-gray-600 bg-gray-50 px-4 py-2 rounded-lg">
|
<div className="flex items-center gap-2 text-sm text-gray-600 bg-gray-50 px-4 py-2 rounded-lg">
|
||||||
<Calendar className="w-4 h-4" />
|
<Calendar className="w-4 h-4" />
|
||||||
<div>
|
<div>
|
||||||
<span className="font-medium">Last Crawl Date:</span>
|
<span className="font-medium">Last Updated:</span>
|
||||||
<span className="ml-2">
|
<span className="ml-2">
|
||||||
{dispensary.last_menu_scrape
|
{dispensary.last_menu_scrape
|
||||||
? new Date(dispensary.last_menu_scrape).toLocaleDateString('en-US', {
|
? new Date(dispensary.last_menu_scrape).toLocaleDateString('en-US', {
|
||||||
@@ -331,7 +290,7 @@ export function DispensaryDetail() {
|
|||||||
</a>
|
</a>
|
||||||
)}
|
)}
|
||||||
<Link
|
<Link
|
||||||
to="/schedule"
|
to={`/dispensaries/${state}/${city}/${slug}/schedule`}
|
||||||
className="flex items-center gap-2 text-sm text-blue-600 hover:text-blue-800"
|
className="flex items-center gap-2 text-sm text-blue-600 hover:text-blue-800"
|
||||||
>
|
>
|
||||||
<Clock className="w-4 h-4" />
|
<Clock className="w-4 h-4" />
|
||||||
@@ -533,57 +492,31 @@ export function DispensaryDetail() {
|
|||||||
`$${product.regular_price}`
|
`$${product.regular_price}`
|
||||||
) : '-'}
|
) : '-'}
|
||||||
</td>
|
</td>
|
||||||
<td className="text-center whitespace-nowrap">
|
<td className="text-center whitespace-nowrap text-sm text-gray-700">
|
||||||
{product.quantity != null ? (
|
{product.quantity != null ? product.quantity : '-'}
|
||||||
<span className={`badge badge-sm ${product.quantity > 0 ? 'badge-info' : 'badge-error'}`}>
|
|
||||||
{product.quantity}
|
|
||||||
</span>
|
|
||||||
) : '-'}
|
|
||||||
</td>
|
</td>
|
||||||
<td className="text-center whitespace-nowrap">
|
<td className="text-center whitespace-nowrap text-sm text-gray-700">
|
||||||
{product.thc_percentage ? (
|
{product.thc_percentage ? `${product.thc_percentage}%` : '-'}
|
||||||
<span className="badge badge-success badge-sm">{product.thc_percentage}%</span>
|
|
||||||
) : '-'}
|
|
||||||
</td>
|
</td>
|
||||||
<td className="text-center whitespace-nowrap">
|
<td className="text-center whitespace-nowrap text-sm text-gray-700">
|
||||||
{product.cbd_percentage ? (
|
{product.cbd_percentage ? `${product.cbd_percentage}%` : '-'}
|
||||||
<span className="badge badge-info badge-sm">{product.cbd_percentage}%</span>
|
|
||||||
) : '-'}
|
|
||||||
</td>
|
</td>
|
||||||
<td className="text-center whitespace-nowrap">
|
<td className="text-center whitespace-nowrap text-sm text-gray-700">
|
||||||
{product.strain_type ? (
|
{product.strain_type || '-'}
|
||||||
<span className="badge badge-ghost badge-sm">{product.strain_type}</span>
|
|
||||||
) : '-'}
|
|
||||||
</td>
|
</td>
|
||||||
<td className="text-center whitespace-nowrap">
|
<td className="text-center whitespace-nowrap text-sm text-gray-700">
|
||||||
{product.in_stock ? (
|
{product.in_stock ? 'Yes' : product.in_stock === false ? 'No' : '-'}
|
||||||
<span className="badge badge-success badge-sm">Yes</span>
|
|
||||||
) : product.in_stock === false ? (
|
|
||||||
<span className="badge badge-error badge-sm">No</span>
|
|
||||||
) : '-'}
|
|
||||||
</td>
|
</td>
|
||||||
<td className="whitespace-nowrap text-xs text-gray-500">
|
<td className="whitespace-nowrap text-xs text-gray-500">
|
||||||
{product.updated_at ? formatDate(product.updated_at) : '-'}
|
{product.updated_at ? formatDate(product.updated_at) : '-'}
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<div className="flex gap-1">
|
<button
|
||||||
{product.dutchie_url && (
|
onClick={() => navigate(`/products/${product.id}`)}
|
||||||
<a
|
className="btn btn-xs btn-ghost text-gray-500 hover:text-gray-700"
|
||||||
href={product.dutchie_url}
|
>
|
||||||
target="_blank"
|
Details
|
||||||
rel="noopener noreferrer"
|
</button>
|
||||||
className="btn btn-xs btn-outline"
|
|
||||||
>
|
|
||||||
Dutchie
|
|
||||||
</a>
|
|
||||||
)}
|
|
||||||
<button
|
|
||||||
onClick={() => navigate(`/products/${product.id}`)}
|
|
||||||
className="btn btn-xs btn-primary"
|
|
||||||
>
|
|
||||||
Details
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
))}
|
))}
|
||||||
|
|||||||
378
cannaiq/src/pages/DispensarySchedule.tsx
Normal file
378
cannaiq/src/pages/DispensarySchedule.tsx
Normal file
@@ -0,0 +1,378 @@
|
|||||||
|
import { useEffect, useState } from 'react';
|
||||||
|
import { useParams, useNavigate, Link } from 'react-router-dom';
|
||||||
|
import { Layout } from '../components/Layout';
|
||||||
|
import { api } from '../lib/api';
|
||||||
|
import {
|
||||||
|
ArrowLeft,
|
||||||
|
Clock,
|
||||||
|
Calendar,
|
||||||
|
CheckCircle,
|
||||||
|
XCircle,
|
||||||
|
AlertCircle,
|
||||||
|
Package,
|
||||||
|
Timer,
|
||||||
|
Building2,
|
||||||
|
} from 'lucide-react';
|
||||||
|
|
||||||
|
interface CrawlHistoryItem {
|
||||||
|
id: number;
|
||||||
|
runId: string | null;
|
||||||
|
profileKey: string | null;
|
||||||
|
crawlerModule: string | null;
|
||||||
|
stateAtStart: string | null;
|
||||||
|
stateAtEnd: string | null;
|
||||||
|
totalSteps: number;
|
||||||
|
durationMs: number | null;
|
||||||
|
success: boolean;
|
||||||
|
errorMessage: string | null;
|
||||||
|
productsFound: number | null;
|
||||||
|
startedAt: string | null;
|
||||||
|
completedAt: string | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface NextSchedule {
|
||||||
|
scheduleId: number;
|
||||||
|
jobName: string;
|
||||||
|
enabled: boolean;
|
||||||
|
baseIntervalMinutes: number;
|
||||||
|
jitterMinutes: number;
|
||||||
|
nextRunAt: string | null;
|
||||||
|
lastRunAt: string | null;
|
||||||
|
lastStatus: string | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Dispensary {
|
||||||
|
id: number;
|
||||||
|
name: string;
|
||||||
|
dba_name: string | null;
|
||||||
|
slug: string;
|
||||||
|
state: string;
|
||||||
|
city: string;
|
||||||
|
menu_type: string | null;
|
||||||
|
platform_dispensary_id: string | null;
|
||||||
|
last_menu_scrape: string | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function DispensarySchedule() {
|
||||||
|
const { state, city, slug } = useParams();
|
||||||
|
const navigate = useNavigate();
|
||||||
|
const [dispensary, setDispensary] = useState<Dispensary | null>(null);
|
||||||
|
const [history, setHistory] = useState<CrawlHistoryItem[]>([]);
|
||||||
|
const [nextSchedule, setNextSchedule] = useState<NextSchedule | null>(null);
|
||||||
|
const [loading, setLoading] = useState(true);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
loadScheduleData();
|
||||||
|
}, [slug]);
|
||||||
|
|
||||||
|
const loadScheduleData = async () => {
|
||||||
|
setLoading(true);
|
||||||
|
try {
|
||||||
|
// First get the dispensary to get the ID
|
||||||
|
const dispData = await api.getDispensary(slug!);
|
||||||
|
if (dispData?.id) {
|
||||||
|
const data = await api.getStoreCrawlHistory(dispData.id);
|
||||||
|
setDispensary(data.dispensary);
|
||||||
|
setHistory(data.history || []);
|
||||||
|
setNextSchedule(data.nextSchedule);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to load schedule data:', error);
|
||||||
|
} finally {
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const formatDate = (dateStr: string | null) => {
|
||||||
|
if (!dateStr) return 'Never';
|
||||||
|
const date = new Date(dateStr);
|
||||||
|
return date.toLocaleDateString('en-US', {
|
||||||
|
year: 'numeric',
|
||||||
|
month: 'short',
|
||||||
|
day: 'numeric',
|
||||||
|
hour: '2-digit',
|
||||||
|
minute: '2-digit',
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const formatTimeAgo = (dateStr: string | null) => {
|
||||||
|
if (!dateStr) return 'Never';
|
||||||
|
const date = new Date(dateStr);
|
||||||
|
const now = new Date();
|
||||||
|
const diffMs = now.getTime() - date.getTime();
|
||||||
|
const diffMinutes = Math.floor(diffMs / (1000 * 60));
|
||||||
|
const diffHours = Math.floor(diffMs / (1000 * 60 * 60));
|
||||||
|
const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24));
|
||||||
|
|
||||||
|
if (diffMinutes < 1) return 'Just now';
|
||||||
|
if (diffMinutes < 60) return `${diffMinutes}m ago`;
|
||||||
|
if (diffHours < 24) return `${diffHours}h ago`;
|
||||||
|
if (diffDays === 1) return 'Yesterday';
|
||||||
|
if (diffDays < 7) return `${diffDays} days ago`;
|
||||||
|
return date.toLocaleDateString();
|
||||||
|
};
|
||||||
|
|
||||||
|
const formatTimeUntil = (dateStr: string | null) => {
|
||||||
|
if (!dateStr) return 'Not scheduled';
|
||||||
|
const date = new Date(dateStr);
|
||||||
|
const now = new Date();
|
||||||
|
const diffMs = date.getTime() - now.getTime();
|
||||||
|
|
||||||
|
if (diffMs < 0) return 'Overdue';
|
||||||
|
|
||||||
|
const diffMinutes = Math.floor(diffMs / (1000 * 60));
|
||||||
|
const diffHours = Math.floor(diffMinutes / 60);
|
||||||
|
|
||||||
|
if (diffMinutes < 60) return `in ${diffMinutes}m`;
|
||||||
|
return `in ${diffHours}h ${diffMinutes % 60}m`;
|
||||||
|
};
|
||||||
|
|
||||||
|
const formatDuration = (ms: number | null) => {
|
||||||
|
if (!ms) return '-';
|
||||||
|
if (ms < 1000) return `${ms}ms`;
|
||||||
|
const seconds = Math.floor(ms / 1000);
|
||||||
|
const minutes = Math.floor(seconds / 60);
|
||||||
|
if (minutes < 1) return `${seconds}s`;
|
||||||
|
return `${minutes}m ${seconds % 60}s`;
|
||||||
|
};
|
||||||
|
|
||||||
|
const formatInterval = (baseMinutes: number, jitterMinutes: number) => {
|
||||||
|
const hours = Math.floor(baseMinutes / 60);
|
||||||
|
const mins = baseMinutes % 60;
|
||||||
|
let base = hours > 0 ? `${hours}h` : '';
|
||||||
|
if (mins > 0) base += `${mins}m`;
|
||||||
|
return `Every ${base} (+/- ${jitterMinutes}m jitter)`;
|
||||||
|
};
|
||||||
|
|
||||||
|
if (loading) {
|
||||||
|
return (
|
||||||
|
<Layout>
|
||||||
|
<div className="text-center py-12">
|
||||||
|
<div className="inline-block animate-spin rounded-full h-8 w-8 border-4 border-gray-400 border-t-transparent"></div>
|
||||||
|
<p className="mt-2 text-sm text-gray-600">Loading schedule...</p>
|
||||||
|
</div>
|
||||||
|
</Layout>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!dispensary) {
|
||||||
|
return (
|
||||||
|
<Layout>
|
||||||
|
<div className="text-center py-12">
|
||||||
|
<p className="text-gray-600">Dispensary not found</p>
|
||||||
|
</div>
|
||||||
|
</Layout>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stats from history
|
||||||
|
const successCount = history.filter(h => h.success).length;
|
||||||
|
const failureCount = history.filter(h => !h.success).length;
|
||||||
|
const lastSuccess = history.find(h => h.success);
|
||||||
|
const avgDuration = history.length > 0
|
||||||
|
? Math.round(history.reduce((sum, h) => sum + (h.durationMs || 0), 0) / history.length)
|
||||||
|
: 0;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Layout>
|
||||||
|
<div className="space-y-6">
|
||||||
|
{/* Header */}
|
||||||
|
<div className="flex items-center justify-between gap-4">
|
||||||
|
<button
|
||||||
|
onClick={() => navigate(`/dispensaries/${state}/${city}/${slug}`)}
|
||||||
|
className="flex items-center gap-2 text-sm text-gray-600 hover:text-gray-900"
|
||||||
|
>
|
||||||
|
<ArrowLeft className="w-4 h-4" />
|
||||||
|
Back to {dispensary.dba_name || dispensary.name}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Dispensary Info */}
|
||||||
|
<div className="bg-white rounded-lg border border-gray-200 p-6">
|
||||||
|
<div className="flex items-start gap-4">
|
||||||
|
<div className="p-3 bg-blue-50 rounded-lg">
|
||||||
|
<Building2 className="w-8 h-8 text-blue-600" />
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<h1 className="text-2xl font-bold text-gray-900">
|
||||||
|
{dispensary.dba_name || dispensary.name}
|
||||||
|
</h1>
|
||||||
|
<p className="text-sm text-gray-600 mt-1">
|
||||||
|
{dispensary.city}, {dispensary.state} - Crawl Schedule & History
|
||||||
|
</p>
|
||||||
|
<div className="flex items-center gap-4 mt-2 text-sm text-gray-500">
|
||||||
|
<span>Slug: {dispensary.slug}</span>
|
||||||
|
{dispensary.menu_type && (
|
||||||
|
<span className="px-2 py-0.5 bg-gray-100 rounded text-xs">
|
||||||
|
{dispensary.menu_type}
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Next Scheduled Crawl */}
|
||||||
|
{nextSchedule && (
|
||||||
|
<div className="bg-white rounded-lg border border-gray-200 p-6">
|
||||||
|
<h2 className="text-lg font-semibold text-gray-900 mb-4 flex items-center gap-2">
|
||||||
|
<Clock className="w-5 h-5 text-blue-500" />
|
||||||
|
Upcoming Schedule
|
||||||
|
</h2>
|
||||||
|
<div className="grid grid-cols-4 gap-6">
|
||||||
|
<div>
|
||||||
|
<p className="text-sm text-gray-500">Next Run</p>
|
||||||
|
<p className="text-xl font-semibold text-blue-600">
|
||||||
|
{formatTimeUntil(nextSchedule.nextRunAt)}
|
||||||
|
</p>
|
||||||
|
<p className="text-xs text-gray-400">
|
||||||
|
{formatDate(nextSchedule.nextRunAt)}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<p className="text-sm text-gray-500">Interval</p>
|
||||||
|
<p className="text-lg font-medium">
|
||||||
|
{formatInterval(nextSchedule.baseIntervalMinutes, nextSchedule.jitterMinutes)}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<p className="text-sm text-gray-500">Last Run</p>
|
||||||
|
<p className="text-lg font-medium">
|
||||||
|
{formatTimeAgo(nextSchedule.lastRunAt)}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<p className="text-sm text-gray-500">Last Status</p>
|
||||||
|
<p className={`text-lg font-medium ${
|
||||||
|
nextSchedule.lastStatus === 'success' ? 'text-green-600' :
|
||||||
|
nextSchedule.lastStatus === 'error' ? 'text-red-600' : 'text-gray-600'
|
||||||
|
}`}>
|
||||||
|
{nextSchedule.lastStatus || '-'}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* Stats Summary */}
|
||||||
|
<div className="grid grid-cols-4 gap-4">
|
||||||
|
<div className="bg-white rounded-lg border border-gray-200 p-4">
|
||||||
|
<div className="flex items-center gap-3">
|
||||||
|
<CheckCircle className="w-8 h-8 text-green-500" />
|
||||||
|
<div>
|
||||||
|
<p className="text-sm text-gray-500">Successful Runs</p>
|
||||||
|
<p className="text-2xl font-bold text-green-600">{successCount}</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div className="bg-white rounded-lg border border-gray-200 p-4">
|
||||||
|
<div className="flex items-center gap-3">
|
||||||
|
<XCircle className="w-8 h-8 text-red-500" />
|
||||||
|
<div>
|
||||||
|
<p className="text-sm text-gray-500">Failed Runs</p>
|
||||||
|
<p className="text-2xl font-bold text-red-600">{failureCount}</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div className="bg-white rounded-lg border border-gray-200 p-4">
|
||||||
|
<div className="flex items-center gap-3">
|
||||||
|
<Timer className="w-8 h-8 text-blue-500" />
|
||||||
|
<div>
|
||||||
|
<p className="text-sm text-gray-500">Avg Duration</p>
|
||||||
|
<p className="text-2xl font-bold">{formatDuration(avgDuration)}</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div className="bg-white rounded-lg border border-gray-200 p-4">
|
||||||
|
<div className="flex items-center gap-3">
|
||||||
|
<Package className="w-8 h-8 text-purple-500" />
|
||||||
|
<div>
|
||||||
|
<p className="text-sm text-gray-500">Last Products Found</p>
|
||||||
|
<p className="text-2xl font-bold">
|
||||||
|
{lastSuccess?.productsFound?.toLocaleString() || '-'}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Crawl History Table */}
|
||||||
|
<div className="bg-white rounded-lg border border-gray-200">
|
||||||
|
<div className="p-4 border-b border-gray-200">
|
||||||
|
<h2 className="text-lg font-semibold text-gray-900 flex items-center gap-2">
|
||||||
|
<Calendar className="w-5 h-5 text-gray-500" />
|
||||||
|
Crawl History
|
||||||
|
</h2>
|
||||||
|
</div>
|
||||||
|
<div className="overflow-x-auto">
|
||||||
|
<table className="table table-sm w-full">
|
||||||
|
<thead className="bg-gray-50">
|
||||||
|
<tr>
|
||||||
|
<th>Status</th>
|
||||||
|
<th>Started</th>
|
||||||
|
<th>Duration</th>
|
||||||
|
<th className="text-right">Products</th>
|
||||||
|
<th>State</th>
|
||||||
|
<th>Error</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{history.length === 0 ? (
|
||||||
|
<tr>
|
||||||
|
<td colSpan={6} className="text-center py-8 text-gray-500">
|
||||||
|
No crawl history available
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
) : (
|
||||||
|
history.map((item) => (
|
||||||
|
<tr key={item.id} className="hover:bg-gray-50">
|
||||||
|
<td>
|
||||||
|
<span className={`inline-flex items-center gap-1 px-2 py-1 rounded text-xs font-medium ${
|
||||||
|
item.success
|
||||||
|
? 'bg-green-100 text-green-700'
|
||||||
|
: 'bg-red-100 text-red-700'
|
||||||
|
}`}>
|
||||||
|
{item.success ? (
|
||||||
|
<CheckCircle className="w-3 h-3" />
|
||||||
|
) : (
|
||||||
|
<XCircle className="w-3 h-3" />
|
||||||
|
)}
|
||||||
|
{item.success ? 'Success' : 'Failed'}
|
||||||
|
</span>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<div className="text-sm">{formatDate(item.startedAt)}</div>
|
||||||
|
<div className="text-xs text-gray-400">{formatTimeAgo(item.startedAt)}</div>
|
||||||
|
</td>
|
||||||
|
<td className="font-mono text-sm">
|
||||||
|
{formatDuration(item.durationMs)}
|
||||||
|
</td>
|
||||||
|
<td className="text-right font-mono text-sm">
|
||||||
|
{item.productsFound?.toLocaleString() || '-'}
|
||||||
|
</td>
|
||||||
|
<td className="text-sm text-gray-600">
|
||||||
|
{item.stateAtEnd || item.stateAtStart || '-'}
|
||||||
|
</td>
|
||||||
|
<td className="max-w-[200px]">
|
||||||
|
{item.errorMessage ? (
|
||||||
|
<span
|
||||||
|
className="text-xs text-red-600 truncate block cursor-help"
|
||||||
|
title={item.errorMessage}
|
||||||
|
>
|
||||||
|
{item.errorMessage.substring(0, 50)}...
|
||||||
|
</span>
|
||||||
|
) : '-'}
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
))
|
||||||
|
)}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</Layout>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export default DispensarySchedule;
|
||||||
@@ -9,7 +9,6 @@ import {
|
|||||||
MapPin,
|
MapPin,
|
||||||
Package,
|
Package,
|
||||||
DollarSign,
|
DollarSign,
|
||||||
RefreshCw,
|
|
||||||
Search,
|
Search,
|
||||||
TrendingUp,
|
TrendingUp,
|
||||||
BarChart3,
|
BarChart3,
|
||||||
@@ -32,7 +31,7 @@ export function IntelligenceBrands() {
|
|||||||
const [brands, setBrands] = useState<BrandData[]>([]);
|
const [brands, setBrands] = useState<BrandData[]>([]);
|
||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
const [searchTerm, setSearchTerm] = useState('');
|
const [searchTerm, setSearchTerm] = useState('');
|
||||||
const [sortBy, setSortBy] = useState<'stores' | 'skus' | 'name'>('stores');
|
const [sortBy, setSortBy] = useState<'stores' | 'skus' | 'name' | 'states'>('stores');
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
loadBrands();
|
loadBrands();
|
||||||
@@ -69,6 +68,8 @@ export function IntelligenceBrands() {
|
|||||||
return b.skuCount - a.skuCount;
|
return b.skuCount - a.skuCount;
|
||||||
case 'name':
|
case 'name':
|
||||||
return a.brandName.localeCompare(b.brandName);
|
return a.brandName.localeCompare(b.brandName);
|
||||||
|
case 'states':
|
||||||
|
return b.states.length - a.states.length;
|
||||||
default:
|
default:
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -100,35 +101,60 @@ export function IntelligenceBrands() {
|
|||||||
<Layout>
|
<Layout>
|
||||||
<div className="space-y-6">
|
<div className="space-y-6">
|
||||||
{/* Header */}
|
{/* Header */}
|
||||||
<div className="flex items-center justify-between">
|
<div className="flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between">
|
||||||
<div>
|
<div>
|
||||||
<h1 className="text-2xl font-bold text-gray-900">Brands Intelligence</h1>
|
<h1 className="text-2xl font-bold text-gray-900">Brands Intelligence</h1>
|
||||||
<p className="text-sm text-gray-600 mt-1">
|
<p className="text-sm text-gray-600 mt-1">
|
||||||
Brand penetration and pricing analytics across markets
|
Brand penetration and pricing analytics across markets
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex gap-2">
|
<div className="flex flex-wrap gap-2 items-center">
|
||||||
<button
|
{/* State Selector */}
|
||||||
onClick={() => navigate('/admin/intelligence/pricing')}
|
<div className="dropdown dropdown-end">
|
||||||
className="btn btn-sm btn-outline gap-1"
|
<button tabIndex={0} className="btn btn-sm gap-2 bg-emerald-50 border-emerald-200 hover:bg-emerald-100">
|
||||||
>
|
{stateLabel}
|
||||||
<DollarSign className="w-4 h-4" />
|
<ChevronDown className="w-4 h-4" />
|
||||||
Pricing
|
</button>
|
||||||
</button>
|
<ul tabIndex={0} className="dropdown-content z-50 menu p-2 shadow-lg bg-white rounded-box w-44 max-h-60 overflow-y-auto border border-gray-200">
|
||||||
<button
|
<li>
|
||||||
onClick={() => navigate('/admin/intelligence/stores')}
|
<a onClick={() => setSelectedState(null)} className={isAllStates ? 'active bg-emerald-100' : ''}>
|
||||||
className="btn btn-sm btn-outline gap-1"
|
All States
|
||||||
>
|
</a>
|
||||||
<MapPin className="w-4 h-4" />
|
</li>
|
||||||
Stores
|
<div className="divider my-1"></div>
|
||||||
</button>
|
{availableStates.map((state) => (
|
||||||
<button
|
<li key={state}>
|
||||||
onClick={loadBrands}
|
<a onClick={() => setSelectedState(state)} className={selectedState === state ? 'active bg-emerald-100' : ''}>
|
||||||
className="btn btn-sm btn-outline gap-2"
|
{state}
|
||||||
>
|
</a>
|
||||||
<RefreshCw className="w-4 h-4" />
|
</li>
|
||||||
Refresh
|
))}
|
||||||
</button>
|
</ul>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Page Navigation */}
|
||||||
|
<div className="flex gap-1">
|
||||||
|
<button
|
||||||
|
className="btn btn-sm gap-1 bg-emerald-600 text-white hover:bg-emerald-700 border-emerald-600"
|
||||||
|
>
|
||||||
|
<Building2 className="w-4 h-4" />
|
||||||
|
<span>Brands</span>
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
onClick={() => navigate('/admin/intelligence/stores')}
|
||||||
|
className="btn btn-sm gap-1 bg-white border-gray-300 text-gray-700 hover:bg-gray-100"
|
||||||
|
>
|
||||||
|
<MapPin className="w-4 h-4" />
|
||||||
|
<span>Stores</span>
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
onClick={() => navigate('/admin/intelligence/pricing')}
|
||||||
|
className="btn btn-sm gap-1 bg-white border-gray-300 text-gray-700 hover:bg-gray-100"
|
||||||
|
>
|
||||||
|
<DollarSign className="w-4 h-4" />
|
||||||
|
<span>Pricing</span>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@@ -180,51 +206,32 @@ export function IntelligenceBrands() {
|
|||||||
|
|
||||||
{/* Top Brands Chart */}
|
{/* Top Brands Chart */}
|
||||||
<div className="bg-white rounded-lg border border-gray-200 p-4">
|
<div className="bg-white rounded-lg border border-gray-200 p-4">
|
||||||
<div className="flex items-center justify-between mb-4">
|
<h3 className="text-lg font-semibold text-gray-900 flex items-center gap-2 mb-4">
|
||||||
<h3 className="text-lg font-semibold text-gray-900 flex items-center gap-2">
|
<BarChart3 className="w-5 h-5 text-emerald-500" />
|
||||||
<BarChart3 className="w-5 h-5 text-blue-500" />
|
Top 10 Brands by Store Count
|
||||||
Top 10 Brands by Store Count
|
</h3>
|
||||||
</h3>
|
|
||||||
<div className="dropdown dropdown-end">
|
|
||||||
<button tabIndex={0} className="btn btn-sm btn-outline gap-2">
|
|
||||||
{stateLabel}
|
|
||||||
<ChevronDown className="w-4 h-4" />
|
|
||||||
</button>
|
|
||||||
<ul tabIndex={0} className="dropdown-content z-[1] menu p-2 shadow bg-base-100 rounded-box w-40 max-h-60 overflow-y-auto">
|
|
||||||
<li>
|
|
||||||
<a onClick={() => setSelectedState(null)} className={isAllStates ? 'active' : ''}>
|
|
||||||
All States
|
|
||||||
</a>
|
|
||||||
</li>
|
|
||||||
<li className="divider"></li>
|
|
||||||
{availableStates.map((state) => (
|
|
||||||
<li key={state}>
|
|
||||||
<a onClick={() => setSelectedState(state)} className={selectedState === state ? 'active' : ''}>
|
|
||||||
{state}
|
|
||||||
</a>
|
|
||||||
</li>
|
|
||||||
))}
|
|
||||||
</ul>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div className="space-y-2">
|
<div className="space-y-2">
|
||||||
{topBrands.map((brand, idx) => (
|
{topBrands.map((brand) => {
|
||||||
<div key={brand.brandName} className="flex items-center gap-3">
|
const barWidth = Math.min((brand.storeCount / maxStoreCount) * 100, 100);
|
||||||
<span className="text-sm text-gray-500 w-6">{idx + 1}.</span>
|
return (
|
||||||
<span className="text-sm font-medium w-40 truncate" title={brand.brandName}>
|
<div key={brand.brandName} className="flex items-center gap-3">
|
||||||
{brand.brandName}
|
<span className="text-sm font-medium w-28 truncate shrink-0" title={brand.brandName}>
|
||||||
</span>
|
{brand.brandName}
|
||||||
<div className="flex-1 bg-gray-100 rounded-full h-4 relative">
|
</span>
|
||||||
<div
|
<div className="flex-1 min-w-0">
|
||||||
className="bg-blue-500 rounded-full h-4"
|
<div className="bg-gray-100 rounded h-5 overflow-hidden">
|
||||||
style={{ width: `${(brand.storeCount / maxStoreCount) * 100}%` }}
|
<div
|
||||||
/>
|
className="bg-gradient-to-r from-emerald-400 to-emerald-500 h-5 rounded transition-all"
|
||||||
|
style={{ width: `${barWidth}%` }}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<span className="text-sm font-mono font-semibold text-emerald-600 w-16 text-right shrink-0">
|
||||||
|
{brand.storeCount}
|
||||||
|
</span>
|
||||||
</div>
|
</div>
|
||||||
<span className="text-sm text-gray-600 w-16 text-right">
|
);
|
||||||
{brand.storeCount} stores
|
})}
|
||||||
</span>
|
|
||||||
</div>
|
|
||||||
))}
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@@ -247,6 +254,7 @@ export function IntelligenceBrands() {
|
|||||||
>
|
>
|
||||||
<option value="stores">Sort by Stores</option>
|
<option value="stores">Sort by Stores</option>
|
||||||
<option value="skus">Sort by SKUs</option>
|
<option value="skus">Sort by SKUs</option>
|
||||||
|
<option value="states">Sort by States</option>
|
||||||
<option value="name">Sort by Name</option>
|
<option value="name">Sort by Name</option>
|
||||||
</select>
|
</select>
|
||||||
<span className="text-sm text-gray-500">
|
<span className="text-sm text-gray-500">
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ import {
|
|||||||
Building2,
|
Building2,
|
||||||
MapPin,
|
MapPin,
|
||||||
Package,
|
Package,
|
||||||
RefreshCw,
|
|
||||||
TrendingUp,
|
TrendingUp,
|
||||||
TrendingDown,
|
TrendingDown,
|
||||||
BarChart3,
|
BarChart3,
|
||||||
@@ -87,56 +86,60 @@ export function IntelligencePricing() {
|
|||||||
<Layout>
|
<Layout>
|
||||||
<div className="space-y-6">
|
<div className="space-y-6">
|
||||||
{/* Header */}
|
{/* Header */}
|
||||||
<div className="flex items-center justify-between">
|
<div className="flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between">
|
||||||
<div>
|
<div>
|
||||||
<h1 className="text-2xl font-bold text-gray-900">Pricing Intelligence</h1>
|
<h1 className="text-2xl font-bold text-gray-900">Pricing Intelligence</h1>
|
||||||
<p className="text-sm text-gray-600 mt-1">
|
<p className="text-sm text-gray-600 mt-1">
|
||||||
Price distribution and trends by category
|
Price distribution and trends by category
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex gap-2">
|
<div className="flex flex-wrap gap-2 items-center">
|
||||||
|
{/* State Selector */}
|
||||||
<div className="dropdown dropdown-end">
|
<div className="dropdown dropdown-end">
|
||||||
<button tabIndex={0} className="btn btn-sm btn-outline gap-2">
|
<button tabIndex={0} className="btn btn-sm gap-2 bg-emerald-50 border-emerald-200 hover:bg-emerald-100">
|
||||||
{stateLabel}
|
{stateLabel}
|
||||||
<ChevronDown className="w-4 h-4" />
|
<ChevronDown className="w-4 h-4" />
|
||||||
</button>
|
</button>
|
||||||
<ul tabIndex={0} className="dropdown-content z-[1] menu p-2 shadow bg-base-100 rounded-box w-40 max-h-60 overflow-y-auto">
|
<ul tabIndex={0} className="dropdown-content z-50 menu p-2 shadow-lg bg-white rounded-box w-44 max-h-60 overflow-y-auto border border-gray-200">
|
||||||
<li>
|
<li>
|
||||||
<a onClick={() => setSelectedState(null)} className={isAllStates ? 'active' : ''}>
|
<a onClick={() => setSelectedState(null)} className={isAllStates ? 'active bg-emerald-100' : ''}>
|
||||||
All States
|
All States
|
||||||
</a>
|
</a>
|
||||||
</li>
|
</li>
|
||||||
<li className="divider"></li>
|
<div className="divider my-1"></div>
|
||||||
{availableStates.map((state) => (
|
{availableStates.map((state) => (
|
||||||
<li key={state}>
|
<li key={state}>
|
||||||
<a onClick={() => setSelectedState(state)} className={selectedState === state ? 'active' : ''}>
|
<a onClick={() => setSelectedState(state)} className={selectedState === state ? 'active bg-emerald-100' : ''}>
|
||||||
{state}
|
{state}
|
||||||
</a>
|
</a>
|
||||||
</li>
|
</li>
|
||||||
))}
|
))}
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
<button
|
|
||||||
onClick={() => navigate('/admin/intelligence/brands')}
|
{/* Page Navigation */}
|
||||||
className="btn btn-sm btn-outline gap-1"
|
<div className="flex gap-1">
|
||||||
>
|
<button
|
||||||
<Building2 className="w-4 h-4" />
|
onClick={() => navigate('/admin/intelligence/brands')}
|
||||||
Brands
|
className="btn btn-sm gap-1 bg-white border-gray-300 text-gray-700 hover:bg-gray-100"
|
||||||
</button>
|
>
|
||||||
<button
|
<Building2 className="w-4 h-4" />
|
||||||
onClick={() => navigate('/admin/intelligence/stores')}
|
<span>Brands</span>
|
||||||
className="btn btn-sm btn-outline gap-1"
|
</button>
|
||||||
>
|
<button
|
||||||
<MapPin className="w-4 h-4" />
|
onClick={() => navigate('/admin/intelligence/stores')}
|
||||||
Stores
|
className="btn btn-sm gap-1 bg-white border-gray-300 text-gray-700 hover:bg-gray-100"
|
||||||
</button>
|
>
|
||||||
<button
|
<MapPin className="w-4 h-4" />
|
||||||
onClick={loadPricing}
|
<span>Stores</span>
|
||||||
className="btn btn-sm btn-outline gap-2"
|
</button>
|
||||||
>
|
<button
|
||||||
<RefreshCw className="w-4 h-4" />
|
className="btn btn-sm gap-1 bg-emerald-600 text-white hover:bg-emerald-700 border-emerald-600"
|
||||||
Refresh
|
>
|
||||||
</button>
|
<DollarSign className="w-4 h-4" />
|
||||||
|
<span>Pricing</span>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ import {
|
|||||||
Building2,
|
Building2,
|
||||||
DollarSign,
|
DollarSign,
|
||||||
Package,
|
Package,
|
||||||
RefreshCw,
|
|
||||||
Search,
|
Search,
|
||||||
Clock,
|
Clock,
|
||||||
Activity,
|
Activity,
|
||||||
@@ -34,12 +33,19 @@ export function IntelligenceStores() {
|
|||||||
const [stores, setStores] = useState<StoreActivity[]>([]);
|
const [stores, setStores] = useState<StoreActivity[]>([]);
|
||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
const [searchTerm, setSearchTerm] = useState('');
|
const [searchTerm, setSearchTerm] = useState('');
|
||||||
const [localStates, setLocalStates] = useState<string[]>([]);
|
const [availableStates, setAvailableStates] = useState<string[]>([]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
loadStores();
|
loadStores();
|
||||||
}, [selectedState]);
|
}, [selectedState]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
// Load available states from orchestrator API
|
||||||
|
api.getOrchestratorStates().then(data => {
|
||||||
|
setAvailableStates(data.states?.map((s: any) => s.state) || []);
|
||||||
|
}).catch(console.error);
|
||||||
|
}, []);
|
||||||
|
|
||||||
const loadStores = async () => {
|
const loadStores = async () => {
|
||||||
try {
|
try {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
@@ -47,12 +53,7 @@ export function IntelligenceStores() {
|
|||||||
state: stateParam,
|
state: stateParam,
|
||||||
limit: 500,
|
limit: 500,
|
||||||
});
|
});
|
||||||
const storeList = data.stores || [];
|
setStores(data.stores || []);
|
||||||
setStores(storeList);
|
|
||||||
|
|
||||||
// Extract unique states from response for dropdown counts
|
|
||||||
const uniqueStates = [...new Set(storeList.map((s: StoreActivity) => s.state))].filter(Boolean).sort() as string[];
|
|
||||||
setLocalStates(uniqueStates);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to load stores:', error);
|
console.error('Failed to load stores:', error);
|
||||||
} finally {
|
} finally {
|
||||||
@@ -110,35 +111,60 @@ export function IntelligenceStores() {
|
|||||||
<Layout>
|
<Layout>
|
||||||
<div className="space-y-6">
|
<div className="space-y-6">
|
||||||
{/* Header */}
|
{/* Header */}
|
||||||
<div className="flex items-center justify-between">
|
<div className="flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between">
|
||||||
<div>
|
<div>
|
||||||
<h1 className="text-2xl font-bold text-gray-900">Store Activity</h1>
|
<h1 className="text-2xl font-bold text-gray-900">Store Activity</h1>
|
||||||
<p className="text-sm text-gray-600 mt-1">
|
<p className="text-sm text-gray-600 mt-1">
|
||||||
Per-store SKU counts, snapshots, and crawl frequency
|
Per-store SKU counts, snapshots, and crawl frequency
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex gap-2">
|
<div className="flex flex-wrap gap-2 items-center">
|
||||||
<button
|
{/* State Selector */}
|
||||||
onClick={() => navigate('/admin/intelligence/brands')}
|
<div className="dropdown dropdown-end">
|
||||||
className="btn btn-sm btn-outline gap-1"
|
<button tabIndex={0} className="btn btn-sm gap-2 bg-emerald-50 border-emerald-200 hover:bg-emerald-100">
|
||||||
>
|
{stateLabel}
|
||||||
<Building2 className="w-4 h-4" />
|
<ChevronDown className="w-4 h-4" />
|
||||||
Brands
|
</button>
|
||||||
</button>
|
<ul tabIndex={0} className="dropdown-content z-50 menu p-2 shadow-lg bg-white rounded-box w-44 max-h-60 overflow-y-auto border border-gray-200">
|
||||||
<button
|
<li>
|
||||||
onClick={() => navigate('/admin/intelligence/pricing')}
|
<a onClick={() => setSelectedState(null)} className={isAllStates ? 'active bg-emerald-100' : ''}>
|
||||||
className="btn btn-sm btn-outline gap-1"
|
All States
|
||||||
>
|
</a>
|
||||||
<DollarSign className="w-4 h-4" />
|
</li>
|
||||||
Pricing
|
<div className="divider my-1"></div>
|
||||||
</button>
|
{availableStates.map((state) => (
|
||||||
<button
|
<li key={state}>
|
||||||
onClick={loadStores}
|
<a onClick={() => setSelectedState(state)} className={selectedState === state ? 'active bg-emerald-100' : ''}>
|
||||||
className="btn btn-sm btn-outline gap-2"
|
{state}
|
||||||
>
|
</a>
|
||||||
<RefreshCw className="w-4 h-4" />
|
</li>
|
||||||
Refresh
|
))}
|
||||||
</button>
|
</ul>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Page Navigation */}
|
||||||
|
<div className="flex gap-1">
|
||||||
|
<button
|
||||||
|
onClick={() => navigate('/admin/intelligence/brands')}
|
||||||
|
className="btn btn-sm gap-1 bg-white border-gray-300 text-gray-700 hover:bg-gray-100"
|
||||||
|
>
|
||||||
|
<Building2 className="w-4 h-4" />
|
||||||
|
<span>Brands</span>
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
className="btn btn-sm gap-1 bg-emerald-600 text-white hover:bg-emerald-700 border-emerald-600"
|
||||||
|
>
|
||||||
|
<MapPin className="w-4 h-4" />
|
||||||
|
<span>Stores</span>
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
onClick={() => navigate('/admin/intelligence/pricing')}
|
||||||
|
className="btn btn-sm gap-1 bg-white border-gray-300 text-gray-700 hover:bg-gray-100"
|
||||||
|
>
|
||||||
|
<DollarSign className="w-4 h-4" />
|
||||||
|
<span>Pricing</span>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@@ -194,26 +220,6 @@ export function IntelligenceStores() {
|
|||||||
className="input input-bordered input-sm w-full pl-10"
|
className="input input-bordered input-sm w-full pl-10"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div className="dropdown">
|
|
||||||
<button tabIndex={0} className="btn btn-sm btn-outline gap-2">
|
|
||||||
{stateLabel}
|
|
||||||
<ChevronDown className="w-4 h-4" />
|
|
||||||
</button>
|
|
||||||
<ul tabIndex={0} className="dropdown-content z-[1] menu p-2 shadow bg-base-100 rounded-box w-40 max-h-60 overflow-y-auto">
|
|
||||||
<li>
|
|
||||||
<a onClick={() => setSelectedState(null)} className={isAllStates ? 'active' : ''}>
|
|
||||||
All States
|
|
||||||
</a>
|
|
||||||
</li>
|
|
||||||
{localStates.map(state => (
|
|
||||||
<li key={state}>
|
|
||||||
<a onClick={() => setSelectedState(state)} className={selectedState === state ? 'active' : ''}>
|
|
||||||
{state}
|
|
||||||
</a>
|
|
||||||
</li>
|
|
||||||
))}
|
|
||||||
</ul>
|
|
||||||
</div>
|
|
||||||
<span className="text-sm text-gray-500">
|
<span className="text-sm text-gray-500">
|
||||||
Showing {filteredStores.length} of {stores.length} stores
|
Showing {filteredStores.length} of {stores.length} stores
|
||||||
</span>
|
</span>
|
||||||
@@ -247,7 +253,7 @@ export function IntelligenceStores() {
|
|||||||
<tr
|
<tr
|
||||||
key={store.id}
|
key={store.id}
|
||||||
className="hover:bg-gray-50 cursor-pointer"
|
className="hover:bg-gray-50 cursor-pointer"
|
||||||
onClick={() => navigate(`/admin/orchestrator/stores?storeId=${store.id}`)}
|
onClick={() => navigate(`/stores/list/${store.id}`)}
|
||||||
>
|
>
|
||||||
<td>
|
<td>
|
||||||
<span className="font-medium">{store.name}</span>
|
<span className="font-medium">{store.name}</span>
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -20,7 +20,6 @@ import {
|
|||||||
DollarSign,
|
DollarSign,
|
||||||
MapPin,
|
MapPin,
|
||||||
ArrowRight,
|
ArrowRight,
|
||||||
RefreshCw,
|
|
||||||
AlertCircle
|
AlertCircle
|
||||||
} from 'lucide-react';
|
} from 'lucide-react';
|
||||||
|
|
||||||
@@ -204,7 +203,6 @@ export default function NationalDashboard() {
|
|||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
const [error, setError] = useState<string | null>(null);
|
const [error, setError] = useState<string | null>(null);
|
||||||
const [summary, setSummary] = useState<NationalSummary | null>(null);
|
const [summary, setSummary] = useState<NationalSummary | null>(null);
|
||||||
const [refreshing, setRefreshing] = useState(false);
|
|
||||||
|
|
||||||
const fetchData = async () => {
|
const fetchData = async () => {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
@@ -229,18 +227,6 @@ export default function NationalDashboard() {
|
|||||||
fetchData();
|
fetchData();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const handleRefreshMetrics = async () => {
|
|
||||||
setRefreshing(true);
|
|
||||||
try {
|
|
||||||
await api.post('/api/admin/states/refresh-metrics');
|
|
||||||
await fetchData();
|
|
||||||
} catch (err) {
|
|
||||||
console.error('Failed to refresh metrics:', err);
|
|
||||||
} finally {
|
|
||||||
setRefreshing(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleStateClick = (stateCode: string) => {
|
const handleStateClick = (stateCode: string) => {
|
||||||
setSelectedState(stateCode);
|
setSelectedState(stateCode);
|
||||||
navigate(`/national/state/${stateCode}`);
|
navigate(`/national/state/${stateCode}`);
|
||||||
@@ -277,23 +263,11 @@ export default function NationalDashboard() {
|
|||||||
<Layout>
|
<Layout>
|
||||||
<div className="space-y-6">
|
<div className="space-y-6">
|
||||||
{/* Header */}
|
{/* Header */}
|
||||||
<div className="flex items-center justify-between">
|
<div>
|
||||||
<div>
|
<h1 className="text-2xl font-bold text-gray-900">National Dashboard</h1>
|
||||||
<h1 className="text-2xl font-bold text-gray-900">National Dashboard</h1>
|
<p className="text-gray-500 mt-1">
|
||||||
<p className="text-gray-500 mt-1">
|
Multi-state cannabis market intelligence
|
||||||
Multi-state cannabis market intelligence
|
</p>
|
||||||
</p>
|
|
||||||
</div>
|
|
||||||
<div className="flex items-center gap-3">
|
|
||||||
<button
|
|
||||||
onClick={handleRefreshMetrics}
|
|
||||||
disabled={refreshing}
|
|
||||||
className="flex items-center gap-2 px-3 py-2 text-sm text-gray-600 hover:text-gray-900 border border-gray-200 rounded-lg hover:bg-gray-50 disabled:opacity-50"
|
|
||||||
>
|
|
||||||
<RefreshCw className={`w-4 h-4 ${refreshing ? 'animate-spin' : ''}`} />
|
|
||||||
Refresh Metrics
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Summary Cards */}
|
{/* Summary Cards */}
|
||||||
@@ -301,7 +275,7 @@ export default function NationalDashboard() {
|
|||||||
<>
|
<>
|
||||||
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4">
|
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4">
|
||||||
<MetricCard
|
<MetricCard
|
||||||
title="States"
|
title="Regions (US + CA)"
|
||||||
value={summary.activeStates}
|
value={summary.activeStates}
|
||||||
icon={Globe}
|
icon={Globe}
|
||||||
/>
|
/>
|
||||||
|
|||||||
@@ -153,29 +153,6 @@ export function StoreDetailPage() {
|
|||||||
Back to Stores
|
Back to Stores
|
||||||
</button>
|
</button>
|
||||||
|
|
||||||
{/* Update Button */}
|
|
||||||
<div className="relative">
|
|
||||||
<button
|
|
||||||
onClick={() => setShowUpdateDropdown(!showUpdateDropdown)}
|
|
||||||
disabled={isUpdating}
|
|
||||||
className="flex items-center gap-2 px-4 py-2 text-sm font-medium text-white bg-blue-600 hover:bg-blue-700 rounded-lg disabled:opacity-50 disabled:cursor-not-allowed"
|
|
||||||
>
|
|
||||||
<RefreshCw className={`w-4 h-4 ${isUpdating ? 'animate-spin' : ''}`} />
|
|
||||||
{isUpdating ? 'Crawling...' : 'Crawl Now'}
|
|
||||||
{!isUpdating && <ChevronDown className="w-4 h-4" />}
|
|
||||||
</button>
|
|
||||||
|
|
||||||
{showUpdateDropdown && !isUpdating && (
|
|
||||||
<div className="absolute right-0 mt-2 w-48 bg-white rounded-lg shadow-lg border border-gray-200 z-10">
|
|
||||||
<button
|
|
||||||
onClick={handleCrawl}
|
|
||||||
className="w-full text-left px-4 py-2 text-sm text-gray-700 hover:bg-gray-100 rounded-lg"
|
|
||||||
>
|
|
||||||
Start Full Crawl
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Store Header */}
|
{/* Store Header */}
|
||||||
@@ -200,7 +177,7 @@ export function StoreDetailPage() {
|
|||||||
<div className="flex items-center gap-2 text-sm text-gray-600 bg-gray-50 px-4 py-2 rounded-lg">
|
<div className="flex items-center gap-2 text-sm text-gray-600 bg-gray-50 px-4 py-2 rounded-lg">
|
||||||
<Clock className="w-4 h-4" />
|
<Clock className="w-4 h-4" />
|
||||||
<div>
|
<div>
|
||||||
<span className="font-medium">Last Crawl:</span>
|
<span className="font-medium">Last Updated:</span>
|
||||||
<span className="ml-2">
|
<span className="ml-2">
|
||||||
{lastCrawl?.completed_at
|
{lastCrawl?.completed_at
|
||||||
? new Date(lastCrawl.completed_at).toLocaleDateString('en-US', {
|
? new Date(lastCrawl.completed_at).toLocaleDateString('en-US', {
|
||||||
@@ -212,15 +189,6 @@ export function StoreDetailPage() {
|
|||||||
})
|
})
|
||||||
: 'Never'}
|
: 'Never'}
|
||||||
</span>
|
</span>
|
||||||
{lastCrawl?.status && (
|
|
||||||
<span className={`ml-2 px-2 py-0.5 rounded text-xs ${
|
|
||||||
lastCrawl.status === 'completed' ? 'bg-green-100 text-green-800' :
|
|
||||||
lastCrawl.status === 'failed' ? 'bg-red-100 text-red-800' :
|
|
||||||
'bg-yellow-100 text-yellow-800'
|
|
||||||
}`}>
|
|
||||||
{lastCrawl.status}
|
|
||||||
</span>
|
|
||||||
)}
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -282,8 +250,8 @@ export function StoreDetailPage() {
|
|||||||
setStockFilter('in_stock');
|
setStockFilter('in_stock');
|
||||||
setSearchQuery('');
|
setSearchQuery('');
|
||||||
}}
|
}}
|
||||||
className={`bg-white rounded-lg border p-4 hover:border-blue-300 hover:shadow-md transition-all cursor-pointer text-left ${
|
className={`bg-white rounded-lg border p-4 hover:border-gray-300 hover:shadow-md transition-all cursor-pointer text-left ${
|
||||||
stockFilter === 'in_stock' ? 'border-blue-500' : 'border-gray-200'
|
stockFilter === 'in_stock' ? 'border-gray-400' : 'border-gray-200'
|
||||||
}`}
|
}`}
|
||||||
>
|
>
|
||||||
<div className="flex items-center gap-3">
|
<div className="flex items-center gap-3">
|
||||||
@@ -303,8 +271,8 @@ export function StoreDetailPage() {
|
|||||||
setStockFilter('out_of_stock');
|
setStockFilter('out_of_stock');
|
||||||
setSearchQuery('');
|
setSearchQuery('');
|
||||||
}}
|
}}
|
||||||
className={`bg-white rounded-lg border p-4 hover:border-blue-300 hover:shadow-md transition-all cursor-pointer text-left ${
|
className={`bg-white rounded-lg border p-4 hover:border-gray-300 hover:shadow-md transition-all cursor-pointer text-left ${
|
||||||
stockFilter === 'out_of_stock' ? 'border-blue-500' : 'border-gray-200'
|
stockFilter === 'out_of_stock' ? 'border-gray-400' : 'border-gray-200'
|
||||||
}`}
|
}`}
|
||||||
>
|
>
|
||||||
<div className="flex items-center gap-3">
|
<div className="flex items-center gap-3">
|
||||||
@@ -320,8 +288,8 @@ export function StoreDetailPage() {
|
|||||||
|
|
||||||
<button
|
<button
|
||||||
onClick={() => setActiveTab('brands')}
|
onClick={() => setActiveTab('brands')}
|
||||||
className={`bg-white rounded-lg border p-4 hover:border-blue-300 hover:shadow-md transition-all cursor-pointer text-left ${
|
className={`bg-white rounded-lg border p-4 hover:border-gray-300 hover:shadow-md transition-all cursor-pointer text-left ${
|
||||||
activeTab === 'brands' ? 'border-blue-500' : 'border-gray-200'
|
activeTab === 'brands' ? 'border-gray-400' : 'border-gray-200'
|
||||||
}`}
|
}`}
|
||||||
>
|
>
|
||||||
<div className="flex items-center gap-3">
|
<div className="flex items-center gap-3">
|
||||||
@@ -337,8 +305,8 @@ export function StoreDetailPage() {
|
|||||||
|
|
||||||
<button
|
<button
|
||||||
onClick={() => setActiveTab('categories')}
|
onClick={() => setActiveTab('categories')}
|
||||||
className={`bg-white rounded-lg border p-4 hover:border-blue-300 hover:shadow-md transition-all cursor-pointer text-left ${
|
className={`bg-white rounded-lg border p-4 hover:border-gray-300 hover:shadow-md transition-all cursor-pointer text-left ${
|
||||||
activeTab === 'categories' ? 'border-blue-500' : 'border-gray-200'
|
activeTab === 'categories' ? 'border-gray-400' : 'border-gray-200'
|
||||||
}`}
|
}`}
|
||||||
>
|
>
|
||||||
<div className="flex items-center gap-3">
|
<div className="flex items-center gap-3">
|
||||||
@@ -364,7 +332,7 @@ export function StoreDetailPage() {
|
|||||||
}}
|
}}
|
||||||
className={`py-4 px-2 text-sm font-medium border-b-2 ${
|
className={`py-4 px-2 text-sm font-medium border-b-2 ${
|
||||||
activeTab === 'products'
|
activeTab === 'products'
|
||||||
? 'border-blue-600 text-blue-600'
|
? 'border-gray-800 text-gray-900'
|
||||||
: 'border-transparent text-gray-600 hover:text-gray-900'
|
: 'border-transparent text-gray-600 hover:text-gray-900'
|
||||||
}`}
|
}`}
|
||||||
>
|
>
|
||||||
@@ -374,7 +342,7 @@ export function StoreDetailPage() {
|
|||||||
onClick={() => setActiveTab('brands')}
|
onClick={() => setActiveTab('brands')}
|
||||||
className={`py-4 px-2 text-sm font-medium border-b-2 ${
|
className={`py-4 px-2 text-sm font-medium border-b-2 ${
|
||||||
activeTab === 'brands'
|
activeTab === 'brands'
|
||||||
? 'border-blue-600 text-blue-600'
|
? 'border-gray-800 text-gray-900'
|
||||||
: 'border-transparent text-gray-600 hover:text-gray-900'
|
: 'border-transparent text-gray-600 hover:text-gray-900'
|
||||||
}`}
|
}`}
|
||||||
>
|
>
|
||||||
@@ -384,7 +352,7 @@ export function StoreDetailPage() {
|
|||||||
onClick={() => setActiveTab('categories')}
|
onClick={() => setActiveTab('categories')}
|
||||||
className={`py-4 px-2 text-sm font-medium border-b-2 ${
|
className={`py-4 px-2 text-sm font-medium border-b-2 ${
|
||||||
activeTab === 'categories'
|
activeTab === 'categories'
|
||||||
? 'border-blue-600 text-blue-600'
|
? 'border-gray-800 text-gray-900'
|
||||||
: 'border-transparent text-gray-600 hover:text-gray-900'
|
: 'border-transparent text-gray-600 hover:text-gray-900'
|
||||||
}`}
|
}`}
|
||||||
>
|
>
|
||||||
@@ -433,7 +401,7 @@ export function StoreDetailPage() {
|
|||||||
|
|
||||||
{productsLoading ? (
|
{productsLoading ? (
|
||||||
<div className="text-center py-8">
|
<div className="text-center py-8">
|
||||||
<div className="inline-block animate-spin rounded-full h-6 w-6 border-4 border-blue-500 border-t-transparent"></div>
|
<div className="inline-block animate-spin rounded-full h-6 w-6 border-4 border-gray-400 border-t-transparent"></div>
|
||||||
<p className="mt-2 text-sm text-gray-600">Loading products...</p>
|
<p className="mt-2 text-sm text-gray-600">Loading products...</p>
|
||||||
</div>
|
</div>
|
||||||
) : products.length === 0 ? (
|
) : products.length === 0 ? (
|
||||||
@@ -485,9 +453,9 @@ export function StoreDetailPage() {
|
|||||||
<div className="line-clamp-2" title={product.brand || '-'}>{product.brand || '-'}</div>
|
<div className="line-clamp-2" title={product.brand || '-'}>{product.brand || '-'}</div>
|
||||||
</td>
|
</td>
|
||||||
<td className="whitespace-nowrap">
|
<td className="whitespace-nowrap">
|
||||||
<span className="badge badge-ghost badge-sm">{product.type || '-'}</span>
|
<span className="text-xs text-gray-500 bg-gray-100 px-1.5 py-0.5 rounded">{product.type || '-'}</span>
|
||||||
{product.subcategory && (
|
{product.subcategory && (
|
||||||
<span className="badge badge-ghost badge-sm ml-1">{product.subcategory}</span>
|
<span className="text-xs text-gray-500 bg-gray-100 px-1.5 py-0.5 rounded ml-1">{product.subcategory}</span>
|
||||||
)}
|
)}
|
||||||
</td>
|
</td>
|
||||||
<td className="text-right font-semibold whitespace-nowrap">
|
<td className="text-right font-semibold whitespace-nowrap">
|
||||||
@@ -500,21 +468,14 @@ export function StoreDetailPage() {
|
|||||||
`$${product.regular_price}`
|
`$${product.regular_price}`
|
||||||
) : '-'}
|
) : '-'}
|
||||||
</td>
|
</td>
|
||||||
<td className="text-center whitespace-nowrap">
|
<td className="text-center whitespace-nowrap text-sm text-gray-700">
|
||||||
{product.thc_percentage ? (
|
{product.thc_percentage ? `${product.thc_percentage}%` : '-'}
|
||||||
<span className="badge badge-success badge-sm">{product.thc_percentage}%</span>
|
|
||||||
) : '-'}
|
|
||||||
</td>
|
</td>
|
||||||
<td className="text-center whitespace-nowrap">
|
<td className="text-center whitespace-nowrap text-sm text-gray-700">
|
||||||
{product.stock_status === 'in_stock' ? (
|
{product.stock_status === 'in_stock' ? 'In Stock' :
|
||||||
<span className="badge badge-success badge-sm">In Stock</span>
|
product.stock_status === 'out_of_stock' ? 'Out' : '-'}
|
||||||
) : product.stock_status === 'out_of_stock' ? (
|
|
||||||
<span className="badge badge-error badge-sm">Out</span>
|
|
||||||
) : (
|
|
||||||
<span className="badge badge-warning badge-sm">Unknown</span>
|
|
||||||
)}
|
|
||||||
</td>
|
</td>
|
||||||
<td className="text-center whitespace-nowrap">
|
<td className="text-center whitespace-nowrap text-sm text-gray-700">
|
||||||
{product.total_quantity != null ? product.total_quantity : '-'}
|
{product.total_quantity != null ? product.total_quantity : '-'}
|
||||||
</td>
|
</td>
|
||||||
<td className="whitespace-nowrap text-xs text-gray-500">
|
<td className="whitespace-nowrap text-xs text-gray-500">
|
||||||
|
|||||||
@@ -12,11 +12,15 @@ import {
|
|||||||
Search,
|
Search,
|
||||||
ChevronDown,
|
ChevronDown,
|
||||||
ChevronUp,
|
ChevronUp,
|
||||||
|
ChevronLeft,
|
||||||
|
ChevronRight,
|
||||||
Gauge,
|
Gauge,
|
||||||
Users,
|
Users,
|
||||||
Power,
|
|
||||||
Play,
|
|
||||||
Square,
|
Square,
|
||||||
|
Plus,
|
||||||
|
X,
|
||||||
|
Calendar,
|
||||||
|
Trash2,
|
||||||
} from 'lucide-react';
|
} from 'lucide-react';
|
||||||
|
|
||||||
interface Task {
|
interface Task {
|
||||||
@@ -66,6 +70,313 @@ interface TaskCounts {
|
|||||||
stale: number;
|
stale: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface Store {
|
||||||
|
id: number;
|
||||||
|
name: string;
|
||||||
|
state_code: string;
|
||||||
|
crawl_enabled: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface CreateTaskModalProps {
|
||||||
|
isOpen: boolean;
|
||||||
|
onClose: () => void;
|
||||||
|
onTaskCreated: () => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
const TASK_ROLES = [
|
||||||
|
{ id: 'product_refresh', name: 'Product Resync', description: 'Re-crawl products for price/stock changes' },
|
||||||
|
{ id: 'product_discovery', name: 'Product Discovery', description: 'Initial crawl for new dispensaries' },
|
||||||
|
{ id: 'store_discovery', name: 'Store Discovery', description: 'Discover new dispensary locations' },
|
||||||
|
{ id: 'entry_point_discovery', name: 'Entry Point Discovery', description: 'Resolve platform IDs from menu URLs' },
|
||||||
|
{ id: 'analytics_refresh', name: 'Analytics Refresh', description: 'Refresh materialized views' },
|
||||||
|
];
|
||||||
|
|
||||||
|
function CreateTaskModal({ isOpen, onClose, onTaskCreated }: CreateTaskModalProps) {
|
||||||
|
const [role, setRole] = useState('product_refresh');
|
||||||
|
const [priority, setPriority] = useState(10);
|
||||||
|
const [scheduleType, setScheduleType] = useState<'now' | 'scheduled'>('now');
|
||||||
|
const [scheduledFor, setScheduledFor] = useState('');
|
||||||
|
const [stores, setStores] = useState<Store[]>([]);
|
||||||
|
const [storeSearch, setStoreSearch] = useState('');
|
||||||
|
const [selectedStores, setSelectedStores] = useState<Store[]>([]);
|
||||||
|
const [loading, setLoading] = useState(false);
|
||||||
|
const [storesLoading, setStoresLoading] = useState(false);
|
||||||
|
const [error, setError] = useState<string | null>(null);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (isOpen) {
|
||||||
|
fetchStores();
|
||||||
|
}
|
||||||
|
}, [isOpen]);
|
||||||
|
|
||||||
|
const fetchStores = async () => {
|
||||||
|
setStoresLoading(true);
|
||||||
|
try {
|
||||||
|
const res = await api.get('/api/stores?limit=500');
|
||||||
|
setStores(res.data.stores || res.data || []);
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Failed to fetch stores:', err);
|
||||||
|
} finally {
|
||||||
|
setStoresLoading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const filteredStores = stores.filter(s =>
|
||||||
|
s.name.toLowerCase().includes(storeSearch.toLowerCase()) ||
|
||||||
|
s.state_code?.toLowerCase().includes(storeSearch.toLowerCase())
|
||||||
|
);
|
||||||
|
|
||||||
|
const toggleStore = (store: Store) => {
|
||||||
|
if (selectedStores.find(s => s.id === store.id)) {
|
||||||
|
setSelectedStores(selectedStores.filter(s => s.id !== store.id));
|
||||||
|
} else {
|
||||||
|
setSelectedStores([...selectedStores, store]);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const selectAll = () => setSelectedStores(filteredStores);
|
||||||
|
const clearAll = () => setSelectedStores([]);
|
||||||
|
|
||||||
|
const handleSubmit = async () => {
|
||||||
|
setLoading(true);
|
||||||
|
setError(null);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const scheduledDate = scheduleType === 'scheduled' && scheduledFor
|
||||||
|
? new Date(scheduledFor).toISOString()
|
||||||
|
: undefined;
|
||||||
|
|
||||||
|
if (role === 'store_discovery' || role === 'analytics_refresh') {
|
||||||
|
await api.post('/api/tasks', {
|
||||||
|
role,
|
||||||
|
priority,
|
||||||
|
scheduled_for: scheduledDate,
|
||||||
|
platform: 'dutchie',
|
||||||
|
});
|
||||||
|
} else if (selectedStores.length === 0) {
|
||||||
|
setError('Please select at least one store');
|
||||||
|
setLoading(false);
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
for (const store of selectedStores) {
|
||||||
|
await api.post('/api/tasks', {
|
||||||
|
role,
|
||||||
|
dispensary_id: store.id,
|
||||||
|
priority,
|
||||||
|
scheduled_for: scheduledDate,
|
||||||
|
platform: 'dutchie',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
onTaskCreated();
|
||||||
|
onClose();
|
||||||
|
setSelectedStores([]);
|
||||||
|
setPriority(10);
|
||||||
|
setScheduleType('now');
|
||||||
|
setScheduledFor('');
|
||||||
|
} catch (err: any) {
|
||||||
|
setError(err.response?.data?.error || err.message || 'Failed to create task');
|
||||||
|
} finally {
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!isOpen) return null;
|
||||||
|
|
||||||
|
const needsStore = role !== 'store_discovery' && role !== 'analytics_refresh';
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="fixed inset-0 z-50 overflow-y-auto">
|
||||||
|
<div className="flex min-h-full items-center justify-center p-4">
|
||||||
|
<div className="fixed inset-0 bg-black/50" onClick={onClose} />
|
||||||
|
<div className="relative bg-white rounded-xl shadow-xl max-w-2xl w-full max-h-[90vh] overflow-hidden">
|
||||||
|
<div className="px-6 py-4 border-b border-gray-200 flex items-center justify-between">
|
||||||
|
<h2 className="text-lg font-semibold text-gray-900">Create New Task</h2>
|
||||||
|
<button onClick={onClose} className="p-1 hover:bg-gray-100 rounded">
|
||||||
|
<X className="w-5 h-5 text-gray-500" />
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="px-6 py-4 space-y-6 overflow-y-auto max-h-[calc(90vh-140px)]">
|
||||||
|
{error && (
|
||||||
|
<div className="bg-red-50 border border-red-200 rounded-lg p-3 text-red-700 text-sm">
|
||||||
|
{error}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium text-gray-700 mb-2">Task Role</label>
|
||||||
|
<div className="grid grid-cols-1 gap-2">
|
||||||
|
{TASK_ROLES.map(r => (
|
||||||
|
<button
|
||||||
|
key={r.id}
|
||||||
|
onClick={() => setRole(r.id)}
|
||||||
|
className={`flex items-start gap-3 p-3 rounded-lg border text-left transition-colors ${
|
||||||
|
role === r.id
|
||||||
|
? 'border-emerald-500 bg-emerald-50'
|
||||||
|
: 'border-gray-200 hover:border-gray-300'
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
<div className={`w-4 h-4 rounded-full border-2 mt-0.5 flex-shrink-0 ${
|
||||||
|
role === r.id ? 'border-emerald-500 bg-emerald-500' : 'border-gray-300'
|
||||||
|
}`}>
|
||||||
|
{role === r.id && (
|
||||||
|
<div className="w-full h-full flex items-center justify-center">
|
||||||
|
<div className="w-1.5 h-1.5 bg-white rounded-full" />
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<p className="font-medium text-gray-900">{r.name}</p>
|
||||||
|
<p className="text-xs text-gray-500">{r.description}</p>
|
||||||
|
</div>
|
||||||
|
</button>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{needsStore && (
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium text-gray-700 mb-2">
|
||||||
|
Select Stores ({selectedStores.length} selected)
|
||||||
|
</label>
|
||||||
|
<div className="border border-gray-200 rounded-lg overflow-hidden">
|
||||||
|
<div className="p-2 border-b border-gray-200 bg-gray-50">
|
||||||
|
<div className="relative">
|
||||||
|
<Search className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-gray-400" />
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
value={storeSearch}
|
||||||
|
onChange={(e) => setStoreSearch(e.target.value)}
|
||||||
|
placeholder="Search stores..."
|
||||||
|
className="w-full pl-9 pr-3 py-2 text-sm border border-gray-200 rounded"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className="flex gap-2 mt-2">
|
||||||
|
<button onClick={selectAll} className="text-xs text-emerald-600 hover:underline">
|
||||||
|
Select all ({filteredStores.length})
|
||||||
|
</button>
|
||||||
|
<span className="text-gray-300">|</span>
|
||||||
|
<button onClick={clearAll} className="text-xs text-gray-500 hover:underline">
|
||||||
|
Clear
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div className="max-h-48 overflow-y-auto">
|
||||||
|
{storesLoading ? (
|
||||||
|
<div className="p-4 text-center text-gray-500">
|
||||||
|
<RefreshCw className="w-5 h-5 animate-spin mx-auto mb-1" />
|
||||||
|
Loading stores...
|
||||||
|
</div>
|
||||||
|
) : filteredStores.length === 0 ? (
|
||||||
|
<div className="p-4 text-center text-gray-500">No stores found</div>
|
||||||
|
) : (
|
||||||
|
filteredStores.map(store => (
|
||||||
|
<label key={store.id} className="flex items-center gap-3 px-3 py-2 hover:bg-gray-50 cursor-pointer">
|
||||||
|
<input
|
||||||
|
type="checkbox"
|
||||||
|
checked={!!selectedStores.find(s => s.id === store.id)}
|
||||||
|
onChange={() => toggleStore(store)}
|
||||||
|
className="w-4 h-4 text-emerald-600 rounded"
|
||||||
|
/>
|
||||||
|
<div className="flex-1 min-w-0">
|
||||||
|
<p className="text-sm text-gray-900 truncate">{store.name}</p>
|
||||||
|
<p className="text-xs text-gray-500">{store.state_code}</p>
|
||||||
|
</div>
|
||||||
|
{!store.crawl_enabled && (
|
||||||
|
<span className="text-xs text-orange-600 bg-orange-50 px-1.5 py-0.5 rounded">disabled</span>
|
||||||
|
)}
|
||||||
|
</label>
|
||||||
|
))
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium text-gray-700 mb-2">Priority: {priority}</label>
|
||||||
|
<input
|
||||||
|
type="range"
|
||||||
|
min="0"
|
||||||
|
max="100"
|
||||||
|
value={priority}
|
||||||
|
onChange={(e) => setPriority(parseInt(e.target.value))}
|
||||||
|
className="w-full h-2 bg-gray-200 rounded-lg appearance-none cursor-pointer"
|
||||||
|
/>
|
||||||
|
<div className="flex justify-between text-xs text-gray-500 mt-1">
|
||||||
|
<span>0 (Low)</span>
|
||||||
|
<span>10 (Normal)</span>
|
||||||
|
<span>50 (High)</span>
|
||||||
|
<span>100 (Urgent)</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium text-gray-700 mb-2">Schedule</label>
|
||||||
|
<div className="flex gap-4">
|
||||||
|
<label className="flex items-center gap-2 cursor-pointer">
|
||||||
|
<input
|
||||||
|
type="radio"
|
||||||
|
name="schedule"
|
||||||
|
checked={scheduleType === 'now'}
|
||||||
|
onChange={() => setScheduleType('now')}
|
||||||
|
className="w-4 h-4 text-emerald-600"
|
||||||
|
/>
|
||||||
|
<span className="text-sm text-gray-700">Run immediately</span>
|
||||||
|
</label>
|
||||||
|
<label className="flex items-center gap-2 cursor-pointer">
|
||||||
|
<input
|
||||||
|
type="radio"
|
||||||
|
name="schedule"
|
||||||
|
checked={scheduleType === 'scheduled'}
|
||||||
|
onChange={() => setScheduleType('scheduled')}
|
||||||
|
className="w-4 h-4 text-emerald-600"
|
||||||
|
/>
|
||||||
|
<span className="text-sm text-gray-700">Schedule for later</span>
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
{scheduleType === 'scheduled' && (
|
||||||
|
<div className="mt-3 relative">
|
||||||
|
<Calendar className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-gray-400" />
|
||||||
|
<input
|
||||||
|
type="datetime-local"
|
||||||
|
value={scheduledFor}
|
||||||
|
onChange={(e) => setScheduledFor(e.target.value)}
|
||||||
|
className="w-full pl-9 pr-3 py-2 text-sm border border-gray-200 rounded"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="px-6 py-4 border-t border-gray-200 bg-gray-50 flex items-center justify-between">
|
||||||
|
<div className="text-sm text-gray-500">
|
||||||
|
{needsStore ? (
|
||||||
|
selectedStores.length > 0 ? `Will create ${selectedStores.length} task${selectedStores.length > 1 ? 's' : ''}` : 'Select stores to create tasks'
|
||||||
|
) : 'Will create 1 task'}
|
||||||
|
</div>
|
||||||
|
<div className="flex gap-3">
|
||||||
|
<button onClick={onClose} className="px-4 py-2 text-sm text-gray-700 hover:bg-gray-100 rounded-lg">
|
||||||
|
Cancel
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
onClick={handleSubmit}
|
||||||
|
disabled={loading || (needsStore && selectedStores.length === 0)}
|
||||||
|
className="px-4 py-2 text-sm bg-emerald-600 text-white rounded-lg hover:bg-emerald-700 disabled:opacity-50 disabled:cursor-not-allowed flex items-center gap-2"
|
||||||
|
>
|
||||||
|
{loading && <RefreshCw className="w-4 h-4 animate-spin" />}
|
||||||
|
Create Task{selectedStores.length > 1 ? 's' : ''}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
const ROLES = [
|
const ROLES = [
|
||||||
'store_discovery',
|
'store_discovery',
|
||||||
'entry_point_discovery',
|
'entry_point_discovery',
|
||||||
@@ -139,7 +450,11 @@ export default function TasksDashboard() {
|
|||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
const [error, setError] = useState<string | null>(null);
|
const [error, setError] = useState<string | null>(null);
|
||||||
const [poolPaused, setPoolPaused] = useState(false);
|
const [poolPaused, setPoolPaused] = useState(false);
|
||||||
const [poolLoading, setPoolLoading] = useState(false);
|
const [showCreateModal, setShowCreateModal] = useState(false);
|
||||||
|
|
||||||
|
// Pagination
|
||||||
|
const [page, setPage] = useState(0);
|
||||||
|
const tasksPerPage = 25;
|
||||||
|
|
||||||
// Filters
|
// Filters
|
||||||
const [roleFilter, setRoleFilter] = useState<string>('');
|
const [roleFilter, setRoleFilter] = useState<string>('');
|
||||||
@@ -173,20 +488,14 @@ export default function TasksDashboard() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const togglePool = async () => {
|
const handleDeleteTask = async (taskId: number) => {
|
||||||
setPoolLoading(true);
|
if (!confirm('Delete this task?')) return;
|
||||||
try {
|
try {
|
||||||
if (poolPaused) {
|
await api.delete(`/api/tasks/${taskId}`);
|
||||||
await api.resumeTaskPool();
|
fetchData();
|
||||||
setPoolPaused(false);
|
|
||||||
} else {
|
|
||||||
await api.pauseTaskPool();
|
|
||||||
setPoolPaused(true);
|
|
||||||
}
|
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
setError(err.message || 'Failed to toggle pool');
|
console.error('Delete error:', err);
|
||||||
} finally {
|
alert(err.response?.data?.error || 'Failed to delete task');
|
||||||
setPoolLoading(false);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -209,6 +518,10 @@ export default function TasksDashboard() {
|
|||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Pagination
|
||||||
|
const paginatedTasks = filteredTasks.slice(page * tasksPerPage, (page + 1) * tasksPerPage);
|
||||||
|
const totalPages = Math.ceil(filteredTasks.length / tasksPerPage);
|
||||||
|
|
||||||
const totalActive = (counts?.claimed || 0) + (counts?.running || 0);
|
const totalActive = (counts?.claimed || 0) + (counts?.running || 0);
|
||||||
const totalPending = counts?.pending || 0;
|
const totalPending = counts?.pending || 0;
|
||||||
|
|
||||||
@@ -225,42 +538,37 @@ export default function TasksDashboard() {
|
|||||||
return (
|
return (
|
||||||
<Layout>
|
<Layout>
|
||||||
<div className="space-y-6">
|
<div className="space-y-6">
|
||||||
{/* Header */}
|
{/* Sticky Header */}
|
||||||
<div className="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4">
|
<div className="sticky top-0 z-10 bg-white pb-4 -mx-6 px-6 pt-2 border-b border-gray-200 shadow-sm">
|
||||||
<div>
|
<div className="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4">
|
||||||
<h1 className="text-2xl font-bold text-gray-900 flex items-center gap-2">
|
<div>
|
||||||
<ListChecks className="w-7 h-7 text-emerald-600" />
|
<h1 className="text-2xl font-bold text-gray-900 flex items-center gap-2">
|
||||||
Task Queue
|
<ListChecks className="w-7 h-7 text-emerald-600" />
|
||||||
</h1>
|
Task Queue
|
||||||
<p className="text-gray-500 mt-1">
|
</h1>
|
||||||
{totalActive} active, {totalPending} pending tasks
|
<p className="text-gray-500 mt-1">
|
||||||
</p>
|
{totalActive} active, {totalPending} pending tasks
|
||||||
</div>
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div className="flex items-center gap-4">
|
<div className="flex items-center gap-4">
|
||||||
{/* Pool Toggle */}
|
{/* Create Task Button */}
|
||||||
<button
|
<button
|
||||||
onClick={togglePool}
|
onClick={() => setShowCreateModal(true)}
|
||||||
disabled={poolLoading}
|
className="flex items-center gap-2 px-4 py-2 bg-emerald-600 text-white rounded-lg hover:bg-emerald-700 transition-colors"
|
||||||
className={`flex items-center gap-2 px-4 py-2 rounded-lg font-medium transition-colors ${
|
>
|
||||||
poolPaused
|
<Plus className="w-4 h-4" />
|
||||||
? 'bg-emerald-100 text-emerald-700 hover:bg-emerald-200'
|
Create Task
|
||||||
: 'bg-red-100 text-red-700 hover:bg-red-200'
|
</button>
|
||||||
}`}
|
{/* Pool status indicator */}
|
||||||
>
|
{poolPaused && (
|
||||||
{poolPaused ? (
|
<span className="inline-flex items-center gap-1.5 px-3 py-1.5 rounded-full text-sm font-medium bg-yellow-100 text-yellow-800">
|
||||||
<>
|
<Square className="w-4 h-4" />
|
||||||
<Play className={`w-5 h-5 ${poolLoading ? 'animate-pulse' : ''}`} />
|
Pool Paused
|
||||||
Start Pool
|
</span>
|
||||||
</>
|
|
||||||
) : (
|
|
||||||
<>
|
|
||||||
<Square className={`w-5 h-5 ${poolLoading ? 'animate-pulse' : ''}`} />
|
|
||||||
Stop Pool
|
|
||||||
</>
|
|
||||||
)}
|
)}
|
||||||
</button>
|
<span className="text-sm text-gray-400">Auto-refreshes every 15s</span>
|
||||||
<span className="text-sm text-gray-400">Auto-refreshes every 15s</span>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@@ -268,6 +576,13 @@ export default function TasksDashboard() {
|
|||||||
<div className="p-4 bg-red-50 text-red-700 rounded-lg">{error}</div>
|
<div className="p-4 bg-red-50 text-red-700 rounded-lg">{error}</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
{/* Create Task Modal */}
|
||||||
|
<CreateTaskModal
|
||||||
|
isOpen={showCreateModal}
|
||||||
|
onClose={() => setShowCreateModal(false)}
|
||||||
|
onTaskCreated={fetchData}
|
||||||
|
/>
|
||||||
|
|
||||||
{/* Status Summary Cards */}
|
{/* Status Summary Cards */}
|
||||||
<div className="grid grid-cols-2 sm:grid-cols-3 lg:grid-cols-6 gap-4">
|
<div className="grid grid-cols-2 sm:grid-cols-3 lg:grid-cols-6 gap-4">
|
||||||
{Object.entries(counts || {}).map(([status, count]) => (
|
{Object.entries(counts || {}).map(([status, count]) => (
|
||||||
@@ -280,7 +595,7 @@ export default function TasksDashboard() {
|
|||||||
>
|
>
|
||||||
<div className="flex items-center gap-2 mb-2">
|
<div className="flex items-center gap-2 mb-2">
|
||||||
<span className={`p-1.5 rounded ${STATUS_COLORS[status]}`}>
|
<span className={`p-1.5 rounded ${STATUS_COLORS[status]}`}>
|
||||||
{STATUS_ICONS[status]}
|
{getStatusIcon(status, poolPaused)}
|
||||||
</span>
|
</span>
|
||||||
<span className="text-sm font-medium text-gray-600 capitalize">{status}</span>
|
<span className="text-sm font-medium text-gray-600 capitalize">{status}</span>
|
||||||
</div>
|
</div>
|
||||||
@@ -470,17 +785,19 @@ export default function TasksDashboard() {
|
|||||||
<th className="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase">
|
<th className="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase">
|
||||||
Error
|
Error
|
||||||
</th>
|
</th>
|
||||||
|
<th className="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase w-16">
|
||||||
|
</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody className="divide-y divide-gray-200">
|
<tbody className="divide-y divide-gray-200">
|
||||||
{filteredTasks.length === 0 ? (
|
{paginatedTasks.length === 0 ? (
|
||||||
<tr>
|
<tr>
|
||||||
<td colSpan={8} className="px-4 py-8 text-center text-gray-500">
|
<td colSpan={9} className="px-4 py-8 text-center text-gray-500">
|
||||||
No tasks found
|
No tasks found
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
) : (
|
) : (
|
||||||
filteredTasks.map((task) => (
|
paginatedTasks.map((task) => (
|
||||||
<tr key={task.id} className="hover:bg-gray-50">
|
<tr key={task.id} className="hover:bg-gray-50">
|
||||||
<td className="px-4 py-3 text-sm font-mono text-gray-600">#{task.id}</td>
|
<td className="px-4 py-3 text-sm font-mono text-gray-600">#{task.id}</td>
|
||||||
<td className="px-4 py-3 text-sm text-gray-900">
|
<td className="px-4 py-3 text-sm text-gray-900">
|
||||||
@@ -511,12 +828,47 @@ export default function TasksDashboard() {
|
|||||||
<td className="px-4 py-3 text-sm text-red-600 max-w-xs truncate">
|
<td className="px-4 py-3 text-sm text-red-600 max-w-xs truncate">
|
||||||
{task.error_message || '-'}
|
{task.error_message || '-'}
|
||||||
</td>
|
</td>
|
||||||
|
<td className="px-4 py-3">
|
||||||
|
{(task.status === 'failed' || task.status === 'completed' || task.status === 'pending') && (
|
||||||
|
<button
|
||||||
|
onClick={() => handleDeleteTask(task.id)}
|
||||||
|
className="p-1 text-gray-400 hover:text-red-500 hover:bg-red-50 rounded transition-colors"
|
||||||
|
title="Delete task"
|
||||||
|
>
|
||||||
|
<Trash2 className="w-4 h-4" />
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
))
|
))
|
||||||
)}
|
)}
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{/* Pagination */}
|
||||||
|
<div className="px-4 py-3 border-t border-gray-200 bg-gray-50 flex items-center justify-between">
|
||||||
|
<div className="text-sm text-gray-500">
|
||||||
|
Showing {page * tasksPerPage + 1} - {Math.min((page + 1) * tasksPerPage, filteredTasks.length)} of {filteredTasks.length} tasks
|
||||||
|
</div>
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<button
|
||||||
|
onClick={() => setPage(p => Math.max(0, p - 1))}
|
||||||
|
disabled={page === 0}
|
||||||
|
className="px-3 py-1 text-sm border border-gray-200 rounded hover:bg-gray-100 disabled:opacity-50 disabled:cursor-not-allowed"
|
||||||
|
>
|
||||||
|
<ChevronLeft className="w-4 h-4" />
|
||||||
|
</button>
|
||||||
|
<span className="text-sm text-gray-600">Page {page + 1} of {totalPages || 1}</span>
|
||||||
|
<button
|
||||||
|
onClick={() => setPage(p => p + 1)}
|
||||||
|
disabled={page >= totalPages - 1}
|
||||||
|
className="px-3 py-1 text-sm border border-gray-200 rounded hover:bg-gray-100 disabled:opacity-50 disabled:cursor-not-allowed"
|
||||||
|
>
|
||||||
|
<ChevronRight className="w-4 h-4" />
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</Layout>
|
</Layout>
|
||||||
|
|||||||
@@ -18,9 +18,11 @@ import {
|
|||||||
Server,
|
Server,
|
||||||
MapPin,
|
MapPin,
|
||||||
Trash2,
|
Trash2,
|
||||||
|
PowerOff,
|
||||||
|
Undo2,
|
||||||
Plus,
|
Plus,
|
||||||
Minus,
|
MemoryStick,
|
||||||
Loader2,
|
AlertTriangle,
|
||||||
} from 'lucide-react';
|
} from 'lucide-react';
|
||||||
|
|
||||||
// Worker from registry
|
// Worker from registry
|
||||||
@@ -39,16 +41,25 @@ interface Worker {
|
|||||||
tasks_completed: number;
|
tasks_completed: number;
|
||||||
tasks_failed: number;
|
tasks_failed: number;
|
||||||
current_task_id: number | null;
|
current_task_id: number | null;
|
||||||
|
current_task_ids?: number[]; // Multiple concurrent tasks
|
||||||
|
active_task_count?: number;
|
||||||
|
max_concurrent_tasks?: number;
|
||||||
health_status: string;
|
health_status: string;
|
||||||
seconds_since_heartbeat: number;
|
seconds_since_heartbeat: number;
|
||||||
|
decommission_requested?: boolean;
|
||||||
|
decommission_reason?: string;
|
||||||
metadata: {
|
metadata: {
|
||||||
cpu?: number;
|
cpu?: number;
|
||||||
memory?: number;
|
memory?: number;
|
||||||
memoryTotal?: number;
|
memoryTotal?: number;
|
||||||
memory_mb?: number;
|
memory_mb?: number;
|
||||||
memory_total_mb?: number;
|
memory_total_mb?: number;
|
||||||
|
memory_percent?: number; // NEW: memory as percentage
|
||||||
cpu_user_ms?: number;
|
cpu_user_ms?: number;
|
||||||
cpu_system_ms?: number;
|
cpu_system_ms?: number;
|
||||||
|
cpu_percent?: number; // NEW: CPU percentage
|
||||||
|
is_backing_off?: boolean; // NEW: resource backoff state
|
||||||
|
backoff_reason?: string; // NEW: why backing off
|
||||||
proxy_location?: {
|
proxy_location?: {
|
||||||
city?: string;
|
city?: string;
|
||||||
state?: string;
|
state?: string;
|
||||||
@@ -72,14 +83,6 @@ interface Task {
|
|||||||
worker_id: string | null;
|
worker_id: string | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// K8s replica info (added 2024-12-10)
|
|
||||||
interface K8sReplicas {
|
|
||||||
current: number;
|
|
||||||
desired: number;
|
|
||||||
available: number;
|
|
||||||
updated: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
function formatRelativeTime(dateStr: string | null): string {
|
function formatRelativeTime(dateStr: string | null): string {
|
||||||
if (!dateStr) return '-';
|
if (!dateStr) return '-';
|
||||||
const date = new Date(dateStr);
|
const date = new Date(dateStr);
|
||||||
@@ -220,69 +223,259 @@ function HealthBadge({ status, healthStatus }: { status: string; healthStatus: s
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Format CPU time for display
|
||||||
|
function formatCpuTime(ms: number): string {
|
||||||
|
if (ms < 1000) return `${ms}ms`;
|
||||||
|
if (ms < 60000) return `${(ms / 1000).toFixed(1)}s`;
|
||||||
|
return `${(ms / 60000).toFixed(1)}m`;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Resource usage badge showing memory%, CPU%, and backoff status
|
||||||
|
function ResourceBadge({ worker }: { worker: Worker }) {
|
||||||
|
const memPercent = worker.metadata?.memory_percent;
|
||||||
|
const cpuPercent = worker.metadata?.cpu_percent;
|
||||||
|
const isBackingOff = worker.metadata?.is_backing_off;
|
||||||
|
const backoffReason = worker.metadata?.backoff_reason;
|
||||||
|
|
||||||
|
if (isBackingOff) {
|
||||||
|
return (
|
||||||
|
<div className="flex items-center gap-1.5" title={backoffReason || 'Backing off due to resource pressure'}>
|
||||||
|
<AlertTriangle className="w-4 h-4 text-amber-500 animate-pulse" />
|
||||||
|
<span className="text-xs text-amber-600 font-medium">Backing off</span>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// No data yet
|
||||||
|
if (memPercent === undefined && cpuPercent === undefined) {
|
||||||
|
return <span className="text-gray-400 text-xs">-</span>;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Color based on usage level
|
||||||
|
const getColor = (pct: number) => {
|
||||||
|
if (pct >= 90) return 'text-red-600';
|
||||||
|
if (pct >= 75) return 'text-amber-600';
|
||||||
|
if (pct >= 50) return 'text-yellow-600';
|
||||||
|
return 'text-emerald-600';
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="flex flex-col gap-0.5 text-xs">
|
||||||
|
{memPercent !== undefined && (
|
||||||
|
<div className="flex items-center gap-1" title={`Memory: ${worker.metadata?.memory_mb || 0}MB / ${worker.metadata?.memory_total_mb || 0}MB`}>
|
||||||
|
<MemoryStick className={`w-3 h-3 ${getColor(memPercent)}`} />
|
||||||
|
<span className={getColor(memPercent)}>{memPercent}%</span>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
{cpuPercent !== undefined && (
|
||||||
|
<div className="flex items-center gap-1">
|
||||||
|
<Cpu className={`w-3 h-3 ${getColor(cpuPercent)}`} />
|
||||||
|
<span className={getColor(cpuPercent)}>{cpuPercent}%</span>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Task count badge showing active/max concurrent tasks
|
||||||
|
function TaskCountBadge({ worker, tasks }: { worker: Worker; tasks: Task[] }) {
|
||||||
|
const activeCount = worker.active_task_count ?? (worker.current_task_id ? 1 : 0);
|
||||||
|
const maxCount = worker.max_concurrent_tasks ?? 1;
|
||||||
|
const taskIds = worker.current_task_ids ?? (worker.current_task_id ? [worker.current_task_id] : []);
|
||||||
|
|
||||||
|
if (activeCount === 0) {
|
||||||
|
return <span className="text-gray-400 text-sm">Idle</span>;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get task names for tooltip
|
||||||
|
const taskNames = taskIds.map(id => {
|
||||||
|
const task = tasks.find(t => t.id === id);
|
||||||
|
return task ? `#${id}: ${task.role}${task.dispensary_name ? ` (${task.dispensary_name})` : ''}` : `#${id}`;
|
||||||
|
}).join('\n');
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="flex items-center gap-2" title={taskNames}>
|
||||||
|
<span className="text-sm font-medium text-blue-600">
|
||||||
|
{activeCount}/{maxCount} tasks
|
||||||
|
</span>
|
||||||
|
{taskIds.length === 1 && (
|
||||||
|
<span className="text-xs text-gray-500">#{taskIds[0]}</span>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pod visualization - shows pod as hub with worker nodes radiating out
|
||||||
|
function PodVisualization({
|
||||||
|
podName,
|
||||||
|
workers,
|
||||||
|
isSelected = false,
|
||||||
|
onSelect
|
||||||
|
}: {
|
||||||
|
podName: string;
|
||||||
|
workers: Worker[];
|
||||||
|
isSelected?: boolean;
|
||||||
|
onSelect?: () => void;
|
||||||
|
}) {
|
||||||
|
const busyCount = workers.filter(w => w.current_task_id !== null).length;
|
||||||
|
const allBusy = busyCount === workers.length;
|
||||||
|
const allIdle = busyCount === 0;
|
||||||
|
|
||||||
|
// Aggregate resource stats for the pod
|
||||||
|
const totalMemoryMb = workers.reduce((sum, w) => sum + (w.metadata?.memory_mb || 0), 0);
|
||||||
|
const totalCpuUserMs = workers.reduce((sum, w) => sum + (w.metadata?.cpu_user_ms || 0), 0);
|
||||||
|
const totalCpuSystemMs = workers.reduce((sum, w) => sum + (w.metadata?.cpu_system_ms || 0), 0);
|
||||||
|
const totalCompleted = workers.reduce((sum, w) => sum + w.tasks_completed, 0);
|
||||||
|
const totalFailed = workers.reduce((sum, w) => sum + w.tasks_failed, 0);
|
||||||
|
|
||||||
|
// Pod color based on worker status
|
||||||
|
const podColor = allBusy ? 'bg-blue-500' : allIdle ? 'bg-emerald-500' : 'bg-yellow-500';
|
||||||
|
const podBorder = allBusy ? 'border-blue-400' : allIdle ? 'border-emerald-400' : 'border-yellow-400';
|
||||||
|
const podGlow = allBusy ? 'shadow-blue-200' : allIdle ? 'shadow-emerald-200' : 'shadow-yellow-200';
|
||||||
|
|
||||||
|
// Selection ring
|
||||||
|
const selectionRing = isSelected ? 'ring-4 ring-purple-400 ring-offset-2' : '';
|
||||||
|
|
||||||
|
// Build pod tooltip
|
||||||
|
const podTooltip = [
|
||||||
|
`Pod: ${podName}`,
|
||||||
|
`Workers: ${busyCount}/${workers.length} busy`,
|
||||||
|
`Memory: ${totalMemoryMb} MB (RSS)`,
|
||||||
|
`CPU: ${formatCpuTime(totalCpuUserMs)} user, ${formatCpuTime(totalCpuSystemMs)} system`,
|
||||||
|
`Tasks: ${totalCompleted} completed, ${totalFailed} failed`,
|
||||||
|
'Click to select',
|
||||||
|
].join('\n');
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="flex flex-col items-center p-4">
|
||||||
|
{/* Pod hub */}
|
||||||
|
<div className="relative">
|
||||||
|
{/* Center pod circle */}
|
||||||
|
<div
|
||||||
|
className={`w-20 h-20 rounded-full ${podColor} border-4 ${podBorder} shadow-lg ${podGlow} ${selectionRing} flex items-center justify-center text-white font-bold text-xs text-center leading-tight z-10 relative cursor-pointer hover:scale-105 transition-all`}
|
||||||
|
title={podTooltip}
|
||||||
|
onClick={onSelect}
|
||||||
|
>
|
||||||
|
<span className="px-1">{podName}</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Worker nodes radiating out */}
|
||||||
|
{workers.map((worker, index) => {
|
||||||
|
const angle = (index * 360) / workers.length - 90; // Start from top
|
||||||
|
const radians = (angle * Math.PI) / 180;
|
||||||
|
const radius = 55; // Distance from center
|
||||||
|
const x = Math.cos(radians) * radius;
|
||||||
|
const y = Math.sin(radians) * radius;
|
||||||
|
|
||||||
|
const isBusy = worker.current_task_id !== null;
|
||||||
|
const isDecommissioning = worker.decommission_requested;
|
||||||
|
const isBackingOff = worker.metadata?.is_backing_off;
|
||||||
|
// Color priority: decommissioning > backing off > busy > idle
|
||||||
|
const workerColor = isDecommissioning ? 'bg-orange-500' : isBackingOff ? 'bg-yellow-500' : isBusy ? 'bg-blue-500' : 'bg-emerald-500';
|
||||||
|
const workerBorder = isDecommissioning ? 'border-orange-300' : isBackingOff ? 'border-yellow-300' : isBusy ? 'border-blue-300' : 'border-emerald-300';
|
||||||
|
|
||||||
|
// Line from center to worker
|
||||||
|
const lineLength = radius - 10;
|
||||||
|
const lineX = Math.cos(radians) * (lineLength / 2 + 10);
|
||||||
|
const lineY = Math.sin(radians) * (lineLength / 2 + 10);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div key={worker.id}>
|
||||||
|
{/* Connection line */}
|
||||||
|
<div
|
||||||
|
className={`absolute w-0.5 ${isDecommissioning ? 'bg-orange-300' : isBackingOff ? 'bg-yellow-300' : isBusy ? 'bg-blue-300' : 'bg-emerald-300'}`}
|
||||||
|
style={{
|
||||||
|
height: `${lineLength}px`,
|
||||||
|
left: '50%',
|
||||||
|
top: '50%',
|
||||||
|
transform: `translate(-50%, -50%) translate(${lineX}px, ${lineY}px) rotate(${angle + 90}deg)`,
|
||||||
|
transformOrigin: 'center',
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
{/* Worker node */}
|
||||||
|
<div
|
||||||
|
className={`absolute w-6 h-6 rounded-full ${workerColor} border-2 ${workerBorder} flex items-center justify-center text-white text-xs font-bold cursor-pointer hover:scale-110 transition-transform`}
|
||||||
|
style={{
|
||||||
|
left: '50%',
|
||||||
|
top: '50%',
|
||||||
|
transform: `translate(-50%, -50%) translate(${x}px, ${y}px)`,
|
||||||
|
}}
|
||||||
|
title={`${worker.friendly_name}\nStatus: ${isDecommissioning ? 'Stopping after current task' : isBackingOff ? `Backing off: ${worker.metadata?.backoff_reason || 'resource pressure'}` : isBusy ? `Working on task #${worker.current_task_id}` : 'Ready - waiting for tasks'}\nMemory: ${worker.metadata?.memory_mb || 0} MB (${worker.metadata?.memory_percent || 0}%)\nCPU: ${formatCpuTime(worker.metadata?.cpu_user_ms || 0)} user, ${formatCpuTime(worker.metadata?.cpu_system_ms || 0)} sys\nCompleted: ${worker.tasks_completed} | Failed: ${worker.tasks_failed}\nLast heartbeat: ${new Date(worker.last_heartbeat_at).toLocaleTimeString()}`}
|
||||||
|
>
|
||||||
|
{index + 1}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Pod stats */}
|
||||||
|
<div className="mt-12 text-center">
|
||||||
|
<p className="text-xs text-gray-500">
|
||||||
|
{busyCount}/{workers.length} busy
|
||||||
|
</p>
|
||||||
|
{isSelected && (
|
||||||
|
<p className="text-xs text-purple-600 font-medium mt-1">Selected</p>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Group workers by pod
|
||||||
|
function groupWorkersByPod(workers: Worker[]): Map<string, Worker[]> {
|
||||||
|
const pods = new Map<string, Worker[]>();
|
||||||
|
for (const worker of workers) {
|
||||||
|
const podName = worker.pod_name || 'Unknown';
|
||||||
|
if (!pods.has(podName)) {
|
||||||
|
pods.set(podName, []);
|
||||||
|
}
|
||||||
|
pods.get(podName)!.push(worker);
|
||||||
|
}
|
||||||
|
return pods;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Format estimated time remaining
|
||||||
|
function formatEstimatedTime(hours: number): string {
|
||||||
|
if (hours < 1) {
|
||||||
|
return `${Math.round(hours * 60)} minutes`;
|
||||||
|
}
|
||||||
|
if (hours < 24) {
|
||||||
|
return `${hours.toFixed(1)} hours`;
|
||||||
|
}
|
||||||
|
const days = hours / 24;
|
||||||
|
if (days < 7) {
|
||||||
|
return `${days.toFixed(1)} days`;
|
||||||
|
}
|
||||||
|
return `${(days / 7).toFixed(1)} weeks`;
|
||||||
|
}
|
||||||
|
|
||||||
export function WorkersDashboard() {
|
export function WorkersDashboard() {
|
||||||
const [workers, setWorkers] = useState<Worker[]>([]);
|
const [workers, setWorkers] = useState<Worker[]>([]);
|
||||||
const [tasks, setTasks] = useState<Task[]>([]);
|
const [tasks, setTasks] = useState<Task[]>([]);
|
||||||
|
const [pendingTaskCount, setPendingTaskCount] = useState<number>(0);
|
||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
const [error, setError] = useState<string | null>(null);
|
const [error, setError] = useState<string | null>(null);
|
||||||
|
|
||||||
// K8s scaling state (added 2024-12-10)
|
// Pod selection state
|
||||||
const [k8sReplicas, setK8sReplicas] = useState<K8sReplicas | null>(null);
|
const [selectedPod, setSelectedPod] = useState<string | null>(null);
|
||||||
const [k8sError, setK8sError] = useState<string | null>(null);
|
|
||||||
const [scaling, setScaling] = useState(false);
|
|
||||||
const [targetReplicas, setTargetReplicas] = useState<number | null>(null);
|
|
||||||
|
|
||||||
// Pagination
|
// Pagination
|
||||||
const [page, setPage] = useState(0);
|
const [page, setPage] = useState(0);
|
||||||
const workersPerPage = 15;
|
const workersPerPage = 15;
|
||||||
|
|
||||||
// Fetch K8s replica count (added 2024-12-10)
|
|
||||||
const fetchK8sReplicas = useCallback(async () => {
|
|
||||||
try {
|
|
||||||
const res = await api.get('/api/workers/k8s/replicas');
|
|
||||||
if (res.data.success && res.data.replicas) {
|
|
||||||
setK8sReplicas(res.data.replicas);
|
|
||||||
if (targetReplicas === null) {
|
|
||||||
setTargetReplicas(res.data.replicas.desired);
|
|
||||||
}
|
|
||||||
setK8sError(null);
|
|
||||||
}
|
|
||||||
} catch (err: any) {
|
|
||||||
// K8s not available (local dev or no RBAC)
|
|
||||||
setK8sError(err.response?.data?.error || 'K8s not available');
|
|
||||||
setK8sReplicas(null);
|
|
||||||
}
|
|
||||||
}, [targetReplicas]);
|
|
||||||
|
|
||||||
// Scale workers (added 2024-12-10)
|
|
||||||
const handleScale = useCallback(async (replicas: number) => {
|
|
||||||
if (replicas < 0 || replicas > 20) return;
|
|
||||||
setScaling(true);
|
|
||||||
try {
|
|
||||||
const res = await api.post('/api/workers/k8s/scale', { replicas });
|
|
||||||
if (res.data.success) {
|
|
||||||
setTargetReplicas(replicas);
|
|
||||||
// Refresh after a short delay to see the change
|
|
||||||
setTimeout(fetchK8sReplicas, 1000);
|
|
||||||
}
|
|
||||||
} catch (err: any) {
|
|
||||||
console.error('Scale error:', err);
|
|
||||||
setK8sError(err.response?.data?.error || 'Failed to scale');
|
|
||||||
} finally {
|
|
||||||
setScaling(false);
|
|
||||||
}
|
|
||||||
}, [fetchK8sReplicas]);
|
|
||||||
|
|
||||||
const fetchData = useCallback(async () => {
|
const fetchData = useCallback(async () => {
|
||||||
try {
|
try {
|
||||||
// Fetch workers from registry
|
// Fetch workers from registry, running tasks, and task counts
|
||||||
const workersRes = await api.get('/api/worker-registry/workers');
|
const [workersRes, tasksRes, countsRes] = await Promise.all([
|
||||||
|
api.get('/api/worker-registry/workers'),
|
||||||
// Fetch running tasks to get current task details
|
api.get('/api/tasks?status=running&limit=100'),
|
||||||
const tasksRes = await api.get('/api/tasks?status=running&limit=100');
|
api.get('/api/tasks/counts'),
|
||||||
|
]);
|
||||||
|
|
||||||
setWorkers(workersRes.data.workers || []);
|
setWorkers(workersRes.data.workers || []);
|
||||||
setTasks(tasksRes.data.tasks || []);
|
setTasks(tasksRes.data.tasks || []);
|
||||||
|
setPendingTaskCount(countsRes.data?.pending || 0);
|
||||||
setError(null);
|
setError(null);
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
console.error('Fetch error:', err);
|
console.error('Fetch error:', err);
|
||||||
@@ -292,16 +485,6 @@ export function WorkersDashboard() {
|
|||||||
}
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
// Cleanup stale workers
|
|
||||||
const handleCleanupStale = async () => {
|
|
||||||
try {
|
|
||||||
await api.post('/api/worker-registry/cleanup', { stale_threshold_minutes: 2 });
|
|
||||||
fetchData();
|
|
||||||
} catch (err: any) {
|
|
||||||
console.error('Cleanup error:', err);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Remove a single worker
|
// Remove a single worker
|
||||||
const handleRemoveWorker = async (workerId: string) => {
|
const handleRemoveWorker = async (workerId: string) => {
|
||||||
if (!confirm('Remove this worker from the registry?')) return;
|
if (!confirm('Remove this worker from the registry?')) return;
|
||||||
@@ -313,16 +496,51 @@ export function WorkersDashboard() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Decommission a worker (graceful shutdown after current task)
|
||||||
|
const handleDecommissionWorker = async (workerId: string, friendlyName: string) => {
|
||||||
|
if (!confirm(`Decommission ${friendlyName}? Worker will stop after completing its current task.`)) return;
|
||||||
|
try {
|
||||||
|
const res = await api.post(`/api/worker-registry/workers/${workerId}/decommission`, {
|
||||||
|
reason: 'Manual decommission from admin UI'
|
||||||
|
});
|
||||||
|
if (res.data.success) {
|
||||||
|
fetchData();
|
||||||
|
}
|
||||||
|
} catch (err: any) {
|
||||||
|
console.error('Decommission error:', err);
|
||||||
|
alert(err.response?.data?.error || 'Failed to decommission worker');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Cancel decommission
|
||||||
|
const handleCancelDecommission = async (workerId: string) => {
|
||||||
|
try {
|
||||||
|
await api.post(`/api/worker-registry/workers/${workerId}/cancel-decommission`);
|
||||||
|
fetchData();
|
||||||
|
} catch (err: any) {
|
||||||
|
console.error('Cancel decommission error:', err);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Add a worker by scaling up the K8s deployment
|
||||||
|
const handleAddWorker = async () => {
|
||||||
|
try {
|
||||||
|
const res = await api.post('/api/workers/k8s/scale-up');
|
||||||
|
if (res.data.success) {
|
||||||
|
// Refresh after a short delay to see the new worker
|
||||||
|
setTimeout(fetchData, 2000);
|
||||||
|
}
|
||||||
|
} catch (err: any) {
|
||||||
|
console.error('Add worker error:', err);
|
||||||
|
alert(err.response?.data?.error || 'Failed to add worker. K8s scaling may not be available.');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
fetchData();
|
fetchData();
|
||||||
fetchK8sReplicas(); // Added 2024-12-10
|
|
||||||
const interval = setInterval(fetchData, 5000);
|
const interval = setInterval(fetchData, 5000);
|
||||||
const k8sInterval = setInterval(fetchK8sReplicas, 10000); // K8s refresh every 10s
|
return () => clearInterval(interval);
|
||||||
return () => {
|
}, [fetchData]);
|
||||||
clearInterval(interval);
|
|
||||||
clearInterval(k8sInterval);
|
|
||||||
};
|
|
||||||
}, [fetchData, fetchK8sReplicas]);
|
|
||||||
|
|
||||||
// Paginated workers
|
// Paginated workers
|
||||||
const paginatedWorkers = workers.slice(
|
const paginatedWorkers = workers.slice(
|
||||||
@@ -362,25 +580,9 @@ export function WorkersDashboard() {
|
|||||||
<h1 className="text-2xl font-bold text-gray-900">Workers</h1>
|
<h1 className="text-2xl font-bold text-gray-900">Workers</h1>
|
||||||
<p className="text-gray-500 mt-1">
|
<p className="text-gray-500 mt-1">
|
||||||
{workers.length} registered workers ({busyWorkers.length} busy, {idleWorkers.length} idle)
|
{workers.length} registered workers ({busyWorkers.length} busy, {idleWorkers.length} idle)
|
||||||
|
<span className="text-xs text-gray-400 ml-2">(auto-refresh 5s)</span>
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex items-center gap-2">
|
|
||||||
<button
|
|
||||||
onClick={handleCleanupStale}
|
|
||||||
className="flex items-center gap-2 px-4 py-2 bg-gray-100 text-gray-700 rounded-lg hover:bg-gray-200 transition-colors"
|
|
||||||
title="Mark stale workers (no heartbeat > 2 min) as offline"
|
|
||||||
>
|
|
||||||
<Trash2 className="w-4 h-4" />
|
|
||||||
Cleanup Stale
|
|
||||||
</button>
|
|
||||||
<button
|
|
||||||
onClick={() => fetchData()}
|
|
||||||
className="flex items-center gap-2 px-4 py-2 bg-emerald-600 text-white rounded-lg hover:bg-emerald-700 transition-colors"
|
|
||||||
>
|
|
||||||
<RefreshCw className="w-4 h-4" />
|
|
||||||
Refresh
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{error && (
|
{error && (
|
||||||
@@ -389,68 +591,6 @@ export function WorkersDashboard() {
|
|||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{/* K8s Scaling Card (added 2024-12-10) */}
|
|
||||||
{k8sReplicas && (
|
|
||||||
<div className="bg-white rounded-lg border border-gray-200 p-4">
|
|
||||||
<div className="flex items-center justify-between">
|
|
||||||
<div className="flex items-center gap-3">
|
|
||||||
<div className="w-10 h-10 bg-purple-100 rounded-lg flex items-center justify-center">
|
|
||||||
<Server className="w-5 h-5 text-purple-600" />
|
|
||||||
</div>
|
|
||||||
<div>
|
|
||||||
<p className="text-sm text-gray-500">K8s Worker Pods</p>
|
|
||||||
<p className="text-xl font-semibold">
|
|
||||||
{k8sReplicas.current} / {k8sReplicas.desired}
|
|
||||||
{k8sReplicas.current !== k8sReplicas.desired && (
|
|
||||||
<span className="text-sm font-normal text-yellow-600 ml-2">scaling...</span>
|
|
||||||
)}
|
|
||||||
</p>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div className="flex items-center gap-2">
|
|
||||||
<button
|
|
||||||
onClick={() => handleScale((targetReplicas || k8sReplicas.desired) - 1)}
|
|
||||||
disabled={scaling || (targetReplicas || k8sReplicas.desired) <= 0}
|
|
||||||
className="w-8 h-8 flex items-center justify-center bg-gray-100 text-gray-700 rounded-lg hover:bg-gray-200 disabled:opacity-50 disabled:cursor-not-allowed transition-colors"
|
|
||||||
title="Scale down"
|
|
||||||
>
|
|
||||||
<Minus className="w-4 h-4" />
|
|
||||||
</button>
|
|
||||||
<input
|
|
||||||
type="number"
|
|
||||||
min="0"
|
|
||||||
max="20"
|
|
||||||
value={targetReplicas ?? k8sReplicas.desired}
|
|
||||||
onChange={(e) => setTargetReplicas(Math.max(0, Math.min(20, parseInt(e.target.value) || 0)))}
|
|
||||||
onBlur={() => {
|
|
||||||
if (targetReplicas !== null && targetReplicas !== k8sReplicas.desired) {
|
|
||||||
handleScale(targetReplicas);
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
onKeyDown={(e) => {
|
|
||||||
if (e.key === 'Enter' && targetReplicas !== null && targetReplicas !== k8sReplicas.desired) {
|
|
||||||
handleScale(targetReplicas);
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
className="w-16 text-center border border-gray-300 rounded-lg px-2 py-1 text-lg font-semibold"
|
|
||||||
/>
|
|
||||||
<button
|
|
||||||
onClick={() => handleScale((targetReplicas || k8sReplicas.desired) + 1)}
|
|
||||||
disabled={scaling || (targetReplicas || k8sReplicas.desired) >= 20}
|
|
||||||
className="w-8 h-8 flex items-center justify-center bg-gray-100 text-gray-700 rounded-lg hover:bg-gray-200 disabled:opacity-50 disabled:cursor-not-allowed transition-colors"
|
|
||||||
title="Scale up"
|
|
||||||
>
|
|
||||||
<Plus className="w-4 h-4" />
|
|
||||||
</button>
|
|
||||||
{scaling && <Loader2 className="w-4 h-4 text-purple-600 animate-spin ml-2" />}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
{k8sError && (
|
|
||||||
<p className="text-xs text-red-500 mt-2">{k8sError}</p>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{/* Stats Cards */}
|
{/* Stats Cards */}
|
||||||
<div className="grid grid-cols-5 gap-4">
|
<div className="grid grid-cols-5 gap-4">
|
||||||
<div className="bg-white rounded-lg border border-gray-200 p-4">
|
<div className="bg-white rounded-lg border border-gray-200 p-4">
|
||||||
@@ -510,6 +650,197 @@ export function WorkersDashboard() {
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{/* Estimated Completion Time Card */}
|
||||||
|
{pendingTaskCount > 0 && activeWorkers.length > 0 && (() => {
|
||||||
|
// Calculate average task rate across all workers
|
||||||
|
const totalHoursUp = activeWorkers.reduce((sum, w) => {
|
||||||
|
if (!w.started_at) return sum;
|
||||||
|
const start = new Date(w.started_at);
|
||||||
|
const now = new Date();
|
||||||
|
return sum + (now.getTime() - start.getTime()) / (1000 * 60 * 60);
|
||||||
|
}, 0);
|
||||||
|
|
||||||
|
const totalTasksDone = totalCompleted + totalFailed;
|
||||||
|
const avgTasksPerHour = totalHoursUp > 0.1 ? totalTasksDone / totalHoursUp : 0;
|
||||||
|
const estimatedHours = avgTasksPerHour > 0 ? pendingTaskCount / avgTasksPerHour : null;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="bg-gradient-to-r from-amber-50 to-orange-50 rounded-lg border border-amber-200 p-4">
|
||||||
|
<div className="flex items-center justify-between">
|
||||||
|
<div className="flex items-center gap-3">
|
||||||
|
<div className="w-10 h-10 bg-amber-100 rounded-lg flex items-center justify-center">
|
||||||
|
<Clock className="w-5 h-5 text-amber-600" />
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<p className="text-sm text-amber-700 font-medium">Estimated Time to Complete Queue</p>
|
||||||
|
<p className="text-2xl font-bold text-amber-900">
|
||||||
|
{estimatedHours !== null ? formatEstimatedTime(estimatedHours) : 'Calculating...'}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div className="text-right text-sm text-amber-700">
|
||||||
|
<p><span className="font-semibold">{pendingTaskCount}</span> pending tasks</p>
|
||||||
|
<p><span className="font-semibold">{activeWorkers.length}</span> active workers</p>
|
||||||
|
{avgTasksPerHour > 0 && (
|
||||||
|
<p className="text-xs text-amber-600 mt-1">
|
||||||
|
~{avgTasksPerHour.toFixed(1)} tasks/hour
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
})()}
|
||||||
|
|
||||||
|
{/* Worker Pods Visualization */}
|
||||||
|
<div className="bg-white rounded-lg border border-gray-200 overflow-hidden">
|
||||||
|
<div className="px-4 py-3 border-b border-gray-200 bg-gray-50">
|
||||||
|
<div className="flex items-center justify-between">
|
||||||
|
<div>
|
||||||
|
<h3 className="text-sm font-semibold text-gray-900 flex items-center gap-2">
|
||||||
|
<Zap className="w-4 h-4 text-emerald-500" />
|
||||||
|
Worker Pods ({Array.from(groupWorkersByPod(workers)).length} pods, {activeWorkers.length} workers)
|
||||||
|
</h3>
|
||||||
|
<p className="text-xs text-gray-500 mt-0.5">
|
||||||
|
<span className="inline-flex items-center gap-1"><span className="w-2 h-2 rounded-full bg-emerald-500"></span> ready</span>
|
||||||
|
<span className="mx-2">|</span>
|
||||||
|
<span className="inline-flex items-center gap-1"><span className="w-2 h-2 rounded-full bg-blue-500"></span> busy</span>
|
||||||
|
<span className="mx-2">|</span>
|
||||||
|
<span className="inline-flex items-center gap-1"><span className="w-2 h-2 rounded-full bg-yellow-500"></span> backing off</span>
|
||||||
|
<span className="mx-2">|</span>
|
||||||
|
<span className="inline-flex items-center gap-1"><span className="w-2 h-2 rounded-full bg-orange-500"></span> stopping</span>
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<div className="text-sm text-gray-500">
|
||||||
|
{busyWorkers.length} busy, {activeWorkers.length - busyWorkers.length} idle
|
||||||
|
{selectedPod && (
|
||||||
|
<button
|
||||||
|
onClick={() => setSelectedPod(null)}
|
||||||
|
className="ml-3 text-xs text-purple-600 hover:text-purple-800 underline"
|
||||||
|
>
|
||||||
|
Clear selection
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{workers.length === 0 ? (
|
||||||
|
<div className="px-4 py-12 text-center text-gray-500">
|
||||||
|
<Users className="w-12 h-12 mx-auto mb-3 text-gray-300" />
|
||||||
|
<p className="font-medium">No worker pods running</p>
|
||||||
|
<p className="text-xs mt-1">Start pods to process tasks from the queue</p>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<div className="p-6">
|
||||||
|
<div className="flex flex-wrap justify-center gap-8">
|
||||||
|
{Array.from(groupWorkersByPod(workers)).map(([podName, podWorkers]) => (
|
||||||
|
<PodVisualization
|
||||||
|
key={podName}
|
||||||
|
podName={podName}
|
||||||
|
workers={podWorkers}
|
||||||
|
isSelected={selectedPod === podName}
|
||||||
|
onSelect={() => setSelectedPod(selectedPod === podName ? null : podName)}
|
||||||
|
/>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Selected Pod Control Panel */}
|
||||||
|
{selectedPod && (() => {
|
||||||
|
const podWorkers = groupWorkersByPod(workers).get(selectedPod) || [];
|
||||||
|
const busyInPod = podWorkers.filter(w => w.current_task_id !== null).length;
|
||||||
|
const idleInPod = podWorkers.filter(w => w.current_task_id === null && !w.decommission_requested).length;
|
||||||
|
const stoppingInPod = podWorkers.filter(w => w.decommission_requested).length;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="mt-6 border-t border-gray-200 pt-6">
|
||||||
|
<div className="bg-purple-50 rounded-lg border border-purple-200 p-4">
|
||||||
|
<div className="flex items-center justify-between mb-4">
|
||||||
|
<div className="flex items-center gap-3">
|
||||||
|
<div className="w-10 h-10 bg-purple-100 rounded-lg flex items-center justify-center">
|
||||||
|
<Server className="w-5 h-5 text-purple-600" />
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<h4 className="font-semibold text-purple-900">{selectedPod}</h4>
|
||||||
|
<p className="text-xs text-purple-600">
|
||||||
|
{podWorkers.length} workers: {busyInPod} busy, {idleInPod} idle{stoppingInPod > 0 && `, ${stoppingInPod} stopping`}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Worker list in selected pod */}
|
||||||
|
<div className="space-y-2">
|
||||||
|
{podWorkers.map((worker) => {
|
||||||
|
const isBusy = worker.current_task_id !== null;
|
||||||
|
const isDecommissioning = worker.decommission_requested;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div key={worker.id} className="flex items-center justify-between bg-white rounded-lg px-3 py-2 border border-purple-100">
|
||||||
|
<div className="flex items-center gap-3">
|
||||||
|
<div className={`w-8 h-8 rounded-full flex items-center justify-center text-white text-sm font-bold ${
|
||||||
|
isDecommissioning ? 'bg-orange-500' :
|
||||||
|
isBusy ? 'bg-blue-500' : 'bg-emerald-500'
|
||||||
|
}`}>
|
||||||
|
{worker.friendly_name?.charAt(0) || '?'}
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<p className="text-sm font-medium text-gray-900">{worker.friendly_name}</p>
|
||||||
|
<p className="text-xs text-gray-500">
|
||||||
|
{isDecommissioning ? (
|
||||||
|
<span className="text-orange-600">Stopping after current task...</span>
|
||||||
|
) : isBusy ? (
|
||||||
|
<span className="text-blue-600">Working on task #{worker.current_task_id}</span>
|
||||||
|
) : (
|
||||||
|
<span className="text-emerald-600">Idle - ready for tasks</span>
|
||||||
|
)}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
{isDecommissioning ? (
|
||||||
|
<button
|
||||||
|
onClick={() => handleCancelDecommission(worker.worker_id)}
|
||||||
|
className="flex items-center gap-1.5 px-3 py-1.5 text-sm bg-white border border-gray-300 text-gray-700 rounded-lg hover:bg-gray-50 transition-colors"
|
||||||
|
title="Cancel decommission"
|
||||||
|
>
|
||||||
|
<Undo2 className="w-4 h-4" />
|
||||||
|
Cancel
|
||||||
|
</button>
|
||||||
|
) : (
|
||||||
|
<button
|
||||||
|
onClick={() => handleDecommissionWorker(worker.worker_id, worker.friendly_name)}
|
||||||
|
className="flex items-center gap-1.5 px-3 py-1.5 text-sm bg-orange-100 text-orange-700 rounded-lg hover:bg-orange-200 transition-colors"
|
||||||
|
title={isBusy ? 'Worker will stop after completing current task' : 'Remove idle worker'}
|
||||||
|
>
|
||||||
|
<PowerOff className="w-4 h-4" />
|
||||||
|
{isBusy ? 'Stop after task' : 'Remove'}
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Add Worker button */}
|
||||||
|
<div className="mt-4 pt-4 border-t border-purple-200">
|
||||||
|
<button
|
||||||
|
onClick={handleAddWorker}
|
||||||
|
className="flex items-center gap-1.5 px-3 py-2 text-sm bg-emerald-100 text-emerald-700 rounded-lg hover:bg-emerald-200 transition-colors"
|
||||||
|
>
|
||||||
|
<Plus className="w-4 h-4" />
|
||||||
|
Add Worker
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
})()}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
{/* Workers Table */}
|
{/* Workers Table */}
|
||||||
<div className="bg-white rounded-lg border border-gray-200 overflow-hidden">
|
<div className="bg-white rounded-lg border border-gray-200 overflow-hidden">
|
||||||
<div className="px-4 py-3 border-b border-gray-200 bg-gray-50 flex items-center justify-between">
|
<div className="px-4 py-3 border-b border-gray-200 bg-gray-50 flex items-center justify-between">
|
||||||
@@ -552,10 +883,10 @@ export function WorkersDashboard() {
|
|||||||
<th className="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase">Worker</th>
|
<th className="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase">Worker</th>
|
||||||
<th className="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase">Role</th>
|
<th className="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase">Role</th>
|
||||||
<th className="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase">Status</th>
|
<th className="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase">Status</th>
|
||||||
<th className="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase">Exit Location</th>
|
<th className="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase">Resources</th>
|
||||||
<th className="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase">Current Task</th>
|
<th className="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase">Tasks</th>
|
||||||
<th className="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase">Duration</th>
|
<th className="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase">Duration</th>
|
||||||
<th className="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase">Utilization</th>
|
<th className="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase">Throughput</th>
|
||||||
<th className="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase">Heartbeat</th>
|
<th className="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase">Heartbeat</th>
|
||||||
<th className="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase"></th>
|
<th className="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase"></th>
|
||||||
</tr>
|
</tr>
|
||||||
@@ -570,16 +901,29 @@ export function WorkersDashboard() {
|
|||||||
<tr key={worker.id} className="hover:bg-gray-50">
|
<tr key={worker.id} className="hover:bg-gray-50">
|
||||||
<td className="px-4 py-3">
|
<td className="px-4 py-3">
|
||||||
<div className="flex items-center gap-3">
|
<div className="flex items-center gap-3">
|
||||||
<div className={`w-10 h-10 rounded-full flex items-center justify-center text-white font-bold text-sm ${
|
<div className={`w-10 h-10 rounded-full flex items-center justify-center text-white font-bold text-sm relative ${
|
||||||
|
worker.decommission_requested ? 'bg-orange-500' :
|
||||||
worker.health_status === 'offline' ? 'bg-gray-400' :
|
worker.health_status === 'offline' ? 'bg-gray-400' :
|
||||||
worker.health_status === 'stale' ? 'bg-yellow-500' :
|
worker.health_status === 'stale' ? 'bg-yellow-500' :
|
||||||
worker.health_status === 'busy' ? 'bg-blue-500' :
|
worker.health_status === 'busy' ? 'bg-blue-500' :
|
||||||
'bg-emerald-500'
|
'bg-emerald-500'
|
||||||
}`}>
|
}`}>
|
||||||
{worker.friendly_name?.charAt(0) || '?'}
|
{worker.friendly_name?.charAt(0) || '?'}
|
||||||
|
{worker.decommission_requested && (
|
||||||
|
<div className="absolute -top-1 -right-1 w-4 h-4 bg-red-500 rounded-full flex items-center justify-center">
|
||||||
|
<PowerOff className="w-2.5 h-2.5 text-white" />
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<p className="font-medium text-gray-900">{worker.friendly_name}</p>
|
<p className="font-medium text-gray-900 flex items-center gap-1.5">
|
||||||
|
{worker.friendly_name}
|
||||||
|
{worker.decommission_requested && (
|
||||||
|
<span className="text-xs text-orange-600 bg-orange-100 px-1.5 py-0.5 rounded" title={worker.decommission_reason || 'Pending decommission'}>
|
||||||
|
stopping
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
</p>
|
||||||
<p className="text-xs text-gray-400 font-mono">{worker.worker_id.slice(0, 20)}...</p>
|
<p className="text-xs text-gray-400 font-mono">{worker.worker_id.slice(0, 20)}...</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -591,45 +935,10 @@ export function WorkersDashboard() {
|
|||||||
<HealthBadge status={worker.status} healthStatus={worker.health_status} />
|
<HealthBadge status={worker.status} healthStatus={worker.health_status} />
|
||||||
</td>
|
</td>
|
||||||
<td className="px-4 py-3">
|
<td className="px-4 py-3">
|
||||||
{(() => {
|
<ResourceBadge worker={worker} />
|
||||||
const loc = worker.metadata?.proxy_location;
|
|
||||||
if (!loc) {
|
|
||||||
return <span className="text-gray-400 text-sm">-</span>;
|
|
||||||
}
|
|
||||||
const parts = [loc.city, loc.state, loc.country].filter(Boolean);
|
|
||||||
if (parts.length === 0) {
|
|
||||||
return loc.isRotating ? (
|
|
||||||
<span className="text-xs text-purple-600 font-medium" title="Rotating proxy - exit location varies per request">
|
|
||||||
Rotating
|
|
||||||
</span>
|
|
||||||
) : (
|
|
||||||
<span className="text-gray-400 text-sm">Unknown</span>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return (
|
|
||||||
<div className="flex items-center gap-1.5" title={loc.timezone || ''}>
|
|
||||||
<MapPin className="w-3 h-3 text-gray-400" />
|
|
||||||
<span className="text-sm text-gray-700">
|
|
||||||
{parts.join(', ')}
|
|
||||||
</span>
|
|
||||||
{loc.isRotating && (
|
|
||||||
<span className="text-xs text-purple-500" title="Rotating proxy">*</span>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
})()}
|
|
||||||
</td>
|
</td>
|
||||||
<td className="px-4 py-3">
|
<td className="px-4 py-3">
|
||||||
{worker.current_task_id ? (
|
<TaskCountBadge worker={worker} tasks={tasks} />
|
||||||
<div>
|
|
||||||
<span className="text-sm text-gray-900">Task #{worker.current_task_id}</span>
|
|
||||||
{currentTask?.dispensary_name && (
|
|
||||||
<p className="text-xs text-gray-500">{currentTask.dispensary_name}</p>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
) : (
|
|
||||||
<span className="text-gray-400 text-sm">Idle</span>
|
|
||||||
)}
|
|
||||||
</td>
|
</td>
|
||||||
<td className="px-4 py-3">
|
<td className="px-4 py-3">
|
||||||
{currentTask?.started_at ? (
|
{currentTask?.started_at ? (
|
||||||
|
|||||||
36
k8s/scraper-rbac.yaml
Normal file
36
k8s/scraper-rbac.yaml
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
# RBAC configuration for scraper pod to control worker scaling
|
||||||
|
# Allows the scraper to read and scale the scraper-worker statefulset
|
||||||
|
apiVersion: v1
|
||||||
|
kind: ServiceAccount
|
||||||
|
metadata:
|
||||||
|
name: scraper-sa
|
||||||
|
namespace: dispensary-scraper
|
||||||
|
---
|
||||||
|
apiVersion: rbac.authorization.k8s.io/v1
|
||||||
|
kind: Role
|
||||||
|
metadata:
|
||||||
|
name: worker-scaler
|
||||||
|
namespace: dispensary-scraper
|
||||||
|
rules:
|
||||||
|
# Allow reading deployment and statefulset status
|
||||||
|
- apiGroups: ["apps"]
|
||||||
|
resources: ["deployments", "statefulsets"]
|
||||||
|
verbs: ["get", "list"]
|
||||||
|
# Allow scaling deployments and statefulsets
|
||||||
|
- apiGroups: ["apps"]
|
||||||
|
resources: ["deployments/scale", "statefulsets/scale"]
|
||||||
|
verbs: ["get", "patch", "update"]
|
||||||
|
---
|
||||||
|
apiVersion: rbac.authorization.k8s.io/v1
|
||||||
|
kind: RoleBinding
|
||||||
|
metadata:
|
||||||
|
name: scraper-worker-scaler
|
||||||
|
namespace: dispensary-scraper
|
||||||
|
subjects:
|
||||||
|
- kind: ServiceAccount
|
||||||
|
name: scraper-sa
|
||||||
|
namespace: dispensary-scraper
|
||||||
|
roleRef:
|
||||||
|
kind: Role
|
||||||
|
name: worker-scaler
|
||||||
|
apiGroup: rbac.authorization.k8s.io
|
||||||
@@ -12,7 +12,7 @@ metadata:
|
|||||||
name: scraper-worker
|
name: scraper-worker
|
||||||
namespace: dispensary-scraper
|
namespace: dispensary-scraper
|
||||||
spec:
|
spec:
|
||||||
replicas: 5
|
replicas: 25
|
||||||
selector:
|
selector:
|
||||||
matchLabels:
|
matchLabels:
|
||||||
app: scraper-worker
|
app: scraper-worker
|
||||||
@@ -40,12 +40,16 @@ spec:
|
|||||||
valueFrom:
|
valueFrom:
|
||||||
fieldRef:
|
fieldRef:
|
||||||
fieldPath: metadata.name
|
fieldPath: metadata.name
|
||||||
|
- name: API_BASE_URL
|
||||||
|
value: "http://scraper"
|
||||||
|
- name: NODE_OPTIONS
|
||||||
|
value: "--max-old-space-size=1500"
|
||||||
resources:
|
resources:
|
||||||
requests:
|
requests:
|
||||||
memory: "256Mi"
|
memory: "1Gi"
|
||||||
cpu: "100m"
|
cpu: "100m"
|
||||||
limits:
|
limits:
|
||||||
memory: "512Mi"
|
memory: "2Gi"
|
||||||
cpu: "500m"
|
cpu: "500m"
|
||||||
livenessProbe:
|
livenessProbe:
|
||||||
exec:
|
exec:
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ spec:
|
|||||||
labels:
|
labels:
|
||||||
app: scraper
|
app: scraper
|
||||||
spec:
|
spec:
|
||||||
|
serviceAccountName: scraper-sa
|
||||||
imagePullSecrets:
|
imagePullSecrets:
|
||||||
- name: regcred
|
- name: regcred
|
||||||
containers:
|
containers:
|
||||||
|
|||||||
@@ -6,6 +6,19 @@ kind: Namespace
|
|||||||
metadata:
|
metadata:
|
||||||
name: woodpecker
|
name: woodpecker
|
||||||
---
|
---
|
||||||
|
# PVC for npm cache - shared across CI jobs
|
||||||
|
apiVersion: v1
|
||||||
|
kind: PersistentVolumeClaim
|
||||||
|
metadata:
|
||||||
|
name: npm-cache
|
||||||
|
namespace: woodpecker
|
||||||
|
spec:
|
||||||
|
accessModes:
|
||||||
|
- ReadWriteMany
|
||||||
|
resources:
|
||||||
|
requests:
|
||||||
|
storage: 5Gi
|
||||||
|
---
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Secret
|
kind: Secret
|
||||||
metadata:
|
metadata:
|
||||||
@@ -52,6 +65,9 @@ spec:
|
|||||||
value: "woodpecker"
|
value: "woodpecker"
|
||||||
- name: WOODPECKER_BACKEND_K8S_VOLUME_SIZE
|
- name: WOODPECKER_BACKEND_K8S_VOLUME_SIZE
|
||||||
value: "10G"
|
value: "10G"
|
||||||
|
# Allow CI steps to mount the npm-cache PVC
|
||||||
|
- name: WOODPECKER_BACKEND_K8S_VOLUMES
|
||||||
|
value: "npm-cache:/npm-cache"
|
||||||
resources:
|
resources:
|
||||||
limits:
|
limits:
|
||||||
memory: "512Mi"
|
memory: "512Mi"
|
||||||
|
|||||||
Reference in New Issue
Block a user