Skip to content

Instantly share code, notes, and snippets.

View zhangxu's full-sized avatar
🏠
Working from home

ZhangXu zhangxu

🏠
Working from home
View GitHub Profile
@zhangxu
zhangxu / json-keys.sql
Last active October 13, 2015 22:51
Get JSON Keys - PostgreSQL
-- all json objects
select field_id,
parcel_tags, ranch_tags, aggregated_values from event_queries
where field_id in
(select field_id from event_queries limit 10);
-- all json keys
select distinct json_object_keys(parcel_tags) as keys
from event_queries
where field_id in
@zhangxu
zhangxu / events_export.scala
Last active October 12, 2015 23:32
Export events as CSV or Excel
import scalikejdbc._
import scalikejdbc.config._
import scalaz._, Scalaz._
import argonaut._, Argonaut._
GlobalSettings.loggingSQLAndTime = new LoggingSQLAndTimeSettings(
enabled = true,
singleLineMode = true,
logLevel = 'DEBUG
)
@zhangxu
zhangxu / realhost.scala
Created September 18, 2015 21:15
Real Host Spray Directive
import shapeless._
import spray.routing._
import Directives._
val realHost: Directive1[String] =
(optionalHeaderValueByName("X-Host") & headerValueByName("Host")).hmap {
case xhost :: host :: HNil => xhost.getOrElse(host)
}
@zhangxu
zhangxu / dispatch.scala
Last active August 29, 2015 14:27
Dispatch
import dispatch._, Defaults._
import scala.concurrent._, scala.concurrent.duration._
val svc = dispatch.url("http://casoilresource.lawr.ucdavis.edu/gmap/get_mapunit_data.php")
val lon = -120.997498
val lat = 37.937062
val asString = dispatch.as.Response( r => {r.getHeader("Content-Type")})
val r = Await.result(Http(svc <<? Map("lon" -> s"$lon", "lat" -> s"$lat") OK asString), 3 seconds)
@zhangxu
zhangxu / csv.rb
Created July 27, 2015 10:53
Rails CSV Processing
require 'csv'
text = File.read('/agls/data/sample.csv')
file = CSV.parse(text.gsub(/\r\n?/, "\n"))
rows = file.select { |r| r.length > 0 && ! r[0].starts_with?('#') && r[0].to_s.downcase != 'ranch name' }
row = rows.first
ranch_name, parcel_name, _, _, task_type, task_name, general_name, n, p, k, scheduled, completed, expected, actual, unit, status = row
@zhangxu
zhangxu / argonaut_json.scala
Last active August 29, 2015 14:23
Argonaut Decode/Encode Either
import argonaut._, Argonaut._
import scala.util._
import scalaz._, Scalaz._
val ranch1 = """{"id":1, "parcels":[1,2,3]}"""
val ranch2 = """{"id":2, "parcels":"*"}"""
case class RanchShared(id: Int, parcels: Either[String, List[Int]])
@zhangxu
zhangxu / ask-timeout.scala
Last active August 29, 2015 14:22
Ask Timeout
import akka.actor._
import akka.pattern.ask
import akka.util._
import play.api.libs.iteratee._
import scala.concurrent.duration._
import scala.concurrent._
import ExecutionContext.Implicits.global
val system = ActorSystem("ask-timeout")
@zhangxu
zhangxu / stackable-trait.scala
Last active May 10, 2019 11:45
Scala Stackable Trait
trait Base {
def greeting: String
def from: String
def greetingAgain: String = greeting + " Again"
}
trait Core extends Base {
override def greeting = "Hello"
override def from = "Patrick"
}
@zhangxu
zhangxu / actor-stash.scala
Last active August 29, 2015 14:21
Akka Actor Stash
import akka.actor._
implicit val system = ActorSystem("system")
class ActorStash extends Actor with Stash with ActorLogging {
def receive = {
case "open" =>
log.debug("Open received.")
unstashAll()
context.become({
@zhangxu
zhangxu / custom-play-iteratee.scala
Last active August 29, 2015 14:21
Custom play Iteratee
import play.api.libs.iteratee._
import scala.concurrent._
import ExecutionContext.Implicits.global
val e = Enumerator(1, 2, 3, 4, 5)
val total = new Iteratee[Int, Int] {
override def fold[B](folder: Step[Int, Int] => Future[B])(implicit ec: ExecutionContext): Future[B] = {
folder(Step.Cont(step(0)))
}