<commit_before>/*
 * Copyright (c) 2013-2014, <NAME> (<<EMAIL>>)
 *
 * 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
 *
 *     http://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.
 */

// Serve up the list of logged events, and individual events as well.

package main

import (
	"net/http"
	"github.com/ctdk/goiardi/log_info"
	"github.com/ctdk/goiardi/actor"
	"github.com/ctdk/goiardi/util"
	"encoding/json"
	"strconv"
	"fmt"
)

// The whole list
func event_list_handler(w http.ResponseWriter, r *http.Request){
	w.Header().Set("Content-Type", "application/json")
	opUser, oerr := actor.GetReqUser(r.Header.Get("X-OPS-USERID"))
	if oerr != nil {
		JsonErrorReport(w, r, oerr.Error(), oerr.Status())
		return
	}
	
	switch r.Method {
		case "GET":
			if !opUser.IsAdmin() {
				JsonErrorReport(w, r, "You must be an admin to do that", http.StatusForbidden)
				return
			}
			le_list := log_info.GetLogInfos()
			le_resp := make([]map[string]interface{}, len(le_list))
			for i, v := range le_list {
				le_resp[i] = make(map[string]interface{})
				le_resp[i]["event"] = v
				le_url := fmt.Sprintf("/events/%d", v.Id)
				le_resp[i]["url"] = util.CustomURL(le_url)
			}
			enc := json.NewEncoder(w)
			if err := enc.Encode(&le_resp); err != nil {
				JsonErrorReport(w, r, err.Error(), http.StatusInternalServerError)
				return
			}
		default:
			JsonErrorReport(w, r, "Bad request", http.StatusBadRequest)
			return
	}
}
<commit_msg>/events can take offset and limit parameters now<commit_after>/*
 * Copyright (c) 2013-2014, <NAME> (<<EMAIL>>)
 *
 * 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
 *
 *     http://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.
 */

// Serve up the list of logged events, and individual events as well.

package main

import (
	"net/http"
	"github.com/ctdk/goiardi/log_info"
	"github.com/ctdk/goiardi/actor"
	"github.com/ctdk/goiardi/util"
	"encoding/json"
	"strconv"
	"fmt"
)

// The whole list
func event_list_handler(w http.ResponseWriter, r *http.Request){
	w.Header().Set("Content-Type", "application/json")
	opUser, oerr := actor.GetReqUser(r.Header.Get("X-OPS-USERID"))
	if oerr != nil {
		JsonErrorReport(w, r, oerr.Error(), oerr.Status())
		return
	}
	
	// Look for offset and limit parameters
	r.ParseForm()
	var offset, limit int
	if o, found := r.Form["offset"]; found {
		if len(o) < 0 {
			JsonErrorReport(w, r, "invalid offsets", http.StatusBadRequest)
			return
		}
		var err error
		offset, err = strconv.Atoi(o[0])
		if err != nil {
			JsonErrorReport(w, r, "invalid offset converstion to int", http.StatusBadRequest)
			return
		}
		if offset < 0 {
			JsonErrorReport(w, r, "invalid negative offset value", http.StatusBadRequest)
			return
		}
	} else {
		offset = 0
	}
	var limit_found bool
	if l, found := r.Form["limit"]; found {
		limit_found = true
		if len(l) < 0 {
			JsonErrorReport(w, r, "invalid limit", http.StatusBadRequest)
			return
		}
		var err error
		limit, err = strconv.Atoi(l[0])
		if err != nil {
			JsonErrorReport(w, r, "invalid limit converstion to int", http.StatusBadRequest)
			return
		}
		if limit < 0 {
			JsonErrorReport(w, r, "invalid negative limit value", http.StatusBadRequest)
			return
		}
	}
	
	
	switch r.Method {
		case "GET":
			if !opUser.IsAdmin() {
				JsonErrorReport(w, r, "You must be an admin to do that", http.StatusForbidden)
				return
			}
			var le_list []*log_info.LogInfo
			if limit_found {
				le_list = log_info.GetLogInfos(offset, limit)
			} else {
				le_list = log_info.GetLogInfos(offset)
			}
			le_resp := make([]map[string]interface{}, len(le_list))
			for i, v := range le_list {
				le_resp[i] = make(map[string]interface{})
				le_resp[i]["event"] = v
				le_url := fmt.Sprintf("/events/%d", v.Id)
				le_resp[i]["url"] = util.CustomURL(le_url)
			}
			enc := json.NewEncoder(w)
			if err := enc.Encode(&le_resp); err != nil {
				JsonErrorReport(w, r, err.Error(), http.StatusInternalServerError)
				return
			}
		default:
			JsonErrorReport(w, r, "Bad request", http.StatusBadRequest)
			return
	}
}

{'fraction_non_alphanumeric': 0.13128434764909425, 'fraction_numerical': 0.00630979035212701, 'mean_word_length': 3.6097560975609757, 'pattern_counts': {'":': 2, '<': 13, '<?xml version=': 0, '>': 9, 'https://': 0, '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': '5117972', 'n_tokens_mistral': 1851, 'n_tokens_neox': 1665, 'n_words': 487}