Skip to content

Commit

Permalink
Add event logs service
Browse files Browse the repository at this point in the history
Signed-off-by: Rodney Osodo <[email protected]>
  • Loading branch information
rodneyosodo committed Jan 12, 2024
1 parent 615f097 commit 4282f24
Show file tree
Hide file tree
Showing 41 changed files with 2,757 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/check-generated-files.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ jobs:
- "auth/policies.go"
- "pkg/events/events.go"
- "provision/service.go"
- "eventlogs/events.go"
- name: Set up protoc
if: steps.changes.outputs.proto == 'true'
Expand Down Expand Up @@ -111,6 +112,8 @@ jobs:
mv ./pkg/events/mocks/publisher.go ./pkg/events/mocks/publisher.go.tmp
mv ./pkg/events/mocks/subscriber.go ./pkg/events/mocks/subscriber.go.tmp
mv ./provision/mocks/service.go ./provision/mocks/service.go.tmp
mv ./eventlogs/mocks/repository.go ./eventlogs/mocks/repository.go.tmp
mv ./eventlogs/mocks/service.go ./eventlogs/mocks/service.go.tmp
make mocks
Expand Down Expand Up @@ -140,3 +143,5 @@ jobs:
check_mock_changes ./pkg/events/mocks/publisher.go "ES Publisher ./pkg/events/mocks/publisher.go"
check_mock_changes ./pkg/events/mocks/subscriber.go "EE Subscriber ./pkg/events/mocks/subscriber.go"
check_mock_changes ./provision/mocks/service.go "Provision Service ./provision/mocks/service.go"
check_mock_changes ./eventlogs/mocks/repository.go "Event Logs Repository ./eventlogs/mocks/repository.go"
check_mock_changes ./eventlogs/mocks/service.go "Event Logs Service ./eventlogs/mocks/service.go"
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ MG_DOCKER_IMAGE_NAME_PREFIX ?= magistrala
BUILD_DIR = build
SERVICES = auth users things http coap ws lora influxdb-writer influxdb-reader mongodb-writer \
mongodb-reader cassandra-writer cassandra-reader postgres-writer postgres-reader timescale-writer timescale-reader cli \
bootstrap opcua twins mqtt provision certs smtp-notifier smpp-notifier invitations
bootstrap opcua twins mqtt provision certs smtp-notifier smpp-notifier invitations event-logs
DOCKERS = $(addprefix docker_,$(SERVICES))
DOCKERS_DEV = $(addprefix docker_dev_,$(SERVICES))
CGO_ENABLED ?= 0
Expand Down Expand Up @@ -78,7 +78,7 @@ endef
ADDON_SERVICES = bootstrap cassandra-reader cassandra-writer certs \
influxdb-reader influxdb-writer lora-adapter mongodb-reader mongodb-writer \
opcua-adapter postgres-reader postgres-writer provision smpp-notifier smtp-notifier \
timescale-reader timescale-writer twins
timescale-reader timescale-writer twins event-logs

EXTERNAL_SERVICES = vault prometheus

Expand Down
278 changes: 278 additions & 0 deletions api/openapi/event-logs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,278 @@
# Copyright (c) Abstract Machines
# SPDX-License-Identifier: Apache-2.0

