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

Report to Odoo16 instead of intermediate postgresql #3

Merged
merged 10 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ blank_issues_enabled: false

contact_links:
- name: ❓ Question
url: https://github.com/appuio/appuio-cloud-reporting/discussions
url: https://github.com/appuio/appuio-reporting/discussions
about: Ask or discuss with us, we're happy to help 🙋
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ dist/
.github/release-notes.md

# Build
appuio-cloud-reporting
appuio-reporting
*.out

# Docs
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ RUN \
ca-certificates \
tzdata

ENTRYPOINT ["appuio-cloud-reporting"]
COPY appuio-cloud-reporting /usr/bin/
ENTRYPOINT ["appuio-reporting"]
COPY appuio-reporting /usr/bin/

USER 65536:0
21 changes: 3 additions & 18 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ build: build-bin build-docker ## All-in-one build
.PHONY: build-bin
build-bin: export CGO_ENABLED = 0
build-bin: fmt vet ## Build binary
@go build -o $(BIN_FILENAME) github.com/appuio/appuio-cloud-reporting
@go build -o $(BIN_FILENAME) github.com/appuio/appuio-reporting

.PHONY: build-docker
build-docker: build-bin ## Build docker image
Expand All @@ -37,23 +37,8 @@ ensure-prometheus: .cache/prometheus ## Ensures that Prometheus is installed in

.PHONY: test
test: export ACR_DB_URL = postgres://user:password@localhost:55432/db?sslmode=disable
test: COMPOSE_FILE = docker-compose-test.yml
test: compose_args = -p reporting-test
test: ensure-prometheus docker-compose-down ping-postgres ## Run full test suite
go run github.com/appuio/appuio-cloud-reporting migrate
go run github.com/appuio/appuio-cloud-reporting migrate --seed
test: ensure-prometheus
go test ./... -tags integration -coverprofile cover.out -covermode atomic
@$(COMPOSE_CMD) $(compose_args) down

.PHONY: gen-golden
gen-golden: export ACR_DB_URL = postgres://user:password@localhost:55432/db?sslmode=disable
gen-golden: COMPOSE_FILE = docker-compose-test.yml
gen-golden: compose_args = -p reporting-test
gen-golden: ensure-prometheus docker-compose-down ping-postgres ## Update golden files
go run github.com/appuio/appuio-cloud-reporting migrate
go run github.com/appuio/appuio-cloud-reporting migrate --seed
go test ./pkg/invoice -update
@$(COMPOSE_CMD) $(compose_args) down

.PHONY: fmt
fmt: ## Run 'go fmt' and `jsonnetfmt` against code
Expand All @@ -74,7 +59,7 @@ generate: ## Generate additional code and artifacts
@go generate ./...

.PHONY: clean
clean: docker-compose-down ## Cleans local build artifacts
clean:
rm -rf docs/node_modules $(docs_out_dir) dist .cache

.cache/prometheus:
Expand Down
15 changes: 0 additions & 15 deletions Makefile.compose.mk

This file was deleted.

7 changes: 1 addition & 6 deletions Makefile.vars.mk
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## These are some common variables for Make

PROJECT_ROOT_DIR = .
PROJECT_NAME ?= appuio-cloud-reporting
PROJECT_NAME ?= appuio-reporting
PROJECT_OWNER ?= appuio

## BUILD:go
Expand All @@ -14,11 +14,6 @@ IMG_TAG ?= latest
# Image URL to use all building/pushing image targets
CONTAINER_IMG ?= local.dev/$(PROJECT_OWNER)/$(PROJECT_NAME):$(IMG_TAG)

## COMPOSE:
COMPOSE_CMD ?= docker-compose
COMPOSE_DB_URL ?= postgres://reporting:reporting@localhost:55432/reporting-db?sslmode=disable
COMPOSE_FILE ?= docker-compose.yml

PROMETHEUS_VERSION ?= 2.40.7
PROMETHEUS_DIST ?= $(shell go env GOOS)
PROMETHEUS_ARCH ?= $(shell go env GOARCH)
Expand Down
149 changes: 23 additions & 126 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,139 +1,36 @@
# APPUiO Cloud Reporting
# APPUiO Reporting

