A high-performance analytics service built with Node.js, Redis, and PostgreSQL, capable of handling millions of requests per hour.
- Real-time event tracking
- 90-day rolling analytics data
- Redis-based caching for high performance
- PostgreSQL for persistent storage
- Docker-based development environment
- Built with TypeScript for type safety
- Docker and Docker Compose
- Node.js 18+ (for local development)
- npm or yarn
- Clone the repository:
git clone https://github.com/jarrettbarnett/node-analytics.git
cd node-analytics- Copy the environment file:
cp .env.example .env- Install dependencies:
npm install- Start the services:
npm run docker:upThe following services will be available:
- Analytics API: http://localhost:3000
- Redis Commander: http://localhost:8081
- pgAdmin: http://localhost:8082 (login: admin@admin.com / password: admin)
analytics-service/
├── src/
│ ├── config/ # Configuration files
│ ├── controllers/ # Request handlers
│ ├── models/ # Data models
│ ├── services/ # Business logic
│ ├── routes/ # API routes
│ ├── utils/ # Utilities
│ └── app.ts # Application entry
├── prisma/ # Database schema
├── tests/ # Test files
└── docker-compose.yml # Docker services
npm run dev: Start development server with hot reloadnpm run build: Build the TypeScript projectnpm run start: Start the production servernpm test: Run testsnpm run docker:up: Start all Docker servicesnpm run docker:down: Stop all Docker servicesnpm run docker:clean: Stop services and remove volumesnpm run prisma:generate: Generate Prisma clientnpm run prisma:migrate: Run database migrations
Track an event:
curl -X POST http://localhost:3000/analytics/event \
-H "Content-Type: application/json" \
-d '{
"eventType": "page_view",
"userId": "user123",
"metadata": {
"page": "/home",
"referrer": "google.com"
}
}'curl http://localhost:3000/analytics/stats?eventType=page_view&days=30Connect to PostgreSQL using pgAdmin:
- Access http://localhost:8082
- Login with admin@admin.com / admin
- Add new server:
- Host: postgres
- Port: 5432
- Database: analytics
- Username: user
- Password: password
Monitor Redis using Redis Commander:
- Access http://localhost:8081
- Browse keys and data directly in the web interface
Run the test suite:
npm testRun tests with coverage:
npm run test:coverageTo view service logs:
docker-compose logs -fAll services
docker-compose logs -fSpecific service
docker-compose logs -f appAccess Redis:
docker-compose exec redis redis-cliAccess PostgreSQL:
docker-compose exec postgres psql -U user -d analyticsFor production deployment:
-
Update environment variables:
- Set
NODE_ENV=production - Use secure passwords
- Configure proper Redis and PostgreSQL settings
- Set
-
Build the production image:
docker build -t analytics-service:prod .