Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add timeline packages #1

Merged
merged 24 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.vscode/
.direnv/
.envrc
result
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Changelog

## 0.1.0.0
- Open source the timeline library we use internally at Bellroy
29 changes: 29 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Copyright (C) 2023 Bellroy Pty Ltd

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the
distribution.

3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 changes: 40 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,42 @@
# timeline

Provides a container type `Timeline a` for handling data that changes over time.
## Motivation

The world is always changing, and often we want to manage the changes of data
using computers. Below are some concrete examples:

- Employee data such as compensation, city, tax rule, time-off, etc.
- Prices of products. A product could have different prices on Amazon and EBay,
and in different currencies.

Timeline data is often implemented by attaching extra fields to your business
object, denoting the start and end time of each interval. However, only
representing and storing the data is not sufficient, we need to run operations
on timeline data, like extracting a single data point at some specific time,
merging multiple timelines together, etc.

If you have a similar use case and don't want to reinvent the wheel, this
library is for you.

## Package Organization

- `timeline` essential types and functions
- `timeline-tests` unit tests
- `timeline-hedgehog` hedgehog generators for timeline types

## Getting Started

The core type is `Timeline a`, refer to
[Haddock](https://hackage.haskell.org/package/timeline-0.0.1.0/docs/Data-Timeline.html)
for its usage.

## Contribution
Bellroy actively maintains this project. Feel free to submit issues and
pull requests!

The code is formatted with [`ormolu`](https://hackage.haskell.org/package/ormolu)

If you use Nix:
- `nix develop` enter a shell with all necessary tools
- `nix build` build and run tests on all GHC versions we support
- Use `nix flake show` to view a full list of outputs
5 changes: 5 additions & 0 deletions cabal.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
packages:
./timeline
./timeline-hedgehog
./timeline-tests
tests: True
59 changes: 59 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

101 changes: 101 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
{
# Use 'nix flake show' to discover the structure of the output.
# Multiple versions of compiler is supported.
inputs = {
nixpkgs.url = "nixpkgs/nixpkgs-unstable";
flake-utils.url = "github:numtide/flake-utils";
flake-compat = {
url = "github:edolstra/flake-compat";
flake = false;
};
};

outputs = inputs:
let
cabalPackages = [ "timeline" "timeline-hedgehog" "timeline-tests" ];
supportedCompilers = [ "ghc8107" "ghc926" "ghc944" ];
defaultCompiler = "ghc926";
kokobd marked this conversation as resolved.
Show resolved Hide resolved
in
inputs.flake-utils.lib.eachDefaultSystem (system:
let
nixpkgs = import inputs.nixpkgs { inherit system; };

makePackageSet = haskellPackages: haskellPackages.override {
overrides = final: prev: with nixpkgs.haskell.lib;
builtins.listToAttrs
(
builtins.map
(name: {
inherit name;
value = prev.callPackage (./. + "/${name}") { };
})
cabalPackages
);
};

essentialTools = with nixpkgs; [
cabal-install
hlint
ormolu
haskellPackages.cabal-fmt
cabal2nix
miniserve
];

makeShell = haskellPackages: (makePackageSet haskellPackages).shellFor {
packages = p: builtins.map (name: p.${name}) cabalPackages;
withHoogle = true;
buildInputs = essentialTools ++ [
nixpkgs.haskellPackages.haskell-language-server
];
};

lightShell = nixpkgs.mkShell {
packages = essentialTools ++ [ nixpkgs.ghc ];
};
in
{
packages =
let packagesWithoutDefault =
builtins.listToAttrs
(
builtins.concatMap
(compilerName:
let pkgSet = makePackageSet nixpkgs.haskell.packages.${compilerName};
in
builtins.map
(name: {
name = "${compilerName}-${name}";
value = pkgSet.${name};
})
cabalPackages
)
supportedCompilers
);
in
packagesWithoutDefault // {
default = nixpkgs.runCommand "aggregate"
{
buildInputs = builtins.map (name: packagesWithoutDefault.${name})
(builtins.attrNames packagesWithoutDefault);
} "touch $out";
};

devShells =
let devShellsWithoutDefault =
builtins.listToAttrs
(
builtins.map
(compilerName: {
name = compilerName;
value = makeShell nixpkgs.haskell.packages.${compilerName};
})
supportedCompilers
); in
devShellsWithoutDefault // {
default = devShellsWithoutDefault.${defaultCompiler};
light = lightShell;
};
}
);
}
Empty file added timeline-hedgehog/CHANGELOG.md
Empty file.
29 changes: 29 additions & 0 deletions timeline-hedgehog/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Copyright (C) 2023 Bellroy Pty Ltd

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the
distribution.

