Merge branch 'feature/number-prefix'

Closes #11
This commit is contained in:
Valentin Brandl 2019-04-30 14:40:06 +02:00
commit 480a7672ec
No known key found for this signature in database
GPG Key ID: 30D341DD34118D7D
8 changed files with 49 additions and 21 deletions

7
Cargo.lock generated
View File

@ -807,6 +807,7 @@ dependencies = [
"futures 0.1.26 (registry+https://github.com/rust-lang/crates.io-index)",
"git2 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"number_prefix 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"openssl-probe 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"pretty_env_logger 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"reqwest 0.9.15 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1211,6 +1212,11 @@ dependencies = [
"libc 0.2.51 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "number_prefix"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "openssl"
version = "0.10.20"
@ -2428,6 +2434,7 @@ dependencies = [
"checksum num-traits 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)" = "92e5113e9fd4cc14ded8e499429f396a20f98c772a47cc8622a736e1ec843c31"
"checksum num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "0b3a5d7cc97d6d30d8b9bc8fa19bf45349ffe46241e8816f50f62f6d6aaabee1"
"checksum num_cpus 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1a23f0ed30a54abaa0c7e83b1d2d87ada7c3c23078d1d87815af3e3b6385fbba"
"checksum number_prefix 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "17b02fc0ff9a9e4b35b3342880f48e896ebf69f2967921fe8646bf5b7125956a"
"checksum openssl 0.10.20 (registry+https://github.com/rust-lang/crates.io-index)" = "5a0d6b781aac4ac1bd6cafe2a2f0ad8c16ae8e1dd5184822a16c50139f8838d9"
"checksum openssl-probe 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "77af24da69f9d9341038eba93a073b1fdaaa1b788221b00a69bce9e762cb32de"
"checksum openssl-sys 0.9.43 (registry+https://github.com/rust-lang/crates.io-index)" = "33c86834957dd5b915623e94f2f4ab2c70dd8f6b70679824155d5ae21dbd495d"

View File

@ -12,6 +12,7 @@ bytes = "0.4.12"
futures = "0.1.25"
git2 = "0.8.0"
lazy_static = "1.3.0"
number_prefix = "0.3.0"
openssl-probe = "0.1.2"
pretty_env_logger = "0.3.0"
reqwest = "0.9.15"

View File

@ -2,8 +2,6 @@
extern crate actix_web;
#[macro_use]
extern crate lazy_static;
extern crate reqwest;
extern crate serde_json;
#[macro_use]
extern crate serde_derive;
@ -25,6 +23,7 @@ use badge::{Badge, BadgeOptions};
use bytes::Bytes;
use futures::{unsync::mpsc, Stream};
use git2::Repository;
use number_prefix::{NumberPrefix, Prefixed, Standalone};
use std::{
fs::create_dir_all,
path::{Path, PathBuf},
@ -36,25 +35,32 @@ use structopt::StructOpt;
include!(concat!(env!("OUT_DIR"), "/templates.rs"));
const COMMIT: &str = env!("VERGEN_SHA_SHORT");
const VERSION: &str = env!("CARGO_PKG_VERSION");
pub struct VersionInfo<'a> {
pub commit: &'a str,
pub version: &'a str,
}
const VERSION_INFO: VersionInfo = VersionInfo {
commit: env!("VERGEN_SHA_SHORT"),
version: env!("CARGO_PKG_VERSION"),
};
lazy_static! {
static ref CLIENT: reqwest::Client = reqwest::Client::new();
static ref OPT: Opt = Opt::from_args();
static ref INDEX: Vec<u8> = {
let mut buf = Vec::new();
templates::index(&mut buf, COMMIT, VERSION, &OPT.domain).unwrap();
templates::index(&mut buf, VERSION_INFO, &OPT.domain).unwrap();
buf
};
static ref P404: Vec<u8> = {
let mut buf = Vec::new();
templates::p404(&mut buf, COMMIT, VERSION).unwrap();
templates::p404(&mut buf, VERSION_INFO).unwrap();
buf
};
static ref P500: Vec<u8> = {
let mut buf = Vec::new();
templates::p500(&mut buf, COMMIT, VERSION).unwrap();
templates::p500(&mut buf, VERSION_INFO).unwrap();
buf
};
}
@ -181,10 +187,14 @@ fn calculate_hoc<T: Service>(
}
pull(&path)?;
let (hoc, _) = hoc(&service_path, &state.repos, &state.cache)?;
let hoc = match NumberPrefix::decimal(hoc as f64) {
Standalone(hoc) => hoc.to_string(),
Prefixed(prefix, hoc) => format!("{:.1}{}", hoc, prefix),
};
let badge_opt = BadgeOptions {
subject: "Hits-of-Code".to_string(),
color: "#007ec6".to_string(),
status: hoc.to_string(),
status: hoc,
};
let badge = Badge::new(badge_opt)?;
@ -224,16 +234,20 @@ fn overview<T: Service>(
}
pull(&path)?;
let (hoc, head) = hoc(&service_path, &state.repos, &state.cache)?;
let hoc_pretty = match NumberPrefix::decimal(hoc as f64) {
Standalone(hoc) => hoc.to_string(),
Prefixed(prefix, hoc) => format!("{:.1}{}", hoc, prefix),
};
let mut buf = Vec::new();
let req_path = format!("{}/{}/{}", T::url_path(), data.0, data.1);
templates::overview(
&mut buf,
COMMIT,
VERSION,
VERSION_INFO,
&OPT.domain,
&req_path,
&url,
hoc,
&hoc_pretty,
&head,
&T::commit_url(&repo, &head),
)?;

View File

@ -1,4 +1,6 @@
@(title: &str, header: &str, content: Content, commit: &str, version: &str)
@use crate::VersionInfo;
@(title: &str, header: &str, content: Content, version_info: VersionInfo)
<!DOCTYPE html>
<html lang="en">
@ -32,7 +34,7 @@
<nav>
<ul>
<li>
<small>HoC v@version - <a href="https://github.com/vbrandl/hoc/commit/@commit">@commit</a></small>
<small>HoC v@version_info.version - <a href="https://github.com/vbrandl/hoc/commit/@version_info.commit">@version_info.commit</a></small>
</li>
</ul>
</nav>

View File

@ -1,6 +1,7 @@
@use super::base;
@use crate::VersionInfo;
@(commit: &str, version: &str, domain: &str)
@(version_info: VersionInfo, domain: &str)
@:base("Hits-of-Code Badges", "Hits-of-Code Badges", {
@ -60,4 +61,4 @@ my <a href="https://mirror.oldsql.cc/key.asc">GPG key</a>
(<a href="http://pool.sks-keyservers.net/pks/lookup?op=get&amp;search=0x1FFE431282F4B8CC0A7579167FB009175885FC76">from a
keyserver</a>), or by using any other UID from my key.
</p>
}, commit, version)
}, version_info)

