Skip to content

Commit

Permalink
Merge branch 'main' into feature/kino-api
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderStorm authored Sep 22, 2023
2 parents 5d51e57 + 888c457 commit 8f8dd3b
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 268 deletions.
20 changes: 4 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ docker compose up -d
```
The docker compose will start the server and a mariadb instance.
The server will be available at `localhost:50051` and the mariadb instance at `localhost:3306`.
Additionally, docker creates the volume `campus-db-data` and `campus-influxdb-data`
to persist the data of the mariadb and influxdb instances.
Additionally, docker creates the volume `campus-db-data` to persist the data of the mariadb instances.

### Setting up the Database
The mariadb schema can be installed by executing the following command inside the mariadb container:
Expand All @@ -93,26 +92,15 @@ The following environment variables need to be set for the server to work proper
* [REQUIRED] `DB_ROOT_PASSWORD`: The password of the root user.
* [OPTIONAL] `DB_PORT`: The port of the database server. Defaults to `3306`.
* [OPTIONAL] `SENTRY_DSN`: The Sentry [Data Source Name](https://sentry-docs-git-patch-1.sentry.dev/product/sentry-basics/dsn-explainer/) for reporting issues and crashes.
* **[InfluxDB [OPTIONAL]](#influxdb)**:
* [OPTIONAL] `INFLUXDB_USER`: The InfluxDB username to set for the systems initial superuser.
* [OPTIONAL] `INFLUXDB_PASSWORD`: The InfluxDB password to set for the systems initial superuser.
* [OPTIONAL] `INFLUXDB_ORG`: The InfluxDB organization to set for the systems initial organization.
* [OPTIONAL] `INFLUXDB_BUCKET`: The InfluxDB bucket to set for the systems initial bucket.
* [REQUIRED] `INFLUXDB_URL`: The InfluxDB URL to use for writing metrics.
* [REQUIRED] `INFLUXDB_ADMIN_TOKEN`: The InfluxDB admin token to use for authenticating with the InfluxDB server. If set initially the system will associate the token with the initial superuser.
* **[iOS Push Notification Service [OPTIONAL]](#ios-push-notifications-service)**:
* [REQUIRED] `APNS_KEY_ID`: The key ID of the APNs key => APNs Key needs to be downloaded from the Apple Developer Portal the name of the file also contains the key ID.
* [REQUIRED] `APNS_TEAM_ID`: The team ID of the iOS app can be found in AppStoreConnect.
* [REQUIRED] `APNS_P8_FILE_PATH`: The path to the APNs key file (e.g. `/secrets/AuthKey_XXXX.p8`) in the docker container. The file itself needs to exist in the same directory as the `docker-compose.yml` file and called `apns_auth_key.p8`.
* [REQUIRED] `CAMPUS_API_TOKEN`: A token used to authenticate with TUMonline (used for example for the grades)

## InfluxDB
InfluxDB can be used to store metrics.

If an InfluxDB instance is already set up, just the `INFLUXDB_URL` and the `INFLUXDB_ADMIN_TOKEN` environment variable needs to be set
to enable the metrics endpoint.
All the other environment variables are optional and only needed if the InfluxDB instance needs to be set up.
If `INFLUXDB_URL` or `INFLUXDB_ADMIN_TOKEN` are not set, the metrics endpoint will be disabled.
## Metrics
Our service uses prometheus to collect metrics to display in grafana.
To see the metrics we aggregate, head over to `http://localhost:50051/metrics`

## iOS Push Notifications Service
The iOS Push Notifications Service can be used to send push notifications to iOS devices.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ spec:
resources:
requests:
cpu: 1000m
memory: 50Mi
memory: 100Mi
limits:
memory: 200Mi
memory: 500Mi
livenessProbe:
httpGet:
path: /health
Expand Down
67 changes: 0 additions & 67 deletions server/backend/influx/influxClient.go

This file was deleted.

60 changes: 0 additions & 60 deletions server/backend/influx/iosLogging.go

This file was deleted.

43 changes: 0 additions & 43 deletions server/backend/influxdb.go

This file was deleted.