3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3 changes: 3 additions & 0 deletions timeline-hedgehog/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# timeline

Provides a container type `Timeline a` for handling data that changes over time.
9 changes: 9 additions & 0 deletions timeline-hedgehog/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{ mkDerivation, base, hedgehog, lib, time, timeline }:
mkDerivation {
pname = "timeline-hedgehog";
version = "0.1.0.0";
src = ./.;
libraryHaskellDepends = [ base hedgehog time timeline ];
description = "Hedgehog generators for the timeline library";
license = lib.licenses.bsd3;
}
54 changes: 54 additions & 0 deletions timeline-hedgehog/src/Data/Timeline/Hedgehog.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{-# LANGUAGE ImportQualifiedPost #-}
{-# LANGUAGE NamedFieldPuns #-}

-- |
-- Hedgehog generators for the timeline library.
module Data.Timeline.Hedgehog
( -- * Timeline Generators
gen,
genRecord,

-- * Helpers
genUTCTime,
)
where

import Data.Time (UTCTime (..), fromGregorian, secondsToDiffTime)
import Data.Timeline
import Hedgehog (MonadGen)
import Hedgehog.Gen qualified as Gen
import Hedgehog.Range qualified as Range

-- | Generator for @'Timeline' a@
gen ::
(MonadGen m) =>
-- | Generator for values
m a ->
m (Timeline a)
gen genValue = do
initialValue <- genValue
values <- Gen.map (Range.linear 0 20) $ (,) <$> genUTCTime <*> genValue
pure Timeline {initialValue, values}

-- | Generator for @'Record' a@
genRecord ::
(MonadGen m) =>
-- | Generator for the value
m a ->
m (Record a)
genRecord valueGen =
Gen.justT $ do
t1 <- genUTCTime
t2 <- Gen.maybe $ Gen.filterT (/= t1) genUTCTime
makeRecord t1 t2 <$> valueGen

-- | A 'UTCTime' generator
genUTCTime :: (MonadGen m) => m UTCTime
genUTCTime = do
y <- toInteger <$> Gen.int (Range.constant 2000 2030)
m <- Gen.int (Range.constant 1 12)
d <- Gen.int (Range.constant 1 28)
let day = fromGregorian y m d
secs <- toInteger <$> Gen.int (Range.constant 0 86401)
let diff = secondsToDiffTime secs
pure $ UTCTime day diff
32 changes: 32 additions & 0 deletions timeline-hedgehog/timeline-hedgehog.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
cabal-version: 2.2
name: timeline-hedgehog
version: 0.1.0.0
synopsis: Hedgehog generators for the timeline library
license: BSD-3-Clause
license-file: LICENSE
author: Bellroy Tech Team <[email protected]>
maintainer: Bellroy Tech Team <[email protected]>
category: Development
build-type: Simple
tested-with: GHC ==8.10.7 || ==9.2.6 || ==9.4.4
extra-source-files:
CHANGELOG.md
README.md

source-repository head
type: git
location: https://github.com/bellroy/timeline.git

common deps
build-depends:
, base >=4.14.3 && <4.18
, hedgehog >=1.1 && <1.3
, time >=1.9.3 && <1.13
, timeline ==0.1.0.0

library
import: deps
hs-source-dirs: src/
exposed-modules: Data.Timeline.Hedgehog
default-language: Haskell2010
ghc-options: -fwarn-unused-imports -Wall -fno-warn-unused-do-bind
Empty file added timeline-tests/CHANGELOG.md
Empty file.
Loading