From 330e4363d8abb509031d2c8c1a89dcc6f955e2c1 Mon Sep 17 00:00:00 2001 From: Ben Sima Date: Fri, 4 Dec 2020 11:16:25 -0500 Subject: Renamespace Devalloc and Que Move them under the Biz root so that we know they are specific to Biz stuff. Biz is for proprietary stuff that we own. I also had to refactor the bild namespace parsing code because it couldn't handle a namespace with 3 parts. I really need to get that namespace library written and tested. --- Biz/Devalloc/Host.hs | 122 ++++++++++++++++++++++++ Biz/Devalloc/Host.nix | 46 +++++++++ Biz/Devalloc/Page/Home.hs | 95 +++++++++++++++++++ Biz/Devalloc/Page/Signup.hs | 46 +++++++++ Biz/Devalloc/main.py | 221 ++++++++++++++++++++++++++++++++++++++++++++ Biz/Devalloc/pitch.md | 40 ++++++++ 6 files changed, 570 insertions(+) create mode 100644 Biz/Devalloc/Host.hs create mode 100644 Biz/Devalloc/Host.nix create mode 100644 Biz/Devalloc/Page/Home.hs create mode 100644 Biz/Devalloc/Page/Signup.hs create mode 100755 Biz/Devalloc/main.py create mode 100644 Biz/Devalloc/pitch.md (limited to 'Biz/Devalloc') diff --git a/Biz/Devalloc/Host.hs b/Biz/Devalloc/Host.hs new file mode 100644 index 0000000..5a9ff80 --- /dev/null +++ b/Biz/Devalloc/Host.hs @@ -0,0 +1,122 @@ +{-# LANGUAGE DataKinds #-} +{-# LANGUAGE DeriveGeneric #-} +{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE TypeApplications #-} +{-# LANGUAGE TypeOperators #-} +{-# LANGUAGE NoImplicitPrelude #-} +{-# OPTIONS_GHC -fno-warn-orphans #-} + +-- Developer allocation +-- +-- : out devalloc-host +-- : dep clay +-- : dep cmark +-- : sys cmark +-- : dep envy +-- : dep lucid +-- : dep miso +-- : dep protolude +-- : dep servant +-- : dep servant-server +-- : dep warp +module Biz.Devalloc.Host + ( main, + ) +where + +import Alpha +import Biz.App (CSS (..), HtmlApp (..)) +-- import qualified CMark as Cmark + +import qualified Biz.Devalloc.Page.Home as Home +import qualified Biz.Devalloc.Page.Signup as Signup +import qualified Biz.Look +import qualified Clay +import qualified Control.Exception as Exception +import qualified Lucid +import qualified Lucid.Base as Lucid +import Miso hiding (node) +import qualified Network.Wai as Wai +import qualified Network.Wai.Handler.Warp as Warp +import Network.Wai.Middleware.RequestLogger (logStdout) +import Servant +import qualified System.Envy as Envy + +main :: IO () +main = Exception.bracket startup shutdown run + where + startup = + Envy.decodeWithDefaults Envy.defConfig >>= \cfg -> do + -- pitchText <- readFile <| pitches cfg + -- let pitch = Cmark.commonmarkToHtml [] pitchText + putText "@" + putText "devalloc" + putText <| "port: " <> (show <| port cfg) + return (cfg, serve (Proxy @AllRoutes) <| serverHandlers) + shutdown :: (Config, Application) -> IO () + shutdown _ = pure () + run :: (Config, Wai.Application) -> IO () + run (cfg, app) = Warp.run (port cfg) (logStdout app) + +type HomeServer = ToServerRoutes Home.Path HtmlApp Home.Move + +type SignupServer = ToServerRoutes Signup.Path HtmlApp Signup.Move + +type AllRoutes = HomeServer :<|> SignupServer :<|> CssRoute + +type CssRoute = "css" :> "main.css" :> Get '[CSS] Text + +cssHandlers :: Server CssRoute +cssHandlers = return . toStrict <| Clay.render look + +instance Lucid.ToHtml a => Lucid.ToHtml (HtmlApp a) where + toHtmlRaw = Lucid.toHtml + toHtml (HtmlApp x) = + Lucid.doctypehtml_ <| do + Lucid.head_ <| do + Lucid.meta_ [Lucid.charset_ "utf-8"] + jsRef "/static/all.js" + jsRef "//unpkg.com/turbolinks@5.2.0/dist/turbolinks.js" + cssRef "/css/main.css" + Lucid.body_ (Lucid.toHtml x) + where + jsRef _href = + Lucid.with + (Lucid.script_ mempty) + [ Lucid.makeAttribute "src" _href, + Lucid.makeAttribute "async" mempty, + Lucid.makeAttribute "defer" mempty + ] + cssRef _href = + Lucid.with + (Lucid.link_ mempty) + [ Lucid.rel_ "stylesheet", + Lucid.type_ "text/css", + Lucid.href_ _href + ] + +data Config = Config + { port :: Warp.Port, + -- | A yaml file of pitches + pitches :: FilePath, + node :: FilePath + } + deriving (Generic, Show) + +instance Envy.DefConfig Config where + defConfig = + Config + { port = 3000, + pitches = "./Biz/Devalloc/pitch.md", + node = "_/bild/dev/Devalloc.Node/static" + } + +instance Envy.FromEnv Config + +serverHandlers :: Server AllRoutes +serverHandlers = Home.host :<|> Signup.host :<|> cssHandlers + +look :: Clay.Css +look = do + Biz.Look.fuckingStyle + "body" Clay.? Biz.Look.fontStack diff --git a/Biz/Devalloc/Host.nix b/Biz/Devalloc/Host.nix new file mode 100644 index 0000000..51aa85d --- /dev/null +++ b/Biz/Devalloc/Host.nix @@ -0,0 +1,46 @@ +{ options +, lib +, config +, pkgs +, modulesPath +}: + +let + cfg = config.services.devalloc-host; +in +{ + options.services.devalloc-host = { + enable = lib.mkEnableOption "Enable the devalloc-host service"; + port = lib.mkOption { + type = lib.types.int; + default = 3000; + description = '' + The port on which devalloc-host will listen for + incoming HTTP traffic. + ''; + }; + package = lib.mkOption { + type = lib.types.package; + description = "devalloc-host package to use"; + }; + }; + config = lib.mkIf cfg.enable { + systemd.services.devalloc-host = { + path = [ cfg.package ]; + wantedBy = [ "multi-user.target" ]; + script = '' + ${cfg.package}/bin/devalloc-host + ''; + description = '' + Devalloc.Host + ''; + serviceConfig = { + Environment = ["PORT=${toString cfg.port}"]; + KillSignal = "INT"; + Type = "simple"; + Restart = "on-abort"; + RestartSec = "1"; + }; + }; + }; +} diff --git a/Biz/Devalloc/Page/Home.hs b/Biz/Devalloc/Page/Home.hs new file mode 100644 index 0000000..9e90e70 --- /dev/null +++ b/Biz/Devalloc/Page/Home.hs @@ -0,0 +1,95 @@ +{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE NoImplicitPrelude #-} + +module Biz.Devalloc.Page.Home + ( Move (..), + Path, + Form (..), + path, + view, + host, + ) +where + +import Alpha +import Biz.App (HtmlApp (..)) +import Miso +import Miso.String +import Servant.API +import Servant.Links +import Servant.Server (Handler) + +data Move = NoMove + +type Path = View Move + +newtype Form = Form + { uri :: URI + } + +path :: URI +path = linkURI <| safeLink (Proxy :: Proxy Path) (Proxy :: Proxy Path) + +host :: Handler (HtmlApp (View Move)) +host = + Form {uri = path} + |> view + |> HtmlApp + |> pure + +signup :: View Move +signup = + p_ + [] + [ a_ + [href_ "mailto:ben@bsima.me?subject=Devalloc+signup"] + [text "Request access via email"] + ] + +view :: Form -> View Move +view _ = + div_ + [] + [ h1_ [] [text "Devalloc"], + p_ + [] + [ text + "Devalloc analyzes your codebase trends, finds patterns \ + \ in how your developers work, and protects against tech debt." + ], + p_ [] [text "Just hook it up to your CI system - it will warn you when it finds a problem."], + signup, + h2_ [] [text "Identify blackholes in your codebase"], + p_ + [] + [ text + <| Miso.String.intercalate + " " + [ "What if none of your active employees have touched some part of the codebase?", + "This happens too often with legacy code, and then it turns into a huge source of tech debt.", + "Devalloc finds these \"blackholes\" and warns you about them so you can be proactive in eliminating tech debt." + ] + ], + h2_ + [] + [text "Protect against lost knowledge"], + p_ + [] + [text "Not everyone can know every part of a codebase. By finding pieces of code that only 1 or 2 people have touched, devalloc identifes siloed knowledge. This allows you to protect against the risk of this knowledge leaving the company if an employee leaves."], + h2_ + [] + [text "Don't just measure code coverage - also know your dev coverage"], + p_ + [] + [text "No matter how smart your employees are, if you are under- or over-utilizing your developers then you will never get optimal performance from your team."], + ul_ + [] + [ li_ [] [text "Find developer hot spots in your code: which pieces of code get continually rewritten, taking up valuable dev time?"], + li_ [] [text "Know how your devs work best: which ones have depth of knowledge, and which ones have breadth?"] + ], + p_ [] [text "(Paid only)"], + h2_ [] [text "See how your teams *actually* organize themselves with cluster analysis"], + p_ [] [text "Does your team feel splintered or not cohesive? Which developers work best together? Devalloc analyzes the collaboration patterns between devs and helps you form optimal pairings and teams based on shared code and mindspace."], + p_ [] [text "(Paid only)"], + signup + ] diff --git a/Biz/Devalloc/Page/Signup.hs b/Biz/Devalloc/Page/Signup.hs new file mode 100644 index 0000000..8debf53 --- /dev/null +++ b/Biz/Devalloc/Page/Signup.hs @@ -0,0 +1,46 @@ +{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE NoImplicitPrelude #-} + +module Biz.Devalloc.Page.Signup + ( Move (..), + Path, + Form (..), + path, + view, + host, + ) +where + +import Alpha +import Biz.App (HtmlApp (..)) +import Miso +import Miso.String +import Servant.API +import Servant.Links +import Servant.Server (Handler) + +data Move = NoMove + +type Path = View Move + +newtype Form = Form + { uri :: URI + } + +path :: URI +path = linkURI <| safeLink (Proxy :: Proxy Path) (Proxy :: Proxy Path) + +host :: Handler (HtmlApp (View Move)) +host = + Form {uri = path} + |> view + |> HtmlApp + |> pure + +view :: Form -> View Move +view _ = + div_ + [] + [ h1_ [] [text "Signup coming soon"], + p_ [] [a_ [href_ "/"] [text "Go back home"]] + ] diff --git a/Biz/Devalloc/main.py b/Biz/Devalloc/main.py new file mode 100755 index 0000000..bb10441 --- /dev/null +++ b/Biz/Devalloc/main.py @@ -0,0 +1,221 @@ +#!/usr/bin/env python +""" +Analyze developer allocation across a codebase. +""" + +import argparse +import datetime +import logging +import os +import re +import subprocess +import sys + + +def find_user(line): + """Given 'Ben Sima ', finds `Ben Sima'. Returns the first + matching string.""" + return re.findall(r"^[^<]*", line)[0].strip() + + +def authors_for(path, active_users): + """Return a dictionary of {author: commits} for given path. Usernames not in + the 'active_users' list will be filtered out.""" + raw = subprocess.check_output( + ["git", "shortlog", "--numbered", "--summary", "--email", "--", path] + ).decode("utf-8") + lines = [s for s in raw.split("\n") if s] + data = {} + for line in lines: + parts = line.strip().split("\t") + author = find_user(parts[1]) + commits = parts[0] + if author in active_users: + data[author] = commits + return data + + +def mailmap_users(): + """Returns users from the .mailmap file.""" + users = [] + with open(".mailmap") as file: + lines = file.readlines() + for line in lines: + users.append(find_user(line)) + return users + + +MAX_SCORE = 10 + + +def score(blackhole, liability, good, total): + "Calculate the score." + weights = { + "blackhole": 0.5, + "liability": 0.7, + } + return ( + MAX_SCORE + * ( + (blackhole * weights["blackhole"]) + + (liability * weights["liability"]) + + good + ) + / total + ) + + +def get_args(): + "Parse CLI arguments." + cli = argparse.ArgumentParser(description=__doc__) + cli.add_argument("repo", default=".", help="the git repo to run on", metavar="REPO") + cli.add_argument( + "-b", + "--blackholes", + action="store_true", + help="print the blackholes (files with one or zero active contributors)", + ) + cli.add_argument( + "-l", + "--liabilities", + action="store_true", + help="print the liabilities (files with < 3 active contributors)", + ) + cli.add_argument( + "-s", + "--stale", + action="store_true", + help="print stale files (haven't been touched in 6 months)", + ) + cli.add_argument( + "-i", "--ignored", nargs="+", default=[], help="patterns to ignore in paths", + ) + cli.add_argument( + "--active-users", + nargs="+", + default=[], + help="list of active user emails. if not provided, this is loaded from .mailmap", + ) + cli.add_argument( + "-v", + "--verbosity", + help="set the log level verbosity", + choices=["debug", "warning", "error"], + default="error", + ) + return cli.parse_args() + + +def guard_git(repo): + "Guard against non-git repos." + is_git = subprocess.run( + ["git", "rev-parse"], + stderr=subprocess.PIPE, + stdout=subprocess.PIPE, + check=False, + ).returncode + if is_git != 0: + sys.exit(f"error: not a git repository: {repo}") + + +def staleness(path, now): + "How long has it been since this file was touched?" + timestamp = datetime.datetime.strptime( + subprocess.check_output(["git", "log", "-n1", "--pretty=%aI", path]) + .decode("utf-8") + .strip(), + "%Y-%m-%dT%H:%M:%S%z", + ) + delta = now - timestamp + return delta.days + + +class Repo: + "Represents a repo and stats for the repo." + + def __init__(self, ignored_paths, active_users): + self.paths = [ + p + for p in subprocess.check_output(["git", "ls-files", "--no-deleted"]) + .decode("utf-8") + .split() + if not any(i in p for i in ignored_paths) + ] + logging.debug("collecting stats") + self.stats = {} + for path in self.paths: + self.stats[path] = authors_for(path, active_users) + self.blackholes = [path for path, authors in self.stats.items() if not authors] + self.liabilities = { + path: list(authors) + for path, authors in self.stats.items() + if 1 <= len(authors) < 3 + } + now = datetime.datetime.utcnow().astimezone() + self.stale = {} + for path, _ in self.stats.items(): + _staleness = staleness(path, now) + if _staleness > 180: + self.stale[path] = _staleness + + def print_blackholes(self, full): + "Print number of blackholes, or list of all blackholes." + # note: file renames may result in false positives + n_blackhole = len(self.blackholes) + print(f"Blackholes: {n_blackhole}") + if full: + for path in self.blackholes: + print(f" {path}") + + def print_liabilities(self, full): + "Print number of liabilities, or list of all liabilities." + n_liabilities = len(self.liabilities) + print(f"Liabilities: {n_liabilities}") + if full: + for path, authors in self.liabilities.items(): + print(f" {path} ({', '.join(authors)})") + + def print_score(self): + "Print the overall score." + n_total = len(self.stats.keys()) + n_blackhole = len(self.blackholes) + n_liabilities = len(self.liabilities) + n_good = n_total - n_blackhole - n_liabilities + print("Total:", n_total) + print( + "Score: {:.2f}/{}".format( + score(n_blackhole, n_liabilities, n_good, n_total), MAX_SCORE + ) + ) + + def print_stale(self, full): + "Print stale files" + n_stale = len(self.stale) + print(f"Stale files: {n_stale}") + if full: + for path, days in self.stale.items(): + print(f" {path} ({days} days)") + + +if __name__ == "__main__": + ARGS = get_args() + logging.basicConfig(stream=sys.stderr, level=ARGS.verbosity.upper()) + + logging.debug("starting") + os.chdir(os.path.abspath(ARGS.repo)) + + guard_git(ARGS.repo) + + # if no active users provided, load from .mailmap + if ARGS.active_users == []: + if os.path.exists(".mailmap"): + ARGS.active_users = mailmap_users() + + # collect data + REPO = Repo(ARGS.ignored, ARGS.active_users) + + # print data + REPO.print_score() + REPO.print_blackholes(ARGS.blackholes) + REPO.print_liabilities(ARGS.liabilities) + REPO.print_stale(ARGS.stale) diff --git a/Biz/Devalloc/pitch.md b/Biz/Devalloc/pitch.md new file mode 100644 index 0000000..cfc0b23 --- /dev/null +++ b/Biz/Devalloc/pitch.md @@ -0,0 +1,40 @@ +# Devalloc + +Devalloc analyzes your codebase trends, finds patterns in how your developers +work, and protects against tech debt. + +Just hook it up to your CI system - it will warn you when it finds a problem. + +## Identify blackholes in your codebase + +What if none of your active employees have touched some part of the codebase? +This happens too often with legacy code, and then it turns into a huge source of +tech debt. Devalloc finds these "blackholes" and warns you about them so you +can be proactive in eliminating tech debt. + +## Protect against lost knowledge + +Not everyone can know every part of a codebase. By finding pieces of code +that only 1 or 2 people have touched, devalloc identifes siloed knowledge. This +allows you to protect against the risk of this knowledge leaving the company if +an employee leaves. + +## Don't just measure "code coverage" - also know your "dev coverage" + +No matter how smart your employees are, if you are under- or over-utilizing your +developers then you will never get optimal performance from your team. + +- Find developer "hot spots" in your code: which pieces of code get continually + rewritten, taking up valuable dev time? +- Know how your devs work best: which ones have depth of knowledge, and which + ones have breadth? + +(Paid only) + +## See how your teams *actually* organize themselves with cluster analysis + +Does your team feel splintered or not cohesive? Which developers work best +together? Devalloc analyzes the collaboration patterns between devs and helps +you form optimal pairings and teams based on shared code and mindspace. + +(Paid only) -- cgit v1.2.3