9 changes: 1 addition & 8 deletions server/backend/ios_notifications/ios_apns/iosAPNsService.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package ios_apns
import (
"errors"

"github.com/TUM-Dev/Campus-Backend/server/backend/influx"
"github.com/TUM-Dev/Campus-Backend/server/backend/ios_notifications/ios_apns/ios_apns_jwt"
"github.com/TUM-Dev/Campus-Backend/server/model"
log "github.com/sirupsen/logrus"
Expand All @@ -27,23 +26,17 @@ type Service struct {
// and the request ID.
func (s *Service) RequestGradeUpdateForDevice(deviceID string) error {
campusRequestToken, err := s.Repository.CreateCampusTokenRequest(deviceID)

if err != nil {
log.WithError(err).Error("Could not create campus token request")
return ErrCouldNotCreateTokenRequest
}

notification := model.NewIOSNotificationPayload(deviceID).Background(campusRequestToken.RequestID, model.IOSBackgroundCampusTokenRequest)

res, err := s.Repository.SendBackgroundNotification(notification)

if err != nil {
if _, err := s.Repository.SendBackgroundNotification(notification); err != nil {
log.WithError(err).Error("Could not send background notification")
return ErrCouldNotSendNotification
}

influx.LogIOSBackgroundRequest(deviceID, campusRequestToken.RequestType, res.Reason)

return nil
}

Expand Down
24 changes: 13 additions & 11 deletions server/backend/ios_notifications/ios_device/iosDeviceService.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package ios_device

import (
pb "github.com/TUM-Dev/Campus-Backend/server/api/tumdev"
"github.com/TUM-Dev/Campus-Backend/server/backend/influx"
"github.com/TUM-Dev/Campus-Backend/server/model"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)

type Service struct {
Expand All @@ -16,6 +18,12 @@ type Service struct {
var (
ErrCouldNotRegisterDevice = status.Error(codes.Internal, "Could not register device")
ErrCouldNotRemoveDevice = status.Error(codes.Internal, "Could not remove device")

iosRegisteredDevices = promauto.NewGauge(prometheus.GaugeOpts{
Subsystem: "ios",
Name: "ios_registered_devices",
Help: "The number of currently registered ios devices",
})
)

func (service *Service) RegisterDevice(request *pb.RegisterDeviceRequest) (*pb.RegisterDeviceReply, error) {
Expand All @@ -24,28 +32,22 @@ func (service *Service) RegisterDevice(request *pb.RegisterDeviceRequest) (*pb.R
PublicKey: request.GetPublicKey(),
}

err := service.Repository.RegisterDevice(&device)

if err != nil {
if err := service.Repository.RegisterDevice(&device); err != nil {
return nil, ErrCouldNotRegisterDevice
}

influx.LogIOSRegisterDevice(request.GetDeviceId())
iosRegisteredDevices.Inc()

return &pb.RegisterDeviceReply{
DeviceId: device.DeviceID,
}, nil
}

func (service *Service) RemoveDevice(request *pb.RemoveDeviceRequest) (*pb.RemoveDeviceReply, error) {
err := service.Repository.RemoveDevice(request.GetDeviceId())

if err != nil {
if err := service.Repository.RemoveDevice(request.GetDeviceId()); err != nil {
return nil, ErrCouldNotRemoveDevice
}

influx.LogIOSRemoveDevice(request.GetDeviceId())

iosRegisteredDevices.Dec()
return &pb.RemoveDeviceReply{
DeviceId: request.GetDeviceId(),
}, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ package ios_request_response
import (
"fmt"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"

pb "github.com/TUM-Dev/Campus-Backend/server/api/tumdev"
"github.com/TUM-Dev/Campus-Backend/server/backend/campus_api"
"github.com/TUM-Dev/Campus-Backend/server/backend/influx"
"github.com/TUM-Dev/Campus-Backend/server/backend/ios_notifications/ios_apns"
"github.com/TUM-Dev/Campus-Backend/server/backend/ios_notifications/ios_device"
"github.com/TUM-Dev/Campus-Backend/server/model"
Expand All @@ -28,6 +30,12 @@ var (
ErrInternalHandleGrades = status.Error(codes.Internal, "Could not handle grades request")
ErrCouldNotGetDevice = status.Error(codes.Internal, "Could not get device")
ErrAPNSNotActive = status.Error(codes.Internal, "APNS is not active")

collectedNewGrades = promauto.NewHistogram(prometheus.HistogramOpts{
Name: "ios_new_grades",
Help: "The total number of processed events",
Buckets: prometheus.LinearBuckets(0, 5, 5),
})
)

func (service *Service) HandleDeviceRequestResponse(request *pb.IOSDeviceRequestResponseRequest, apnsIsActive bool) (*pb.IOSDeviceRequestResponseReply, error) {
Expand All @@ -43,8 +51,6 @@ func (service *Service) HandleDeviceRequestResponse(request *pb.IOSDeviceRequest
return nil, ErrOutdatedRequest
}

influx.LogIOSBackgroundRequestResponse(requestLog.DeviceID, requestLog.RequestType)

switch requestLog.RequestType {
case model.IOSBackgroundCampusTokenRequest.String():
campusToken := request.GetPayload()
Expand Down Expand Up @@ -94,6 +100,7 @@ func (service *Service) handleDeviceCampusTokenRequest(requestLog *model.IOSDevi
}

newGrades := compareAndFindNewGrades(apiGrades.Grades, oldGrades)
collectedNewGrades.Observe(float64(len(newGrades)))
if len(newGrades) == 0 {
log.Info("No new grades found")
service.deleteRequestLog(requestLog)
Expand All @@ -116,7 +123,6 @@ func (service *Service) handleDeviceCampusTokenRequest(requestLog *model.IOSDevi
if len(newGrades) > 0 && len(oldGrades) > 0 {
apnsRepository := ios_apns.NewRepository(service.Repository.DB, service.Repository.Token)
sendGradesToDevice(device, newGrades, apnsRepository)
influx.LogIOSNewGrades(requestLog.DeviceID, len(newGrades))
}

service.deleteRequestLog(requestLog)
Expand Down
Loading

0 comments on commit 8f8dd3b

Please sign in to comment.