<commit_before>package main

import (
	"context"
	"fmt"
	"math"
	"os"

	"github.com/grafana/metrictank/api"
	"github.com/grafana/metrictank/mdata/chunk"
	"github.com/grafana/metrictank/store/cassandra"
	"github.com/raintank/schema"
)

// printPoints prints points in the store corresponding to the given requirements
func printPoints(ctx context.Context, store *cassandra.CassandraStore, tables []cassandra.Table, metrics []Metric, fromUnix, toUnix, fix uint32) {
	for _, metric := range metrics {
		fmt.Println("## Metric", metric)
		for _, table := range tables {
			fmt.Println("### Table", table.Name)
			if fix != 0 {
				points := getSeries(ctx, store, table, metric.AMKey, fromUnix, toUnix, fix)
				printPointsNormal(points, fromUnix, toUnix)
			} else {
				igens, err := store.SearchTable(ctx, metric.AMKey, table, fromUnix, toUnix)
				if err != nil {
					panic(err)
				}
				printNormal(igens, fromUnix, toUnix)
			}
		}
	}
}

// printPointSummary prints a summarized view of the points in the store corresponding to the given requirements
func printPointSummary(ctx context.Context, store *cassandra.CassandraStore, tables []cassandra.Table, metrics []Metric, fromUnix, toUnix, fix uint32) {
	for _, metric := range metrics {
		fmt.Println("## Metric", metric)
		for _, table := range tables {
			fmt.Println("### Table", table.Name)
			if fix != 0 {
				points := getSeries(ctx, store, table, metric.AMKey, fromUnix, toUnix, fix)
				printPointsSummary(points, fromUnix, toUnix)
			} else {
				igens, err := store.SearchTable(ctx, metric.AMKey, table, fromUnix, toUnix)
				if err != nil {
					panic(err)
				}
				printSummary(igens, fromUnix, toUnix)
			}
		}
	}
}

func getSeries(ctx context.Context, store *cassandra.CassandraStore, table cassandra.Table, amkey schema.AMKey, fromUnix, toUnix, interval uint32) []schema.Point {
	var points []schema.Point
	itgens, err := store.SearchTable(ctx, amkey, table, fromUnix, toUnix)
	if err != nil {
		panic(err)
	}

	for i, itgen := range itgens {
		iter, err := itgen.Get()
		if err != nil {
			fmt.Fprintf(os.Stderr, "chunk %d itergen.Get: %s", i, err)
			continue
		}
		for iter.Next() {
			ts, val := iter.Values()
			if ts >= fromUnix && ts < toUnix {
				points = append(points, schema.Point{Val: val, Ts: ts})
			}
		}
	}
	return api.Fix(points, fromUnix, toUnix, interval)
}

func printNormal(igens []chunk.IterGen, from, to uint32) {
	fmt.Println("number of chunks:", len(igens))
	for i, ig := range igens {
		fmt.Printf("#### chunk %d (t0:%s, span:%d)\n", i, printTime(ig.T0), ig.Span())<commit_msg>mt-store-cat : also display chunk format. why not?<commit_after>package main

import (
	"context"
	"fmt"
	"math"
	"os"

	"github.com/grafana/metrictank/api"
	"github.com/grafana/metrictank/mdata/chunk"
	"github.com/grafana/metrictank/store/cassandra"
	"github.com/raintank/schema"
)

// printPoints prints points in the store corresponding to the given requirements
func printPoints(ctx context.Context, store *cassandra.CassandraStore, tables []cassandra.Table, metrics []Metric, fromUnix, toUnix, fix uint32) {
	for _, metric := range metrics {
		fmt.Println("## Metric", metric)
		for _, table := range tables {
			fmt.Println("### Table", table.Name)
			if fix != 0 {
				points := getSeries(ctx, store, table, metric.AMKey, fromUnix, toUnix, fix)
				printPointsNormal(points, fromUnix, toUnix)
			} else {
				igens, err := store.SearchTable(ctx, metric.AMKey, table, fromUnix, toUnix)
				if err != nil {
					panic(err)
				}
				printNormal(igens, fromUnix, toUnix)
			}
		}
	}
}

// printPointSummary prints a summarized view of the points in the store corresponding to the given requirements
func printPointSummary(ctx context.Context, store *cassandra.CassandraStore, tables []cassandra.Table, metrics []Metric, fromUnix, toUnix, fix uint32) {
	for _, metric := range metrics {
		fmt.Println("## Metric", metric)
		for _, table := range tables {
			fmt.Println("### Table", table.Name)
			if fix != 0 {
				points := getSeries(ctx, store, table, metric.AMKey, fromUnix, toUnix, fix)
				printPointsSummary(points, fromUnix, toUnix)
			} else {
				igens, err := store.SearchTable(ctx, metric.AMKey, table, fromUnix, toUnix)
				if err != nil {
					panic(err)
				}
				printSummary(igens, fromUnix, toUnix)
			}
		}
	}
}

func getSeries(ctx context.Context, store *cassandra.CassandraStore, table cassandra.Table, amkey schema.AMKey, fromUnix, toUnix, interval uint32) []schema.Point {
	var points []schema.Point
	itgens, err := store.SearchTable(ctx, amkey, table, fromUnix, toUnix)
	if err != nil {
		panic(err)
	}

	for i, itgen := range itgens {
		iter, err := itgen.Get()
		if err != nil {
			fmt.Fprintf(os.Stderr, "chunk %d itergen.Get: %s", i, err)
			continue
		}
		for iter.Next() {
			ts, val := iter.Values()
			if ts >= fromUnix && ts < toUnix {
				points = append(points, schema.Point{Val: val, Ts: ts})
			}
		}
	}
	return api.Fix(points, fromUnix, toUnix, interval)
}

func printNormal(igens []chunk.IterGen, from, to uint32) {
	fmt.Println("number of chunks:", len(igens))
	for i, ig := range igens {
		fmt.Printf("#### chunk %d (t0:%s, span:%d, format:%s)\n", i, printTime(ig.T0), ig.Span(), ig.Format())
{'fraction_non_alphanumeric': 0.1423910957589714, 'fraction_numerical': 0.004605641911341394, 'mean_word_length': 3.9077212806026367, 'pattern_counts': {'":': 0, '<': 5, '<?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': '3797700', 'n_tokens_mistral': 2046, 'n_tokens_neox': 1837, 'n_words': 489}