[![Build](https://img.shields.io/github/workflow/status/appuio/appuio-cloud-reporting/Test)][build]
![Go version](https://img.shields.io/github/go-mod/go-version/appuio/appuio-cloud-reporting)
[![Version](https://img.shields.io/github/v/release/appuio/appuio-cloud-reporting)][releases]
[![Maintainability](https://img.shields.io/codeclimate/maintainability/appuio/appuio-cloud-reporting)][codeclimate]
[![Coverage](https://img.shields.io/codeclimate/coverage/appuio/appuio-cloud-reporting)][codeclimate]
[![GitHub downloads](https://img.shields.io/github/downloads/appuio/appuio-cloud-reporting/total)][releases]
[![Build](https://img.shields.io/github/workflow/status/appuio/appuio-reporting/Test)][build]
![Go version](https://img.shields.io/github/go-mod/go-version/appuio/appuio-reporting)
[![Version](https://img.shields.io/github/v/release/appuio/appuio-reporting)][releases]
[![Maintainability](https://img.shields.io/codeclimate/maintainability/appuio/appuio-reporting)][codeclimate]
[![Coverage](https://img.shields.io/codeclimate/coverage/appuio/appuio-reporting)][codeclimate]
[![GitHub downloads](https://img.shields.io/github/downloads/appuio/appuio-reporting/total)][releases]

[build]: https://github.com/appuio/appuio-cloud-reporting/actions?query=workflow%3ATest
[releases]: https://github.com/appuio/appuio-cloud-reporting/releases
[codeclimate]: https://codeclimate.com/github/appuio/appuio-cloud-reporting

## Use APPUiO Global instance

```sh
# Follow the login instructions to get a token
oc login --server=https://api.cloudscale-lpg-2.appuio.cloud:6443

# Forward database and thanos to local host
kubectl -n appuio-reporting port-forward svc/reporting-db 5432 &
kubectl --as=cluster-admin -n appuio-thanos port-forward svc/thanos-query 9090 &

# Check for pending migrations
DB_USER=$(kubectl -n appuio-reporting get secret/reporting-db-superuser -o jsonpath='{.data.user}' | base64 --decode)
DB_PASSWORD=$(kubectl -n appuio-reporting get secret/reporting-db-superuser -o jsonpath='{.data.password}' | base64 --decode)
export ACR_DB_URL="postgres://${DB_USER}:${DB_PASSWORD}@localhost/reporting?sslmode=disable"
go run . migrate --show-pending

# Run a query
go run . report --query-name ping --begin "2022-01-17T09:00:00Z"

# Connect to the database's interactive terminal
DB_USER=$(kubectl -n appuio-reporting get secret/reporting-db-superuser -o jsonpath='{.data.user}' | base64 --decode)
export PGPASSWORD=$(kubectl -n appuio-reporting get secret/reporting-db-superuser -o jsonpath='{.data.password}' | base64 --decode)
psql -U "${DB_USER}" -w -h localhost reporting
```

## Local Installation

```sh
SUPERUSER_PW=$(pwgen 40 1)

kubectl create ns appuio-reporting
kubectl -n appuio-reporting create secret generic reporting-db-superuser --from-literal=user=reporting-db-superuser "--from-literal=password=${SUPERUSER_PW}"
kubectl -n appuio-reporting apply -k manifests/base
```

### Grafana

There is a Grafana deployment prepared under `manifests/grafana`.
To be able to use the deployment, customize the parameters in `grafana-helm-values.yaml` and run `make` to generate the manifest.

Add the required Grafana Helm chart using `helm repo add grafana https://grafana.github.io/helm-charts`.

The deployment requires a secret `grafana-creds` containing the admin username and password:

```sh
oc -n appuio-reporting create secret generic grafana-creds --from-literal=admin-password=$(pwgen 40 1) --from-literal=admin-user=admin
```
[build]: https://github.com/appuio/appuio-reporting/actions?query=workflow%3ATest
[releases]: https://github.com/appuio/appuio-reporting/releases
[codeclimate]: https://codeclimate.com/github/appuio/appuio-reporting

## Usage

### Run Report

```sh
kubectl -n appuio-reporting port-forward svc/reporting-db 5432 &
kubectl --as=cluster-admin -n appuio-thanos port-forward svc/thanos-query 9090 &

DB_USER=$(kubectl -n appuio-reporting get secret/reporting-db-superuser -o jsonpath='{.data.user}' | base64 --decode)
DB_PASSWORD=$(kubectl -n appuio-reporting get secret/reporting-db-superuser -o jsonpath='{.data.password}' | base64 --decode)
export ACR_DB_URL="postgres://${DB_USER}:${DB_PASSWORD}@localhost/reporting?sslmode=disable"

go run . report --query-name ping --begin "2022-01-17T09:00:00Z"
```

### Migrate to Most Recent Schema

```sh
kubectl -n appuio-reporting port-forward svc/reporting-db 5432 &

DB_USER=$(kubectl -n appuio-reporting get secret/reporting-db-superuser -o jsonpath='{.data.user}' | base64 --decode)
DB_PASSWORD=$(kubectl -n appuio-reporting get secret/reporting-db-superuser -o jsonpath='{.data.password}' | base64 --decode)
export ACR_DB_URL="postgres://${DB_USER}:${DB_PASSWORD}@localhost/reporting?sslmode=disable"

go run . migrate --show-pending

go run . migrate
```

### Connect to the Database

```sh
kubectl -n appuio-reporting port-forward svc/reporting-db 5432 &

DB_USER=$(kubectl -n appuio-reporting get secret/reporting-db-superuser -o jsonpath='{.data.user}' | base64 --decode)
export PGPASSWORD=$(kubectl -n appuio-reporting get secret/reporting-db-superuser -o jsonpath='{.data.password}' | base64 --decode)

psql -U "${DB_USER}" -w -h localhost reporting
```

## Local Development

Local development assumes a locally installed PostgreSQL database.
This can be achieved by running `make docker-compose-up`.
See `docker-compose.yml` for the configuration.

```sh
# Needs to be repeated after a Docker restart
make docker-compose-up

# Next command asks for a password, it is "reporting"
createdb --username=reporting -h localhost -p 5432 appuio-cloud-reporting-test

export ACR_DB_URL="postgres://reporting:reporting@localhost/appuio-cloud-reporting-test?sslmode=disable"

# Required for tests
make ensure-prometheus

go run . migrate
go run . migrate --seed
go test ./...

# To connect to the DB:
psql -U reporting -W -h localhost appuio-cloud-reporting-test
```
# Follow the login instructions to get a token
oc login --server=https://api.cloudscale-lpg-2.appuio.cloud:6443

### IDE Integration
# Forward mimir to local host
kubectl --as cluster-admin -nvshn-appuio-mimir service/vshn-appuio-mimir-query-frontend 8080

To enable IDE Test/Debug support, `ACR_DB_URL` should be added to the test environment.
# Set environment
export ACR_PROM_URL="http://localhost:8080/prometheus"
export ACR_ORG_ID="appuio-managed-openshift-billing" # mimir organization in which data is stored
export ACR_ODOO_URL=https://test.central.vshn.ch/api/v2/product_usage_report_POST
export ACR_ODOO_OAUTH_TOKEN_URL="https://test.central.vshn.ch/api/v2/authentication/oauth2/token"
export ACR_ODOO_OAUTH_CLIENT_ID="your_client_id" # see https://docs.central.vshn.ch/rest-api.html#_authentication_and_authorization
export ACR_ODOO_OAUTH_CLIENT_SECRET="your_client_secret"

#### VS Code
# Run a query
go run . report --query 'sum by (label) (metric)' --begin "2023-07-08T13:00:00Z" --product-id "your-odoo-product-id" --instance-jsonnet 'local labels = std.extVar("labels"); "instance-%(label)s" % labels' --unit-id "your_odoo_unit_id" --timerange 1h --item-description-jsonnet '"This is a description."' --item-group-description-jsonnet 'local labels = std.extVar("labels"); "Instance %(label)s" % labels'

```sh
mkdir -p .vscode
touch .vscode/settings.json
jq -s '(.[0] // {}) | ."go.testEnvVars"."ACR_DB_URL" = $ENV."ACR_DB_URL"' .vscode/settings.json > .vscode/settings.json.i
mv .vscode/settings.json.i .vscode/settings.json
```
87 changes: 0 additions & 87 deletions check_command.go

This file was deleted.

18 changes: 5 additions & 13 deletions common_flags.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
package main

import (
"github.com/appuio/appuio-cloud-reporting/pkg/db"
"github.com/urfave/cli/v2"
)

const defaultTestForRequiredFlags = "<required>"

func newDbURLFlag(destination *string) *cli.StringFlag {
return &cli.StringFlag{Name: "db-url", Usage: "Database connection URL in the form of postgres://user@host:port/db-name?option=value",
EnvVars: envVars("DB_URL"), Destination: destination, Required: true, DefaultText: defaultTestForRequiredFlags}
}
const defaultTextForRequiredFlags = "<required>"
const defaultTextForOptionalFlags = "<optional>"

func newPromURLFlag(destination *string) *cli.StringFlag {
return &cli.StringFlag{Name: "prom-url", Usage: "Prometheus connection URL in the form of http://host:port",
EnvVars: envVars("PROM_URL"), Destination: destination, Value: "http://localhost:9090"}
}

func queryNames(queries []db.Query) []string {
names := make([]string, len(queries))
for i := range queries {
names[i] = queries[i].Name
}
return names
func newOdooURLFlag(destination *string) *cli.StringFlag {
return &cli.StringFlag{Name: "odoo-url", Usage: "URL of the Odoo Metered Billing API",
EnvVars: envVars("ODOO_URL"), Destination: destination, Value: "http://localhost:8080"}
}
Loading