Skip to content

Instantly share code, notes, and snippets.

View goodpic's full-sized avatar

Jun Kaneko goodpic

View GitHub Profile
@goodpic
goodpic / drop.js
Last active September 15, 2018 22:16
Node.js client to delete BigQuery table
const program = require('commander');
const { deleteTable } = require('../src/bigquery');
program
.version('0.1.0')
.option('-t, --table [table]', 'Specify the table to drop', '')
.option('-d, --dataset [datasetId]', 'Specify the dataset of the table to drop.', '')
.parse(process.argv);
@goodpic
goodpic / create.js
Created September 15, 2018 22:02
Commander.js command line to create a new BigQuery table
const program = require('commander');
const { createTable } = require('../src/bigquery');
const { SchemaSalesByHour } = require('../schemas/SchemaSalesByHour');
program
.version('0.1.0')
.option('-t, --table [table]', 'Specify the table to create', '')
.parse(process.argv);
@goodpic
goodpic / SchemaSalesByHour.js
Created September 15, 2018 21:54
Create BigQuery table
exports.SchemaSalesByHour = [
{
description: 'Date and hour of the transactions',
name: 'date_hour',
type: 'DATETIME',
mode: 'REQUIRED'
},
{
description: 'UTC timestamp of the transactions',
name: 'timestamp',
@goodpic
goodpic / bigquery-create-a-new-table.js
Last active September 15, 2018 21:47
BigQuery Create a new table
const BigQuery = require('@google-cloud/bigquery');
const bigquery = new BigQuery({ YOUR_PROJECT_ID });
const handleError = (err) => {
if (err && err.name === 'PartialFailureError') {
if (err.errors && err.errors.length > 0) {
console.log('Insert errors:');
err.errors.forEach(error => console.error(error));
}
} else {
+-------------------+-----------+----------+-----------------------------------------+
| Field name | Type | Mode | Description |
+-------------------+-----------+----------+-----------------------------------------+
| id | INTEGER | REQUIRED | |
| shop_name | STRING | NULLABLE | |
| transaction_id | INTEGER | NULLABLE | Transaction which this item belongs |
| product_id | INTEGER | NULLABLE | Unique internal product ID |
| barcode | STRING | NULLABLE | Universal product ID |
| category_code | STRING | NULLABLE | ID of the category |
| category_name | STRING | NULLABLE | Name of the category |
@goodpic
goodpic / hourly_sales.sql
Last active September 11, 2018 00:21
Hourly sales
SELECT
TIMESTAMP_TRUNC(invoiced_at, HOUR) AS timestamp
, DATETIME_TRUNC(local_invoiced_at, HOUR) AS date_hour
, FORMAT_DATETIME("%A", local_invoiced_at) AS day
, MOD(CAST(FORMAT_DATETIME("%u", local_invoiced_at) AS int64), 7) AS day_num
, FORMAT_DATETIME("%H", local_invoiced_at) AS hour
, shop_name
, COUNT(*) AS transactions
, SUM(customers) AS customers
, SUM(sales) AS sales
@goodpic
goodpic / gist:d3e168a58dcfd4c984df7ab0052ac2f2
Last active September 10, 2018 23:55
Sample table: Shop transaction
+-------------------+-----------+----------+-------------------------+
| Field name | Type | Mode | Description |
+-------------------+-----------+----------+-------------------------+
| id | INTEGER | REQUIRED | Transaction ID |
| shop_name | STRING | NULLABLE | |
| customers | INTEGER | NULLABLE | Number of customers |
| sales | NUMERIC | NULLABLE | Total sales |
| tax | NUMERIC | NULLABLE | VAT tax paid |
| cost | NUMERIC | NULLABLE | Total costs |
| discount | NUMERIC | NULLABLE | Total discount |
@goodpic
goodpic / ProductScanRNCamera.js
Last active April 24, 2023 11:54
RNCamera : Use the barcode scanner on React Native
import React, { Component } from 'react';
import { Button, Text, View } from 'react-native';
import { RNCamera } from 'react-native-camera';
class ProductScanRNCamera extends Component {
constructor(props) {
super(props);
this.camera = null;
this.barcodeCodes = [];
module Types
# Product schema
class ProductType < GraphQL::Schema::Object
# Name of this Type
graphql_name 'Product'
field :sales_history, [Types::SalesHistoryType],
null: true,
resolve: ->(product, args, ctx) {
days = ctx.irep_node.parent.arguments.days || 7
module Types
class QueryType < GraphQL::Schema::Object
# Add root-level fields here.
# They will be entry points for queries on your schema.
# queries are represented as fields, use [Types::XXX] for the list
field :product, Types::ProductType,
null: true,
resolve: ->(_obj, args, _ctx) {
Product.find(args.id)