> ## Documentation Index
> Fetch the complete documentation index at: https://compass.docs.sunnyscoach.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Daily Email Activity Trend

> Retrieves the trend of incoming and sent emails across the specified date range.   - If no `startDate` or `endDate` is provided, the system defaults to the last 7 days (a week from today).   - Each label in `labels` corresponds to a date in the range, and `series` contains counts of sent and received emails for each day.


Retrieves the trend of incoming and sent emails across the specified date range. Each label in `labels` corresponds to a date in the range, and `series` contains counts of sent and received emails for each day.

## Request

### Headers

| Name          | Type   | Required | Description  |
| ------------- | ------ | -------- | ------------ |
| Authorization | string | Yes      | Bearer token |

### Path Parameters

| Parameter | Type   | Required | Description                                                         |
| --------- | ------ | -------- | ------------------------------------------------------------------- |
| startDate | string | Yes      | Start date of the range (ISO 8601). Defaults to 7 days before today |
| endDate   | string | Yes      | End date of the range (ISO 8601). Defaults to today                 |

#### Parameter Format

* **Format**: `YYYY-MM-DD` (ISO 8601 date)
* **Timezone**: UTC
* **Default**: Last 7 days if not provided

## Response

### 200 OK - Successfully retrieved email activity trend

```json theme={null}
{
  "message": "",
  "data": {
    "series": [
      {
        "name": "Incoming",
        "data": [12, 18, 9, 25, 14, 22, 19]
      },
      {
        "name": "Sent",
        "data": [8, 15, 7, 20, 12, 18, 16]
      }
    ],
    "labels": [
      "2025-10-01",
      "2025-10-02",
      "2025-10-03",
      "2025-10-04",
      "2025-10-05",
      "2025-10-06",
      "2025-10-07"
    ]
  }
}
```

### 401 Unauthorized

```json theme={null}
{
  "error": {
    "code": "UNAUTHORIZED",
    "message": "Missing or invalid token"
  }
}
```

### 500 Internal Server Error

```json theme={null}
{
  "error": {
    "code": "SERVER_ERROR",
    "message": "Internal server error"
  }
}
```

## Example

```bash theme={null}
curl -X GET 'http://localhost:2000/email-meter/stats/emails-trend/2025-10-01/2025-10-07' \
  -H 'Authorization: Bearer your-jwt-token'
```

## Data Structure

### Series Array

Each object in the series represents:

| Field | Type   | Description                             |
| ----- | ------ | --------------------------------------- |
| name  | string | Series name ("Incoming" or "Sent")      |
| data  | array  | Email counts corresponding to each date |

### Labels Array

| Field  | Type  | Description                |
| ------ | ----- | -------------------------- |
| labels | array | Dates in YYYY-MM-DD format |

### Data Alignment

* `series[0].data[i]` corresponds to `labels[i]` for Incoming emails
* `series[1].data[i]` corresponds to `labels[i]` for Sent emails
* All arrays have the same length

## Use Cases

* **Trend Analysis**: Visualize email volume over time
* **Capacity Planning**: Identify peak email periods
* **Performance Monitoring**: Track daily email patterns
* **Dashboard Visualization**: Create line charts and graphs
* **Seasonal Patterns**: Identify weekly/monthly trends

## Visualization Examples

### Line Chart

```javascript theme={null}
// Example using Chart.js
const ctx = document.getElementById('emailTrend').getContext('2d');
new Chart(ctx, {
  type: 'line',
  data: {
    labels: response.data.labels,
    datasets: response.data.series
  },
  options: {
    responsive: true,
    scales: {
      y: {
        beginAtZero: true,
        title: {
          display: true,
          text: 'Email Count'
        }
      }
    }
  }
});
```

### Bar Chart

```javascript theme={null}
// Example for daily comparison
new Chart(ctx, {
  type: 'bar',
  data: {
    labels: response.data.labels,
    datasets: response.data.series
  }
});
```

## Analysis Metrics

### Daily Totals

```javascript theme={null}
const dailyTotals = response.data.labels.map((label, index) => ({
  date: label,
  total: response.data.series[0].data[index] + response.data.series[1].data[index],
  incoming: response.data.series[0].data[index],
  sent: response.data.series[1].data[index]
}));
```