View File

@ -1,11 +1,12 @@
@use super::base;
@use crate::VersionInfo;
@(commit: &str, version: &str, domain: &str, path: &str, url: &str, hoc: u64, head: &str, commit_url: &str)
@(version_info: VersionInfo, domain: &str, path: &str, url: &str, hoc: u64, hoc_pretty: &str, head: &str, commit_url: &str)
@:base("Hits-of-Code Badges", "Overview", {
<p>
The project at <a href="@url">@url</a> has <strong>@hoc</strong> hits of code at <a href="@commit_url">@head</a>.
The project <a href="@url">@url</a> has <strong>@hoc_pretty</strong> (exactly @hoc) hits of code at <a href="@commit_url">@head</a>.
</p>
<p>
@ -15,4 +16,4 @@ To include the badge in your readme, use the following markdown:
<pre>
[![Hits-of-Code](https://@domain/@path)](https://@domain/view/@path)
</pre>
}, commit, version)
}, version_info)

View File

@ -1,6 +1,7 @@
@use super::base;
@use crate::VersionInfo;
@(commit: &str, version: &str)
@(version_info: VersionInfo)
@:base("Page not Found - Hits-of-Code Badges", "404 - Page not Found", {
<p>
@ -10,4 +11,4 @@
<p>
If you think, this is a mistake on my side, please <a href="mailto:mail+hoc@@vbrandl.net">drop me a mail</a>.
</p>
}, commit, version)
}, version_info)

View File

@ -1,6 +1,7 @@
@use super::base;
@use crate::VersionInfo;
@(commit: &str, version: &str)
@(version_info: VersionInfo)
@:base("Internal Server Error - Hits-of-Code Badges", "500 - Internal Server Error", {
<p>
@ -10,4 +11,4 @@
<p>
If you think, this is a bug, please <a href="mailto:mail+hoc@@vbrandl.net">drop me a mail</a>.
</p>
}, commit, version)
}, version_info)