Radish alpha
r
rad:zwTxygwuz5LDGBq255RA2CbNGrz8
Radicle CI broker
Radicle
Git
fix(src/pages.rs): sort RSS feed items by time
Lars Wirzenius committed 9 months ago
commit 05283ad35d3598afeb483bbe111cd076137e9273
parent 4db74c7
2 files changed +56 -26
modified src/pages.rs
@@ -617,18 +617,11 @@ impl PageData {
        let mut channel = ChannelBuilder::default();
        channel
            .title("Radicle CI broker run information")
-
            .description("All CI runs known to this instance of the Radicle CI broker.")
+
            .description("Latest CI runs known on this instance of the Radicle CI broker.")
            .link("FIXME:link");
-
        let mut items = vec![];
-
        for (_alias, repo_id) in self.repos() {
-
            for run in self.runs(repo_id) {
-
                items.push(Self::rss_item_from_run(run)?);
-
            }
-
        }
-
        items.sort_by_key(|item| item.pub_date().map(|s| s.to_string()));
-
        items.reverse();
-
        for item in items.iter().take(MAX_RSS_ENTRIES).cloned() {
-
            channel.item(item);
+

+
        for item in self.sorted_items()?.iter().take(MAX_RSS_ENTRIES) {
+
            channel.item((*item).clone());
        }
        Ok(channel.build())
    }
@@ -637,15 +630,19 @@ impl PageData {
        let mut channel = ChannelBuilder::default();
        channel
            .title("Radicle CI broker run information")
-
            .description("All CI runs known to this instance of the Radicle CI broker.")
+
            .description("Latest FAILED CI runs on this instance of the Radicle CI broker.")
            .link("FIXME:link");
-
        for (_alias, repo_id) in self.repos() {
-
            for run in self.runs(repo_id) {
-
                if run.state() == RunState::Finished && run.result() == Some(&RunResult::Failure) {
-
                    channel.item(Self::rss_item_from_run(run)?);
-
                }
-
            }
+

+
        for item in self
+
            .sorted_items_for_runs(|run| {
+
                run.state() == RunState::Finished && run.result() == Some(&RunResult::Failure)
+
            })?
+
            .iter()
+
            .take(MAX_RSS_ENTRIES)
+
        {
+
            channel.item(item.clone());
        }
+

        Ok(channel.build())
    }

@@ -653,19 +650,52 @@ impl PageData {
        let mut channel = ChannelBuilder::default();
        channel
            .title("Radicle CI broker run information")
-
            .description("All CI runs known to this instance of the Radicle CI broker.")
+
            .description("Latest UNFINISHED CI runs on this instance of the Radicle CI broker.")
            .link("FIXME:link");
+
        for item in self
+
            .sorted_items_for_runs(|run| {
+
                run.state() == RunState::Triggered || run.state() == RunState::Running
+
            })?
+
            .iter()
+
            .take(MAX_RSS_ENTRIES)
+
        {
+
            channel.item(item.clone());
+
        }
+
        Ok(channel.build())
+
    }
+

+
    fn sorted_items(&self) -> Result<Vec<Item>, PageError> {
+
        let mut items = vec![];
        for (_alias, repo_id) in self.repos() {
            for run in self.runs(repo_id) {
-
                if run.state() == RunState::Triggered || run.state() == RunState::Running {
-
                    channel.item(Self::rss_item_from_run(run)?);
+
                if run.state() == RunState::Finished && run.result() == Some(&RunResult::Failure) {
+
                    items.push(Self::rss_item_from_run(run)?);
                }
            }
        }
-
        Ok(channel.build())
+
        items.sort_by_key(|(ts, _)| *ts);
+
        items.reverse();
+
        Ok(items.iter().map(|(_, item)| item.clone()).collect())
+
    }
+

+
    fn sorted_items_for_runs<F>(&self, pred: F) -> Result<Vec<Item>, PageError>
+
    where
+
        F: Fn(&Run) -> bool,
+
    {
+
        let mut items = vec![];
+
        for (_alias, repo_id) in self.repos() {
+
            for run in self.runs(repo_id) {
+
                if pred(run) {
+
                    items.push(Self::rss_item_from_run(run)?);
+
                }
+
            }
+
        }
+
        items.sort_by_key(|(ts, _)| *ts);
+
        items.reverse();
+
        Ok(items.iter().map(|(_, item)| item.clone()).collect())
    }

-
    fn rss_item_from_run(run: &Run) -> Result<Item, PageError> {
+
    fn rss_item_from_run(run: &Run) -> Result<(OffsetDateTime, Item), PageError> {
        let mut guid = Guid::default();
        guid.set_value(run.broker_run_id().to_string());
        guid.permalink = false; // guid permalink defaults to true as our guid is not a url set this to false
@@ -683,7 +713,7 @@ impl PageData {
        let ts = run.timestamp().to_string();
        let parsed =
            parse_timestamp(&ts).map_err(|err| PageError::RssTimestamp(ts.clone(), err))?;
-
        let ts = rfc822_timestamp(parsed).map_err(|err| PageError::RssTimestamp(ts, err))?;
+
        let ts = rfc822_timestamp(&parsed).map_err(|err| PageError::RssTimestamp(ts, err))?;

        let entry = RssEntry {
            repoid: run.repo_id(),
@@ -707,7 +737,7 @@ impl PageData {
            item.set_link(Some(url.into()));
        };

-
        Ok(item)
+
        Ok((parsed, item))
    }
}

modified src/util.rs
@@ -121,7 +121,7 @@ pub fn parse_timestamp(timestamp: &str) -> Result<OffsetDateTime, UtilError> {
    }
}

-
pub fn rfc822_timestamp(ts: OffsetDateTime) -> Result<String, UtilError> {
+
pub fn rfc822_timestamp(ts: &OffsetDateTime) -> Result<String, UtilError> {
    let ts = ts.format(&Rfc2822).map_err(UtilError::TimeFormat)?;
    Ok(ts.to_string())
}