<commit_before>/*
Copyright 2019 The Knative Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
	"context"
	"encoding/csv"
	"fmt"
	"log"
	"net"
	"os"
	"strings"

	"github.com/golang/protobuf/jsonpb"

	"google.golang.org/grpc"
	qspb "knative.dev/pkg/third_party/mako/proto/quickstore_go_proto"
)

const (
	port = ":9813"
)

type server struct {
	stopCh chan struct{}
}

func (s *server) Store(ctx context.Context, in *qspb.StoreInput) (*qspb.StoreOutput, error) {
	m := jsonpb.Marshaler{}
	qi, _ := m.MarshalToString(in.GetQuickstoreInput())
	fmt.Printf("# %s\n", qi)
	cols := []string{"inputValue", "errorMessage"}
	writer := csv.NewWriter(os.Stdout)

	for _, sp := range in.GetSamplePoints() {
		for _, mv := range sp.GetMetricValueList() {
			cols = updateCols(cols, mv.GetValueKey())
			vals := map[string]string{"inputValue": fmt.Sprintf("%f", sp.GetInputValue())}
			vals[mv.GetValueKey()] = fmt.Sprintf("%f", mv.GetValue())
			writer.Write(makeRow(cols, vals))
		}
	}

	for _, ra := range in.GetRunAggregates() {
		cols = updateCols(cols, ra.GetValueKey())
		vals := map[string]string{ra.GetValueKey(): fmt.Sprintf("%f", ra.GetValue())}
		writer.Write(makeRow(cols, vals))
	}

	for _, sa := range in.GetSampleErrors() {
		vals := map[string]string{"inputValue": fmt.Sprintf("%f", sa.GetInputValue()), "errorMessage": sa.GetErrorMessage()}
		writer.Write(makeRow(cols, vals))
	}

	writer.Flush()
	fmt.Printf("# %s\n", strings.Join(cols, ","))

	return &qspb.StoreOutput{}, nil
}

func updateCols(prototype []string, k string) []string {
	for _, n := range prototype {
		if n == k {
			return prototype
		}
	}
	prototype = append(prototype, k)
	return prototype
}

func makeRow(prototype []string, points map[string]string) []string {
	row := make([]string, len(prototype))
	// n^2 but whatever
	for k, v := range points {
		for i, n := range prototype {
			if k == n {
				row[i] = v
			}
		}
	}
	return row
}

func (s *server) ShutdownMicroservice(ctx context.Context, in *qspb.ShutdownInput) (*qspb.ShutdownOutput, error) {
	close(s.stopCh)
	return &qspb.ShutdownOutput{}, nil
}

func main() {
	lis, err := net.Listen("tcp", port)
	if err != nil {
		log.Fatalf("failed to listen: %v", err)
	}
	s := grpc.NewServer()
	stopCh := make(chan struct{})
	go func() {<commit_msg>Close the stop channel only once (#697)<commit_after>/*
Copyright 2019 The Knative Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
	"context"
	"encoding/csv"
	"fmt"
	"log"
	"net"
	"os"
	"strings"
	"sync"

	"github.com/golang/protobuf/jsonpb"

	"google.golang.org/grpc"
	qspb "knative.dev/pkg/third_party/mako/proto/quickstore_go_proto"
)

const (
	port = ":9813"
)

type server struct {
	stopOnce sync.Once
	stopCh   chan struct{}
}

func (s *server) Store(ctx context.Context, in *qspb.StoreInput) (*qspb.StoreOutput, error) {
	m := jsonpb.Marshaler{}
	qi, _ := m.MarshalToString(in.GetQuickstoreInput())
	fmt.Printf("# %s\n", qi)
	cols := []string{"inputValue", "errorMessage"}
	writer := csv.NewWriter(os.Stdout)

	for _, sp := range in.GetSamplePoints() {
		for _, mv := range sp.GetMetricValueList() {
			cols = updateCols(cols, mv.GetValueKey())
			vals := map[string]string{"inputValue": fmt.Sprintf("%f", sp.GetInputValue())}
			vals[mv.GetValueKey()] = fmt.Sprintf("%f", mv.GetValue())
			writer.Write(makeRow(cols, vals))
		}
	}

	for _, ra := range in.GetRunAggregates() {
		cols = updateCols(cols, ra.GetValueKey())
		vals := map[string]string{ra.GetValueKey(): fmt.Sprintf("%f", ra.GetValue())}
		writer.Write(makeRow(cols, vals))
	}

	for _, sa := range in.GetSampleErrors() {
		vals := map[string]string{"inputValue": fmt.Sprintf("%f", sa.GetInputValue()), "errorMessage": sa.GetErrorMessage()}
		writer.Write(makeRow(cols, vals))
	}

	writer.Flush()
	fmt.Printf("# %s\n", strings.Join(cols, ","))

	return &qspb.StoreOutput{}, nil
}

func updateCols(prototype []string, k string) []string {
	for _, n := range prototype {
		if n == k {
			return prototype
		}
	}
	prototype = append(prototype, k)
	return prototype
}

func makeRow(prototype []string, points map[string]string) []string {
	row := make([]string, len(prototype))
	// n^2 but whatever
	for k, v := range points {
		for i, n := range prototype {
			if k == n {
				row[i] = v
			}
		}
	}
	return row
}

func (s *server) ShutdownMicroservice(ctx context.Context, in *qspb.ShutdownInput) (*qspb.ShutdownOutput, error) {
	s.stopOnce.Do(func() { close(s.stopCh) })
	return &qspb.ShutdownOutput{}, nil
}

func main() {
	lis, err := net.Listen("tcp", port)
	if err != nil {
		log.Fatalf("failed to listen: %v", err)
	}
	s := grpc.NewServer()
	stopCh := make(chan struct{})
	go func() {
{'fraction_non_alphanumeric': 0.15305761753269706, 'fraction_numerical': 0.005125486037469071, 'mean_word_length': 4.318609022556391, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '3285942', 'n_tokens_mistral': 2062, 'n_tokens_neox': 1957, 'n_words': 536}