#49 only update cluster box if necessary

This commit is contained in:
Henning Jacobs
2017-01-03 16:49:32 +01:00
parent b2b857505b
commit bd353b23ef
3 changed files with 49 additions and 57 deletions

17
app.py
View File

@@ -195,18 +195,15 @@ def generate_mock_cluster_data(index: int):
}
def get_mock_clusters(cluster_ids: set):
def get_mock_clusters():
for i in range(3):
data = generate_mock_cluster_data(i)
if not cluster_ids or data['id'] in cluster_ids:
yield data
yield data
def get_kubernetes_clusters(cluster_ids: set):
def get_kubernetes_clusters():
for api_server_url in (os.getenv('CLUSTERS') or DEFAULT_CLUSTERS).split(','):
cluster_id = generate_cluster_id(api_server_url)
if cluster_ids and cluster_id not in cluster_ids:
continue
if 'localhost' not in api_server_url:
# TODO: hacky way of detecting whether we need a token or not
session.headers['Authorization'] = 'Bearer {}'.format(tokens.get('read-only'))
@@ -270,7 +267,8 @@ def get_kubernetes_clusters(cluster_ids: set):
def event(cluster_ids: set):
while True:
for cluster in STORE._data.values():
yield 'event: clusterupdate\ndata: ' + json.dumps(cluster, separators=(',', ':')) + '\n\n'
if not cluster_ids or cluster['id'] in cluster_ids:
yield 'event: clusterupdate\ndata: ' + json.dumps(cluster, separators=(',', ':')) + '\n\n'
gevent.sleep(5)
@@ -319,11 +317,10 @@ def get_auth_oauth_token():
def update():
while True:
try:
cluster_ids = set()
if MOCK:
clusters = get_mock_clusters(cluster_ids)
clusters = get_mock_clusters()
else:
clusters = get_kubernetes_clusters(cluster_ids)
clusters = get_kubernetes_clusters()
for cluster in clusters:
STORE.set(cluster['id'], cluster)
except:

View File

@@ -12,46 +12,39 @@ export default class App {
constructor() {
const params = this.parseLocationHash()
this.filterString = params.q || ''
this.selectedClusters = new Set((params.clusters || '').split(',').filter(x => x))
this.filterString = params.get('q') || ''
this.selectedClusters = new Set((params.get('clusters') || '').split(',').filter(x => x))
this.seenPods = new Set()
this.sorterFn = ''
this.theme = Theme.get(localStorage.getItem('theme'))
this.eventSource = null
this.clusters = []
this.clusters = new Map()
}
parseLocationHash() {
// hash startswith #
const hash = document.location.hash.substring(1)
const params = {}
const params = new Map()
for (const pair of hash.split(';')) {
const keyValue = pair.split('=', 2)
if (keyValue.length == 2) {
params[keyValue[0]] = keyValue[1]
params.set(keyValue[0], keyValue[1])
}
}
return params
}
changeLocationHash(key, value) {
const hash = document.location.hash.substring(1)
const params = {}
for (const pair of hash.split(';')) {
const keyValue = pair.split('=', 2)
if (keyValue.length == 2) {
params[keyValue[0]] = keyValue[1]
}
}
params[key] = value
const params = this.parseLocationHash()
params.set(key, value)
const pairs = []
for (const key of Object.keys(params).sort()) {
if (params[key]) {
pairs.push(key + '=' + params[key])
for (const [key, value] of params) {
if (value) {
pairs.push(key + '=' + value)
}
}
document.location.hash = '#' + pairs.join(';')
document.location.hash = '#' + pairs.sort().join(';')
}
filter() {
@@ -268,13 +261,12 @@ export default class App {
PIXI.ticker.shared.add(tick)
this.stage.addChild(pod)
}
update(clusters) {
this.clusters = clusters
update() {
const that = this
let changes = 0
const firstTime = this.seenPods.size == 0
const podKeys = new Set()
for (const cluster of clusters) {
for (const cluster of this.clusters.values()) {
for (const node of cluster.nodes) {
for (const pod of node.pods) {
podKeys.add(cluster.id + '/' + pod.namespace + '/' + pod.name)
@@ -303,26 +295,31 @@ export default class App {
changes++
}
}
this.viewContainer.removeChildren()
const clusterComponentById = {}
for (const component of this.viewContainer.children) {
clusterComponentById[component.cluster.id] = component
}
let y = 0
for (const cluster of clusters) {
const clusterIds = new Set()
for (const cluster of this.clusters.values()) {
if (!this.selectedClusters.size || this.selectedClusters.has(cluster.id)) {
for (const node of cluster.nodes) {
for (const pod of node.pods) {
podKeys.add(cluster.id + '/' + pod.namespace + '/' + pod.name)
}
clusterIds.add(cluster.id)
let clusterBox = clusterComponentById[cluster.id]
if (!clusterBox) {
clusterBox = new Cluster(cluster, this.tooltip)
this.viewContainer.addChild(clusterBox)
}
for (const pod of cluster.unassigned_pods) {
podKeys.add(cluster.id + '/' + pod.namespace + '/' + pod.name)
}
const clusterBox = new Cluster(cluster, this.tooltip)
clusterBox.draw()
clusterBox.x = 0
clusterBox.y = y
this.viewContainer.addChild(clusterBox)
y += clusterBox.height + 10
}
}
for (const component of this.viewContainer.children) {
if (!clusterIds.has(component.cluster.id)) {
this.viewContainer.removeChild(component)
}
}
this.filter()
for (const key of Object.keys(ALL_PODS)) {
@@ -347,13 +344,13 @@ export default class App {
changeSorting(newSortFunction) {
this.sorterFn = newSortFunction
this.update(this.clusters)
this.update()
}
switchTheme(newTheme) {
this.theme = Theme.get(newTheme)
this.draw()
this.update(this.clusters)
this.update()
localStorage.setItem('theme', newTheme)
}
@@ -364,7 +361,9 @@ export default class App {
this.selectedClusters.add(clusterId)
}
this.changeLocationHash('clusters', Array.from(this.selectedClusters).join(','))
this.update(this.clusters)
// make sure we are updating our EventSource filter
this.listen()
this.update()
}
listen() {
@@ -373,24 +372,19 @@ export default class App {
this.eventSource = null
}
const that = this
let url = '/events'
const clusterIds = Array.from(this.selectedClusters).join(',')
const eventSource = this.eventSource = new EventSource('/events?cluster_ids=' + clusterIds, {credentials: 'include'})
if (clusterIds) {
url += '?cluster_ids=' + clusterIds
}
const eventSource = this.eventSource = new EventSource(url, {credentials: 'include'})
eventSource.onerror = function(event) {
that.listen()
}
eventSource.addEventListener('clusterupdate', function(event) {
const cluster = JSON.parse(event.data)
let found = false
for (let i=0; i<that.clusters.length; i++) {
if (that.clusters[i]['id'] == cluster['id']) {
that.clusters[i] = cluster
found = true
}
}
if (!found) {
that.clusters.push(cluster)
}
that.update(that.clusters)
that.clusters.set(cluster.id, cluster)
that.update()
})
}

View File

@@ -11,6 +11,7 @@ export default class Cluster extends PIXI.Graphics {
}
draw () {
this.removeChildren()
const left = 10
const top = 20
const padding = 5