hngprojects / hng_boilerplate_nestjs

Description
Apache License 2.0
181 stars 105 forks source link

[FEAT] Implement Backend Support for Analytics Charts on Dashboard #124

Closed fazzy12 closed 1 week ago

fazzy12 commented 1 month ago

Labels: backend, API, analytics, charts, security, documentation

Description

Implement backend support for analytics charts to visualize data insights for users on the dashboard. This involves creating API endpoints, processing data, ensuring security, and providing documentation.

Acceptance Criteria

  1. API endpoints to fetch chart data are implemented and tested.
  2. Data is processed and aggregated to provide meaningful insights.
  3. Endpoints are secured and accessible only to authorized users.
  4. Real-time data updates (if applicable) are implemented using WebSocket or similar technologies.
  5. Clear documentation is provided for the API endpoints.

Purpose

To enable the frontend to integrate and display analytics charts by providing the necessary data and ensuring seamless data flow and security.

Requirements

  1. Data API Endpoints:

    • Create and document API endpoints for fetching data required by various types of charts (e.g., line charts, bar charts, pie charts).
  2. Data Aggregation and Processing:

    • Aggregate and process raw data to provide the necessary insights and trends for the charts.
  3. Authentication and Authorization:

    • Implement authentication and authorization checks to secure the data endpoints.
  4. Real-Time Data Updates (if applicable):

    • Implement WebSocket or similar technologies to push real-time data updates to the frontend.
  5. Documentation:

    • Provide detailed documentation on how to access and use the API endpoints, including request and response structures.

Expected Outcome

  1. Users can view and interact with analytics charts on the dashboard, gaining valuable insights from visualized data trends.
  2. API endpoints are performant and secure, handling large datasets efficiently.
  3. Clear and comprehensive documentation is available, enabling easy integration with the frontend.

Steps to Complete

  1. Design API Schema:

    • Define the structure of the data that the backend will provide to the frontend.
  2. Implement Endpoints:

    • Develop the API endpoints in the backend to fetch and process the data required for the charts.
  3. Test Endpoints:

    • Test the API endpoints to ensure they return the correct data and handle errors gracefully.
  4. Secure Endpoints:

    • Implement authentication and authorization to secure the endpoints.
  5. Real-Time Data Updates:

    • If applicable, implement WebSocket or similar technologies for real-time data updates.
  6. Documentation:

    • Write clear documentation on how to use the API endpoints, including examples of requests and responses.

Endpoints and Request/Response Formats

  1. Summary Endpoint:

    • Endpoint: GET /api/v1/analytics/summary
    • Request Body: None
    • Response Body: (Status Code: 200)
      {
       "status": true,
       "status_code": 200,
       "total_users": 1000,
       "active_users": 800,
       "new_users": 50,
       "total_revenue": 10000
      }
    • Error Status Code: 404
    • Error Response:
      {
       "status": false,
       "status_code": 404,
       "error": "ResourceNotFound",
       "message": "The summary data could not be found.",
       "details": {
           "resource": "Summary"
       }
      }
  2. Line Chart Data:

    • Endpoint: GET /api/v1/analytics/line-chart-data
    • Request Body: None
    • Response Body: (Status Code: 200)
      {
       "status": true,
       "status_code": 200,
       "labels": ["Jan", "Feb", "Mar", "Apr", "May"],
       "data": [10, 20, 30, 25, 15]
      }
    • Error Status Code: 404
    • Error Response:
      {
       "status": false,
       "status_code": 404,
       "error": "DataFetchError",
       "message": "There was an error fetching the line chart data.",
       "details": {
           "chart_type": "line"
       }
      }
  3. Bar Chart Data:

    • Endpoint: GET /api/v1/analytics/bar-chart-data
    • Request Body: None
    • Response Body: (Status Code: 200)
      {
       "status": true,
       "status_code": 200,
       "categories": ["Category 1", "Category 2", "Category 3"],
       "data": [50, 70, 100]
      }
    • Error Status Code: 404
    • Error Response:
      {
       "status": false,
       "status_code": 404,
       "error": "DataFetchError",
       "message": "There was an error fetching the bar chart data.",
       "details": {
           "chart_type": "bar"
       }
      }
  4. Pie Chart Data:

    • Endpoint: GET /api/v1/analytics/pie-chart-data
    • Request Body: None
    • Response Body: (Status Code: 200)
      {
       "status": true,
       "status_code": 200,
       "segments": ["Segment 1", "Segment 2", "Segment 3"],
       "values": [30, 45, 25]
      }
    • Error Status Code: 404
    • Error Response:
      {
       "status": false,
       "status_code": 404,
       "error": "DataFetchError",
       "message": "There was an error fetching the pie chart data.",
       "details": {
           "chart_type": "pie"
       }
      }
  5. Real-Time Data Updates:

    • Endpoint: WS /api/v1/analytics/realtime
    • Request Body: None (WebSocket connection)
    • Response Body: (Status Code: 200)
      {
       "status": true,
       "status_code": 200,
       "type": "update",
       "data": {
           "metric": "active_users",
           "value": 820
       }
      }
    • Error Status Code: 500
    • Error Response:
      {
       "status": false,
       "status_code": 500,
       "error": "WebSocketConnectionError",
       "message": "Failed to establish a WebSocket connection for real-time updates.",
       "details": {
           "connection": "WebSocket"
       }
      }

