Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Commit

Permalink
feat(shipyard-controller): Add dbdump endpoint to the Debug-UI (#8618)
Browse files Browse the repository at this point in the history
* debug ui db dump endpoint

Signed-off-by: Andreas Samonik <andreas.samonik@dynatrace.com>

* debug ui db dump endpoint

Signed-off-by: Andreas Samonik <andreas.samonik@dynatrace.com>

* bson

Signed-off-by: Andreas Samonik <andreas.samonik@dynatrace.com>

* removed commented out code

Signed-off-by: Andreas Samonik <andreas.samonik@dynatrace.com>

* additional repo test

Signed-off-by: Andreas Samonik <andreas.samonik@dynatrace.com>

* pr fixes

Signed-off-by: Andreas Samonik <andreas.samonik@dynatrace.com>

* frontend api change

Signed-off-by: Andreas Samonik <andreas.samonik@dynatrace.com>

* additional repo test case

Signed-off-by: Andreas Samonik <andreas.samonik@dynatrace.com>

* repo test

Signed-off-by: Andreas Samonik <andreas.samonik@dynatrace.com>

* additional filter

Signed-off-by: Andreas Samonik <andreas.samonik@dynatrace.com>

* html alignment fix

Signed-off-by: Andreas Samonik <andreas.samonik@dynatrace.com>

Signed-off-by: Andreas Samonik <andreas.samonik@dynatrace.com>
  • Loading branch information
asamonik committed Aug 24, 2022
1 parent 27c5524 commit 0d5e228
Show file tree
Hide file tree
Showing 15 changed files with 732 additions and 8 deletions.
2 changes: 2 additions & 0 deletions shipyard-controller/debug-ui/fetch_sequences.js
@@ -1,6 +1,7 @@
function fetchSequences(
project_name,
show_triggered,
show_started,
show_finished,
show_timedOut,
table
Expand All @@ -18,6 +19,7 @@ function fetchSequences(
response.sequenceExecutions.forEach((object) => {
if (
(object.status.state === "triggered" && show_triggered) ||
(object.status.state === "started" && show_started) ||
(object.status.state === "finished" && show_finished) ||
(object.status.state === "timedOut" && show_timedOut)
) {
Expand Down
3 changes: 1 addition & 2 deletions shipyard-controller/debug-ui/index.css
Expand Up @@ -55,7 +55,7 @@ img {
}

input {
transform: scale(1.5);
/*transform: scale(1.5);*/
}

.finished {
Expand All @@ -69,7 +69,6 @@ input {
button {
background-color: white;
border: 2px solid #006bb8;
width: 100%;
}

span {
Expand Down
55 changes: 53 additions & 2 deletions shipyard-controller/debug-ui/index.html
Expand Up @@ -33,6 +33,16 @@
/>
</li>

<li class="header">
Started:
<input
onChange="fetchSequencesHelper()"
id="checkboxStarted"
type="checkbox"
checked
/>
</li>

<li class="header">
TimedOut:
<input
Expand All @@ -48,6 +58,15 @@

<select id="projectSelector" onChange="fetchSequencesHelper()"></select>
</li>

<li class="header">
Export MongoDB collection:
<select id="collectionSelector" onChange="updateDumpButton()"></select>

<a id="adownload" href="#" download="dump.json">
<button>Download</button>
</a>
</li>
</ul>

<table id="prodTable">
Expand Down Expand Up @@ -82,22 +101,54 @@

fetchSequencesHelper();
});
</script>

<script>
// get collections
fetch("/dbdump/listcollections", {
method: "get",
})
.then((res) => {
return res.json();
})
.then((response) => {
var collectionSelector =
document.getElementById("collectionSelector");
collectionSelector.innerHTML = "";

response.forEach((collection) => {
el_collection = document.createElement("option");
el_collection.innerHTML = collection;
collectionSelector.appendChild(el_collection);
});

updateDumpButton();
})
.catch((err) => {
console.log(e);
});

function updateDumpButton() {
const adownload = document.getElementById("adownload");
const collection = document.getElementById("collectionSelector").value;
adownload.href = `/dbdump/collection/${collection}`;
adownload.download = `${collection}.json`;
}

function fetchSequencesHelper() {
const project_name = document.getElementById("projectSelector").value;
let table = document.getElementById("prodTable");

const show_finished =
document.getElementById("checkboxFinished").checked;
const show_started =
document.getElementById("checkboxStarted").checked;
const show_triggered =
document.getElementById("checkboxTriggered").checked;
const show_timedOut =
document.getElementById("checkboxTimedOut").checked;

fetchSequences(
project_name,
show_started,
show_triggered,
show_finished,
show_timedOut,
Expand Down
106 changes: 106 additions & 0 deletions shipyard-controller/internal/db/mock/dbdump_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 60 additions & 0 deletions shipyard-controller/internal/db/mongodb_dbdump_repo.go
@@ -0,0 +1,60 @@
package db

import (
"context"
"time"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)

type MongoDBDumpRepo struct {
DbConnection *MongoDBConnection
}

func NewMongoDBDumpRepo(dbConnection *MongoDBConnection) *MongoDBDumpRepo {
return &MongoDBDumpRepo{DbConnection: dbConnection}
}

func (mdbrepo *MongoDBDumpRepo) GetDump(collectionName string) ([]bson.M, error) {
collection, ctx, cancel, err := mdbrepo.getCollectionAndContext(collectionName)
if err != nil {
return nil, err
}
defer cancel()

cursor, err := collection.Find(ctx, bson.D{})

if err != nil {
return nil, err
}

var result = []bson.M{}

cursor.All(ctx, &result)

return result, err
}

func (mdbrepo *MongoDBDumpRepo) ListAllCollections() ([]string, error) {
err := mdbrepo.DbConnection.EnsureDBConnection()
if err != nil {
return nil, err
}

ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
result, err := mdbrepo.DbConnection.Client.Database(getDatabaseName()).ListCollectionNames(ctx, bson.D{})

return result, err
}

func (mdbrepo *MongoDBDumpRepo) getCollectionAndContext(collectionName string) (*mongo.Collection, context.Context, context.CancelFunc, error) {
err := mdbrepo.DbConnection.EnsureDBConnection()
if err != nil {
return nil, nil, nil, err
}
collection := mdbrepo.DbConnection.Client.Database(getDatabaseName()).Collection(collectionName)

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
return collection, ctx, cancel, nil
}
88 changes: 88 additions & 0 deletions shipyard-controller/internal/db/mongodb_dbdump_repo_test.go
@@ -0,0 +1,88 @@
package db

import (
"encoding/json"
"testing"

apimodels "github.com/keptn/go-utils/pkg/api/models"
"github.com/stretchr/testify/require"
)

func TestMongoDBDumpRepo_ListAllCollections(t *testing.T) {
dumpRepo := NewMongoDBDumpRepo(GetMongoDBConnectionInstance())
projectsRepo := NewMongoDBProjectsRepo(GetMongoDBConnectionInstance())

err := projectsRepo.CreateProject(&apimodels.ExpandedProject{
ProjectName: "my-project",
})
require.Nil(t, err)

projectNames, err := dumpRepo.ListAllCollections()

require.Len(t, projectNames, 1)
require.Nil(t, err)
require.Contains(t, projectNames, "keptnProjectsMV")

CleanupDB(t, projectsRepo)
}

func TestMongoDBDumpRepo_GetDump(t *testing.T) {
dumpRepo := NewMongoDBDumpRepo(GetMongoDBConnectionInstance())
projectsRepo := NewMongoDBProjectsRepo(GetMongoDBConnectionInstance())

// get random collection dump
_, err := dumpRepo.GetDump("test")
require.Nil(t, err)

// compare to project repo: 0 projects
projects, err := projectsRepo.GetProjects()
require.Nil(t, err)

dump, err := dumpRepo.GetDump("keptnProjectsMV")
require.Nil(t, err)

var projects_dump []*apimodels.ExpandedProject
bytes, err := json.Marshal(dump)
err = json.Unmarshal(bytes, &projects_dump)

require.Nil(t, err)
require.Equal(t, projects_dump, projects)

// compare to project repo: 1 project
err = projectsRepo.CreateProject(&apimodels.ExpandedProject{
ProjectName: "my-project",
})
require.Nil(t, err)

projects, err = projectsRepo.GetProjects()
require.Nil(t, err)

dump, err = dumpRepo.GetDump("keptnProjectsMV")
require.Nil(t, err)

bytes, err = json.Marshal(dump)
err = json.Unmarshal(bytes, &projects_dump)

require.Nil(t, err)
require.Equal(t, projects_dump, projects)

// compare to project repo: 2 projects
err = projectsRepo.CreateProject(&apimodels.ExpandedProject{
ProjectName: "my-project2",
})
require.Nil(t, err)

projects, err = projectsRepo.GetProjects()
require.Nil(t, err)

dump, err = dumpRepo.GetDump("keptnProjectsMV")
require.Nil(t, err)

bytes, err = json.Marshal(dump)
err = json.Unmarshal(bytes, &projects_dump)

require.Nil(t, err)
require.Equal(t, projects_dump, projects)

CleanupDB(t, projectsRepo)
}

0 comments on commit 0d5e228

Please sign in to comment.