<commit_before>﻿using Chalmers.ILL.Models;
using Examine;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Chalmers.ILL.Extensions;

namespace Chalmers.ILL.Providers
{
    public class ProviderService : IProviderService
    {
        ISearcher _orderItemsSearcher;

        public ProviderService(ISearcher orderItemsSearcher)
        {
            _orderItemsSearcher = orderItemsSearcher;
        }

        public List<String> FetchAndCreateListOfUsedProviders()
        {
            var res = new List<String>();

            var searchCriteria = _orderItemsSearcher.CreateSearchCriteria(Examine.SearchCriteria.BooleanOperation.Or);
            // NOTE: Should probably only fetch orders that are not too old, to keep the numbers down and to keep the data relevant.
            var allOrders = _orderItemsSearcher.Search(searchCriteria.RawQuery("nodeTypeAlias:ChalmersILLOrderItem"));

            return allOrders.Where(x => x.Fields.ContainsKey("ProviderName") && x.Fields["ProviderName"] != "")
                .Select(x => x.Fields["ProviderName"])
                .GroupBy(x => x)
                .OrderByDescending(x => x.Count())
                .Select(x => x.Key)
                .ToList();
        }

        public int GetSuggestedDeliveryTimeInHoursForProvider(string providerName)
        {
            Int64 totalTime = 0;
            int count = 0;

            if (!String.IsNullOrWhiteSpace(providerName))
            {
                var searchCriteria = _orderItemsSearcher.CreateSearchCriteria(Examine.SearchCriteria.BooleanOperation.Or);
                // NOTE: Should probably only fetch orders that are not too old, to keep the numbers down and to keep the data relevant.
                var orders = _orderItemsSearcher.Search(searchCriteria.RawQuery("ProviderName:\"" + providerName + "\""));

                foreach (var order in orders)
                {
                    DateTime latestDeliveryStatus;
                    DateTime latestOrderedStatus;
                    foreach (var logItem in JsonConvert.DeserializeObject<List<LogItem>>(order.Fields.GetValueString("Log")))
                    {
                        latestDeliveryStatus = new DateTime(1970, 1, 1);
                        latestOrderedStatus = new DateTime(1970, 1, 1);

                        if (logItem.Type == "STATUS" && logItem.Message.Contains("till Beställd"))
                        {
                            latestOrderedStatus = logItem.CreateDate;
                        }
                        else if (logItem.Type == "STATUS" && (logItem.Message.Contains("till Levererad") || logItem.Message.Contains("till Utlånad")))
                        {
                            latestDeliveryStatus = logItem.CreateDate;
                        }

                        var diff = (latestDeliveryStatus - latestOrderedStatus).Hours;
                        if (diff > 0)
                        {
                            count++;
                        }
                        totalTime += diff;
                    }
                }
            }

            count = count > 0 ? count : 1;

            return Convert.ToInt32(Math.Ceiling(Convert.ToDouble(totalTime / count)));
        }
    }
}<commit_msg>KISS: Delivery time. 24h for subito, 168h for everything else.<commit_after>﻿using Chalmers.ILL.Models;
using Examine;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Chalmers.ILL.Extensions;

namespace Chalmers.ILL.Providers
{
    public class ProviderService : IProviderService
    {
        ISearcher _orderItemsSearcher;

        public ProviderService(ISearcher orderItemsSearcher)
        {
            _orderItemsSearcher = orderItemsSearcher;
        }

        public List<String> FetchAndCreateListOfUsedProviders()
        {
            var res = new List<String>();

            var searchCriteria = _orderItemsSearcher.CreateSearchCriteria(Examine.SearchCriteria.BooleanOperation.Or);
            // NOTE: Should probably only fetch orders that are not too old, to keep the numbers down and to keep the data relevant.
            var allOrders = _orderItemsSearcher.Search(searchCriteria.RawQuery("nodeTypeAlias:ChalmersILLOrderItem"));

            return allOrders.Where(x => x.Fields.ContainsKey("ProviderName") && x.Fields["ProviderName"] != "")
                .Select(x => x.Fields["ProviderName"])
                .GroupBy(x => x)
                .OrderByDescending(x => x.Count())
                .Select(x => x.Key)
                .ToList();
        }

        public int GetSuggestedDeliveryTimeInHoursForProvider(string providerName)
        {
            int res = 168;

            if (!String.IsNullOrWhiteSpace(providerName) && providerName.ToLower().Contains("subito"))
            {
                res = 24;
            }

            return res;
        }
    }
}
{'fraction_non_alphanumeric': 0.09016064257028113, 'fraction_numerical': 0.006224899598393574, 'mean_word_length': 1.884192240880139, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 21, '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': '3735290', 'n_tokens_mistral': 1316, 'n_tokens_neox': 1232, 'n_words': 277}