Example Breakdown

Based on the above, you could have the following endpoints:

  1. Summary Endpoint:

    • GET /api/v1/analytics/summary
  2. Chart-Specific Endpoints:

    • GET /api/v1/analytics/line-chart-data
    • GET /api/v1/analytics/bar-chart-data
    • GET /api/v1/analytics/pie-chart-data
    • GET /api/v1/analytics/area-chart-data (if needed)
    • GET /api/v1/analytics/scatter-plot-data (if needed)
  3. Real-Time Data Endpoint:

    • WS /api/v1/analytics/realtime

Minimum Required Endpoints

To get started with basic chart types (line, bar, pie), you would need at least:

  1. GET /api/v1/analytics/summary
  2. GET /api/v1/analytics/line-chart-data
  3. GET /api/v1/analytics/bar-chart-data
  4. GET /api/v1/analytics/pie-chart-data

So, a minimum of 4 endpoints if you are including the summary and three types of charts. If you add real-time capabilities or more chart types, the number of endpoints will increase accordingly.

ChielokaCode commented 1 month ago

Interested to work on this

fazzy12 commented 1 month ago

Interested to work on this

cool

buka4rill commented 1 month ago

What's the shape of the error body when the api calls fail?

fazzy12 commented 1 month ago

What's the shape of the error body when the api calls fail?

its been updated....

nedssoft commented 1 month ago

Use the approved naming conventions for object keys, use the approved conventions for api versioning

fazzy12 commented 1 month ago

Use the approved naming conventions for object keys, use the approved conventions for api versioning

Okay sire

fazzy12 commented 1 month ago

@nedssoft coreected sir

phurhard commented 1 month ago

Status_code for errors? And "status": False or True

In essence, your error output not yet conforming to guideline, quickly fix that. All rest is good

fazzy12 commented 1 month ago

@nedssoft @buka4rill @phurhard @markessien made necessary changes.

Shullyd7 commented 1 month ago

You're still not adding status_code

fazzy12 commented 1 month ago

You're still not adding status_code

sorry was missing that..

fazzy12 commented 1 month ago

@nedssoft @buka4rill @phurhard @markessien @Shullyd7 fixed status code and True & False.

Shullyd7 commented 1 month ago

statusCode is not in snake case

fazzy12 commented 1 month ago

done sir @Shullyd7 @nedssoft @buka4rill @phurhard @markessien

Shullyd7 commented 1 month ago

Why is the status code 500 for a Data Fetch Error? @fazzy12 apologies @Shullyd7, it was a mixup.

fazzy12 commented 1 month ago

Why is the status code 500 for a Data Fetch Error? @fazzy12

apologies @Shullyd7, it was a mixup.

zee-ham-su commented 1 month ago

@fazzy12 hello

zee-ham-su commented 1 month ago

Labels: backend, API, analytics, charts, security, documentation

Description

Implement backend support for analytics charts to visualize data insights for users on the dashboard. This involves creating API endpoints, processing data, ensuring security, and providing documentation.

Acceptance Criteria

  1. API endpoints to fetch chart data are implemented and tested.
  2. Data is processed and aggregated to provide meaningful insights.
  3. Endpoints are secured and accessible only to authorized users.
  4. Real-time data updates (if applicable) are implemented using WebSocket or similar technologies.
  5. Clear documentation is provided for the API endpoints.

Purpose

To enable the frontend to integrate and display analytics charts by providing the necessary data and ensuring seamless data flow and security.

Requirements

  1. Data API Endpoints:

    • Create and document API endpoints for fetching data required by various types of charts (e.g., line charts, bar charts, pie charts).
  2. Data Aggregation and Processing:

    • Aggregate and process raw data to provide the necessary insights and trends for the charts.
  3. Authentication and Authorization:

    • Implement authentication and authorization checks to secure the data endpoints.
  4. Real-Time Data Updates (if applicable):

    • Implement WebSocket or similar technologies to push real-time data updates to the frontend.
  5. Documentation:

    • Provide detailed documentation on how to access and use the API endpoints, including request and response structures.

