Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/si 2427 apiv2 consolidate analytics endpoint for dinc service in api #88

22 changes: 13 additions & 9 deletions cmd/device-data-api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ import (
"strings"
"time"

"github.com/DIMO-Network/device-data-api/internal/config"
"github.com/DIMO-Network/device-data-api/internal/controllers"
"github.com/DIMO-Network/device-data-api/internal/middleware/metrics"
"github.com/DIMO-Network/device-data-api/internal/middleware/owner"
"github.com/DIMO-Network/device-data-api/internal/services"
"github.com/DIMO-Network/device-data-api/internal/services/elastic"
"github.com/DIMO-Network/shared/db"
"github.com/DIMO-Network/shared/middleware/privilegetoken"
"github.com/DIMO-Network/shared/privileges"
Expand All @@ -25,6 +19,13 @@ import (
"github.com/gofiber/fiber/v2/middleware/skip"
"github.com/gofiber/swagger"
"github.com/rs/zerolog"

"github.com/DIMO-Network/device-data-api/internal/config"
"github.com/DIMO-Network/device-data-api/internal/controllers"
"github.com/DIMO-Network/device-data-api/internal/middleware/metrics"
"github.com/DIMO-Network/device-data-api/internal/middleware/owner"
"github.com/DIMO-Network/device-data-api/internal/services"
"github.com/DIMO-Network/device-data-api/internal/services/elastic"
)

func startWebAPI(logger zerolog.Logger, settings *config.Settings, dbs func() *db.ReaderWriter,
Expand Down Expand Up @@ -76,6 +77,7 @@ func startWebAPI(logger zerolog.Logger, settings *config.Settings, dbs func() *d
}
deviceStatusSvc := services.NewDeviceStatusService(definitionsAPIService)
deviceDataController := controllers.NewDeviceDataController(settings, &logger, deviceAPIService, esService, definitionsAPIService, deviceStatusSvc, dbs)
deviceDataControllerV2 := controllers.NewDeviceDataControllerV2(settings, &logger, deviceAPIService, esService, deviceStatusSvc, dbs)

logger.Info().Str("jwkUrl", settings.TokenExchangeJWTKeySetURL).Str("vehicleAddr", settings.VehicleNFTAddress).Msg("Privileges enabled.")
privilegeAuth := jwtware.New(jwtware.Config{
Expand All @@ -101,9 +103,11 @@ func startWebAPI(logger zerolog.Logger, settings *config.Settings, dbs func() *d
vTokenV1.Get("/status", tk.OneOf(vehicleAddr, []privileges.Privilege{privileges.VehicleNonLocationData, privileges.VehicleCurrentLocation, privileges.VehicleAllTimeLocation}), cacheHandler, deviceDataController.GetVehicleStatus)
vTokenV1.Get("/status-raw", tk.OneOf(vehicleAddr, []privileges.Privilege{privileges.VehicleNonLocationData, privileges.VehicleCurrentLocation, privileges.VehicleAllTimeLocation}), cacheHandler, deviceDataController.GetVehicleStatusRaw)

vTokenV2.Get("/status", tk.OneOf(vehicleAddr, []privileges.Privilege{privileges.VehicleNonLocationData, privileges.VehicleCurrentLocation, privileges.VehicleAllTimeLocation}), cacheHandler, deviceDataController.GetVehicleStatusV2)
vTokenV2.Get("/history", tk.OneOf(vehicleAddr, []privileges.Privilege{privileges.VehicleNonLocationData, privileges.VehicleAllTimeLocation}), cacheHandler, deviceDataController.GetHistoricalPermissionedV2)

vTokenV2.Get("/status", tk.OneOf(vehicleAddr, []privileges.Privilege{privileges.VehicleNonLocationData, privileges.VehicleCurrentLocation, privileges.VehicleAllTimeLocation}), cacheHandler, deviceDataControllerV2.GetVehicleStatus)
vTokenV2.Get("/history", tk.OneOf(vehicleAddr, []privileges.Privilege{privileges.VehicleNonLocationData, privileges.VehicleAllTimeLocation}), cacheHandler, deviceDataControllerV2.GetHistoricalPermissioned)
vTokenV2.Get("/analytics/daily-distance", tk.OneOf(vehicleAddr, []privileges.Privilege{privileges.VehicleNonLocationData}), cacheHandler, deviceDataControllerV2.GetDailyDistance)
vTokenV2.Get("/analytics/total-distance", tk.OneOf(vehicleAddr, []privileges.Privilege{privileges.VehicleNonLocationData}), cacheHandler, deviceDataControllerV2.GetDistanceDriven)

udMw := owner.New(usersClient, deviceAPIService, &logger)
v1Auth := app.Group("/v1", jwtAuth)

Expand Down
23 changes: 12 additions & 11 deletions cmd/device-data-api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ package main
import (
"context"
"flag"
"net"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"time"

"github.com/DIMO-Network/device-data-api/internal/services/fingerprint"

Expand All @@ -15,13 +20,6 @@ import (

"github.com/DIMO-Network/device-data-api/internal/rpc"

"net"
"net/http"
"os"
"strings"
"time"

"github.com/DIMO-Network/device-data-api/internal/middleware/metrics"
"github.com/DIMO-Network/shared/db"
"github.com/IBM/sarama"
"github.com/burdiyan/kafkautil"
Expand All @@ -31,15 +29,18 @@ import (
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
"github.com/lovoo/goka"

_ "github.com/DIMO-Network/device-data-api/docs"
"github.com/DIMO-Network/device-data-api/internal/config"
"github.com/DIMO-Network/device-data-api/internal/services"
dddatagrpc "github.com/DIMO-Network/device-data-api/pkg/grpc"
"github.com/DIMO-Network/device-data-api/internal/middleware/metrics"

"github.com/DIMO-Network/shared"
"github.com/gofiber/fiber/v2"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/rs/zerolog"
"google.golang.org/grpc"

_ "github.com/DIMO-Network/device-data-api/docs"
"github.com/DIMO-Network/device-data-api/internal/config"
"github.com/DIMO-Network/device-data-api/internal/services"
dddatagrpc "github.com/DIMO-Network/device-data-api/pkg/grpc"
)

// @title DIMO Device Data API
Expand Down
84 changes: 80 additions & 4 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ const docTemplate = `{
"BearerAuth": []
}
],
"description": "Get kilometers driven for a userDeviceID each day.",
"description": "[🔴__Warning - API Shutdown by June 30, 2024, Use ` + "`" + `/v2/vehicles/:tokenId/analytics/daily-distance` + "`" + ` instead__🔴] Get kilometers driven for a userDeviceID each day.",
"produces": [
"application/json"
],
"tags": [
"device-data"
"device-data [End Of Life Warning]"
],
"parameters": [
{
Expand Down Expand Up @@ -104,12 +104,12 @@ const docTemplate = `{
"BearerAuth": []
}
],
"description": "Get kilometers driven for a userDeviceID since connected (ie. since we have data available)\nif it returns 0 for distanceDriven it means we have no odometer data.",
"description": "Get kilometers driven for a userDeviceID since connected (ie. since we have data available)\n[🔴__Warning - API Shutdown by June 30, 2024, Use ` + "`" + `/v2/vehicles/:tokenId/analytics/total-distance` + "`" + ` instead__🔴] if it returns 0 for distanceDriven it means we have no odometer data.",
"produces": [
"application/json"
],
"tags": [
"device-data"
"device-data [End Of Life Warning]"
],
"parameters": [
{
Expand Down Expand Up @@ -433,6 +433,82 @@ const docTemplate = `{
}
}
}
},
"/v2/vehicles/{tokenID}/analytics/daily-distance": {
"get": {
"security": [
{
"BearerAuth": []
}
],
"description": "Get kilometers driven for a userDeviceID each day.",
"produces": [
"application/json"
],
"tags": [
"device-data"
],
"parameters": [
{
"type": "string",
"description": "user device id",
"name": "userDeviceID",
"in": "path",
"required": true
},
{
"type": "string",
"description": "IANAS time zone id, e.g., America/Los_Angeles",
"name": "time_zone",
"in": "query",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/internal_controllers.DailyDistanceResp"
}
},
"404": {
"description": "no device found for user with provided parameters"
}
}
}
},
"/v2/vehicles/{tokenID}/analytics/total-distance": {
"get": {
"security": [
{
"BearerAuth": []
}
],
"description": "Get kilometers driven for a userDeviceID since connected (ie. since we have data available)\nif it returns 0 for distanceDriven it means we have no odometer data.",
"produces": [
"application/json"
],
"tags": [
"device-data"
],
"parameters": [
{
"type": "string",
"description": "user device id",
"name": "userDeviceID",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK"
},
"404": {
"description": "no device found for user with provided parameters"
}
}
}
}
},
"definitions": {
Expand Down
84 changes: 80 additions & 4 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@
"BearerAuth": []
}
],
"description": "Get kilometers driven for a userDeviceID each day.",
"description": "[🔴__Warning - API Shutdown by June 30, 2024, Use `/v2/vehicles/:tokenId/analytics/daily-distance` instead__🔴] Get kilometers driven for a userDeviceID each day.",
"produces": [
"application/json"
],
"tags": [
"device-data"
"device-data [End Of Life Warning]"
],
"parameters": [
{
Expand Down Expand Up @@ -95,12 +95,12 @@
"BearerAuth": []
}
],
"description": "Get kilometers driven for a userDeviceID since connected (ie. since we have data available)\nif it returns 0 for distanceDriven it means we have no odometer data.",
"description": "Get kilometers driven for a userDeviceID since connected (ie. since we have data available)\n[🔴__Warning - API Shutdown by June 30, 2024, Use `/v2/vehicles/:tokenId/analytics/total-distance` instead__🔴] if it returns 0 for distanceDriven it means we have no odometer data.",
"produces": [
"application/json"
],
"tags": [
"device-data"
"device-data [End Of Life Warning]"
],
"parameters": [
{
Expand Down Expand Up @@ -424,6 +424,82 @@
}
}
}
},
"/v2/vehicles/{tokenID}/analytics/daily-distance": {
"get": {
"security": [
{
"BearerAuth": []
}
],
"description": "Get kilometers driven for a userDeviceID each day.",
"produces": [
"application/json"
],
"tags": [
"device-data"
],
"parameters": [
{
"type": "string",
"description": "user device id",
"name": "userDeviceID",
"in": "path",
"required": true
},
{
"type": "string",
"description": "IANAS time zone id, e.g., America/Los_Angeles",
"name": "time_zone",
"in": "query",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/internal_controllers.DailyDistanceResp"
}
},
"404": {
"description": "no device found for user with provided parameters"
}
}
}
},
"/v2/vehicles/{tokenID}/analytics/total-distance": {
"get": {
"security": [
{
"BearerAuth": []
}
],
"description": "Get kilometers driven for a userDeviceID since connected (ie. since we have data available)\nif it returns 0 for distanceDriven it means we have no odometer data.",
"produces": [
"application/json"
],
"tags": [
"device-data"
],
"parameters": [
{
"type": "string",
"description": "user device id",
"name": "userDeviceID",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK"
},
"404": {
"description": "no device found for user with provided parameters"
}
}
}
}
},
"definitions": {
Expand Down
Loading
Loading