Skip to content

Commit

Permalink
Move ingest rate behind feature flag.
Browse files Browse the repository at this point in the history
  • Loading branch information
Syfaro committed Feb 29, 2024
1 parent 570c950 commit 4833f8f
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 55 deletions.
118 changes: 63 additions & 55 deletions frontend/src/entrypoints/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,70 +11,78 @@ interface IngestStatUpdate {
counts: Record<string, number>;
}

let pointDates: Array<Date> = [];
let series: Record<string, any> = {};
let previousValues: Record<string, number> = {};
let addedPoints = -1;
const ingestStatElement = document.getElementById("ingest-stats");

const chart = echarts.init(document.getElementById("ingest-stats"));
function ingestStatsGraph(elem: HTMLElement) {
let pointDates: Array<Date> = [];
let series: Record<string, any> = {};
let previousValues: Record<string, number> = {};
let addedPoints = -1;

const opts = {
title: {
left: "center",
text: "Ingested Media",
},
tooltip: {
trigger: "axis",
},
toolbox: {
feature: {
dataZoom: {
yAxisIndex: "none",
const chart = echarts.init(elem);

const opts = {
title: {
left: "center",
text: "Ingested Media",
},
tooltip: {
trigger: "axis",
},
toolbox: {
feature: {
dataZoom: {
yAxisIndex: "none",
},
restore: {},
saveAsImage: {},
},
restore: {},
saveAsImage: {},
},
},
xAxis: {
type: "category",
boundaryGap: false,
data: pointDates,
},
yAxis: {
name: "Additions",
type: "value",
boundaryGap: [0, "100%"],
},
series: [],
};
xAxis: {
type: "category",
boundaryGap: false,
data: pointDates,
},
yAxis: {
name: "Additions",
type: "value",
boundaryGap: [0, "100%"],
},
series: [],
};

chart.setOption(opts);

chart.setOption(opts);
const evtSource = new EventSource("/api/ingest/stats");

const evtSource = new EventSource("/api/ingest/stats");
evtSource.onmessage = (e) => {
const data: IngestStatUpdate = JSON.parse(e.data);

evtSource.onmessage = (e) => {
const data: IngestStatUpdate = JSON.parse(e.data);
for (let [key, value] of Object.entries(data["counts"])) {
if (series[key] === undefined) {
series[key] = {
name: key,
type: "line",
data: Array(addedPoints + 1).fill(0),
};
}

for (let [key, value] of Object.entries(data["counts"])) {
if (series[key] === undefined) {
series[key] = {
name: key,
type: "line",
data: Array(addedPoints + 1).fill(0),
};
series[key]["data"].push(value - (previousValues[key] || 0));
previousValues[key] = value;
}

series[key]["data"].push(value - (previousValues[key] || 0));
previousValues[key] = value;
}
pointDates.push(new Date(data["timestamp"] * 1000));
addedPoints += 1;

pointDates.push(new Date(data["timestamp"] * 1000));
addedPoints += 1;
chart.setOption({
xAxis: {
data: pointDates,
},
series: Object.values(series),
});
};
}

chart.setOption({
xAxis: {
data: pointDates,
},
series: Object.values(series),
});
};
if (ingestStatElement) {
ingestStatsGraph(ingestStatElement);
}
9 changes: 9 additions & 0 deletions src/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@ pub fn service() -> Scope {
struct Admin {
subreddits: Vec<models::RedditSubreddit>,
recent_flist_runs: Vec<models::FListImportRun>,

ingest_rate_graph: bool,
}

#[get("/tools", name = "admin_tools")]
async fn admin(
unleash: web::Data<crate::Unleash>,
request: actix_web::HttpRequest,
conn: web::Data<sqlx::PgPool>,
user: models::User,
Expand All @@ -48,6 +51,12 @@ async fn admin(
let body = Admin {
subreddits,
recent_flist_runs,

ingest_rate_graph: unleash.is_enabled(
crate::Features::AdminIngestRate,
Some(&user.context()),
false,
),
}
.wrap(&request, Some(&user))
.await
Expand Down
9 changes: 9 additions & 0 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,13 +290,22 @@ async fn events(

#[get("/ingest/stats")]
async fn ingest_stats(
unleash: web::Data<crate::Unleash>,
user: models::User,
nats: web::Data<async_nats::Client>,
) -> impl actix_web::Responder {
if !user.is_admin {
return Err(Error::Unauthorized);
}

if !unleash.is_enabled(
crate::Features::AdminIngestRate,
Some(&user.context()),
false,
) {
return Err(Error::Unauthorized);
}

let (tx, rx) = tokio::sync::mpsc::channel::<Result<_, Error>>(10);

tokio::task::spawn_local(
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,8 @@ enum Features {
MergeMedia,
#[serde(rename = "fuzzysearch.owo.webauthn")]
Webauthn,
#[serde(rename = "fuzzysearch.owo.admin-ingest-rate")]
AdminIngestRate,
}

type Unleash = foxlib::flags::Unleash<Features>;
Expand Down
2 changes: 2 additions & 0 deletions templates/admin/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ <h1 class="title has-text-centered">Admin</h1>
</div>
</div>

{% if ingest_rate_graph %}
<div class="columns is-centered is-desktop">
<div class="column">
<div class="block">
<div id="ingest-stats" style="height: 400px;"></div>
</div>
</div>
</div>
{% endif %}

<div class="columns is-centered is-desktop">
<div class="column is-one-third-desktop">
Expand Down

0 comments on commit 4833f8f

Please sign in to comment.