Skip to content

Commit

Permalink
feat: display workers
Browse files Browse the repository at this point in the history
  • Loading branch information
jiegec committed Mar 11, 2024
1 parent fe2f4cb commit daeae40
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 3 deletions.
46 changes: 45 additions & 1 deletion frontend/src/pages/workers.vue
Original file line number Diff line number Diff line change
@@ -1,14 +1,58 @@
<template>
TODO
<v-container>
<v-row>
<v-col>
<v-data-table-server
:items-per-page="itemsPerPage"
:headers="headers"
:items="serverItems"
:items-length="totalItems"
:loading="loading"
item-value="id"
@update:options="loadItems">

</v-data-table-server>
</v-col>
</v-row>
</v-container>
</template>

<script lang="ts" setup>
//
</script>

<script lang="ts">
import axios from 'axios';
import prettyBytes from 'pretty-bytes';
import { hostname } from '@/common';
interface LoadItemsOpts {
page: number;
itemsPerPage: number;
}
export default {
data: () => ({
itemsPerPage: 10,
headers: [
{ title: 'Worker ID', key: 'id', sortable: false },
{ title: 'Hostname', key: 'hostname', sortable: false },
{ title: 'Architecture', key: 'arch', sortable: false },
{ title: 'Logical Cores', key: 'logical_cores', sortable: false },
{ title: 'Memory Size', key: 'memory_bytes', sortable: false, value: (item: any) => prettyBytes(item.memory_bytes) },
],
loading: true,
totalItems: 0,
serverItems: []
}),
methods: {
async loadItems (opts: LoadItemsOpts) {
this.loading = true;
let data = (await axios.get(hostname + `/api/worker/list?page=${opts.page}&items_per_page=${opts.itemsPerPage}`)).data;
this.totalItems = data.total_items;
this.serverItems = data.items;
this.loading = false;
}
}
}
</script>
1 change: 0 additions & 1 deletion server/src/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::ARGS;
use octocrab::models::pulls::PullRequest;
use octocrab::{models::InstallationId, Octocrab};
use serde::{Deserialize, Serialize};
use teloxide::prelude::*;
use teloxide::types::{ChatId, Message};

#[derive(Deserialize, Serialize, Debug)]
Expand Down
4 changes: 3 additions & 1 deletion server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use diesel::r2d2::Pool;
use server::bot::{answer, Command};
use server::recycler::recycler_worker;
use server::routes::{
dashboard_status, ping, job_list, pipeline_list, pipeline_new_pr, worker_job_update, worker_poll, AppState
dashboard_status, job_list, ping, pipeline_list, pipeline_new_pr, worker_job_update,
worker_list, worker_poll, AppState,
};
use server::routes::{pipeline_new, worker_heartbeat};
use server::routes::{pipeline_status, worker_status};
Expand Down Expand Up @@ -70,6 +71,7 @@ async fn main() -> anyhow::Result<()> {
.route("/api/worker/poll", post(worker_poll))
.route("/api/worker/job_update", post(worker_job_update))
.route("/api/worker/status", get(worker_status))
.route("/api/worker/list", get(worker_list))
.route("/api/dashboard/status", get(dashboard_status))
.fallback_service(serve_dir)
.with_state(state)
Expand Down
63 changes: 63 additions & 0 deletions server/src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -869,3 +869,66 @@ pub async fn job_list(
})?,
))
}

#[derive(Deserialize)]
pub struct WorkerListRequest {
page: i64,
items_per_page: i64,
}

#[derive(Serialize)]
pub struct WorkerListResponseItem {
id: i32,
hostname: String,
arch: String,
logical_cores: i32,
memory_bytes: i64,
}

#[derive(Serialize)]
pub struct WorkerListResponse {
total_items: i64,
items: Vec<WorkerListResponseItem>,
}

pub async fn worker_list(
Query(query): Query<WorkerListRequest>,
State(AppState { pool, .. }): State<AppState>,
) -> Result<Json<WorkerListResponse>, AnyhowError> {
let mut conn = pool
.get()
.context("Failed to get db connection from pool")?;

Ok(Json(
conn.transaction::<WorkerListResponse, diesel::result::Error, _>(|conn| {
let total_items = crate::schema::workers::dsl::workers
.count()
.get_result(conn)?;

let workers = if query.items_per_page == -1 {
crate::schema::workers::dsl::workers
.order_by(crate::schema::workers::dsl::id)
.load::<Worker>(conn)?
} else {
crate::schema::workers::dsl::workers
.order_by(crate::schema::workers::dsl::id)
.offset((query.page - 1) * query.items_per_page)
.limit(query.items_per_page)
.load::<Worker>(conn)?
};

let mut items = vec![];
for worker in workers {
items.push(WorkerListResponseItem {
id: worker.id,
hostname: worker.hostname,
arch: worker.arch,
logical_cores: worker.logical_cores,
memory_bytes: worker.memory_bytes,
});
}

Ok(WorkerListResponse { total_items, items })
})?,
))
}

0 comments on commit daeae40

Please sign in to comment.