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

add cache, generate grafana dashboard #1

Merged
merged 5 commits into from
Sep 29, 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
Empty file added .github/badges/.gitkeep
Empty file.
1 change: 1 addition & 0 deletions .github/badges/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 52 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: Analysis
on:
push:
branches:
- main
pull_request:

permissions:
contents: read

jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: '1.20'
cache: false
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
# Require: The version of golangci-lint to use.
# When `install-mode` is `binary` (default) the value can be v1.2 or v1.2.3 or `latest` to use the latest version.
# When `install-mode` is `goinstall` the value can be v1.2.3, `latest`, or the hash of a commit.
version: v1.54

# Optional: working directory, useful for monorepos
# working-directory: somedir

# Optional: golangci-lint command line arguments.
#
# Note: By default, the `.golangci.yml` file should be at the root of the repository.
# The location of the configuration file can be changed by using `--config=`
# args: --timeout=30m --config=/my/path/.golangci.yml --issues-exit-code=0

# Optional: show only new issues if it's a pull request. The default value is `false`.
# only-new-issues: true

# Optional: if set to true, then all caching functionality will be completely disabled,
# takes precedence over all other caching options.
# skip-cache: true

# Optional: if set to true, then the action won't cache or restore ~/go/pkg.
# skip-pkg-cache: true

# Optional: if set to true, then the action won't cache or restore ~/.cache/go-build.
# skip-build-cache: true

# Optional: The mode to install golangci-lint. It can be 'binary' or 'goinstall'.
# install-mode: "goinstall"
52 changes: 52 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: Test

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:

test:
name: Test
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
with:
repository: ${{ github.event.pull_request.head.repo.full_name }}
ref: ${{ github.event.pull_request.head.ref }}
- uses: actions/setup-go@v4
with:
go-version: '^1.20'
- run: go version


- name: Build
run: go build -v ./...

- name: Test
run: |
CVPKG=$(go list ./... | grep -v mocks | tr '\n' ',')
go test -p 1 -coverpkg=${CVPKG} -coverprofile=coverage.out -covermode=count ./...

- name: Code Coverage Badge
run: |
set -x
total=`go tool cover -func=coverage.out | grep total | grep -Eo '[0-9]+\.[0-9]+'`
if (( $(echo "$total <= 50" | bc -l) )) ; then
COLOR=red
elif (( $(echo "$total > 80" | bc -l) )); then
COLOR=green
else
COLOR=orange
fi
curl "https://img.shields.io/badge/coverage-$total%25-$COLOR" > .github/badges/coverage.svg

- name: Commit and push the badge (if it changed)
uses: EndBug/add-and-commit@v9
with:
default_author: github_actor
message: 'commit badge'
add: '*.svg'
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.idea/*
.idea/*

cache/*.json
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
run:
export OTEL_EXPORTER_OTLP_METRICS_ENDPOINT=http://localhost:4317
go run main.go

test:
go test
golangci-lint run
Empty file added cache/.gitkeep
Empty file.
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,7 @@ services:
dockerfile: Dockerfile
environment:
- OTEL_EXPORTER_OTLP_METRICS_ENDPOINT=http://otel-collector:4317
volumes:
- ./cache:/app/cache
depends_on:
- otel-collector
86 changes: 86 additions & 0 deletions file_cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package main

import (
"encoding/json"
"os"
)

type SingleFileCache struct {
location string
}

func NewSingleFileCache(location string) *SingleFileCache {
if _, err := os.Stat(location); os.IsNotExist(err) {
// Create an empty JSON file if it doesn't exist
data := []byte("{}")
err := os.WriteFile(location, data, 0644)
if err != nil {
panic(err)
}
}

return &SingleFileCache{
location: location,
}
}

func (c *SingleFileCache) AddToCache(key string, value string) error {
// Read the existing cache
data, err := os.ReadFile(c.location)
if err != nil {
return err
}

// Unmarshal the JSON data into a map
cache := map[string]string{}
err = json.Unmarshal(data, &cache)
if err != nil {
return err
}

// Add or update the key-value pair
cache[key] = value

// Marshal the updated cache and write it back to the file
updatedData, err := json.Marshal(cache)
if err != nil {
return err
}

err = os.WriteFile(c.location, updatedData, 0644)
if err != nil {
return err
}

return nil
}

func (c *SingleFileCache) RetrieveValue(key string) (string, error) {
// Read the existing cache
data, err := os.ReadFile(c.location)
if err != nil {
return "", err
}

// Unmarshal the JSON data into a map
cache := map[string]string{}
err = json.Unmarshal(data, &cache)
if err != nil {
return "", err
}

return cache[key], nil
}

func (c *SingleFileCache) DeleteCache() error {
if _, err := os.Stat(c.location); os.IsNotExist(err) {
return nil // Cache file doesn't exist, nothing to delete
}

err := os.Remove(c.location)
if err != nil {
return err
}

return nil
}
27 changes: 27 additions & 0 deletions file_cache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestCacheAdd(t *testing.T) {
cache := NewSingleFileCache("test-cache")
err := cache.AddToCache("test", "value")
if err != nil {
t.Failed()
}

value, err := cache.RetrieveValue("test")
if err != nil {
t.Failed()
}

assert.Equal(t, "value", value)
empty, _ := cache.RetrieveValue("not existent")
assert.Empty(t, empty)
err = cache.DeleteCache()
if err != nil {
t.Failed()
}
}
6 changes: 3 additions & 3 deletions github_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
)

Expand Down Expand Up @@ -44,7 +44,7 @@ func (c *GitHubClient) GetMostRecentCommit(repo, timestamp, branch string) (stri

if response.StatusCode == http.StatusOK {
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
body, err := io.ReadAll(response.Body)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -80,7 +80,7 @@ func (c *GitHubClient) GetFileAtCommit(repository, filepath, commitSHA string) (

if response.StatusCode == http.StatusOK {
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
body, err := io.ReadAll(response.Body)
if err != nil {
return "", err
}
Expand Down
Loading