openapi: 3.0.3
info:
title: Magistrala Event Logs Service
description: |
This is the Event Logs Server based on the OpenAPI 3.0 specification. It is the HTTP API for viewing event logs history. You can now help us improve the API whether it's by making changes to the definition itself or to the code.
Some useful links:
- [The Magistrala repository](https://github.com/absmach/magistrala)
contact:
email: [email protected]
license:
name: Apache 2.0
url: https://github.com/absmach/magistrala/blob/master/LICENSE
version: 0.14.0

servers:
- url: http://localhost:9021
- url: https://localhost:9021

tags:
- name: event-logs
description: Everything about your Event Logs
externalDocs:
description: Find out more about Event Logs
url: http://docs.mainflux.io/

paths:
/events/{id}{type}:
get:
tags:
- event-logs
summary: List event logs
description: |
Retrieves a list of events. Due to performance concerns, data
is retrieved in subsets. The API must ensure that the entire
dataset is consumed either by making subsequent requests, or by
increasing the subset size of the initial request.
parameters:
- $ref: "#/components/parameters/id"
- $ref: "#/components/parameters/type"
- $ref: "#/components/parameters/limit"
- $ref: "#/components/parameters/offset"
- $ref: "#/components/parameters/operation"
- $ref: "#/components/parameters/from"
- $ref: "#/components/parameters/to"
security:
- bearerAuth: []
responses:
"200":
$ref: "#/components/responses/EventPageRes"
"400":
description: Failed due to malformed query parameters.
"401":
description: |
Missing or invalid access token provided.
This endpoint is available only for administrators.
"404":
description: A non-existent entity request.
"422":
description: Database can't process request.
"500":
$ref: "#/components/responses/ServiceError"

/health:
get:
summary: Retrieves service health check info.
tags:
- health
responses:
"200":
$ref: "#/components/responses/HealthRes"
"500":
$ref: "#/components/responses/ServiceError"

components:
schemas:
Event:
type: object
properties:
id:
type: string
format: uuid
example: bb7edb32-2eac-4aad-aebe-ed96fe073879
description: Entity unique identifier.
operation:
type: string
example: users.create
description: Event operation.
occurred_at:
type: string
format: date-time
example: "2024-01-11T12:05:07.449053Z"
description: Time when the event occurred.
payload:
type: object
description: Event payload.
example:
{
"created_at": "2024-01-11T12:05:07.435357Z",
"metadata": "e30=",
"name": "Moors-Mlacak",
"owner": "599a7f51-3f16-41b2-8224-3b1044cea83b",
"status": "enabled",
}
xml:
name: event

EventPage:
type: object
properties:
events:
type: array
minItems: 0
uniqueItems: true
items:
$ref: "#/components/schemas/Event"
total:
type: integer
example: 1
description: Total number of items.
offset:
type: integer
description: Number of items to skip during retrieval.
limit:
type: integer
example: 10
description: Maximum number of items to return in one page.
required:
- events
- total
- offset

Error:
type: object
properties:
error:
type: string
description: Error message
example: { "error": "malformed entity specification" }

HealthRes:
type: object
properties:
status:
type: string
description: Service status.
enum:
- pass
version:
type: string
description: Service version.
example: 0.14.0
commit:
type: string
description: Service commit hash.
example: 7d6f4dc4f7f0c1fa3dc24eddfb18bb5073ff4f62
description:
type: string
description: Service description.
example: <service_name> service
build_time:
type: string
description: Service build time.
example: 1970-01-01_00:00:00

parameters:
id:
name: id
description: Unique identifier for an entity, e.g. user, group, domain, etc.
in: path
schema:
type: string
format: uuid
required: true
example: bb7edb32-2eac-4aad-aebe-ed96fe073879

type:
name: type
description: Type of entity, e.g. user, group, domain, etc.
in: path
schema:
type: string
enum:
- user
- group
- domain
- thing
- platform
- thing
required: true
example: user

offset:
name: offset
description: Number of items to skip during retrieval.
in: query
schema:
type: integer
default: 0
minimum: 0
required: false
example: "0"

limit:
name: limit
description: Size of the subset to retrieve.
in: query
schema:
type: integer
default: 10
maximum: 10
minimum: 1
required: false
example: "10"

operation:
name: operation
description: Event operation.
in: query
schema:
type: string
required: false
example: users.create

from:
name: from
description: Start date in nanoseconds.
in: query
schema:
type: string
format: int64
required: false
example: 1704974066014408296

to:
name: to
description: End date in nanoseconds.
in: query
schema:
type: string
format: int64
required: false
example: 1704974066014408296

responses:
EventPageRes:
description: Data retrieved.
content:
application/json:
schema:
$ref: "#/components/schemas/EventPage"

HealthRes:
description: Service Health Check.
content:
application/health+json:
schema:
$ref: "#/components/schemas/HealthRes"

ServiceError:
description: Unexpected server-side error occurred.
content:
application/json:
schema:
$ref: "#/components/schemas/Error"

securitySchemes:
bearerAuth:
type: http
scheme: bearer
bearerFormat: JWT
description: |
* User access: "Authorization: Bearer <user_access_token>"
security:
- bearerAuth: []
50 changes: 50 additions & 0 deletions cli/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0

package cli

import (
mgxsdk "github.com/absmach/magistrala/pkg/sdk/go"
"github.com/spf13/cobra"
)

var cmdEvents = cobra.Command{
Use: "get <id> <entity_type> <user_auth_token>",
Short: "Get events",
Long: "Get events\n" +
"Usage:\n" +
"\tmagistrala-cli events get <id> <entity_type> <user_auth_token> - lists all events\n" +
"\tmagistrala-cli events get <id> <entity_type> <user_auth_token> --offset <offset> --limit <limit> - lists all events with provided offset and limit\n",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 3 {
logUsage(cmd.Use)
return
}

pageMetadata := mgxsdk.PageMetadata{
Offset: Offset,
Limit: Limit,
}

events, err := sdk.Events(pageMetadata, args[0], args[1], args[2])
if err != nil {
logError(err)
return
}

logJSON(events)
},
}

// NewEventsCmd returns invitations command.
func NewEventsCmd() *cobra.Command {
cmd := cobra.Command{
Use: "events get",
Short: "events logs",
Long: `events to read event history`,
}

cmd.AddCommand(&cmdEvents)

return &cmd
}
Loading

0 comments on commit 4282f24

Please sign in to comment.