Expected Outcome

  1. Users can view and interact with analytics charts on the dashboard, gaining valuable insights from visualized data trends.
  2. API endpoints are performant and secure, handling large datasets efficiently.
  3. Clear and comprehensive documentation is available, enabling easy integration with the frontend.

Steps to Complete

  1. Design API Schema:

    • Define the structure of the data that the backend will provide to the frontend.
  2. Implement Endpoints:

    • Develop the API endpoints in the backend to fetch and process the data required for the charts.
  3. Test Endpoints:

    • Test the API endpoints to ensure they return the correct data and handle errors gracefully.
  4. Secure Endpoints:

    • Implement authentication and authorization to secure the endpoints.
  5. Real-Time Data Updates:

    • If applicable, implement WebSocket or similar technologies for real-time data updates.
  6. Documentation:

    • Write clear documentation on how to use the API endpoints, including examples of requests and responses.

Endpoints and Request/Response Formats

  1. Summary Endpoint:

    • Endpoint: GET /api/v1/analytics/summary

    • Request Body: None

    • Response Body: (Status Code: 200)

      {
      "status": true,
      "status_code": 200,
      "total_users": 1000,
      "active_users": 800,
      "new_users": 50,
      "total_revenue": 10000
      }
    • Error Status Code: 404

    • Error Response:

      {
      "status": false,
      "status_code": 404,
      "error": "ResourceNotFound",
      "message": "The summary data could not be found.",
      "details": {
       "resource": "Summary"
      }
      }
  2. Line Chart Data:

    • Endpoint: GET /api/v1/analytics/line-chart-data

    • Request Body: None

    • Response Body: (Status Code: 200)

      {
      "status": true,
      "status_code": 200,
      "labels": ["Jan", "Feb", "Mar", "Apr", "May"],
      "data": [10, 20, 30, 25, 15]
      }
    • Error Status Code: 404

    • Error Response:

      {
      "status": false,
      "status_code": 404,
      "error": "DataFetchError",
      "message": "There was an error fetching the line chart data.",
      "details": {
       "chart_type": "line"
      }
      }
  3. Bar Chart Data:

    • Endpoint: GET /api/v1/analytics/bar-chart-data

    • Request Body: None

    • Response Body: (Status Code: 200)

      {
      "status": true,
      "status_code": 200,
      "categories": ["Category 1", "Category 2", "Category 3"],
      "data": [50, 70, 100]
      }
    • Error Status Code: 404

    • Error Response:

      {
      "status": false,
      "status_code": 404,
      "error": "DataFetchError",
      "message": "There was an error fetching the bar chart data.",
      "details": {
       "chart_type": "bar"
      }
      }
  4. Pie Chart Data:

    • Endpoint: GET /api/v1/analytics/pie-chart-data

    • Request Body: None

    • Response Body: (Status Code: 200)

      {
      "status": true,
      "status_code": 200,
      "segments": ["Segment 1", "Segment 2", "Segment 3"],
      "values": [30, 45, 25]
      }
    • Error Status Code: 404

    • Error Response:

      {
      "status": false,
      "status_code": 404,
      "error": "DataFetchError",
      "message": "There was an error fetching the pie chart data.",
      "details": {
       "chart_type": "pie"
      }
      }
  5. Real-Time Data Updates:

    • Endpoint: WS /api/v1/analytics/realtime

    • Request Body: None (WebSocket connection)

    • Response Body: (Status Code: 200)

      {
      "status": true,
      "status_code": 200,
      "type": "update",
      "data": {
       "metric": "active_users",
       "value": 820
      }
      }
    • Error Status Code: 500

    • Error Response:

      {
      "status": false,
      "status_code": 500,
      "error": "WebSocketConnectionError",
      "message": "Failed to establish a WebSocket connection for real-time updates.",
      "details": {
       "connection": "WebSocket"
      }
      }

Example Breakdown

Based on the above, you could have the following endpoints:

  1. Summary Endpoint:

    • GET /api/v1/analytics/summary
  2. Chart-Specific Endpoints:

    • GET /api/v1/analytics/line-chart-data
    • GET /api/v1/analytics/bar-chart-data
    • GET /api/v1/analytics/pie-chart-data
    • GET /api/v1/analytics/area-chart-data (if needed)
    • GET /api/v1/analytics/scatter-plot-data (if needed)
  3. Real-Time Data Endpoint:

    • WS /api/v1/analytics/realtime

Minimum Required Endpoints

To get started with basic chart types (line, bar, pie), you would need at least:

  1. GET /api/v1/analytics/summary
  2. GET /api/v1/analytics/line-chart-data
  3. GET /api/v1/analytics/bar-chart-data
  4. GET /api/v1/analytics/pie-chart-data

So, a minimum of 4 endpoints if you are including the summary and three types of charts. If you add real-time capabilities or more chart types, the number of endpoints will increase accordingly.