Skip to content

Instantly share code, notes, and snippets.

View clarkmcc's full-sized avatar

Clark McCauley clarkmcc

View GitHub Profile
@clarkmcc
clarkmcc / device.ts
Last active September 16, 2019 16:18
AWS IoT Device - Connect over port 443
/**
* Creates a new instance of the iot.device class that we can connect to and listen on
* @private
* @type {*}
* @memberof Device
*/
private _device: any = iot.device({
keyPath: __dirname + "device.private.key",
certPath: __dirname + "device.cert.pem",
caPath: __dirname + "root-CA.crt", // https://www.amazontrust.com/repository/AmazonRootCA1.pem
@clarkmcc
clarkmcc / mr-test.ts
Created September 16, 2019 21:11
Test New Meter Read Format
const myMeterRead = new MeterRead({
'reportDtTm' : null,
'pageCounts' : {
'equiv' : {
'total' : {
'displayName' : 'Total',
'value' : 6660
},
'totalBlack' : {
'displayName' : 'Total Black',
@clarkmcc
clarkmcc / getModuleVersions.ts
Created October 29, 2019 01:41
This method is attached to a server-side inversify service that retrieves a list of executable's depending on the provided parameters. It returns a list of those executables (after parsing them) to the called. This snippet makes use of several string methods (split, pop, replace) as well as array methods (filter, forEach).
public async getModuleVersions(modulePrefix: string): Promise<any> {
try {
let fileExtension: string;
let moduleLocation: string;
switch (modulePrefix) {
case 'module1':
fileExtension = 'exe';
moduleLocation = '';
break;
@clarkmcc
clarkmcc / util.go
Created January 13, 2020 22:34
Accepts a slice of strings and returns a slice of slices of strings where each child slices's length is the length of the size parameter
package util
// Accepts a slice of strings and returns a slice of slices of strings where each child slices's length
// is the length of the batchSize
func StringBatch(size int, s []string) [][]string {
var b [][]string
for size < len(s) {
s, b = s[size:], append(b, s[0:size:size])
}
b = append(b, s)
@clarkmcc
clarkmcc / aggregate.go
Created January 13, 2020 22:46
Handy functions for creating MongoDB aggregate pipelines in Go
package aggregate
import "go.mongodb.org/mongo-driver/bson"
type Operation bson.M
// The Pipe functions similar to the RxJS pipe function
// (https://rxjs-dev.firebaseapp.com/api/index/function/pipe) in that
// it accepts a set of functions as parameters. Each function receives
// as its input, the output of the previous function. The allows you
@clarkmcc
clarkmcc / startup.go
Last active March 5, 2020 17:53
This package is a dependency installer for Golang. Dependencies can be registered with the startup.Engine, checked, and installed.
package startup
import (
"fmt"
"log"
"sync"
)
const BufferedChan int = 1
const FailedToInstallDependency = "failed to install dependency"
@clarkmcc
clarkmcc / get_arch_windows.go
Created March 5, 2020 17:56
Golang function to determine whether a windows machine is 32 bit or 64 bit.
func GetArch() (string, *errors.ErrorCode) {
cmd := exec.Command(
"wmic",
"os",
"get",
"osarchitecture",
)
output, err := cmd.CombinedOutput()
if err != nil {
@clarkmcc
clarkmcc / get_arch_darwin.go
Created March 5, 2020 17:58
Golang function to determine whether a MacOS machine is 32 bit or 64 bit
func GetArch() (string, *errors.ErrorCode) {
cmd := exec.Command(
"getconf",
"LONG_BIT",
)
output, err := cmd.CombinedOutput()
if err != nil {
log.Printf("error getting arch: %v", err)
return "", errors.New(errors.UnsupportedArchitecture)
@clarkmcc
clarkmcc / throttler.go
Last active March 19, 2020 04:36
Throttler implements a leaky bucket request throttling algorithm where the duration between two requests is never less than the MinInterval
type ThrottleEngine struct {
// The key to this map represents a unique user being throttled, such as
// an api key, or some user identification obtained from a header
MostRecentRequest map[string]time.Time
MinInterval time.Duration
M *sync.Mutex
}
type HttpHandlerFunc func(http.ResponseWriter, *http.Request)
@clarkmcc
clarkmcc / byte_counter.go
Last active March 19, 2020 04:52
Used for tracking metadata about a request and dumping it into a data warehouse such as Google BigQuery
package counter
import (
"cloud.google.com/go/civil"
"context"
"fmt"
"gitlab.com/ptmesh/api/pkg/repositories"
"go.mongodb.org/mongo-driver/bson/primitive"
"net/http"
"sync"