Skip to content

Instantly share code, notes, and snippets.

View huntc's full-sized avatar

Christopher Hunt huntc

View GitHub Profile
### Keybase proof
I hereby claim:
* I am huntc on github.
* I am huntc (https://keybase.io/huntc) on keybase.
* I have a public key ASAK5MC9gaWxTBKWnZcUlTofK-51TIDi2R-UDtMGGUh_7Ao
To claim this, I am signing this object:
@huntc
huntc / esliteImpl.scala
Created March 10, 2016 22:42
Sample of using Scala with the Java DSL of Lagom
package com.lightbend.conductr.eslite
import javax.inject.Named
import akka.NotUsed
import akka.actor.ActorRef
import akka.pattern.ask
import com.fasterxml.jackson.databind.node.ObjectNode
import com.google.inject.Inject
import com.lightbend.conductr.eslite.dto._
@huntc
huntc / server.scala
Last active December 12, 2017 04:45
A complete server using Akka streams that reads some source, batches its data and then publishes. If the data cannot be published then it backs off with a best-effort of sending that data again.
val (recycleQueue, recycleSource) =
Source
.queue[SoilStateReading](100, OverflowStrategy.dropTail)
.prefixAndTail(0)
.map(_._2)
.toMat(Sink.head)(Keep.both)
.run()
StreamConverters.fromInputStream(() => this.getClass.getClassLoader.getResourceAsStream("sensors.log"))
.via(SoilStateReading.csvParser)
.merge(Source.fromFutureSource(recycleSource))
import akka.NotUsed
import akka.stream.scaladsl.Flow
case object NotZero
// Emits either an Int when zero, or a domain object indicating
// that the Int is non zero. Use this approach instead of relying
// on exceptions. Exceptions are for exceptional conditions,
// largely unanticipated, and they will terminate a stream.
val flow: Flow[Int, Either[NotZero.type, Int], NotUsed] =
private val tracerConfig = new Configuration(
config.getString("lora-server.jaeger.service-name"),
if (config.getBoolean("lora-server.jaeger.constant-sampling"))
new SamplerConfiguration("const", 1)
else new SamplerConfiguration(),
new ReporterConfiguration(
true, // logSpans
config.getString("lora-server.jaeger.agent-host"),
config.getInt("lora-server.jaeger.agent-port"),
config.getDuration("lora-server.jaeger.flush-interval").toMillis.toInt,
@huntc
huntc / events.scala
Last active July 13, 2018 04:59
OO inheritance for data modelling - describes types of events that share a common identifier
sealed abstract class Event(val id: Int)
final case class CountersUpdated(override val id: Int,
counter: Option[Int])
extends Event(id)
final case class DeviceIdUpdated(override val id: Int, deviceId: Int) extends Event(id)
@huntc
huntc / SemVer.scala
Last active March 22, 2019 18:40
Semantic version case class
import scala.util.Try
type PreRelease = Either[String, Int]
object SemVer {
val pattern = """^(\d+)\.(\d+)\.(\d+)(-(([1-9a-zA-Z][0-9a-zA-Z-]*)(\.([1-9a-zA-Z][0-9a-zA-Z-]*))))?$""".r
def apply(s: String): SemVer =
s match {
case pattern(major, minor, patch, _, _, preRelease1, _, preRelease2) =>
@huntc
huntc / hyquest.ino
Last active May 12, 2019 06:08
LoRaWAN Arduino integration for the Hyquest rain water sensor
/*
* Copyright 2018 Titan Class Pty Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@huntc
huntc / main.rs
Last active January 1, 2020 10:04
Some Rust to read a CSV file and fold over its lines - seems significantly slower than its Scala counterpart without having optimised it
use std::env;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::os::raw::c_double;
use chrono::{Duration, NaiveDateTime};
struct State {
x0: Option<(NaiveDateTime, c_double)>,
x1: Option<(NaiveDateTime, c_double)>,
@huntc
huntc / DashboardEvents.scala
Last active May 25, 2020 01:32
An example of sourcing and persisting state in Streambed
val storageLoader = storage
.load(LatestDashboardsEvents.Codec(
principal.getSecret(DashboardsEvents.EventsKey)),
latestDashboardsEventsId)
.map { maybeSnapshot =>
maybeSnapshot.getOrElse(LatestDashboardsEvents(List.empty, None))
}
def tailFromOffset(
offset: Option[Long],