### Trend Calculations

```javascript theme={null}
// Calculate moving average
const movingAverage = (data, period) => {
  return data.map((_, index) => {
    const start = Math.max(0, index - period + 1);
    const subset = data.slice(start, index + 1);
    return subset.reduce((a, b) => a + b, 0) / subset.length;
  });
};
```

## Best Practices

1. **Date Range**: Use reasonable ranges (recommended: max 90 days)
2. **Caching**: Cache trend data for dashboard performance
3. **Real-time Updates**: Update trends periodically for live dashboards
4. **Empty Data**: Handle days with zero emails gracefully
5. **Time Zones**: Consider converting to local time zones for display

## Performance Considerations

* **Large Ranges**: Longer date ranges may take more time to process
* **Data Points**: Each day represents a data point
* **Caching**: Results are cached for 15 minutes by default
* **Memory Usage**: Large datasets may impact client-side rendering

## Related Endpoints

* Use `/email-meter/stats/hourly-trends` for hourly breakdown
* Use `/email-meter/stats/email-counts` for aggregate statistics
* Use `/email-meter/stats/clients/incoming-trends` for client-specific trends

## Notes

* Data includes all email types (incoming, outgoing, internal)
* Weekends and holidays are included in the trend
* Zero-value days are included for complete timeline
* Data is calculated based on email timestamps in UTC
* Automated emails are included in counts unless filtered


## OpenAPI

````yaml GET /email-meter/stats/emails-trend/{startDate}/{endDate}
openapi: 3.0.3
info:
  title: COMPASS API
  version: 1.0.0
  description: OpenAPI specification for the Compass backend routes.
servers:
  - url: http://localhost:2000
    description: Local development server
security: []
tags:
  - name: Auth
    description: >-
      Authentication and session management endpoints (OAuth and session
      validation)
  - name: Users
    description: Operations for managing user records
  - name: Departments
    description: Operations for managing departments
  - name: Email Meter
    description: APIs to get Email Meter Stats
  - name: HubSpot Tickets
    description: Feedback APIs
  - name: Permissions
    description: Permissions APIs
  - name: Transcription
    description: Transcription APIs
paths:
  /email-meter/stats/emails-trend/{startDate}/{endDate}:
    get:
      tags:
        - Email Meter
      summary: Get daily email activity trend
      description: >
        Retrieves the trend of incoming and sent emails across the specified
        date range.   - If no `startDate` or `endDate` is provided, the system
        defaults to the last 7 days (a week from today).   - Each label in
        `labels` corresponds to a date in the range, and `series` contains
        counts of sent and received emails for each day.
      parameters:
        - name: startDate
          in: path
          required: true
          schema:
            type: string
            format: date
          description: >-
            Start date of the range (ISO 8601). Defaults to 7 days before today
            if not provided.
          example: '2025-10-01'
        - name: endDate
          in: path
          required: true
          schema:
            type: string
            format: date
          description: End date of the range (ISO 8601). Defaults to today if not provided.
          example: '2025-10-07'
      responses:
        '200':
          description: Successfully retrieved email activity trend
          content:
            application/json:
              schema:
                type: object
                properties:
                  message:
                    type: string
                  data:
                    type: object
                    properties:
                      series:
                        type: array
                        description: List of data series for incoming and sent emails
                        items:
                          type: object
                          properties:
                            name:
                              type: string
                              example: Incoming
                            data:
                              type: array
                              items:
                                type: integer
                              example:
                                - 12
                                - 18
                                - 9
                                - 25
                                - 14
                                - 22
                                - 19
                      labels:
                        type: array
                        description: Dates corresponding to each data point
                        items:
                          type: string
                          format: date
                        example:
                          - '2025-10-01'
                          - '2025-10-02'
                          - '2025-10-03'
                          - '2025-10-04'
                          - '2025-10-05'
                          - '2025-10-06'
                          - '2025-10-07'
        '401':
          description: Unauthorized (missing or invalid token)
        '500':
          description: Server error
      security:
        - bearerAuth: []
components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: JWT

````