<commit_before>package main

import (
	"database/sql"
	"fmt"
	"time"

	_ "github.com/lib/pq"
	"github.com/mgutz/dat/v1"
	"github.com/mgutz/dat/v1/sqlx-runner"
)

func insert(media Media) (bool, error) {
	var exists int64
	conn.Select("1").From("media").Where("iid = $1", media.IID).QueryScalar(&exists)
	if exists > 0 {
		return false, nil
	}
	_, err := conn.Insect("media").
		Columns("iid", "document", "created_at", "subscription_id").
		Blacklist("id").
		Record(media).
		Where("iid = $1", media.IID).
		Exec()
	return true, err
}

func notificationCount() (int, error) {
	var cnt int
	err := conn.SQL("SELECT COUNT(*) FROM media").QueryScalar(&cnt)
	return cnt, err
}

func getSubscription(sid string) Subscription {
	sub := Subscription{}
	conn.Select("*").From("subscriptions").Where("id = $1", sid).QueryStruct(&sub)
	return sub
}

var (
	p    pinger
	conn *runner.Connection
)

// Media represents a single image or video on Instagram
type Media struct {
	ID             int       `db:"id"`
	IID            string    `db:"iid"`
	MediaJSON      dat.JSON  `db:"document"`
	CreatedAt      time.Time `db:"created_at"`
	SubscriptionID int       `db:"subscription_id"`
}

// Subscription represents a named location for which we store Media
type Subscription struct {
	ID     int     `db:"id"`
	Name   string  `db:"name"`<commit_msg>Return fetch results as JSON<commit_after>package main

import (
	"database/sql"
	"fmt"
	"time"

	_ "github.com/lib/pq"
	"github.com/mgutz/dat/v1"
	"github.com/mgutz/dat/v1/sqlx-runner"
)

func insert(media Media) (bool, error) {
	var exists int64
	conn.Select("1").From("media").Where("iid = $1", media.IID).QueryScalar(&exists)
	if exists > 0 {
		return false, nil
	}
	_, err := conn.Insect("media").
		Columns("iid", "document", "created_at", "subscription_id").
		Blacklist("id").
		Record(media).
		Where("iid = $1", media.IID).
		Exec()
	return true, err
}

func notificationCount() ([]byte, error) {
	json, err := conn.SQL(`
	SELECT subscription_id,
	       s.name,
	       COUNT(iid)
	FROM media m
	JOIN subscriptions s ON m.subscription_id = s.id
	GROUP BY subscription_id,
	         s.name
	ORDER BY subscription_id
		`).QueryJSON()
	return json, err
}

func getSubscription(sid string) Subscription {
	sub := Subscription{}
	conn.Select("*").From("subscriptions").Where("id = $1", sid).QueryStruct(&sub)
	return sub
}

var (
	p    pinger
	conn *runner.Connection
)

// Media represents a single image or video on Instagram
type Media struct {
	ID             int       `db:"id"`
	IID            string    `db:"iid"`
	MediaJSON      dat.JSON  `db:"document"`
	CreatedAt      time.Time `db:"created_at"`
	SubscriptionID int       `db:"subscription_id"`
}

// Subscription represents a named location for which we store Media
type Subscription struct {
	ID     int     `db:"id"`
	Name   string  `db:"name"`
{'fraction_non_alphanumeric': 0.1497720098211154, 'fraction_numerical': 0.006313574184496668, 'mean_word_length': 3.4423676012461057, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1096352', 'n_tokens_mistral': 1008, 'n_tokens_neox': 992, 'n_words': 255}